platform/graphql_parser/test/variable_test.dart

44 lines
1 KiB
Dart
Raw Normal View History

2017-07-03 15:53:19 +00:00
import 'package:test/test.dart';
2018-08-05 00:44:41 +00:00
2017-07-03 15:53:19 +00:00
import 'common.dart';
main() {
test('variables', () {
expect(r'$a', isVariable('a'));
expect(r'$abc', isVariable('abc'));
expect(r'$abc123', isVariable('abc123'));
expect(r'$_', isVariable('_'));
expect(r'$___', isVariable('___'));
expect(r'$_123', isVariable('_123'));
});
test('exceptions', () {
2018-08-05 00:44:41 +00:00
var throwsSyntaxError = predicate((x) {
var parser = parse(x.toString())..parseVariable();
return parser.errors.isNotEmpty;
}, 'fails to parse variable');
expect(r'$', throwsSyntaxError);
2017-07-03 15:53:19 +00:00
});
}
Matcher isVariable(String name) => new _IsVariable(name);
class _IsVariable extends Matcher {
final String name;
_IsVariable(this.name);
@override
Description describe(Description description) {
return description.add('parses as a variable named "$name"');
}
@override
2018-08-02 13:05:52 +00:00
bool matches(item, Map matchState) {
var p = parse(item.toString());
2017-07-03 15:53:19 +00:00
var v = p.parseVariable();
return equals(name).matches(v?.name, matchState);
}
}