Variable also work
This commit is contained in:
parent
0f68a1c500
commit
5c2649d2d9
17 changed files with 130 additions and 31 deletions
18
.idea/graphql_parser.iml
Normal file
18
.idea/graphql_parser.iml
Normal file
|
@ -0,0 +1,18 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module type="WEB_MODULE" version="4">
|
||||
<component name="NewModuleRootManager">
|
||||
<content url="file://$MODULE_DIR$">
|
||||
<excludeFolder url="file://$MODULE_DIR$/.pub" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/.tmp" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/build" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/example/packages" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/packages" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/temp" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/tmp" />
|
||||
</content>
|
||||
<orderEntry type="inheritedJdk" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
<orderEntry type="library" name="Dart SDK" level="project" />
|
||||
<orderEntry type="library" name="Dart Packages" level="project" />
|
||||
</component>
|
||||
</module>
|
8
.idea/modules.xml
Normal file
8
.idea/modules.xml
Normal file
|
@ -0,0 +1,8 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ProjectModuleManager">
|
||||
<modules>
|
||||
<module fileurl="file://$PROJECT_DIR$/.idea/graphql_parser.iml" filepath="$PROJECT_DIR$/.idea/graphql_parser.iml" />
|
||||
</modules>
|
||||
</component>
|
||||
</project>
|
6
.idea/runConfigurations/All_Tests.xml
Normal file
6
.idea/runConfigurations/All_Tests.xml
Normal file
|
@ -0,0 +1,6 @@
|
|||
<component name="ProjectRunConfigurationManager">
|
||||
<configuration default="false" name="All Tests" type="DartCommandLineRunConfigurationType" factoryName="Dart Command Line Application" singleton="true">
|
||||
<option name="filePath" value="$PROJECT_DIR$/test/all.dart" />
|
||||
<method />
|
||||
</configuration>
|
||||
</component>
|
8
.idea/runConfigurations/tests_in_graphql_parser.xml
Normal file
8
.idea/runConfigurations/tests_in_graphql_parser.xml
Normal file
|
@ -0,0 +1,8 @@
|
|||
<component name="ProjectRunConfigurationManager">
|
||||
<configuration default="false" name="tests in graphql_parser" type="DartTestRunConfigurationType" factoryName="Dart Test" singleton="true" nameIsGenerated="true">
|
||||
<option name="filePath" value="$PROJECT_DIR$" />
|
||||
<option name="scope" value="FOLDER" />
|
||||
<option name="testRunnerOptions" value="-j 4" />
|
||||
<method />
|
||||
</configuration>
|
||||
</component>
|
6
.idea/vcs.xml
Normal file
6
.idea/vcs.xml
Normal file
|
@ -0,0 +1,6 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="VcsDirectoryMappings">
|
||||
<mapping directory="$PROJECT_DIR$" vcs="Git" />
|
||||
</component>
|
||||
</project>
|
|
@ -7,7 +7,8 @@ final String INPUT = '''
|
|||
tagline
|
||||
}
|
||||
}
|
||||
'''.trim();
|
||||
'''
|
||||
.trim();
|
||||
|
||||
main() {
|
||||
var tokens = scan(INPUT);
|
||||
|
|
|
@ -1,5 +1,3 @@
|
|||
import 'node.dart';
|
||||
|
||||
abstract class DefinitionContext extends Node {
|
||||
|
||||
}
|
||||
abstract class DefinitionContext extends Node {}
|
||||
|
|
|
@ -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());
|
||||
|
|
|
@ -45,7 +45,8 @@ List<Token> 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<Token> 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);
|
||||
}
|
||||
|
|
6
test/all.dart
Normal file
6
test/all.dart
Normal file
|
@ -0,0 +1,6 @@
|
|||
import 'package:test/test.dart';
|
||||
import 'value_test.dart' as value;
|
||||
|
||||
main() {
|
||||
group('value', value.main);
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
final Matcher throwsSyntaxError = throwsA(const isInstanceOf<SyntaxError>());
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
37
test/variable_test.dart
Normal file
37
test/variable_test.dart
Normal file
|
@ -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);
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue