Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(161)

Side by Side Diff: pkg/polymer_expressions/test/tokenizer_test.dart

Issue 22950008: move fancy_syntax into Dart SVN (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « pkg/polymer_expressions/test/syntax_test.html ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file.
4
5 library tokenizer_test;
6
7 import 'package:polymer_expressions/tokenizer.dart';
8 import 'package:unittest/unittest.dart';
9
10 main() {
11
12 group('tokenizer', () {
13
14 test('should tokenize an empty expression', () {
15 expectTokens('', []);
16 });
17
18 test('should tokenize an identifier', () {
19 expectTokens('abc', [t(IDENTIFIER_TOKEN, 'abc')]);
20 });
21
22 test('should tokenize a double quoted String', () {
23 expectTokens('"abc"', [t(STRING_TOKEN, 'abc')]);
24 });
25
26 test('should tokenize a single quoted String', () {
27 expectTokens("'abc'", [t(STRING_TOKEN, 'abc')]);
28 });
29
30 test('should tokenize a String with escaping', () {
31 expectTokens('"a\\b\\\\c\\\'\\""', [t(STRING_TOKEN, 'ab\\c\'"')]);
32 });
33
34 test('should tokenize a dot operator', () {
35 expectTokens('a.b', [
36 t(IDENTIFIER_TOKEN, 'a'),
37 t(DOT_TOKEN, '.'),
38 t(IDENTIFIER_TOKEN, 'b')]);
39 });
40
41 test('should tokenize a unary plus operator', () {
42 expectTokens('+a', [
43 t(OPERATOR_TOKEN, '+'),
44 t(IDENTIFIER_TOKEN, 'a')]);
45 });
46
47 test('should tokenize a binary plus operator', () {
48 expectTokens('a + b', [
49 t(IDENTIFIER_TOKEN, 'a'),
50 t(OPERATOR_TOKEN, '+'),
51 t(IDENTIFIER_TOKEN, 'b')]);
52 });
53
54 test('should tokenize a logical and operator', () {
55 expectTokens('a && b', [
56 t(IDENTIFIER_TOKEN, 'a'),
57 t(OPERATOR_TOKEN, '&&'),
58 t(IDENTIFIER_TOKEN, 'b')]);
59 });
60
61 test('should tokenize an iterate expression with "in" keyword', () {
62 expectTokens('item in items', [
63 t(IDENTIFIER_TOKEN, 'item'),
64 t(KEYWORD_TOKEN, 'in'),
65 t(IDENTIFIER_TOKEN, 'items')]);
66 });
67
68 test('should tokenize keywords', () {
69 expectTokens('in', [t(KEYWORD_TOKEN, 'in')]);
70 expectTokens('this', [t(KEYWORD_TOKEN, 'this')]);
71 });
72
73 test('should tokenize groups', () {
74 expectTokens('a(b)[]{}', [
75 t(IDENTIFIER_TOKEN, 'a'),
76 t(GROUPER_TOKEN, '('),
77 t(IDENTIFIER_TOKEN, 'b'),
78 t(GROUPER_TOKEN, ')'),
79 t(GROUPER_TOKEN, '['),
80 t(GROUPER_TOKEN, ']'),
81 t(GROUPER_TOKEN, '{'),
82 t(GROUPER_TOKEN, '}')]);
83 });
84
85 test('should tokenize argument lists', () {
86 expectTokens('(a, b)', [
87 t(GROUPER_TOKEN, '('),
88 t(IDENTIFIER_TOKEN, 'a'),
89 t(COMMA_TOKEN, ','),
90 t(IDENTIFIER_TOKEN, 'b'),
91 t(GROUPER_TOKEN, ')')]);
92 });
93
94 test('should tokenize maps', () {
95 expectTokens("{'a': b}", [
96 t(GROUPER_TOKEN, '{'),
97 t(STRING_TOKEN, 'a'),
98 t(COLON_TOKEN, ':'),
99 t(IDENTIFIER_TOKEN, 'b'),
100 t(GROUPER_TOKEN, '}')]);
101 });
102
103 test('should tokenize integers', () {
104 expectTokens('123', [t(INTEGER_TOKEN, '123')]);
105 expectTokens('+123', [t(OPERATOR_TOKEN, '+'), t(INTEGER_TOKEN, '123')]);
106 expectTokens('-123', [t(OPERATOR_TOKEN, '-'), t(INTEGER_TOKEN, '123')]);
107 });
108
109 test('should tokenize decimals', () {
110 expectTokens('1.23', [t(DECIMAL_TOKEN, '1.23')]);
111 expectTokens('+1.23', [t(OPERATOR_TOKEN, '+'), t(DECIMAL_TOKEN, '1.23')]);
112 expectTokens('-1.23', [t(OPERATOR_TOKEN, '-'), t(DECIMAL_TOKEN, '1.23')]);
113 });
114
115 test('should tokenize booleans as identifiers', () {
116 expectTokens('true', [t(IDENTIFIER_TOKEN, 'true')]);
117 expectTokens('false', [t(IDENTIFIER_TOKEN, 'false')]);
118 });
119
120 });
121 }
122
123 TokenMatcher isToken(int index, String text) => new TokenMatcher(index, text);
124
125 class TokenMatcher extends Matcher {
126 final int kind;
127 final String value;
128
129 TokenMatcher(this.kind, this.value);
130
131 bool matches(Token t, Map m) => t.kind == kind && t.value == value;
132
133 Description describe(Description d) => d.add('isToken($kind, $value) ');
134 }
135
136 expectTokens(String s, List<Token> expected) {
137 var tokens = new Tokenizer(s).tokenize();
138 var matchers = expected.map((t) => isToken(t.kind, t.value)).toList();
139 expect(tokens, matchList(matchers), reason: s);
140 }
141
142 Token t(int kind, String value) => new Token(kind, value);
143
144 MatcherList matchList(List matchers) => new MatcherList(matchers);
145
146 class MatcherList extends Matcher {
147 final List<Matcher> matchers;
148
149 MatcherList(this.matchers);
150
151 bool matches(List o, Map matchState) {
152 if (o.length != matchers.length) return false;
153 for (int i = 0; i < o.length; i++) {
154 var state = new Map();
155 if (!matchers[i].matches(o[i], state)) {
156 matchState = {
157 'index': i,
158 'value': o[i],
159 'state': state,
160 };
161 return false;
162 }
163 }
164 return true;
165 }
166
167 Description describe(Description d) {
168 d.add('matches all: ');
169 matchers.forEach((m) => m.describe(d));
170 }
171
172 Description describeMismatch(item, Description mismatchDescription,
173 Map matchState, bool verbose) {
174 if (matchState != null) {
175 var index = matchState['index'];
176 var value = matchState['value'];
177 var state = matchState['state'];
178 var matcher = matchers[index];
179 mismatchDescription.add("Mismatch at index $index: ");
180 matcher.describeMismatch(value, mismatchDescription, state, verbose);
181 } else {
182 if (item.length != matchers.length) {
183 mismatchDescription.add('wrong lengths');
184 } else {
185 mismatchDescription.add('was ').addDescriptionOf(item);
186 }
187 }
188 }
189
190 }
OLDNEW
« no previous file with comments | « pkg/polymer_expressions/test/syntax_test.html ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698