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