Chromium Code Reviews| OLD | NEW |
|---|---|
| (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 peek() is called after end-of-file was " | |
| 30 "consumed", () { | |
| 31 var scanner = new Scanner("( )"); | |
| 32 scanner.next(); | |
| 33 scanner.next(); | |
| 34 scanner.next(); | |
| 35 expect(() => scanner.peek(), throwsStateError); | |
| 36 }); | |
| 37 }); | |
| 38 | |
| 39 group("next()", () { | |
| 40 test("next() consumes and returns the next token", () { | |
| 41 var scanner = new Scanner("( )"); | |
| 42 expect(scanner.next().type, equals(TokenType.leftParen)); | |
| 43 expect(scanner.peek().type, equals(TokenType.rightParen)); | |
| 44 expect(scanner.next().type, equals(TokenType.rightParen)); | |
| 45 }); | |
| 46 | |
| 47 test("returns an end-of-file token at the end of a file", () { | |
|
Bob Nystrom
2015/03/11 20:05:53
This is a dupe of line 18.
nweiz
2015/03/12 19:48:58
It calls [scanner.next] instead of [scanner.peek].
Bob Nystrom
2015/03/12 20:37:21
My reading comprehension grade: F.
| |
| 48 var scanner = new Scanner("( )"); | |
| 49 scanner.next(); | |
| 50 scanner.next(); | |
| 51 | |
| 52 var token = scanner.next(); | |
| 53 expect(token.type, equals(TokenType.endOfFile)); | |
| 54 expect(token.span.start.offset, equals(3)); | |
| 55 expect(token.span.end.offset, equals(3)); | |
| 56 }); | |
| 57 | |
| 58 test("throws a StateError if peek() is called after end-of-file was " | |
|
Bob Nystrom
2015/03/11 20:05:53
Ditto.
nweiz
2015/03/12 19:48:58
Ditto.
| |
| 59 "consumed", () { | |
| 60 var scanner = new Scanner("( )"); | |
| 61 scanner.next(); | |
| 62 scanner.next(); | |
| 63 scanner.next(); | |
| 64 expect(() => scanner.next(), throwsStateError); | |
| 65 }); | |
| 66 }); | |
| 67 | |
| 68 group("scans a simple token:", () { | |
| 69 test("left paren", () => _expectSimpleScan("(", TokenType.leftParen)); | |
| 70 test("right paren", () => _expectSimpleScan(")", TokenType.rightParen)); | |
| 71 test("or", () => _expectSimpleScan("||", TokenType.or)); | |
| 72 test("and", () => _expectSimpleScan("&&", TokenType.and)); | |
| 73 test("not", () => _expectSimpleScan("!", TokenType.not)); | |
| 74 test("question mark", () => _expectSimpleScan("?", TokenType.questionMark)); | |
| 75 test("colon", () => _expectSimpleScan(":", TokenType.colon)); | |
| 76 }); | |
| 77 | |
| 78 group("scans an identifier that", () { | |
| 79 test("is simple", () { | |
| 80 var token = _scan(" foo "); | |
| 81 expect(token.name, equals("foo")); | |
| 82 expect(token.span.text, equals("foo")); | |
| 83 expect(token.span.start.offset, equals(3)); | |
| 84 expect(token.span.end.offset, equals(6)); | |
| 85 }); | |
| 86 | |
| 87 test("is a single character", () { | |
| 88 var token = _scan("f"); | |
| 89 expect(token.name, equals("f")); | |
| 90 }); | |
| 91 | |
| 92 test("has a leading underscore", () { | |
| 93 var token = _scan("_foo"); | |
| 94 expect(token.name, equals("_foo")); | |
| 95 }); | |
| 96 | |
| 97 test("has a leading dash", () { | |
| 98 var token = _scan("-foo"); | |
| 99 expect(token.name, equals("-foo")); | |
| 100 }); | |
| 101 | |
| 102 test("contains an underscore", () { | |
| 103 var token = _scan("foo_bar"); | |
| 104 expect(token.name, equals("foo_bar")); | |
| 105 }); | |
| 106 | |
| 107 test("contains a dash", () { | |
| 108 var token = _scan("foo-bar"); | |
| 109 expect(token.name, equals("foo-bar")); | |
| 110 }); | |
| 111 | |
| 112 test("is capitalized", () { | |
| 113 var token = _scan("FOO"); | |
| 114 expect(token.name, equals("FOO")); | |
| 115 }); | |
| 116 | |
| 117 test("contains numbers", () { | |
| 118 var token = _scan("foo123"); | |
| 119 expect(token.name, equals("foo123")); | |
| 120 }); | |
| 121 }); | |
| 122 | |
| 123 test("scans an empty selector", () { | |
| 124 expect(_scan("").type, equals(TokenType.endOfFile)); | |
| 125 }); | |
| 126 | |
| 127 test("scans multiple tokens", () { | |
| 128 var scanner = new Scanner("(foo && bar)"); | |
| 129 | |
| 130 var token = scanner.next(); | |
| 131 expect(token.type, equals(TokenType.leftParen)); | |
| 132 expect(token.span.start.offset, equals(0)); | |
| 133 expect(token.span.end.offset, equals(1)); | |
| 134 | |
| 135 token = scanner.next(); | |
| 136 expect(token.type, equals(TokenType.identifier)); | |
| 137 expect(token.name, equals("foo")); | |
| 138 expect(token.span.start.offset, equals(1)); | |
| 139 expect(token.span.end.offset, equals(4)); | |
| 140 | |
| 141 token = scanner.next(); | |
| 142 expect(token.type, equals(TokenType.and)); | |
| 143 expect(token.span.start.offset, equals(5)); | |
| 144 expect(token.span.end.offset, equals(7)); | |
| 145 | |
| 146 token = scanner.next(); | |
| 147 expect(token.type, equals(TokenType.identifier)); | |
| 148 expect(token.name, equals("bar")); | |
| 149 expect(token.span.start.offset, equals(8)); | |
| 150 expect(token.span.end.offset, equals(11)); | |
| 151 | |
| 152 token = scanner.next(); | |
| 153 expect(token.type, equals(TokenType.rightParen)); | |
| 154 expect(token.span.start.offset, equals(11)); | |
| 155 expect(token.span.end.offset, equals(12)); | |
| 156 | |
| 157 token = scanner.next(); | |
| 158 expect(token.type, equals(TokenType.endOfFile)); | |
| 159 expect(token.span.start.offset, equals(12)); | |
| 160 expect(token.span.end.offset, equals(12)); | |
| 161 }); | |
| 162 | |
| 163 group("ignores", () { | |
| 164 test("a single-line comment", () { | |
| 165 var scanner = new Scanner("( // &&\n// ||\n)"); | |
| 166 expect(scanner.next().type, equals(TokenType.leftParen)); | |
| 167 expect(scanner.next().type, equals(TokenType.rightParen)); | |
| 168 expect(scanner.next().type, equals(TokenType.endOfFile)); | |
| 169 }); | |
| 170 | |
| 171 test("a single-line comment without a trailing newline", () { | |
| 172 var scanner = new Scanner("( // &&"); | |
| 173 expect(scanner.next().type, equals(TokenType.leftParen)); | |
| 174 expect(scanner.next().type, equals(TokenType.endOfFile)); | |
| 175 }); | |
| 176 | |
| 177 test("a multi-line comment", () { | |
| 178 var scanner = new Scanner("( /* && * /\n|| */\n)"); | |
| 179 expect(scanner.next().type, equals(TokenType.leftParen)); | |
| 180 expect(scanner.next().type, equals(TokenType.rightParen)); | |
| 181 expect(scanner.next().type, equals(TokenType.endOfFile)); | |
| 182 }); | |
| 183 | |
| 184 test("a multi-line nested comment", () { | |
| 185 var scanner = new Scanner("(/* && /* ? /* || */ : */ ! */)"); | |
| 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("Dart's notion of whitespace", () { | |
| 192 var scanner = new Scanner("( \t \n)"); | |
| 193 expect(scanner.next().type, equals(TokenType.leftParen)); | |
| 194 expect(scanner.next().type, equals(TokenType.rightParen)); | |
| 195 expect(scanner.next().type, equals(TokenType.endOfFile)); | |
| 196 }); | |
| 197 }); | |
| 198 | |
| 199 group("disallows", () { | |
| 200 test("a single |", () { | |
| 201 expect(() => _scan("|"), throwsFormatException); | |
| 202 }); | |
| 203 | |
| 204 test('"| |"', () { | |
| 205 expect(() => _scan("| |"), throwsFormatException); | |
| 206 }); | |
| 207 | |
| 208 test("a single &", () { | |
| 209 expect(() => _scan("&"), throwsFormatException); | |
| 210 }); | |
| 211 | |
| 212 test('"& &"', () { | |
| 213 expect(() => _scan("& &"), throwsFormatException); | |
| 214 }); | |
| 215 | |
| 216 test("an unknown operator", () { | |
| 217 expect(() => _scan("=="), throwsFormatException); | |
| 218 }); | |
| 219 | |
| 220 test("unicode", () { | |
| 221 expect(() => _scan("öh"), throwsFormatException); | |
| 222 }); | |
| 223 | |
| 224 test("an unclosed multi-line comment", () { | |
| 225 expect(() => _scan("/*"), throwsFormatException); | |
| 226 }); | |
| 227 | |
| 228 test("an unopened multi-line comment", () { | |
| 229 expect(() => _scan("*/"), throwsFormatException); | |
| 230 }); | |
| 231 }); | |
| 232 } | |
| 233 | |
| 234 /// Asserts that the first token scanned from [selector] has type [type], | |
| 235 /// and that that token's span is exactly [selector]. | |
| 236 void _expectSimpleScan(String selector, TokenType type) { | |
| 237 // Complicate the selector to test that the span covers it correctly. | |
| 238 var token = _scan(" $selector "); | |
| 239 expect(token.type, equals(type)); | |
| 240 expect(token.span.text, equals(selector)); | |
| 241 expect(token.span.start.offset, equals(3)); | |
| 242 expect(token.span.end.offset, equals(3 + selector.length)); | |
| 243 } | |
| 244 | |
| 245 /// Scans a single token from [selector]. | |
| 246 Token _scan(String selector) => new Scanner(selector).next(); | |
| OLD | NEW |