2017-07-03 23:03:36 +00:00
|
|
|
import 'package:graphql_parser/graphql_parser.dart';
|
|
|
|
import 'package:matcher/matcher.dart';
|
|
|
|
import 'package:test/test.dart';
|
|
|
|
import 'argument_test.dart';
|
|
|
|
import 'common.dart';
|
|
|
|
|
|
|
|
main() {
|
|
|
|
test('name only', () {
|
|
|
|
expect('@foo', isDirective('foo'));
|
|
|
|
});
|
|
|
|
|
|
|
|
test('with value or variable', () {
|
|
|
|
expect('@foo: 2', isDirective('foo', valueOrVariable: equals(2)));
|
|
|
|
expect(r'@foo: $bar', isDirective('foo', valueOrVariable: equals('bar')));
|
|
|
|
});
|
|
|
|
|
|
|
|
test('with argument', () {
|
|
|
|
expect('@foo (bar: 2)', isDirective('foo', argument: isArgument('bar', 2)));
|
|
|
|
expect(r'@foo (bar: $baz)',
|
|
|
|
isDirective('foo', argument: isArgument('bar', r'baz')));
|
|
|
|
});
|
|
|
|
|
|
|
|
test('exceptions', () {
|
2018-08-05 00:44:41 +00:00
|
|
|
var isSyntaxError = predicate((x) {
|
|
|
|
var parser = parse(x.toString())..parseDirective();
|
|
|
|
return parser.errors.isNotEmpty;
|
|
|
|
}, 'fails to parse directive');
|
2019-03-29 18:45:02 +00:00
|
|
|
|
2018-08-05 00:44:41 +00:00
|
|
|
expect('@', isSyntaxError);
|
|
|
|
expect('@foo:', isSyntaxError);
|
|
|
|
expect('@foo (', isSyntaxError);
|
|
|
|
expect('@foo (bar: 2', isSyntaxError);
|
|
|
|
expect('@foo (', isSyntaxError);
|
2017-07-03 23:03:36 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
DirectiveContext parseDirective(String text) => parse(text).parseDirective();
|
|
|
|
|
|
|
|
Matcher isDirective(String name, {Matcher valueOrVariable, Matcher argument}) =>
|
|
|
|
new _IsDirective(name,
|
|
|
|
valueOrVariable: valueOrVariable, argument: argument);
|
|
|
|
|
2017-07-04 16:46:01 +00:00
|
|
|
Matcher isDirectiveList(List<Matcher> directives) =>
|
|
|
|
new _IsDirectiveList(directives);
|
|
|
|
|
2017-07-03 23:03:36 +00:00
|
|
|
class _IsDirective extends Matcher {
|
|
|
|
final String name;
|
|
|
|
final Matcher valueOrVariable, argument;
|
|
|
|
|
|
|
|
_IsDirective(this.name, {this.valueOrVariable, this.argument});
|
|
|
|
|
|
|
|
@override
|
|
|
|
Description describe(Description description) {
|
|
|
|
var desc = description.add('is a directive with name "$name"');
|
|
|
|
|
|
|
|
if (valueOrVariable != null) {
|
|
|
|
return valueOrVariable.describe(desc.add(' and '));
|
|
|
|
} else if (argument != null) {
|
|
|
|
return argument.describe(desc.add(' and '));
|
|
|
|
} else
|
|
|
|
return desc;
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
2017-07-04 16:46:01 +00:00
|
|
|
bool matches(item, Map matchState) {
|
2018-08-04 19:18:53 +00:00
|
|
|
var directive =
|
|
|
|
item is DirectiveContext ? item : parseDirective(item.toString());
|
2017-07-03 23:03:36 +00:00
|
|
|
if (directive == null) return false;
|
|
|
|
if (valueOrVariable != null) {
|
2019-08-08 01:55:20 +00:00
|
|
|
if (directive.value == null)
|
2017-07-03 23:03:36 +00:00
|
|
|
return false;
|
2019-08-08 01:55:20 +00:00
|
|
|
else {
|
|
|
|
var v = directive.value;
|
|
|
|
if (v is VariableContext) {
|
|
|
|
return valueOrVariable.matches(v.name, matchState);
|
|
|
|
} else {
|
|
|
|
return valueOrVariable.matches(
|
|
|
|
directive.value.computeValue({}), matchState);
|
|
|
|
}
|
|
|
|
}
|
2017-07-03 23:03:36 +00:00
|
|
|
} else if (argument != null) {
|
|
|
|
if (directive.argument == null)
|
|
|
|
return false;
|
|
|
|
else
|
|
|
|
return argument.matches(directive.argument, matchState);
|
|
|
|
} else
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2017-07-04 16:46:01 +00:00
|
|
|
|
|
|
|
class _IsDirectiveList extends Matcher {
|
|
|
|
final List<Matcher> directives;
|
|
|
|
|
|
|
|
_IsDirectiveList(this.directives);
|
|
|
|
|
|
|
|
@override
|
|
|
|
Description describe(Description description) {
|
|
|
|
return description.add('is list of ${directives.length} directive(s)');
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
bool matches(item, Map matchState) {
|
2018-08-04 19:18:53 +00:00
|
|
|
var args = item is List<DirectiveContext>
|
|
|
|
? item
|
|
|
|
: parse(item.toString()).parseDirectives();
|
2017-07-04 16:46:01 +00:00
|
|
|
|
|
|
|
if (args.length != directives.length) return false;
|
|
|
|
|
|
|
|
for (int i = 0; i < args.length; i++) {
|
|
|
|
if (!directives[i].matches(args[i], matchState)) return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|