platform/packages/graphql/graphql_parser/test/inline_fragment_test.dart

77 lines
2.4 KiB
Dart
Raw Normal View History

2017-07-04 17:32:52 +00:00
import 'package:graphql_parser/graphql_parser.dart';
import 'package:test/test.dart';
import 'common.dart';
import 'argument_test.dart';
import 'directive_test.dart';
import 'field_test.dart';
import 'fragment_spread_test.dart';
import 'selection_set_test.dart';
main() {
test('no directives', () {
expect(
'... on foo {bar, baz: quux}',
isInlineFragment('foo',
selectionSet: isSelectionSet([
isField(fieldName: isFieldName('bar')),
isField(fieldName: isFieldName('baz', alias: 'quux'))
])));
});
test('with directives', () {
expect(
'... on foo @bar @baz: 2 @quux(one: 1) {... bar}',
isInlineFragment('foo',
directives: isDirectiveList([
isDirective('bar'),
isDirective('baz', valueOrVariable: equals(2)),
isDirective('quux', argument: isArgument('one', 1))
]),
selectionSet: isSelectionSet([isFragmentSpread('bar')])));
});
test('exceptions', () {
2018-08-05 00:44:41 +00:00
var throwsSyntaxError = predicate((x) {
var parser = parse(x.toString())..parseInlineFragment();
return parser.errors.isNotEmpty;
}, 'fails to parse inline fragment');
expect('... on foo', throwsSyntaxError);
expect('... on foo @bar', throwsSyntaxError);
expect('... on', throwsSyntaxError);
expect('...', throwsSyntaxError);
2017-07-04 17:32:52 +00:00
});
}
InlineFragmentContext parseInlineFragment(String text) =>
parse(text).parseInlineFragment();
Matcher isInlineFragment(String name,
{Matcher directives, Matcher selectionSet}) =>
2019-08-08 02:24:19 +00:00
_IsInlineFragment(name, directives, selectionSet);
2017-07-04 17:32:52 +00:00
class _IsInlineFragment extends Matcher {
final String name;
final Matcher directives, selectionSet;
_IsInlineFragment(this.name, this.directives, this.selectionSet);
@override
Description describe(Description description) {
return description.add('is an inline fragment named "$name"');
}
@override
bool matches(item, Map matchState) {
2018-08-04 19:18:53 +00:00
var fragment = item is InlineFragmentContext
? item
: parseInlineFragment(item.toString());
2017-07-04 17:32:52 +00:00
if (fragment == null) return false;
if (fragment.typeCondition.typeName.name != name) return false;
if (directives != null &&
!directives.matches(fragment.directives, matchState)) return false;
if (selectionSet != null &&
!selectionSet.matches(fragment.selectionSet, matchState)) return false;
return true;
}
}