platform/packages/graphql/graphql_parser/test/variable_test.dart
Tobe O 4e69153e3e Add 'packages/graphql/' from commit '33e2f86ba73d559197b6270df036256104726aca'
git-subtree-dir: packages/graphql
git-subtree-mainline: ac29392d7f
git-subtree-split: 33e2f86ba7
2020-02-15 18:22:07 -05:00

43 lines
1 KiB
Dart

import 'package:test/test.dart';
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', () {
var throwsSyntaxError = predicate((x) {
var parser = parse(x.toString())..parseVariable();
return parser.errors.isNotEmpty;
}, 'fails to parse variable');
expect(r'$', throwsSyntaxError);
});
}
Matcher isVariable(String name) => _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
bool matches(item, Map matchState) {
var p = parse(item.toString());
var v = p.parseVariable();
return equals(name).matches(v?.name, matchState);
}
}