|
| 1 | +import assert = require('assert'); |
1 | 2 | import { expect } from 'chai'; |
| 3 | +import { commands, Position, Selection, TextEditor, window } from 'vscode'; |
2 | 4 |
|
3 | | -import { hasRemote, normalizeUrl } from '../src/utilities'; |
| 5 | +import { getSelectedRange, hasRemote, normalizeUrl, toSelection } from '../src/utilities'; |
4 | 6 |
|
5 | 7 | describe('utilities', () => { |
6 | 8 | describe('hasRemote', () => { |
@@ -50,4 +52,78 @@ describe('utilities', () => { |
50 | 52 | expect(normalizeUrl('ssh://example.com/')).to.equal('example.com'); |
51 | 53 | }); |
52 | 54 | }); |
| 55 | + |
| 56 | + describe('getSelectedRange', () => { |
| 57 | + let editor: TextEditor; |
| 58 | + |
| 59 | + before(async () => { |
| 60 | + await commands.executeCommand('openEditors.newUntitledFile'); |
| 61 | + |
| 62 | + assert(window.activeTextEditor !== undefined); |
| 63 | + editor = window.activeTextEditor; |
| 64 | + |
| 65 | + await editor.edit((b) => { |
| 66 | + b.insert( |
| 67 | + new Position(0, 0), |
| 68 | + ['first line', 'second', 'third', 'fourth line', 'fifth'].join('\n') |
| 69 | + ); |
| 70 | + }); |
| 71 | + }); |
| 72 | + |
| 73 | + after(async () => { |
| 74 | + await commands.executeCommand('workbench.action.closeActiveEditor'); |
| 75 | + }); |
| 76 | + |
| 77 | + it('should convert to one-based values.', () => { |
| 78 | + editor.selection = new Selection(new Position(1, 2), new Position(3, 4)); |
| 79 | + |
| 80 | + expect(getSelectedRange(editor)).to.deep.equal({ |
| 81 | + startLine: 2, |
| 82 | + startColumn: 3, |
| 83 | + endLine: 4, |
| 84 | + endColumn: 5 |
| 85 | + }); |
| 86 | + }); |
| 87 | + |
| 88 | + it('should return correct value when selection is single point at the start of the line.', () => { |
| 89 | + editor.selection = new Selection(new Position(1, 0), new Position(1, 0)); |
| 90 | + |
| 91 | + expect(getSelectedRange(editor)).to.deep.equal({ |
| 92 | + startLine: 2, |
| 93 | + startColumn: 1, |
| 94 | + endLine: 2, |
| 95 | + endColumn: 1 |
| 96 | + }); |
| 97 | + }); |
| 98 | + |
| 99 | + it('should return correct value when selection is a single line.', () => { |
| 100 | + editor.selection = new Selection(new Position(3, 2), new Position(3, 5)); |
| 101 | + |
| 102 | + expect(getSelectedRange(editor)).to.deep.equal({ |
| 103 | + startLine: 4, |
| 104 | + startColumn: 3, |
| 105 | + endLine: 4, |
| 106 | + endColumn: 6 |
| 107 | + }); |
| 108 | + }); |
| 109 | + |
| 110 | + it('should exclude the last line if selection ends at the start of a new line.', () => { |
| 111 | + editor.selection = new Selection(new Position(2, 0), new Position(3, 0)); |
| 112 | + |
| 113 | + expect(getSelectedRange(editor)).to.deep.equal({ |
| 114 | + startLine: 3, |
| 115 | + startColumn: 1, |
| 116 | + endLine: 3, |
| 117 | + endColumn: 6 |
| 118 | + }); |
| 119 | + }); |
| 120 | + }); |
| 121 | + |
| 122 | + describe('toSelection', () => { |
| 123 | + it('should convert to zero-based values.', () => { |
| 124 | + expect( |
| 125 | + toSelection({ startLine: 10, startColumn: 20, endLine: 30, endColumn: 40 }) |
| 126 | + ).to.deep.equal(new Selection(new Position(9, 19), new Position(29, 39))); |
| 127 | + }); |
| 128 | + }); |
53 | 129 | }); |
0 commit comments