platform/graphql_parser/test/value_test.dart

66 lines
1.8 KiB
Dart
Raw Normal View History

2017-07-03 15:37:35 +00:00
import 'dart:math' as math;
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';
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
test('exceptions', () {
expect(() => parseValue('[1'), throwsSyntaxError);
});
}
ValueContext parseValue(String text) => parse(text).parseValue();
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) {
2018-08-02 13:05:52 +00:00
var v = item is ValueContext ? item : parseValue(item.toString());
2017-07-03 15:53:19 +00:00
return equals(value).matches(v.value, matchState);
}
2017-07-03 15:37:35 +00:00
}