platform/graphql_parser/test/argument_test.dart

89 lines
2.4 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';
2018-08-05 00:44:41 +00:00
2017-07-03 23:03:36 +00:00
import 'common.dart';
main() {
test('argument', () {
expect('foo: 2', isArgument('foo', 2));
expect(r'foo: $bar', isArgument('foo', 'bar'));
});
test('exception', () {
2018-08-05 00:44:41 +00:00
var isSyntaxError = predicate((x) {
var parser = parse(x.toString())..parseArgument();
return parser.errors.isNotEmpty;
}, 'fails to parse argument');
var isSyntaxErrorOnArguments = predicate((x) {
var parser = parse(x.toString())..parseArguments();
return parser.errors.isNotEmpty;
}, 'fails to parse arguments');
expect('foo', isSyntaxError);
expect('foo:', isSyntaxError);
expect(r'(foo: $bar', isSyntaxErrorOnArguments);
2017-07-03 23:03:36 +00:00
});
}
ArgumentContext parseArgument(String text) => parse(text).parseArgument();
2018-08-05 00:44:41 +00:00
2018-08-04 19:18:53 +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) {
2018-08-02 13:05:52 +00:00
var arg = item is ArgumentContext ? item : parseArgument(item.toString());
2017-07-03 23:03:36 +00:00
if (arg == null) return false;
2018-08-02 13:25:41 +00:00
print(arg.span.highlight());
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) {
2018-08-04 19:18:53 +00:00
var args = item is List<ArgumentContext>
? item
: parse(item.toString()).parseArguments();
2017-07-04 16:46:01 +00:00
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;
}
}