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

Remove 'invariant' calls from 'stripIgnoredCharacters' tests #2065

Merged
merged 1 commit into from
Jul 30, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 47 additions & 24 deletions src/utilities/__tests__/stripIgnoredCharacters-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { expect } from 'chai';
import { describe, it } from 'mocha';

import dedent from '../../jsutils/dedent';
import invariant from '../../jsutils/invariant';

import { parse } from '../../language/parser';
import { Source } from '../../language/source';
Expand Down Expand Up @@ -61,41 +60,51 @@ const nonPunctuatorTokens = [
function lexValue(str) {
const lexer = createLexer(new Source(str));
const value = lexer.advance().value;
invariant(lexer.advance().kind === '<EOF>');

/* istanbul ignore if */
if (lexer.advance().kind !== '<EOF>') {
throw new Error('Expected EOF');
}
return value;
}

// Called only to make error messages for failing tests
/* istanbul ignore next */
function inspectStr(str) {
return (JSON.stringify(str) || '')
.replace(/^"|"$/g, '`')
.replace(/\\"/g, '"');
}

function expectStripped(docString) {
return {
toEqual(expected) {
const stripped = stripIgnoredCharacters(docString);
invariant(
stripped === expected,
`Expected stripIgnoredCharacters(${inspectStr(docString)})\n` +
`\tto equal ${inspectStr(expected)}\n` +
`\tbut got ${inspectStr(stripped)}`,
);

/* istanbul ignore if */
if (stripped !== expected) {
throw new Error(dedent`
Expected stripIgnoredCharacters(${inspectStr(docString)})
to equal ${inspectStr(expected)}
but got ${inspectStr(stripped)}
`);
}

const strippedTwice = stripIgnoredCharacters(stripped);
invariant(
stripped === strippedTwice,
`Expected stripIgnoredCharacters(${inspectStr(stripped)})\n` +
`\tto equal ${inspectStr(stripped)}\n` +
`\tbut got ${inspectStr(strippedTwice)}`,
);

/* istanbul ignore if */
if (stripped !== strippedTwice) {
throw new Error(dedent`
Expected stripIgnoredCharacters(${inspectStr(stripped)})
to equal ${inspectStr(stripped)}
but got ${inspectStr(strippedTwice)}
`);
}
},
toStayTheSame() {
this.toEqual(docString);
},
};

function inspectStr(str) {
// Called only to make error messages for failing tests
/* istanbul ignore next */
return JSON.stringify(str)
.replace(/^"|"$/g, '`')
.replace(/\\"/g, '"');
}
}

describe('stripIgnoredCharacters', () => {
Expand Down Expand Up @@ -405,7 +414,14 @@ describe('stripIgnoredCharacters', () => {
const strippedStr = stripIgnoredCharacters(blockStr);
const strippedValue = lexValue(strippedStr);

invariant(originalValue === strippedValue);
/* istanbul ignore if */
if (originalValue !== strippedValue) {
throw new Error(dedent`
Expected lextOne(stripIgnoredCharacters(${inspectStr(blockStr)}))
to equal ${inspectStr(originalValue)}
but got ${inspectStr(strippedValue)}
`);
}
return expectStripped(blockStr);
}

Expand Down Expand Up @@ -460,7 +476,14 @@ describe('stripIgnoredCharacters', () => {
const strippedStr = stripIgnoredCharacters(testStr);
const strippedValue = lexValue(strippedStr);

invariant(testValue === strippedValue);
/* istanbul ignore if */
if (testValue !== strippedValue) {
throw new Error(dedent`
Expected lextOne(stripIgnoredCharacters(${inspectStr(testStr)}))
to equal ${inspectStr(testValue)}
but got ${inspectStr(strippedValue)}
`);
}
}
}
});
Expand Down