Skip to content

Commit 1d07ebf

Browse files
committed
Cleanup contain subset (#1317)
Squashed commit of the following: commit 5ca694025f75ba9c52d9db5a61e834b828d93185 Author: Lee Byron <[email protected]> Date: Mon Apr 23 16:37:25 2018 -0400 Rebase and generalize helper commit 118fb7872a80ff1fe416ca2c68464d3c4a896dc7 Author: Ivan Goncharov <[email protected]> Date: Fri Apr 20 12:37:45 2018 +0300 Replace 'containSubset' with 'locationsToJSON' commit 68dc47afa1de8d93e3c812e7c94ead440581134d Author: Ivan Goncharov <[email protected]> Date: Thu Apr 19 18:18:14 2018 +0300 Remove containSubset
1 parent 4b734a4 commit 1d07ebf

File tree

8 files changed

+369
-299
lines changed

8 files changed

+369
-299
lines changed

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"url": "http://github.com/graphql/graphql-js.git"
1616
},
1717
"options": {
18-
"mocha": "--require ./resources/mocha-bootload --check-leaks --full-trace --timeout 15000 src/**/__tests__/**/*-test.js"
18+
"mocha": "--check-leaks --full-trace --timeout 15000 src/**/__tests__/**/*-test.js"
1919
},
2020
"scripts": {
2121
"watch": "babel-node ./resources/watch.js",
@@ -57,7 +57,6 @@
5757
"beautify-benchmark": "0.2.4",
5858
"benchmark": "2.1.4",
5959
"chai": "4.1.2",
60-
"chai-subset": "1.6.0",
6160
"coveralls": "3.0.0",
6261
"eslint": "4.19.1",
6362
"eslint-plugin-babel": "5.1.0",

resources/mocha-bootload.js

Lines changed: 0 additions & 9 deletions
This file was deleted.

src/language/__tests__/parser-test.js

Lines changed: 81 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,22 @@
55
* LICENSE file in the root directory of this source tree.
66
*/
77

8+
import { inspect } from 'util';
9+
import { readFileSync } from 'fs';
10+
import { join } from 'path';
11+
812
import { Kind } from '../kinds';
913
import { expect } from 'chai';
1014
import { describe, it } from 'mocha';
1115
import { parse, parseValue, parseType } from '../parser';
1216
import { Source } from '../source';
13-
import { readFileSync } from 'fs';
14-
import { join } from 'path';
1517
import dedent from '../../jsutils/dedent';
18+
import toJSONDeep from './toJSONDeep';
1619

1720
function expectSyntaxError(text, message, location) {
18-
try {
19-
parse(text);
20-
expect.fail('Expected to throw syntax error');
21-
} catch (error) {
22-
expect(error.message).to.contain(message);
23-
expect(error.locations).to.deep.equal([location]);
24-
}
21+
expect(() => parse(text))
22+
.to.throw(message)
23+
.with.deep.property('locations', [location]);
2524
}
2625

2726
describe('Parser', () => {
@@ -43,21 +42,19 @@ describe('Parser', () => {
4342
caughtError = error;
4443
}
4544

46-
expect(caughtError.message).to.equal(
47-
'Syntax Error: Expected Name, found <EOF>',
48-
);
45+
expect(caughtError).to.deep.contain({
46+
message: 'Syntax Error: Expected Name, found <EOF>',
47+
positions: [1],
48+
locations: [{ line: 1, column: 2 }],
49+
});
4950

5051
expect(String(caughtError)).to.equal(dedent`
5152
Syntax Error: Expected Name, found <EOF>
5253
5354
GraphQL request (1:2)
5455
1: {
5556
^
56-
`);
57-
58-
expect(caughtError.positions).to.deep.equal([1]);
59-
60-
expect(caughtError.locations).to.deep.equal([{ line: 1, column: 2 }]);
57+
`);
6158

6259
expectSyntaxError(
6360
`
@@ -127,31 +124,15 @@ describe('Parser', () => {
127124

128125
it('parses multi-byte characters', () => {
129126
// Note: \u0A0A could be naively interpretted as two line-feed chars.
130-
expect(
131-
parse(`
132-
# This comment has a \u0A0A multi-byte character.
133-
{ field(arg: "Has a \u0A0A multi-byte character.") }
134-
`),
135-
).to.containSubset({
136-
definitions: [
137-
{
138-
selectionSet: {
139-
selections: [
140-
{
141-
arguments: [
142-
{
143-
value: {
144-
kind: Kind.STRING,
145-
value: 'Has a \u0A0A multi-byte character.',
146-
},
147-
},
148-
],
149-
},
150-
],
151-
},
152-
},
153-
],
154-
});
127+
const ast = parse(`
128+
# This comment has a \u0A0A multi-byte character.
129+
{ field(arg: "Has a \u0A0A multi-byte character.") }
130+
`);
131+
132+
expect(ast).to.have.nested.property(
133+
'definitions[0].selectionSet.selections[0].arguments[0].value.value',
134+
'Has a \u0A0A multi-byte character.',
135+
);
155136
});
156137

157138
const kitchenSink = readFileSync(join(__dirname, '/kitchen-sink.graphql'), {
@@ -173,22 +154,20 @@ describe('Parser', () => {
173154
'false',
174155
];
175156
nonKeywords.forEach(keyword => {
176-
let fragmentName = keyword;
177157
// You can't define or reference a fragment named `on`.
178-
if (keyword === 'on') {
179-
fragmentName = 'a';
180-
}
181-
expect(() => {
182-
parse(dedent`
183-
query ${keyword} {
184-
... ${fragmentName}
185-
... on ${keyword} { field }
186-
}
187-
fragment ${fragmentName} on Type {
188-
${keyword}(${keyword}: $${keyword})
189-
@${keyword}(${keyword}: ${keyword})
190-
}`);
191-
}).to.not.throw();
158+
const fragmentName = keyword !== 'on' ? keyword : 'a';
159+
const document = `
160+
query ${keyword} {
161+
... ${fragmentName}
162+
... on ${keyword} { field }
163+
}
164+
fragment ${fragmentName} on Type {
165+
${keyword}(${keyword}: $${keyword})
166+
@${keyword}(${keyword}: ${keyword})
167+
}
168+
`;
169+
170+
expect(() => parse(document)).to.not.throw();
192171
});
193172
});
194173

@@ -233,16 +212,16 @@ describe('Parser', () => {
233212
});
234213

235214
it('creates ast', () => {
236-
const source = new Source(`{
237-
node(id: 4) {
238-
id,
239-
name
240-
}
241-
}
242-
`);
243-
const result = parse(source);
215+
const result = parse(dedent`
216+
{
217+
node(id: 4) {
218+
id,
219+
name
220+
}
221+
}
222+
`);
244223

245-
expect(result).to.containSubset({
224+
expect(toJSONDeep(result)).to.deep.equal({
246225
kind: Kind.DOCUMENT,
247226
loc: { start: 0, end: 41 },
248227
definitions: [
@@ -324,15 +303,15 @@ describe('Parser', () => {
324303
});
325304

326305
it('creates ast from nameless query without variables', () => {
327-
const source = new Source(`query {
328-
node {
329-
id
330-
}
331-
}
332-
`);
333-
const result = parse(source);
306+
const result = parse(dedent`
307+
query {
308+
node {
309+
id
310+
}
311+
}
312+
`);
334313

335-
expect(result).to.containSubset({
314+
expect(toJSONDeep(result)).to.deep.equal({
336315
kind: Kind.DOCUMENT,
337316
loc: { start: 0, end: 30 },
338317
definitions: [
@@ -386,56 +365,52 @@ describe('Parser', () => {
386365
});
387366

388367
it('allows parsing without source location information', () => {
389-
const source = new Source('{ id }');
390-
const result = parse(source, { noLocation: true });
368+
const result = parse('{ id }', { noLocation: true });
391369
expect(result.loc).to.equal(undefined);
392370
});
393371

394372
it('Experimental: allows parsing fragment defined variables', () => {
395-
const source = new Source(
396-
'fragment a($v: Boolean = false) on t { f(v: $v) }',
397-
);
373+
const document = 'fragment a($v: Boolean = false) on t { f(v: $v) }';
374+
398375
expect(() =>
399-
parse(source, { experimentalFragmentVariables: true }),
376+
parse(document, { experimentalFragmentVariables: true }),
400377
).to.not.throw();
401-
expect(() => parse(source)).to.throw('Syntax Error');
378+
expect(() => parse(document)).to.throw('Syntax Error');
402379
});
403380

404381
it('contains location information that only stringifys start/end', () => {
405-
const source = new Source('{ id }');
406-
const result = parse(source);
382+
const result = parse('{ id }');
383+
407384
expect(JSON.stringify(result.loc)).to.equal('{"start":0,"end":6}');
408-
// NB: util.inspect used to suck
409-
if (parseFloat(process.version.slice(1)) > 0.1) {
410-
expect(require('util').inspect(result.loc)).to.equal(
411-
'{ start: 0, end: 6 }',
412-
);
413-
}
385+
expect(inspect(result.loc)).to.equal('{ start: 0, end: 6 }');
414386
});
415387

416388
it('contains references to source', () => {
417389
const source = new Source('{ id }');
418390
const result = parse(source);
391+
419392
expect(result.loc.source).to.equal(source);
420393
});
421394

422395
it('contains references to start and end tokens', () => {
423-
const source = new Source('{ id }');
424-
const result = parse(source);
396+
const result = parse('{ id }');
397+
425398
expect(result.loc.startToken.kind).to.equal('<SOF>');
426399
expect(result.loc.endToken.kind).to.equal('<EOF>');
427400
});
428401

429402
describe('parseValue', () => {
430403
it('parses null value', () => {
431-
expect(parseValue('null')).to.containSubset({
404+
const result = parseValue('null');
405+
expect(toJSONDeep(result)).to.deep.equal({
432406
kind: Kind.NULL,
433407
loc: { start: 0, end: 4 },
434408
});
435409
});
436410

437411
it('parses list values', () => {
438-
expect(parseValue('[123 "abc"]')).to.containSubset({
412+
const result = parseValue('[123 "abc"]');
413+
expect(toJSONDeep(result)).to.deep.equal({
439414
kind: Kind.LIST,
440415
loc: { start: 0, end: 11 },
441416
values: [
@@ -448,13 +423,15 @@ describe('Parser', () => {
448423
kind: Kind.STRING,
449424
loc: { start: 5, end: 10 },
450425
value: 'abc',
426+
block: false,
451427
},
452428
],
453429
});
454430
});
455431

456432
it('parses block strings', () => {
457-
expect(parseValue('["""long""" "short"]')).to.containSubset({
433+
const result = parseValue('["""long""" "short"]');
434+
expect(toJSONDeep(result)).to.deep.equal({
458435
kind: Kind.LIST,
459436
loc: { start: 0, end: 20 },
460437
values: [
@@ -477,7 +454,8 @@ describe('Parser', () => {
477454

478455
describe('parseType', () => {
479456
it('parses well known types', () => {
480-
expect(parseType('String')).to.containSubset({
457+
const result = parseType('String');
458+
expect(toJSONDeep(result)).to.deep.equal({
481459
kind: Kind.NAMED_TYPE,
482460
loc: { start: 0, end: 6 },
483461
name: {
@@ -489,7 +467,8 @@ describe('Parser', () => {
489467
});
490468

491469
it('parses custom types', () => {
492-
expect(parseType('MyType')).to.containSubset({
470+
const result = parseType('MyType');
471+
expect(toJSONDeep(result)).to.deep.equal({
493472
kind: Kind.NAMED_TYPE,
494473
loc: { start: 0, end: 6 },
495474
name: {
@@ -501,7 +480,8 @@ describe('Parser', () => {
501480
});
502481

503482
it('parses list types', () => {
504-
expect(parseType('[MyType]')).to.containSubset({
483+
const result = parseType('[MyType]');
484+
expect(toJSONDeep(result)).to.deep.equal({
505485
kind: Kind.LIST_TYPE,
506486
loc: { start: 0, end: 8 },
507487
type: {
@@ -517,7 +497,8 @@ describe('Parser', () => {
517497
});
518498

519499
it('parses non-null types', () => {
520-
expect(parseType('MyType!')).to.containSubset({
500+
const result = parseType('MyType!');
501+
expect(toJSONDeep(result)).to.deep.equal({
521502
kind: Kind.NON_NULL_TYPE,
522503
loc: { start: 0, end: 7 },
523504
type: {
@@ -533,7 +514,8 @@ describe('Parser', () => {
533514
});
534515

535516
it('parses nested types', () => {
536-
expect(parseType('[MyType!]')).to.containSubset({
517+
const result = parseType('[MyType!]');
518+
expect(toJSONDeep(result)).to.deep.equal({
537519
kind: Kind.LIST_TYPE,
538520
loc: { start: 0, end: 9 },
539521
type: {

0 commit comments

Comments
 (0)