forked from microsoft/rushstack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTextRange.test.ts
More file actions
70 lines (57 loc) · 1.93 KB
/
TextRange.test.ts
File metadata and controls
70 lines (57 loc) · 1.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
// See LICENSE in the project root for license information.
import { TextRange } from '../TextRange';
function escape(s: string): string {
return s.replace(/\n/g, '[n]')
.replace(/\r/g, '[r]')
.replace(/\t/g, '[t]');
}
function matchSnapshot(textRange: TextRange): void {
for (let i: number = -1; i <= textRange.end + 1; ++i) {
// Show the current character
const c: string = escape(textRange.buffer.substr(Math.max(i, 0), 1))
.replace(/\n/g, '[n]').replace(/\r/g, '[r]');
// Show the next 10 characters of context
const context: string = escape(textRange.buffer.substr(Math.max(i, 0), 10));
expect({
c: c,
context: context,
i: i,
location: textRange.getLocation(i)
}).toMatchSnapshot();
}
}
test('construction scenarios', () => {
const buffer: string = '0123456789';
const textRange: TextRange = TextRange.fromString(buffer);
expect(textRange.toString()).toEqual(buffer);
const subRange: TextRange = textRange.getNewRange(3, 6);
expect(subRange).toMatchSnapshot('subRange');
});
test('getLocation() basic', () => {
const textRange: TextRange = TextRange.fromString([
'L1',
'L2',
'', // (line 3 is blank)
'L4',
'L5+CR\rL5+CRLF\r\nL6+LFCR\n\rL7'
].join('\n'));
matchSnapshot(textRange);
});
test('getLocation() empty string', () => {
const textRange: TextRange = TextRange.fromString('');
matchSnapshot(textRange);
});
test('getLocation() CR string', () => {
const textRange: TextRange = TextRange.fromString('\r');
matchSnapshot(textRange);
});
test('getLocation() LF string', () => {
const textRange: TextRange = TextRange.fromString('\n');
matchSnapshot(textRange);
});
test('getLocation() tab characters', () => {
// Tab character advances by only one column
const textRange: TextRange = TextRange.fromString('1\t3');
matchSnapshot(textRange);
});