Skip to content

Commit be12613

Browse files
printSchema: handle descriptions that are non-printable as block strings (#3375)
1 parent 11a0802 commit be12613

File tree

8 files changed

+382
-201
lines changed

8 files changed

+382
-201
lines changed

src/language/__tests__/blockString-fuzz.ts

Lines changed: 34 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { invariant } from '../../jsutils/invariant';
88

99
import { Lexer } from '../lexer';
1010
import { Source } from '../source';
11-
import { printBlockString } from '../blockString';
11+
import { printBlockString, isPrintableAsBlockString } from '../blockString';
1212

1313
function lexValue(str: string): string {
1414
const lexer = new Lexer(new Source(str));
@@ -19,6 +19,34 @@ function lexValue(str: string): string {
1919
return value;
2020
}
2121

22+
function testPrintableBlockString(
23+
testValue: string,
24+
options?: { minimize: boolean },
25+
): void {
26+
const blockString = printBlockString(testValue, options);
27+
const printedValue = lexValue(blockString);
28+
invariant(
29+
testValue === printedValue,
30+
dedent`
31+
Expected lexValue(${inspectStr(blockString)})
32+
to equal ${inspectStr(testValue)}
33+
but got ${inspectStr(printedValue)}
34+
`,
35+
);
36+
}
37+
38+
function testNonPrintableBlockString(testValue: string): void {
39+
const blockString = printBlockString(testValue);
40+
const printedValue = lexValue(blockString);
41+
invariant(
42+
testValue !== printedValue,
43+
dedent`
44+
Expected lexValue(${inspectStr(blockString)})
45+
to not equal ${inspectStr(testValue)}
46+
`,
47+
);
48+
}
49+
2250
describe('printBlockString', () => {
2351
it('correctly print random strings', () => {
2452
// Testing with length >7 is taking exponentially more time. However it is
@@ -27,39 +55,13 @@ describe('printBlockString', () => {
2755
allowedChars: ['\n', '\t', ' ', '"', 'a', '\\'],
2856
maxLength: 7,
2957
})) {
30-
const testStr = '"""' + fuzzStr + '"""';
31-
32-
let testValue;
33-
try {
34-
testValue = lexValue(testStr);
35-
} catch (e) {
36-
continue; // skip invalid values
58+
if (!isPrintableAsBlockString(fuzzStr)) {
59+
testNonPrintableBlockString(fuzzStr);
60+
continue;
3761
}
38-
invariant(typeof testValue === 'string');
39-
40-
const printedValue = lexValue(printBlockString(testValue));
41-
42-
invariant(
43-
testValue === printedValue,
44-
dedent`
45-
Expected lexValue(printBlockString(${inspectStr(testValue)}))
46-
to equal ${inspectStr(testValue)}
47-
but got ${inspectStr(printedValue)}
48-
`,
49-
);
50-
51-
const printedMultilineString = lexValue(
52-
printBlockString(testValue, true),
53-
);
5462

55-
invariant(
56-
testValue === printedMultilineString,
57-
dedent`
58-
Expected lexValue(printBlockString(${inspectStr(testValue)}, true))
59-
to equal ${inspectStr(testValue)}
60-
but got ${inspectStr(printedMultilineString)}
61-
`,
62-
);
63+
testPrintableBlockString(fuzzStr);
64+
testPrintableBlockString(fuzzStr, { minimize: true });
6365
}
6466
}).timeout(20000);
6567
});

0 commit comments

Comments
 (0)