/
test-util.js
171 lines (151 loc) · 3.98 KB
/
test-util.js
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import test from 'ava';
import dedent from 'dedent';
import {getLineAndColumn, getLineAndColumnMessage} from '../src/util.js';
// --------------------------------------------------------------------
// Tests
// --------------------------------------------------------------------
test('getLineAndColumn().toString()', t => {
t.is(
getLineAndColumn('', 0).toString(),
dedent`
Line 1, col 1:
> 1 |
^\n`,
'empty input',
);
t.is(
getLineAndColumn('3 + 4', 2).toString([0, 1], [4, 5]),
dedent`
Line 1, col 3:
> 1 | 3 + 4
~ ^ ~\n`,
'more than one range',
);
});
test('getLineAndColumnMessage', t => {
t.is(
getLineAndColumnMessage('', 0),
dedent`
Line 1, col 1:
> 1 |
^\n`,
'empty input',
);
let expected = dedent`
Line 1, col 1:
> 1 |
^
2 | \n`;
t.is(getLineAndColumnMessage('\n', 0), expected, 'past last char on empty line');
t.is(getLineAndColumnMessage('\r\n', 0), expected, '...and with CRLF, offset at CR');
t.is(getLineAndColumnMessage('\r\n', 1), expected, '...and with CRLF, offset at LF');
expected = dedent`
Line 2, col 1:
1 | a
> 2 | foo
^
3 | \n`;
t.is(getLineAndColumnMessage('a\nfoo\n', 2), expected, 'char after an empty line');
t.is(getLineAndColumnMessage('a\r\nfoo\r\n', 3), expected, '...and with CRLF');
expected = dedent`
Line 2, col 1:
1 |
> 2 |
^\n`;
t.is(getLineAndColumnMessage('\n', 1), expected, 'past last char on 2nd empty line');
t.is(getLineAndColumnMessage('\r\n', 2), expected, '...and with CRLF');
expected = dedent`
Line 1, col 1:
> 1 | a
^
2 | \n`;
t.is(getLineAndColumnMessage('a\n\n', 0), expected, 'two trailing empty lines');
t.is(getLineAndColumnMessage('a\r\n\r\n', 0), expected, '...and with CRLF');
let input = new Array(9).join('\n') + 'a\nhi!\nb';
expected = dedent`
Line 10, col 1:
9 | a
> 10 | hi!
^
11 | b\n`;
t.is(getLineAndColumnMessage(input, 10), expected, 'prev line num has fewer digits');
input = new Array(99).join('\n') + 'hi!\n';
expected = dedent`
Line 99, col 1:
98 |
> 99 | hi!
^
100 | \n`;
t.is(getLineAndColumnMessage(input, 98), expected, 'next line num has more digits');
expected = dedent`
Line 1, col 1:
> 1 | x
^\n`;
t.is(getLineAndColumnMessage('x', 0), expected, 'no next or prev line');
});
test('getLineAndColumnMessage with ranges', t => {
t.is(
getLineAndColumnMessage('3 + 4', 2, [0, 1]),
dedent`
Line 1, col 3:
> 1 | 3 + 4
~ ^\n`,
'a simple range',
);
t.is(
getLineAndColumnMessage('3 + 4', 2, [0, 1], [4, 5]),
dedent`
Line 1, col 3:
> 1 | 3 + 4
~ ^ ~\n`,
'more than one range',
);
t.is(
getLineAndColumnMessage('3 + 4', 2, [0, 100]),
dedent`
Line 1, col 3:
> 1 | 3 + 4
~~^~~\n`,
'end index out of bounds',
);
t.is(
getLineAndColumnMessage('3 + 4', 2, [0, 0]),
getLineAndColumnMessage('3 + 4', 2),
'empty range',
);
t.is(
getLineAndColumnMessage('3 + 4', 0, [0, 3], [1, 5]),
dedent`
Line 1, col 1:
> 1 | 3 + 4
^~~~~\n`,
'overlapping ranges',
);
t.is(
getLineAndColumnMessage('blah\n3 + 4', 7, [5, 6]),
dedent`
Line 2, col 3:
1 | blah
> 2 | 3 + 4
~ ^\n`,
'range on second line',
);
t.is(
getLineAndColumnMessage('blah\n3 + 4', 7, [0, 6]),
dedent`
Line 2, col 3:
1 | blah
> 2 | 3 + 4
~ ^\n`,
'range crossing lines',
);
t.is(
getLineAndColumnMessage('blah\n3 + 4', 7, [0, 50]),
dedent`
Line 2, col 3:
1 | blah
> 2 | 3 + 4
~~^~~\n`,
'range crossing lines at start and end',
);
});