Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

printSchema: handle descriptions that are non-printable as block strings #3375

Merged
merged 5 commits into from Nov 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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);
});