platform/graphql_parser/test/value_test.dart

94 lines
2.4 KiB
Dart
Raw Normal View History

2017-07-03 15:37:35 +00:00
import 'dart:math' as math;
2018-08-05 00:44:41 +00:00
2017-07-03 15:53:19 +00:00
import 'package:graphql_parser/graphql_parser.dart';
2017-07-03 15:37:35 +00:00
import 'package:test/test.dart';
2018-08-05 00:44:41 +00:00
2017-07-03 15:37:35 +00:00
import 'common.dart';
main() {
test('boolean', () {
2017-07-04 15:58:22 +00:00
expect('true', isValue(true));
expect('false', isValue(false));
2017-07-03 15:37:35 +00:00
});
test('number', () {
2017-07-04 15:58:22 +00:00
expect('1', isValue(1));
expect('1.0', isValue(1.0));
expect('-1', isValue(-1));
expect('-1.0', isValue(-1.0));
expect('6.26e-34', isValue(6.26 * math.pow(10, -34)));
expect('-6.26e-34', isValue(-6.26 * math.pow(10, -34)));
expect('-6.26e34', isValue(-6.26 * math.pow(10, 34)));
2017-07-03 15:37:35 +00:00
});
test('array', () {
2017-07-04 15:58:22 +00:00
expect('[]', isValue([]));
expect('[1,2]', isValue([1, 2]));
expect('[1,2, 3]', isValue([1, 2, 3]));
expect('["a"]', isValue(['a']));
2017-07-03 15:37:35 +00:00
});
test('string', () {
2017-07-04 15:58:22 +00:00
expect('""', isValue(''));
expect('"a"', isValue('a'));
expect('"abc"', isValue('abc'));
expect('"\\""', isValue('"'));
expect('"\\b"', isValue('\b'));
expect('"\\f"', isValue('\f'));
expect('"\\n"', isValue('\n'));
expect('"\\r"', isValue('\r'));
expect('"\\t"', isValue('\t'));
expect('"\\u0123"', isValue('\u0123'));
expect('"\\u0123\\u4567"', isValue('\u0123\u4567'));
2017-07-03 15:37:35 +00:00
});
2017-07-03 15:53:19 +00:00
2018-08-05 00:44:41 +00:00
test('block string', () {
expect('""""""', isValue(''));
expect('"""abc"""', isValue('abc'));
expect('"""\n\n\nabc\n\n\n"""', isValue('abc'));
});
test('object', () {
expect('{}', isValue(<String, dynamic>{}));
expect('{a: 2}', isValue(<String, dynamic>{'a': 2}));
expect('{a: 2, b: "c"}', isValue(<String, dynamic>{'a': 2, 'b': 'c'}));
});
test('null', () {
expect('null', isValue(null));
});
test('enum', () {
expect('FOO_BAR', isValue('FOO_BAR'));
});
2017-07-03 15:53:19 +00:00
test('exceptions', () {
2018-08-05 00:44:41 +00:00
var throwsSyntaxError = predicate((x) {
var parser = parse(x.toString())..parseValue();
return parser.errors.isNotEmpty;
}, 'fails to parse value');
expect('[1', throwsSyntaxError);
2017-07-03 15:53:19 +00:00
});
}
2019-08-08 01:56:48 +00:00
InputValueContext parseValue(String text) => parse(text).parseInputValue();
2018-08-05 00:44:41 +00:00
2017-07-04 15:58:22 +00:00
Matcher isValue(value) => new _IsValue(value);
2017-07-03 15:53:19 +00:00
2017-07-04 15:58:22 +00:00
class _IsValue extends Matcher {
2017-07-03 15:53:19 +00:00
final value;
2017-07-04 15:58:22 +00:00
_IsValue(this.value);
2017-07-03 15:53:19 +00:00
@override
Description describe(Description description) =>
description.add('equals $value when parsed as a GraphQL value');
@override
2017-07-04 15:58:22 +00:00
bool matches(item, Map matchState) {
2019-08-08 01:56:48 +00:00
var v = item is InputValueContext ? item : parseValue(item.toString());
return equals(value).matches(v.computeValue({}), matchState);
2017-07-03 15:53:19 +00:00
}
2017-07-03 15:37:35 +00:00
}