From 5c2649d2d9c44d632ed3a692be67636f25a8203f Mon Sep 17 00:00:00 2001 From: thosakwe Date: Mon, 3 Jul 2017 11:53:19 -0400 Subject: [PATCH] Variable also work --- .idea/graphql_parser.iml | 18 +++++++++ .idea/modules.xml | 8 ++++ .idea/runConfigurations/All_Tests.xml | 6 +++ .../tests_in_graphql_parser.xml | 8 ++++ .idea/vcs.xml | 6 +++ example/basic.dart | 3 +- lib/graphql_parser.dart | 2 +- lib/src/language/ast/definition.dart | 4 +- lib/src/language/ast/type_condition.dart | 4 +- lib/src/language/ast/variable.dart | 2 + lib/src/language/language.dart | 2 +- lib/src/language/lexer.dart | 4 +- lib/src/language/token_type.dart | 2 +- test/all.dart | 6 +++ test/common.dart | 20 +--------- test/value_test.dart | 29 ++++++++++++++- test/variable_test.dart | 37 +++++++++++++++++++ 17 files changed, 130 insertions(+), 31 deletions(-) create mode 100644 .idea/graphql_parser.iml create mode 100644 .idea/modules.xml create mode 100644 .idea/runConfigurations/All_Tests.xml create mode 100644 .idea/runConfigurations/tests_in_graphql_parser.xml create mode 100644 .idea/vcs.xml create mode 100644 test/all.dart create mode 100644 test/variable_test.dart diff --git a/.idea/graphql_parser.iml b/.idea/graphql_parser.iml new file mode 100644 index 00000000..7fe256fb --- /dev/null +++ b/.idea/graphql_parser.iml @@ -0,0 +1,18 @@ + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 00000000..98dbd53e --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/runConfigurations/All_Tests.xml b/.idea/runConfigurations/All_Tests.xml new file mode 100644 index 00000000..0e975a39 --- /dev/null +++ b/.idea/runConfigurations/All_Tests.xml @@ -0,0 +1,6 @@ + + + + \ No newline at end of file diff --git a/.idea/runConfigurations/tests_in_graphql_parser.xml b/.idea/runConfigurations/tests_in_graphql_parser.xml new file mode 100644 index 00000000..e3ea6792 --- /dev/null +++ b/.idea/runConfigurations/tests_in_graphql_parser.xml @@ -0,0 +1,8 @@ + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 00000000..94a25f7f --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/example/basic.dart b/example/basic.dart index 661c7617..c4ef1d54 100644 --- a/example/basic.dart +++ b/example/basic.dart @@ -7,7 +7,8 @@ final String INPUT = ''' tagline } } - '''.trim(); + ''' + .trim(); main() { var tokens = scan(INPUT); diff --git a/lib/graphql_parser.dart b/lib/graphql_parser.dart index a03df794..864f124c 100644 --- a/lib/graphql_parser.dart +++ b/lib/graphql_parser.dart @@ -1,2 +1,2 @@ export 'src/language/ast/ast.dart'; -export 'src/language/language.dart'; \ No newline at end of file +export 'src/language/language.dart'; diff --git a/lib/src/language/ast/definition.dart b/lib/src/language/ast/definition.dart index 042d5876..d982eef2 100644 --- a/lib/src/language/ast/definition.dart +++ b/lib/src/language/ast/definition.dart @@ -1,5 +1,3 @@ import 'node.dart'; -abstract class DefinitionContext extends Node { - -} \ No newline at end of file +abstract class DefinitionContext extends Node {} diff --git a/lib/src/language/ast/type_condition.dart b/lib/src/language/ast/type_condition.dart index e05d6e44..5acc5c7f 100644 --- a/lib/src/language/ast/type_condition.dart +++ b/lib/src/language/ast/type_condition.dart @@ -6,10 +6,10 @@ class TypeConditionContext extends Node { final TypeNameContext typeName; TypeConditionContext(this.typeName); - + @override SourceSpan get span => typeName.span; @override String toSource() => typeName.toSource(); -} \ No newline at end of file +} diff --git a/lib/src/language/ast/variable.dart b/lib/src/language/ast/variable.dart index 0d492d2d..e9753f99 100644 --- a/lib/src/language/ast/variable.dart +++ b/lib/src/language/ast/variable.dart @@ -7,6 +7,8 @@ class VariableContext extends Node { VariableContext(this.DOLLAR, this.NAME); + String get name => NAME.text; + @override SourceSpan get span => new SourceSpan(DOLLAR?.span?.start, NAME?.span?.end, toSource()); diff --git a/lib/src/language/language.dart b/lib/src/language/language.dart index 95db2ae3..31ec5a43 100644 --- a/lib/src/language/language.dart +++ b/lib/src/language/language.dart @@ -4,4 +4,4 @@ export 'lexer.dart'; export 'parser.dart'; export 'syntax_error.dart'; export 'token.dart'; -export 'token_type.dart'; \ No newline at end of file +export 'token_type.dart'; diff --git a/lib/src/language/lexer.dart b/lib/src/language/lexer.dart index 36395398..63eb94c6 100644 --- a/lib/src/language/lexer.dart +++ b/lib/src/language/lexer.dart @@ -45,7 +45,8 @@ List scan(String text) { for (var pattern in _patterns.keys) { if (scanner.matches(pattern)) { - potential.add(new Token(_patterns[pattern], scanner.lastMatch[0], scanner.lastSpan)); + potential.add(new Token( + _patterns[pattern], scanner.lastMatch[0], scanner.lastSpan)); } } @@ -57,7 +58,6 @@ List scan(String text) { // Choose longest token potential.sort((a, b) => b.text.length.compareTo(a.text.length)); var chosen = potential.first; - var start = scanner.state; out.add(chosen); scanner.scan(chosen.text); } diff --git a/lib/src/language/token_type.dart b/lib/src/language/token_type.dart index cffc0bf1..6452546d 100644 --- a/lib/src/language/token_type.dart +++ b/lib/src/language/token_type.dart @@ -23,4 +23,4 @@ enum TokenType { STRING, NAME -} \ No newline at end of file +} diff --git a/test/all.dart b/test/all.dart new file mode 100644 index 00000000..4b6eb3f9 --- /dev/null +++ b/test/all.dart @@ -0,0 +1,6 @@ +import 'package:test/test.dart'; +import 'value_test.dart' as value; + +main() { + group('value', value.main); +} \ No newline at end of file diff --git a/test/common.dart b/test/common.dart index a08dc700..f0f028e3 100644 --- a/test/common.dart +++ b/test/common.dart @@ -1,23 +1,7 @@ import 'package:graphql_parser/graphql_parser.dart'; import 'package:matcher/matcher.dart'; +import 'package:test/test.dart'; Parser parse(String text) => new Parser(scan(text)); -Matcher equalsParsed(value) => new _EqualsParsed(value); - -class _EqualsParsed extends Matcher { - final value; - - _EqualsParsed(this.value); - - @override - Description describe(Description description) - => description.add('equals $value when parsed as a GraphQL value'); - - @override - bool matches(String item, Map matchState) { - var p = parse(item); - var v = p.parseValue(); - return equals(value).matches(v.value, matchState); - } -} \ No newline at end of file +final Matcher throwsSyntaxError = throwsA(const isInstanceOf()); diff --git a/test/value_test.dart b/test/value_test.dart index 19e1c504..281c7e90 100644 --- a/test/value_test.dart +++ b/test/value_test.dart @@ -1,4 +1,5 @@ import 'dart:math' as math; +import 'package:graphql_parser/graphql_parser.dart'; import 'package:test/test.dart'; import 'common.dart'; @@ -20,8 +21,8 @@ main() { test('array', () { expect('[]', equalsParsed([])); - expect('[1,2]', equalsParsed([1,2])); - expect('[1,2, 3]', equalsParsed([1,2,3])); + expect('[1,2]', equalsParsed([1, 2])); + expect('[1,2, 3]', equalsParsed([1, 2, 3])); expect('["a"]', equalsParsed(['a'])); }); @@ -38,4 +39,28 @@ main() { expect('"\\u0123"', equalsParsed('\u0123')); expect('"\\u0123\\u4567"', equalsParsed('\u0123\u4567')); }); + + test('exceptions', () { + expect(() => parseValue('[1'), throwsSyntaxError); + }); +} + +ValueContext parseValue(String text) => parse(text).parseValue(); +Matcher equalsParsed(value) => new _EqualsParsed(value); + +class _EqualsParsed extends Matcher { + final value; + + _EqualsParsed(this.value); + + @override + Description describe(Description description) => + description.add('equals $value when parsed as a GraphQL value'); + + @override + bool matches(String item, Map matchState) { + var p = parse(item); + var v = p.parseValue(); + return equals(value).matches(v.value, matchState); + } } diff --git a/test/variable_test.dart b/test/variable_test.dart new file mode 100644 index 00000000..ee9ff10d --- /dev/null +++ b/test/variable_test.dart @@ -0,0 +1,37 @@ +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', () { + expect(() => parse(r'$').parseVariable(), throwsSyntaxError); + }); +} + +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 + bool matches(String item, Map matchState) { + var p = parse(item); + var v = p.parseVariable(); + return equals(name).matches(v?.name, matchState); + } +}