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

59 lines
1.6 KiB
Dart
Raw Normal View History

2017-07-04 16:46:01 +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';
main() {
test('name only', () {
expect(['...foo', '... foo'], everyElement(isFragmentSpread('foo')));
});
test('with directives', () {
expect(
'... foo @bar @baz: 2 @quux(one: 1)',
isFragmentSpread('foo',
directives: isDirectiveList([
isDirective('bar'),
isDirective('baz', valueOrVariable: equals(2)),
isDirective('quux', argument: isArgument('one', 1))
])));
});
}
FragmentSpreadContext parseFragmentSpread(String text) =>
parse(text).parseFragmentSpread();
Matcher isFragmentSpread(String name, {Matcher directives}) =>
2019-08-08 02:24:19 +00:00
_IsFragmentSpread(name, directives);
2017-07-04 16:46:01 +00:00
class _IsFragmentSpread extends Matcher {
final String name;
final Matcher directives;
_IsFragmentSpread(this.name, this.directives);
@override
Description describe(Description description) {
2019-08-08 02:28:20 +00:00
if (directives != null) {
2017-07-04 16:46:01 +00:00
return directives.describe(
description.add('is a fragment spread named "$name" that also '));
2019-08-08 02:28:20 +00:00
}
2017-07-04 16:46:01 +00:00
return description.add('is a fragment spread named "$name"');
}
@override
bool matches(item, Map matchState) {
2018-08-04 19:18:53 +00:00
var spread = item is FragmentSpreadContext
? item
: parseFragmentSpread(item.toString());
2017-07-04 16:46:01 +00:00
if (spread == null) return false;
if (spread.name != name) return false;
2019-08-08 02:28:20 +00:00
if (directives != null) {
2017-07-04 16:46:01 +00:00
return directives.matches(spread.directives, matchState);
2019-08-08 02:28:20 +00:00
} else {
2017-07-04 16:46:01 +00:00
return true;
2019-08-08 02:28:20 +00:00
}
2017-07-04 16:46:01 +00:00
}
}