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
|
|
|
|
2019-08-08 02:24:19 +00:00
|
|
|
Matcher isArgument(String name, value) => _IsArgument(name, value);
|
2017-07-03 23:03:36 +00:00
|
|
|
|
2019-08-08 02:45:51 +00:00
|
|
|
Matcher isArgumentList(List<Matcher> arguments) => _IsArgumentList(arguments);
|
2017-07-04 16:46:01 +00:00
|
|
|
|
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());
|
2019-08-08 02:03:01 +00:00
|
|
|
|
|
|
|
var v = arg.value;
|
2017-07-03 23:03:36 +00:00
|
|
|
return equals(name).matches(arg.name, matchState) &&
|
2019-08-08 02:03:01 +00:00
|
|
|
((v is VariableContext && equals(value).matches(v.name, matchState)) ||
|
2019-08-08 01:53:11 +00:00
|
|
|
equals(value).matches(arg.value.computeValue({}), matchState));
|
2017-07-03 23:03:36 +00:00
|
|
|
}
|
|
|
|
}
|
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;
|
|
|
|
}
|
|
|
|
}
|