/
blockString-test.ts
294 lines (260 loc) · 7.33 KB
/
blockString-test.ts
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
import { expect } from 'chai';
import { describe, it } from 'mocha';
import {
isPrintableAsBlockString,
dedentBlockStringLines,
printBlockString,
} from '../blockString';
function joinLines(...args: ReadonlyArray<string>) {
return args.join('\n');
}
describe('dedentBlockStringLines', () => {
function expectDedent(lines: ReadonlyArray<string>) {
return expect(dedentBlockStringLines(lines));
}
it('handles empty string', () => {
expectDedent(['']).to.deep.equal([]);
});
it('do not dedent first line', () => {
expectDedent([' a']).to.deep.equal([' a']);
expectDedent([' a', ' b']).to.deep.equal([' a', 'b']);
});
it('removes minimal indentation length', () => {
expectDedent(['', ' a', ' b']).to.deep.equal(['a', ' b']);
expectDedent(['', ' a', ' b']).to.deep.equal([' a', 'b']);
expectDedent(['', ' a', ' b', 'c']).to.deep.equal([' a', ' b', 'c']);
});
it('dedent both tab and space as single character', () => {
expectDedent(['', '\ta', ' b']).to.deep.equal(['a', ' b']);
expectDedent(['', '\t a', ' b']).to.deep.equal(['a', ' b']);
expectDedent(['', ' \t a', ' b']).to.deep.equal(['a', ' b']);
});
it('dedent do not take empty lines into account', () => {
expectDedent(['a', '', ' b']).to.deep.equal(['a', '', 'b']);
expectDedent(['a', ' ', ' b']).to.deep.equal(['a', '', 'b']);
});
it('removes uniform indentation from a string', () => {
const lines = [
'',
' Hello,',
' World!',
'',
' Yours,',
' GraphQL.',
];
expectDedent(lines).to.deep.equal([
'Hello,',
' World!',
'',
'Yours,',
' GraphQL.',
]);
});
it('removes empty leading and trailing lines', () => {
const lines = [
'',
'',
' Hello,',
' World!',
'',
' Yours,',
' GraphQL.',
'',
'',
];
expectDedent(lines).to.deep.equal([
'Hello,',
' World!',
'',
'Yours,',
' GraphQL.',
]);
});
it('removes blank leading and trailing lines', () => {
const lines = [
' ',
' ',
' Hello,',
' World!',
'',
' Yours,',
' GraphQL.',
' ',
' ',
];
expectDedent(lines).to.deep.equal([
'Hello,',
' World!',
'',
'Yours,',
' GraphQL.',
]);
});
it('retains indentation from first line', () => {
const lines = [
' Hello,',
' World!',
'',
' Yours,',
' GraphQL.',
];
expectDedent(lines).to.deep.equal([
' Hello,',
' World!',
'',
'Yours,',
' GraphQL.',
]);
});
it('does not alter trailing spaces', () => {
const lines = [
' ',
' Hello, ',
' World! ',
' ',
' Yours, ',
' GraphQL. ',
' ',
];
expectDedent(lines).to.deep.equal([
'Hello, ',
' World! ',
' ',
'Yours, ',
' GraphQL. ',
]);
});
});
describe('isPrintableAsBlockString', () => {
function expectPrintable(str: string) {
return expect(isPrintableAsBlockString(str)).to.equal(true);
}
function expectNonPrintable(str: string) {
return expect(isPrintableAsBlockString(str)).to.equal(false);
}
it('accepts valid strings', () => {
expectPrintable('');
expectPrintable(' a');
expectPrintable('\t"\n"');
expectNonPrintable('\t"\n \n\t"');
});
it('rejects strings with only whitespace', () => {
expectNonPrintable(' ');
expectNonPrintable('\t');
expectNonPrintable('\t ');
expectNonPrintable(' \t');
});
it('rejects strings with non-printable character', () => {
expectNonPrintable('\x00');
expectNonPrintable('a\x00b');
});
it('rejects strings with non-printable character', () => {
expectNonPrintable('\x00');
expectNonPrintable('a\x00b');
});
it('rejects strings with only empty lines', () => {
expectNonPrintable('\n');
expectNonPrintable('\n\n');
expectNonPrintable('\n\n\n');
expectNonPrintable(' \n \n');
expectNonPrintable('\t\n\t\t\n');
});
it('rejects strings with carriage return', () => {
expectNonPrintable('\r');
expectNonPrintable('\n\r');
expectNonPrintable('\r\n');
expectNonPrintable('a\rb');
});
it('rejects strings with leading empty lines', () => {
expectNonPrintable('\na');
expectNonPrintable(' \na');
expectNonPrintable('\t\na');
expectNonPrintable('\n\na');
});
it('rejects strings with leading empty lines', () => {
expectNonPrintable('a\n');
expectNonPrintable('a\n ');
expectNonPrintable('a\n\t');
expectNonPrintable('a\n\n');
});
});
describe('printBlockString', () => {
function expectBlockString(str: string) {
return {
toEqual(expected: string | { readable: string; minimize: string }) {
const { readable, minimize } =
typeof expected === 'string'
? { readable: expected, minimize: expected }
: expected;
expect(printBlockString(str)).to.equal(readable);
expect(printBlockString(str, { minimize: true })).to.equal(minimize);
},
};
}
it('do not escape characters', () => {
const str = '" \\ / \b \f \n \r \t';
expectBlockString(str).toEqual({
readable: '"""\n' + str + '\n"""',
minimize: '"""\n' + str + '"""',
});
});
it('by default print block strings as single line', () => {
const str = 'one liner';
expectBlockString(str).toEqual('"""one liner"""');
});
it('by default print block strings ending with triple quotation as multi-line', () => {
const str = 'triple quotation """';
expectBlockString(str).toEqual({
readable: '"""\ntriple quotation \\"""\n"""',
minimize: '"""triple quotation \\""""""',
});
});
it('correctly prints single-line with leading space', () => {
const str = ' space-led string';
expectBlockString(str).toEqual('""" space-led string"""');
});
it('correctly prints single-line with leading space and trailing quotation', () => {
const str = ' space-led value "quoted string"';
expectBlockString(str).toEqual(
'""" space-led value "quoted string"\n"""',
);
});
it('correctly prints single-line with trailing backslash', () => {
const str = 'backslash \\';
expectBlockString(str).toEqual({
readable: '"""\nbackslash \\\n"""',
minimize: '"""backslash \\\n"""',
});
});
it('correctly prints multi-line with internal indent', () => {
const str = 'no indent\n with indent';
expectBlockString(str).toEqual({
readable: '"""\nno indent\n with indent\n"""',
minimize: '"""\nno indent\n with indent"""',
});
});
it('correctly prints string with a first line indentation', () => {
const str = joinLines(
' first ',
' line ',
'indentation',
' string',
);
expectBlockString(str).toEqual({
readable: joinLines(
'"""',
' first ',
' line ',
'indentation',
' string',
'"""',
),
minimize: joinLines(
'""" first ',
' line ',
'indentation',
' string"""',
),
});
});
});