Skip to content

Commit

Permalink
printSchema: handle descriptions that are non-printable as block stri…
Browse files Browse the repository at this point in the history
…ngs (#3375)
  • Loading branch information
IvanGoncharov committed Nov 22, 2021
1 parent 11a0802 commit be12613
Show file tree
Hide file tree
Showing 8 changed files with 382 additions and 201 deletions.
66 changes: 34 additions & 32 deletions src/language/__tests__/blockString-fuzz.ts
Expand Up @@ -8,7 +8,7 @@ import { invariant } from '../../jsutils/invariant';

import { Lexer } from '../lexer';
import { Source } from '../source';
import { printBlockString } from '../blockString';
import { printBlockString, isPrintableAsBlockString } from '../blockString';

function lexValue(str: string): string {
const lexer = new Lexer(new Source(str));
Expand All @@ -19,6 +19,34 @@ function lexValue(str: string): string {
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
Expand All @@ -27,39 +55,13 @@ describe('printBlockString', () => {
allowedChars: ['\n', '\t', ' ', '"', 'a', '\\'],
maxLength: 7,
})) {
const testStr = '"""' + fuzzStr + '"""';

let testValue;
try {
testValue = lexValue(testStr);
} catch (e) {
continue; // skip invalid values
if (!isPrintableAsBlockString(fuzzStr)) {
testNonPrintableBlockString(fuzzStr);
continue;
}
invariant(typeof testValue === 'string');

const printedValue = lexValue(printBlockString(testValue));

invariant(
testValue === printedValue,
dedent`
Expected lexValue(printBlockString(${inspectStr(testValue)}))
to equal ${inspectStr(testValue)}
but got ${inspectStr(printedValue)}
`,
);

const printedMultilineString = lexValue(
printBlockString(testValue, true),
);

invariant(
testValue === printedMultilineString,
dedent`
Expected lexValue(printBlockString(${inspectStr(testValue)}, true))
to equal ${inspectStr(testValue)}
but got ${inspectStr(printedMultilineString)}
`,
);
testPrintableBlockString(fuzzStr);
testPrintableBlockString(fuzzStr, { minimize: true });
}
}).timeout(20000);
});

0 comments on commit be12613

Please sign in to comment.