/
blockString-fuzz.ts
67 lines (58 loc) · 2 KB
/
blockString-fuzz.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
import { describe, it } from 'mocha';
import { dedent } from '../../__testUtils__/dedent';
import { inspectStr } from '../../__testUtils__/inspectStr';
import { genFuzzStrings } from '../../__testUtils__/genFuzzStrings';
import { invariant } from '../../jsutils/invariant';
import { Lexer } from '../lexer';
import { Source } from '../source';
import { printBlockString, isPrintableAsBlockString } from '../blockString';
function lexValue(str: string): string {
const lexer = new Lexer(new Source(str));
const value = lexer.advance().value;
invariant(typeof value === 'string');
invariant(lexer.advance().kind === '<EOF>', 'Expected EOF');
return value;
}
function testPrintableBlockString(
testValue: string,
options?: { minimize: boolean },
): void {
const blockString = printBlockString(testValue, options);
const printedValue = lexValue(blockString);
invariant(
testValue === printedValue,
dedent`
Expected lexValue(${inspectStr(blockString)})
to equal ${inspectStr(testValue)}
but got ${inspectStr(printedValue)}
`,
);
}
function testNonPrintableBlockString(testValue: string): void {
const blockString = printBlockString(testValue);
const printedValue = lexValue(blockString);
invariant(
testValue !== printedValue,
dedent`
Expected lexValue(${inspectStr(blockString)})
to not equal ${inspectStr(testValue)}
`,
);
}
describe('printBlockString', () => {
it('correctly print random strings', () => {
// Testing with length >7 is taking exponentially more time. However it is
// highly recommended to test with increased limit if you make any change.
for (const fuzzStr of genFuzzStrings({
allowedChars: ['\n', '\t', ' ', '"', 'a', '\\'],
maxLength: 7,
})) {
if (!isPrintableAsBlockString(fuzzStr)) {
testNonPrintableBlockString(fuzzStr);
continue;
}
testPrintableBlockString(fuzzStr);
testPrintableBlockString(fuzzStr, { minimize: true });
}
}).timeout(20000);
});