2017-07-04 16:46:01 +00:00
|
|
|
import 'package:graphql_parser/graphql_parser.dart';
|
|
|
|
import 'package:test/test.dart';
|
|
|
|
import 'common.dart';
|
|
|
|
import 'field_test.dart';
|
|
|
|
import 'fragment_spread_test.dart';
|
2017-07-04 17:32:52 +00:00
|
|
|
import 'inline_fragment_test.dart';
|
2017-07-04 16:46:01 +00:00
|
|
|
|
|
|
|
main() {
|
|
|
|
test('empty', () {
|
|
|
|
expect('{}', isSelectionSet([]));
|
|
|
|
});
|
|
|
|
|
|
|
|
test('with commas', () {
|
|
|
|
expect(
|
|
|
|
'{foo, bar: baz}',
|
|
|
|
isSelectionSet([
|
|
|
|
isField(fieldName: isFieldName('foo')),
|
|
|
|
isField(fieldName: isFieldName('bar', alias: 'baz'))
|
|
|
|
]));
|
|
|
|
});
|
|
|
|
|
|
|
|
test('no commas', () {
|
|
|
|
expect(
|
2017-07-04 17:32:52 +00:00
|
|
|
'''
|
|
|
|
{
|
|
|
|
foo
|
|
|
|
bar: baz ...quux
|
|
|
|
... on foo {bar, baz}
|
|
|
|
}'''
|
|
|
|
.split('\n')
|
|
|
|
.map((s) => s.trim())
|
|
|
|
.join(' '),
|
2017-07-04 16:46:01 +00:00
|
|
|
isSelectionSet([
|
|
|
|
isField(fieldName: isFieldName('foo')),
|
|
|
|
isField(fieldName: isFieldName('bar', alias: 'baz')),
|
2017-07-04 17:32:52 +00:00
|
|
|
isFragmentSpread('quux'),
|
|
|
|
isInlineFragment('foo',
|
|
|
|
selectionSet: isSelectionSet([
|
|
|
|
isField(fieldName: isFieldName('bar')),
|
|
|
|
isField(fieldName: isFieldName('baz')),
|
|
|
|
]))
|
2017-07-04 16:46:01 +00:00
|
|
|
]));
|
|
|
|
});
|
|
|
|
|
|
|
|
test('exceptions', () {
|
2018-08-05 00:44:41 +00:00
|
|
|
var throwsSyntaxError = predicate((x) {
|
|
|
|
var parser = parse(x.toString())..parseSelectionSet();
|
|
|
|
return parser.errors.isNotEmpty;
|
|
|
|
}, 'fails to parse selection set');
|
2019-03-29 18:45:02 +00:00
|
|
|
|
2018-08-05 00:44:41 +00:00
|
|
|
expect('{foo,bar,baz', throwsSyntaxError);
|
2017-07-04 16:46:01 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
SelectionSetContext parseSelectionSet(String text) =>
|
|
|
|
parse(text).parseSelectionSet();
|
|
|
|
|
2019-08-08 02:28:20 +00:00
|
|
|
Matcher isSelectionSet(List<Matcher> selections) => _IsSelectionSet(selections);
|
2017-07-04 16:46:01 +00:00
|
|
|
|
|
|
|
class _IsSelectionSet extends Matcher {
|
|
|
|
final List<Matcher> selections;
|
|
|
|
|
|
|
|
_IsSelectionSet(this.selections);
|
|
|
|
|
|
|
|
@override
|
|
|
|
Description describe(Description description) {
|
|
|
|
return description
|
|
|
|
.add('is selection set with ${selections.length} selection(s)');
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
bool matches(item, Map matchState) {
|
2018-08-04 19:18:53 +00:00
|
|
|
var set =
|
|
|
|
item is SelectionSetContext ? item : parseSelectionSet(item.toString());
|
2019-03-31 20:29:10 +00:00
|
|
|
|
|
|
|
// if (set != null) {
|
|
|
|
// print('Item: $set has ${set.selections.length} selection(s):');
|
|
|
|
// for (var s in set.selections) {
|
|
|
|
// print(' * $s (${s.span.text})');
|
|
|
|
// }
|
|
|
|
// }
|
2019-03-31 20:21:11 +00:00
|
|
|
|
2017-07-04 16:46:01 +00:00
|
|
|
if (set == null) return false;
|
|
|
|
if (set.selections.length != selections.length) return false;
|
|
|
|
|
|
|
|
for (int i = 0; i < set.selections.length; i++) {
|
|
|
|
var sel = set.selections[i];
|
|
|
|
if (!selections[i].matches(
|
2019-08-08 02:28:20 +00:00
|
|
|
sel.field ?? sel.fragmentSpread ?? sel.inlineFragment, matchState)) {
|
2017-07-04 16:46:01 +00:00
|
|
|
return false;
|
2019-08-08 02:28:20 +00:00
|
|
|
}
|
2017-07-04 16:46:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|