OLD | NEW |
1 /** | 1 /// This package contains matches to write tests for parsers. |
2 * This package contains matches to write tests for parsers. | 2 /// |
3 * | 3 /// Examples: |
4 * Examples: | 4 /// |
5 * | 5 /// var json = new JsonParser(); |
6 * var json = new JsonParser(); | 6 /// |
7 * | 7 /// // verifies that the input gets parsed and all input is consumed |
8 * // verifies that the input gets parsed and all input is consumed | 8 /// expect('{"a": 1}', accepts(new JsonParser())); |
9 * expect('{"a": 1}', accepts(new JsonParser())); | 9 /// |
10 * | 10 /// // verifies that the input gets parsed to a dictionary and that all inpu
t is consumed |
11 * // verifies that the input gets parsed to a dictionary and that all input
is consumed | 11 /// expect('{"a": 1}', parses(new JsonParser(), {'a': 1})); |
12 * expect('{"a": 1}', parses(new JsonParser(), {'a': 1})); | |
13 */ | |
14 | 12 |
15 library test_util; | 13 library petitparser.test_util; |
16 | 14 |
17 import 'package:matcher/matcher.dart'; | 15 import 'package:matcher/matcher.dart'; |
18 import 'package:petitparser/petitparser.dart' hide predicate; | 16 import 'package:petitparser/petitparser.dart' hide predicate; |
19 | 17 |
20 /** | 18 /// Returns a matcher that succeeds if the [parser] accepts the input. |
21 * Returns a matcher that succeeds if the [parser] accepts the input. | |
22 */ | |
23 Matcher accept(Parser parser) { | 19 Matcher accept(Parser parser) { |
24 return parse(parser, predicate((value) => true, 'input')); | 20 return parse(parser, predicate((value) => true, 'input')); |
25 } | 21 } |
26 | 22 |
27 /** | 23 /// Returns a matcher that succeeds if the [parser] succeeds and accepts the pro
vided [matcher]. |
28 * Returns a matcher that succeeds if the [parser] succeeds and accepts the prov
ided [matcher]. | |
29 */ | |
30 Matcher parse(Parser parser, matcher, [int position = -1]) { | 24 Matcher parse(Parser parser, matcher, [int position = -1]) { |
31 return new _Parse(parser, wrapMatcher(matcher), position); | 25 return new _Parse(parser, wrapMatcher(matcher), position); |
32 } | 26 } |
33 | 27 |
34 class _Parse extends Matcher { | 28 class _Parse extends Matcher { |
35 | 29 |
36 final Parser parser; | 30 final Parser parser; |
37 final Matcher matcher; | 31 final Matcher matcher; |
38 final int position; | 32 final int position; |
39 | 33 |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
85 description | 79 description |
86 .add(' that consumes input to ') | 80 .add(' that consumes input to ') |
87 .add(matchState['result'].position.toString()) | 81 .add(matchState['result'].position.toString()) |
88 .add(' instead of ') | 82 .add(' instead of ') |
89 .add(position.toString()); | 83 .add(position.toString()); |
90 return description; | 84 return description; |
91 } | 85 } |
92 throw new Exception('Internal matcher error'); | 86 throw new Exception('Internal matcher error'); |
93 } | 87 } |
94 } | 88 } |
OLD | NEW |