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

Side by Side Diff: test/backend/platform_selector/scanner_test.dart

Issue 997593003: Add a scanner and parser for platform selectors. (Closed) Base URL: git@github.com:dart-lang/unittest@master
Patch Set: Code review changes Created 5 years, 9 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
« no previous file with comments | « test/backend/platform_selector/parser_test.dart ('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) 2015, 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 import 'package:unittest/unittest.dart';
6 import 'package:unittest/src/backend/platform_selector/scanner.dart';
7 import 'package:unittest/src/backend/platform_selector/token.dart';
8
9 void main() {
10 group("peek()", () {
11 test("returns the next token without consuming it", () {
12 var scanner = new Scanner("( )");
13 expect(scanner.peek().type, equals(TokenType.leftParen));
14 expect(scanner.peek().type, equals(TokenType.leftParen));
15 expect(scanner.peek().type, equals(TokenType.leftParen));
16 });
17
18 test("returns an end-of-file token at the end of a file", () {
19 var scanner = new Scanner("( )");
20 scanner.next();
21 scanner.next();
22
23 var token = scanner.peek();
24 expect(token.type, equals(TokenType.endOfFile));
25 expect(token.span.start.offset, equals(3));
26 expect(token.span.end.offset, equals(3));
27 });
28
29 test("throws a StateError if called after end-of-file was consumed", () {
30 var scanner = new Scanner("( )");
31 scanner.next();
32 scanner.next();
33 scanner.next();
34 expect(() => scanner.peek(), throwsStateError);
35 });
36 });
37
38 group("next()", () {
39 test("consumes and returns the next token", () {
40 var scanner = new Scanner("( )");
41 expect(scanner.next().type, equals(TokenType.leftParen));
42 expect(scanner.peek().type, equals(TokenType.rightParen));
43 expect(scanner.next().type, equals(TokenType.rightParen));
44 });
45
46 test("returns an end-of-file token at the end of a file", () {
47 var scanner = new Scanner("( )");
48 scanner.next();
49 scanner.next();
50
51 var token = scanner.next();
52 expect(token.type, equals(TokenType.endOfFile));
53 expect(token.span.start.offset, equals(3));
54 expect(token.span.end.offset, equals(3));
55 });
56
57 test("throws a StateError if called after end-of-file was consumed", () {
58 var scanner = new Scanner("( )");
59 scanner.next();
60 scanner.next();
61 scanner.next();
62 expect(() => scanner.next(), throwsStateError);
63 });
64 });
65
66 group("scan()", () {
67 test("consumes a matching token and returns true", () {
68 var scanner = new Scanner("( )");
69 expect(scanner.scan(TokenType.leftParen), isTrue);
70 expect(scanner.peek().type, equals(TokenType.rightParen));
71 });
72
73 test("doesn't consume a matching token and returns false", () {
74 var scanner = new Scanner("( )");
75 expect(scanner.scan(TokenType.questionMark), isFalse);
76 expect(scanner.peek().type, equals(TokenType.leftParen));
77 });
78
79 test("throws a StateError called after end-of-file was consumed", () {
80 var scanner = new Scanner("( )");
81 scanner.next();
82 scanner.next();
83 scanner.next();
84 expect(() => scanner.scan(TokenType.endOfFile), throwsStateError);
85 });
86 });
87
88 group("scans a simple token:", () {
89 test("left paren", () => _expectSimpleScan("(", TokenType.leftParen));
90 test("right paren", () => _expectSimpleScan(")", TokenType.rightParen));
91 test("or", () => _expectSimpleScan("||", TokenType.or));
92 test("and", () => _expectSimpleScan("&&", TokenType.and));
93 test("not", () => _expectSimpleScan("!", TokenType.not));
94 test("question mark", () => _expectSimpleScan("?", TokenType.questionMark));
95 test("colon", () => _expectSimpleScan(":", TokenType.colon));
96 });
97
98 group("scans an identifier that", () {
99 test("is simple", () {
100 var token = _scan(" foo ");
101 expect(token.name, equals("foo"));
102 expect(token.span.text, equals("foo"));
103 expect(token.span.start.offset, equals(3));
104 expect(token.span.end.offset, equals(6));
105 });
106
107 test("is a single character", () {
108 var token = _scan("f");
109 expect(token.name, equals("f"));
110 });
111
112 test("has a leading underscore", () {
113 var token = _scan("_foo");
114 expect(token.name, equals("_foo"));
115 });
116
117 test("has a leading dash", () {
118 var token = _scan("-foo");
119 expect(token.name, equals("-foo"));
120 });
121
122 test("contains an underscore", () {
123 var token = _scan("foo_bar");
124 expect(token.name, equals("foo_bar"));
125 });
126
127 test("contains a dash", () {
128 var token = _scan("foo-bar");
129 expect(token.name, equals("foo-bar"));
130 });
131
132 test("is capitalized", () {
133 var token = _scan("FOO");
134 expect(token.name, equals("FOO"));
135 });
136
137 test("contains numbers", () {
138 var token = _scan("foo123");
139 expect(token.name, equals("foo123"));
140 });
141 });
142
143 test("scans an empty selector", () {
144 expect(_scan("").type, equals(TokenType.endOfFile));
145 });
146
147 test("scans multiple tokens", () {
148 var scanner = new Scanner("(foo && bar)");
149
150 var token = scanner.next();
151 expect(token.type, equals(TokenType.leftParen));
152 expect(token.span.start.offset, equals(0));
153 expect(token.span.end.offset, equals(1));
154
155 token = scanner.next();
156 expect(token.type, equals(TokenType.identifier));
157 expect(token.name, equals("foo"));
158 expect(token.span.start.offset, equals(1));
159 expect(token.span.end.offset, equals(4));
160
161 token = scanner.next();
162 expect(token.type, equals(TokenType.and));
163 expect(token.span.start.offset, equals(5));
164 expect(token.span.end.offset, equals(7));
165
166 token = scanner.next();
167 expect(token.type, equals(TokenType.identifier));
168 expect(token.name, equals("bar"));
169 expect(token.span.start.offset, equals(8));
170 expect(token.span.end.offset, equals(11));
171
172 token = scanner.next();
173 expect(token.type, equals(TokenType.rightParen));
174 expect(token.span.start.offset, equals(11));
175 expect(token.span.end.offset, equals(12));
176
177 token = scanner.next();
178 expect(token.type, equals(TokenType.endOfFile));
179 expect(token.span.start.offset, equals(12));
180 expect(token.span.end.offset, equals(12));
181 });
182
183 group("ignores", () {
184 test("a single-line comment", () {
185 var scanner = new Scanner("( // &&\n// ||\n)");
186 expect(scanner.next().type, equals(TokenType.leftParen));
187 expect(scanner.next().type, equals(TokenType.rightParen));
188 expect(scanner.next().type, equals(TokenType.endOfFile));
189 });
190
191 test("a single-line comment without a trailing newline", () {
192 var scanner = new Scanner("( // &&");
193 expect(scanner.next().type, equals(TokenType.leftParen));
194 expect(scanner.next().type, equals(TokenType.endOfFile));
195 });
196
197 test("a multi-line comment", () {
198 var scanner = new Scanner("( /* && * /\n|| */\n)");
199 expect(scanner.next().type, equals(TokenType.leftParen));
200 expect(scanner.next().type, equals(TokenType.rightParen));
201 expect(scanner.next().type, equals(TokenType.endOfFile));
202 });
203
204 test("a multi-line nested comment", () {
205 var scanner = new Scanner("(/* && /* ? /* || */ : */ ! */)");
206 expect(scanner.next().type, equals(TokenType.leftParen));
207 expect(scanner.next().type, equals(TokenType.rightParen));
208 expect(scanner.next().type, equals(TokenType.endOfFile));
209 });
210
211 test("Dart's notion of whitespace", () {
212 var scanner = new Scanner("( \t \n)");
213 expect(scanner.next().type, equals(TokenType.leftParen));
214 expect(scanner.next().type, equals(TokenType.rightParen));
215 expect(scanner.next().type, equals(TokenType.endOfFile));
216 });
217 });
218
219 group("disallows", () {
220 test("a single |", () {
221 expect(() => _scan("|"), throwsFormatException);
222 });
223
224 test('"| |"', () {
225 expect(() => _scan("| |"), throwsFormatException);
226 });
227
228 test("a single &", () {
229 expect(() => _scan("&"), throwsFormatException);
230 });
231
232 test('"& &"', () {
233 expect(() => _scan("& &"), throwsFormatException);
234 });
235
236 test("an unknown operator", () {
237 expect(() => _scan("=="), throwsFormatException);
238 });
239
240 test("unicode", () {
241 expect(() => _scan("öh"), throwsFormatException);
242 });
243
244 test("an unclosed multi-line comment", () {
245 expect(() => _scan("/*"), throwsFormatException);
246 });
247
248 test("an unopened multi-line comment", () {
249 expect(() => _scan("*/"), throwsFormatException);
250 });
251 });
252 }
253
254 /// Asserts that the first token scanned from [selector] has type [type],
255 /// and that that token's span is exactly [selector].
256 void _expectSimpleScan(String selector, TokenType type) {
257 // Complicate the selector to test that the span covers it correctly.
258 var token = _scan(" $selector ");
259 expect(token.type, equals(type));
260 expect(token.span.text, equals(selector));
261 expect(token.span.start.offset, equals(3));
262 expect(token.span.end.offset, equals(3 + selector.length));
263 }
264
265 /// Scans a single token from [selector].
266 Token _scan(String selector) => new Scanner(selector).next();
OLDNEW
« no previous file with comments | « test/backend/platform_selector/parser_test.dart ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698