-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add test to check printer idempotence
- Loading branch information
Showing
1 changed file
with
59 additions
and
0 deletions.
There are no files selected for viewing
59 changes: 59 additions & 0 deletions
59
packages/babel-plugin-relay/__tests__/printGraphQL-test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/** | ||
* @flow | ||
* @format | ||
*/ | ||
|
||
'use strict'; | ||
|
||
const print = require('../printGraphQL'); | ||
|
||
const { parse } = require('graphql'); | ||
const path = require('path'); | ||
const fs = require('fs'); | ||
|
||
type OutputFixture = { name: string, input: string, output: string }; | ||
type ErrorFixture = { name: string, input: string, error: string }; | ||
type PrinterFixture = OutputFixture | ErrorFixture; | ||
|
||
describe('printGraphQL', () => { | ||
const outputFixtures = loadPrinterFixtures() | ||
.filter(fixture => fixture.output) | ||
// object key format doesn't work | ||
.map(fixture => [fixture.name, fixture.input, fixture.output]); | ||
|
||
it.each(outputFixtures)('tests printer idempotence: %s', (_name, input, expected) => { | ||
expect(print(parse(input))).toEqual(expected); | ||
}); | ||
}); | ||
|
||
function loadPrinterFixtures(): PrinterFixture[] { | ||
const fixturesPath = path.join( | ||
__dirname, | ||
'../../../compiler/crates/graphql-text-printer/tests/print_ast/fixtures', | ||
); | ||
const fixtures = []; | ||
for (const file of fs.readdirSync(fixturesPath)) { | ||
if (!file.endsWith('.expected')) { | ||
continue; | ||
} | ||
const content = fs.readFileSync(path.join(fixturesPath, file), 'utf8'); | ||
try { | ||
const fixture = parsePrintFixture(file, content); | ||
fixtures.push(fixture); | ||
} catch (err) { | ||
console.error(err, `\n\nFailed to parse ${file}`); | ||
} | ||
} | ||
return fixtures; | ||
} | ||
|
||
function parsePrintFixture(name: string, content: string): PrinterFixture { | ||
const successPatttern = /^=+ INPUT =+\n(?<input>[\s\S]*)\n=+ OUTPUT =+\n(?<output>[\s\S]*)$/; | ||
const failurePattern = /^=+ INPUT =+\n(?<input>[\s\S]*)\n=+ ERROR =+\n(?<error>[\s\S]*)$/; | ||
|
||
const match = content.match(successPatttern) ?? content.match(failurePattern); | ||
if (!match) { | ||
throw new Error('Unknown fixture format from the graphql-text-printer crate!'); | ||
} | ||
return { name, ...match.groups } as PrinterFixture; | ||
} |