platform/graphql_parser/test/argument_test.dart

75 lines
2 KiB
Dart
Raw Normal View History

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 'common.dart';
main() {
test('argument', () {
expect('foo: 2', isArgument('foo', 2));
expect(r'foo: $bar', isArgument('foo', 'bar'));
});
test('exception', () {
expect(() => parseArgument('foo'), throwsSyntaxError);
expect(() => parseArgument('foo:'), throwsSyntaxError);
2017-07-04 17:32:52 +00:00
expect(() => parseArgumentList(r'(foo: $bar'), throwsSyntaxError);
2017-07-03 23:03:36 +00:00
});
}
ArgumentContext parseArgument(String text) => parse(text).parseArgument();
2017-07-04 17:32:52 +00:00
List<ArgumentContext> parseArgumentList(String text) => parse(text).parseArguments();
2017-07-03 23:03:36 +00:00
Matcher isArgument(String name, value) => new _IsArgument(name, value);
2017-07-04 16:46:01 +00:00
Matcher isArgumentList(List<Matcher> arguments) =>
new _IsArgumentList(arguments);
2017-07-03 23:03:36 +00:00
class _IsArgument extends Matcher {
final String name;
final value;
_IsArgument(this.name, this.value);
@override
Description describe(Description description) {
return description.add('is an argument named "$name" with value $value');
}
@override
bool matches(item, Map matchState) {
var arg = item is ArgumentContext ? item : parseArgument(item);
if (arg == null) return false;
2017-07-05 22:44:13 +00:00
print(arg.toSource());
2017-07-03 23:03:36 +00:00
return equals(name).matches(arg.name, matchState) &&
equals(value).matches(
arg.valueOrVariable.value?.value ??
arg.valueOrVariable.variable?.name,
matchState);
}
}
2017-07-04 16:46:01 +00:00
class _IsArgumentList extends Matcher {
final List<Matcher> arguments;
_IsArgumentList(this.arguments);
@override
Description describe(Description description) {
return description.add('is list of ${arguments.length} argument(s)');
}
@override
bool matches(item, Map matchState) {
var args =
item is List<ArgumentContext> ? item : parse(item).parseArguments();
if (args.length != arguments.length) return false;
for (int i = 0; i < args.length; i++) {
if (!arguments[i].matches(args[i], matchState)) return false;
}
return true;
}
}