OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011, 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('../../../../frog/lang.dart', prefix:'lang'); |
| 6 #import("../../../css/css.dart"); |
| 7 |
| 8 class SelectorLiteralTest { |
| 9 |
| 10 static testMain() { |
| 11 initCssWorld(); |
| 12 |
| 13 testClass(); |
| 14 testId(); |
| 15 testElement(); |
| 16 testNamespace(); |
| 17 testSelectorGroups(); |
| 18 testCombinator(); |
| 19 testWildcard(); |
| 20 testPseudo(); |
| 21 testAttribute(); |
| 22 testNegation(); |
| 23 } |
| 24 |
| 25 static void testClass() { |
| 26 Parser parser = new Parser(new lang.SourceFile( |
| 27 lang.SourceFile.IN_MEMORY_FILE, ".foobar")); |
| 28 |
| 29 List<SelectorGroup> exprTree = parser.preprocess(); |
| 30 Expect.isNotNull(exprTree); |
| 31 Expect.equals(exprTree.length, 1); |
| 32 for (selectorGroup in exprTree) { |
| 33 Expect.equals(selectorGroup.selectors.length, 1); |
| 34 for (selector in selectorGroup.selectors) { |
| 35 Expect.isTrue(selector is ClassSelector); |
| 36 Expect.isTrue(selector.isCombinatorNone()); |
| 37 Expect.equals(selector.name, "foobar"); |
| 38 } |
| 39 } |
| 40 |
| 41 parser = new Parser(new lang.SourceFile(lang.SourceFile.IN_MEMORY_FILE, |
| 42 ".foobar .bar .no-story")); |
| 43 |
| 44 exprTree = parser.preprocess(); |
| 45 Expect.isNotNull(exprTree); |
| 46 Expect.equals(exprTree.length, 1); |
| 47 for (selectorGroup in exprTree) { |
| 48 var idx = 0; |
| 49 for (selector in selectorGroup.selectors) { |
| 50 if (idx == 0) { |
| 51 Expect.isTrue(selector is ClassSelector); |
| 52 Expect.isTrue(selector.isCombinatorNone()); |
| 53 Expect.equals(selector.name, "foobar"); |
| 54 } else if (idx == 1) { |
| 55 Expect.isTrue(selector is ClassSelector); |
| 56 Expect.isTrue(selector.isCombinatorDescendant()); |
| 57 Expect.equals(selector.name, "bar"); |
| 58 } else if (idx == 2) { |
| 59 Expect.isTrue(selector is ClassSelector); |
| 60 Expect.isTrue(selector.isCombinatorDescendant()); |
| 61 Expect.equals(selector.name, "no-story"); |
| 62 } else { |
| 63 Expect.fail("unexpected expression"); |
| 64 } |
| 65 |
| 66 idx++; |
| 67 } |
| 68 } |
| 69 } |
| 70 |
| 71 static void testId() { |
| 72 Parser parser = new Parser(new lang.SourceFile( |
| 73 lang.SourceFile.IN_MEMORY_FILE, "#elemId")); |
| 74 |
| 75 List<SelectorGroup> exprTree = parser.preprocess(); |
| 76 Expect.isNotNull(exprTree); |
| 77 Expect.equals(exprTree.length, 1); |
| 78 Expect.isNotNull(exprTree); |
| 79 for (selectorGroup in exprTree) { |
| 80 Expect.equals(selectorGroup.selectors.length, 1); |
| 81 for (selector in selectorGroup.selectors) { |
| 82 Expect.isTrue(selector is IdSelector); |
| 83 Expect.isTrue(selector.isCombinatorNone()); |
| 84 Expect.equals(selector.name, "elemId"); |
| 85 } |
| 86 } |
| 87 } |
| 88 |
| 89 static void testElement() { |
| 90 Parser parser = new Parser(new lang.SourceFile( |
| 91 lang.SourceFile.IN_MEMORY_FILE, "div")); |
| 92 List<SelectorGroup> exprTree = parser.preprocess(); |
| 93 Expect.isNotNull(exprTree); |
| 94 Expect.equals(exprTree.length, 1); |
| 95 for (selectorGroup in exprTree) { |
| 96 Expect.equals(selectorGroup.selectors.length, 1); |
| 97 for (selector in selectorGroup.selectors) { |
| 98 Expect.isTrue(selector is ElementSelector); |
| 99 Expect.isTrue(selector.isCombinatorNone()); |
| 100 Expect.equals(selector.name, "div"); |
| 101 } |
| 102 } |
| 103 |
| 104 parser = new Parser(new lang.SourceFile(lang.SourceFile.IN_MEMORY_FILE, |
| 105 "div div span")); |
| 106 exprTree = parser.preprocess(); |
| 107 Expect.isNotNull(exprTree); |
| 108 Expect.equals(exprTree.length, 1); |
| 109 for (selectorGroup in exprTree) { |
| 110 var idx = 0; |
| 111 for (selector in selectorGroup.selectors) { |
| 112 if (idx == 0) { |
| 113 Expect.isTrue(selector is ElementSelector); |
| 114 Expect.isTrue(selector.isCombinatorNone()); |
| 115 Expect.equals(selector.name, "div"); |
| 116 } else if (idx == 1) { |
| 117 Expect.isTrue(selector is ElementSelector); |
| 118 Expect.isTrue(selector.isCombinatorDescendant()); |
| 119 Expect.equals(selector.name, "div"); |
| 120 } else if (idx == 2) { |
| 121 Expect.isTrue(selector is ElementSelector); |
| 122 Expect.isTrue(selector.isCombinatorDescendant()); |
| 123 Expect.equals(selector.name, "span"); |
| 124 } else { |
| 125 Expect.fail("unexpected expression"); |
| 126 } |
| 127 |
| 128 idx++; |
| 129 } |
| 130 } |
| 131 } |
| 132 |
| 133 static void testNamespace() { |
| 134 Parser parser = new Parser(new lang.SourceFile( |
| 135 lang.SourceFile.IN_MEMORY_FILE, "ns1|div")); |
| 136 List<SelectorGroup> exprTree = parser.preprocess(); |
| 137 Expect.isNotNull(exprTree); |
| 138 Expect.equals(exprTree.length, 1); |
| 139 for (selectorGroup in exprTree) { |
| 140 Expect.equals(selectorGroup.selectors.length, 1); |
| 141 for (selector in selectorGroup.selectors) { |
| 142 Expect.isTrue(selector is NamespaceSelector); |
| 143 Expect.isTrue(selector.isCombinatorNone()); |
| 144 Expect.isFalse(selector.isNamespaceWildcard()); |
| 145 Expect.equals(selector.namespace, "ns1"); |
| 146 ElementSelector elementSelector = selector.nameAsSimpleSelector; |
| 147 Expect.isTrue(elementSelector is ElementSelector); |
| 148 Expect.isFalse(elementSelector.isWildcard()); |
| 149 Expect.equals(elementSelector.name, "div"); |
| 150 } |
| 151 } |
| 152 |
| 153 parser = new Parser(new lang.SourceFile(lang.SourceFile.IN_MEMORY_FILE, |
| 154 "ns1|div div ns2|span .foobar")); |
| 155 exprTree = parser.preprocess(); |
| 156 Expect.isNotNull(exprTree); |
| 157 Expect.equals(exprTree.length, 1); |
| 158 for (selectorGroup in exprTree) { |
| 159 var idx = 0; |
| 160 for (selector in selectorGroup.selectors) { |
| 161 if (idx == 0) { |
| 162 Expect.isTrue(selector is NamespaceSelector); |
| 163 Expect.isTrue(selector.isCombinatorNone()); |
| 164 Expect.equals(selector.namespace, "ns1"); |
| 165 ElementSelector elementSelector = selector.nameAsSimpleSelector; |
| 166 Expect.isTrue(elementSelector is ElementSelector); |
| 167 Expect.isFalse(elementSelector.isWildcard()); |
| 168 Expect.equals(elementSelector.name, "div"); |
| 169 } else if (idx == 1) { |
| 170 Expect.isTrue(selector is ElementSelector); |
| 171 Expect.isTrue(selector.isCombinatorDescendant()); |
| 172 Expect.equals(selector.name, "div"); |
| 173 } else if (idx == 2) { |
| 174 Expect.isTrue(selector is NamespaceSelector); |
| 175 Expect.isTrue(selector.isCombinatorDescendant()); |
| 176 Expect.equals(selector.namespace, "ns2"); |
| 177 ElementSelector elementSelector = selector.nameAsSimpleSelector; |
| 178 Expect.isTrue(elementSelector is ElementSelector); |
| 179 Expect.isFalse(elementSelector.isWildcard()); |
| 180 Expect.equals(elementSelector.name, "span"); |
| 181 } else if (idx == 3) { |
| 182 Expect.isTrue(selector is ClassSelector); |
| 183 Expect.isTrue(selector.isCombinatorDescendant()); |
| 184 Expect.equals(selector.name, "foobar"); |
| 185 } else { |
| 186 Expect.fail("unexpected expression"); |
| 187 } |
| 188 |
| 189 idx++; |
| 190 } |
| 191 } |
| 192 } |
| 193 |
| 194 static void testSelectorGroups() { |
| 195 Parser parser = new Parser(new lang.SourceFile( |
| 196 lang.SourceFile.IN_MEMORY_FILE, |
| 197 "div, .foobar ,#elemId, .xyzzy .test, ns1|div div #elemId .foobar")); |
| 198 List<SelectorGroup> exprTree = parser.preprocess(); |
| 199 Expect.isNotNull(exprTree); |
| 200 Expect.equals(exprTree.length, 5); |
| 201 var groupIdx = 0; |
| 202 for (selectorGroup in exprTree) { |
| 203 var idx = 0; |
| 204 for (selector in selectorGroup.selectors) { |
| 205 switch (groupIdx) { |
| 206 case 0: // First selector group. |
| 207 Expect.equals(selectorGroup.selectors.length, 1); |
| 208 Expect.isTrue(selector is ElementSelector); |
| 209 Expect.isTrue(selector.isCombinatorNone()); |
| 210 Expect.equals(selector.name, "div"); |
| 211 break; |
| 212 case 1: // Second selector group. |
| 213 Expect.equals(selectorGroup.selectors.length, 1); |
| 214 Expect.isTrue(selector is ClassSelector); |
| 215 Expect.isTrue(selector.isCombinatorNone()); |
| 216 Expect.equals(selector.name, "foobar"); |
| 217 break; |
| 218 case 2: // Third selector group. |
| 219 Expect.equals(selectorGroup.selectors.length, 1); |
| 220 Expect.isTrue(selector is IdSelector); |
| 221 Expect.isTrue(selector.isCombinatorNone()); |
| 222 Expect.equals(selector.name, "elemId"); |
| 223 break; |
| 224 case 3: // Fourth selector group. |
| 225 Expect.equals(selectorGroup.selectors.length, 2); |
| 226 if (idx == 0) { |
| 227 Expect.isTrue(selector is ClassSelector); |
| 228 Expect.isTrue(selector.isCombinatorNone()); |
| 229 Expect.equals(selector.name, "xyzzy"); |
| 230 } else if (idx == 1) { |
| 231 Expect.isTrue(selector is ClassSelector); |
| 232 Expect.isTrue(selector.isCombinatorDescendant()); |
| 233 Expect.equals(selector.name, "test"); |
| 234 } else { |
| 235 Expect.fail("unexpected expression"); |
| 236 } |
| 237 break; |
| 238 case 4: // Fifth selector group. |
| 239 Expect.equals(selectorGroup.selectors.length, 4); |
| 240 if (idx == 0) { |
| 241 Expect.isTrue(selector is NamespaceSelector); |
| 242 Expect.isTrue(selector.isCombinatorNone()); |
| 243 Expect.equals(selector.namespace, "ns1"); |
| 244 ElementSelector elementSelector = selector.nameAsSimpleSelector; |
| 245 Expect.isTrue(elementSelector is ElementSelector); |
| 246 Expect.isFalse(elementSelector.isWildcard()); |
| 247 Expect.equals(elementSelector.name, "div"); |
| 248 } else if (idx == 1) { |
| 249 Expect.isTrue(selector is ElementSelector); |
| 250 Expect.isTrue(selector.isCombinatorDescendant()); |
| 251 Expect.equals(selector.name, "div"); |
| 252 } else if (idx == 2) { |
| 253 Expect.isTrue(selector is IdSelector); |
| 254 Expect.isTrue(selector.isCombinatorDescendant()); |
| 255 Expect.equals(selector.name, "elemId"); |
| 256 } else if (idx == 3) { |
| 257 Expect.isTrue(selector is ClassSelector); |
| 258 Expect.isTrue(selector.isCombinatorDescendant()); |
| 259 Expect.equals(selector.name, "foobar"); |
| 260 } else { |
| 261 Expect.fail("unexpected expression"); |
| 262 } |
| 263 break; |
| 264 } |
| 265 idx++; |
| 266 } |
| 267 groupIdx++; |
| 268 } |
| 269 } |
| 270 |
| 271 static void testCombinator() { |
| 272 Parser parser = new Parser(new lang.SourceFile( |
| 273 lang.SourceFile.IN_MEMORY_FILE, |
| 274 ".foobar > .bar + .no-story ~ myNs|div #elemId")); |
| 275 |
| 276 List<SelectorGroup> exprTree = parser.preprocess(); |
| 277 Expect.isNotNull(exprTree); |
| 278 Expect.equals(exprTree.length, 1); |
| 279 for (selectorGroup in exprTree) { |
| 280 var idx = 0; |
| 281 Expect.equals(selectorGroup.selectors.length, 5); |
| 282 for (selector in selectorGroup.selectors) { |
| 283 if (idx == 0) { |
| 284 Expect.isTrue(selector is ClassSelector); |
| 285 Expect.isTrue(selector.isCombinatorNone()); |
| 286 Expect.equals(selector.name, "foobar"); |
| 287 } else if (idx == 1) { |
| 288 Expect.isTrue(selector is ClassSelector); |
| 289 Expect.isTrue(selector.isCombinatorGreater()); |
| 290 Expect.equals(selector.name, "bar"); |
| 291 } else if (idx == 2) { |
| 292 Expect.isTrue(selector is ClassSelector); |
| 293 Expect.isTrue(selector.isCombinatorPlus()); |
| 294 Expect.equals(selector.name, "no-story"); |
| 295 } else if (idx == 3) { |
| 296 Expect.isTrue(selector is NamespaceSelector); |
| 297 Expect.isTrue(selector.isCombinatorTilde()); |
| 298 Expect.equals(selector.namespace, "myNs"); |
| 299 ElementSelector elementSelector = selector.nameAsSimpleSelector; |
| 300 Expect.isTrue(elementSelector is ElementSelector); |
| 301 Expect.isFalse(elementSelector.isWildcard()); |
| 302 Expect.equals(elementSelector.name, "div"); |
| 303 } else if (idx == 4) { |
| 304 Expect.isTrue(selector is IdSelector); |
| 305 Expect.isTrue(selector.isCombinatorDescendant()); |
| 306 Expect.equals(selector.name, "elemId"); |
| 307 } else { |
| 308 Expect.fail("unexpected expression"); |
| 309 } |
| 310 |
| 311 idx++; |
| 312 } |
| 313 } |
| 314 } |
| 315 |
| 316 static void testWildcard() { |
| 317 Parser parser = new Parser(new lang.SourceFile( |
| 318 lang.SourceFile.IN_MEMORY_FILE, "*")); |
| 319 |
| 320 List<SelectorGroup> exprTree = parser.preprocess(); |
| 321 Expect.isNotNull(exprTree); |
| 322 Expect.equals(exprTree.length, 1); |
| 323 for (selectorGroup in exprTree) { |
| 324 Expect.equals(selectorGroup.selectors.length, 1); |
| 325 for (selector in selectorGroup.selectors) { |
| 326 Expect.isTrue(selector is ElementSelector); |
| 327 Expect.isTrue(selector.isCombinatorNone()); |
| 328 Expect.isTrue(selector.isWildcard()); |
| 329 Expect.equals(selector.name, "*"); |
| 330 } |
| 331 } |
| 332 |
| 333 parser = new Parser(new lang.SourceFile(lang.SourceFile.IN_MEMORY_FILE, |
| 334 "*.foobar")); |
| 335 |
| 336 exprTree = parser.preprocess(); |
| 337 Expect.isNotNull(exprTree); |
| 338 Expect.equals(exprTree.length, 1); |
| 339 for (selectorGroup in exprTree) { |
| 340 var idx = 0; |
| 341 for (selector in selectorGroup.selectors) { |
| 342 Expect.equals(selectorGroup.selectors.length, 2); |
| 343 if (idx == 0) { |
| 344 Expect.isTrue(selector is ElementSelector); |
| 345 Expect.isTrue(selector.isCombinatorNone()); |
| 346 Expect.isTrue(selector.isWildcard()); |
| 347 Expect.equals(selector.name, "*"); |
| 348 } else if (idx == 1) { |
| 349 Expect.isTrue(selector is ClassSelector); |
| 350 Expect.isTrue(selector.isCombinatorNone()); |
| 351 Expect.equals(selector.name, "foobar"); |
| 352 } else { |
| 353 Expect.fail("unexpected expression"); |
| 354 } |
| 355 |
| 356 idx++; |
| 357 } |
| 358 } |
| 359 |
| 360 parser = new Parser(new lang.SourceFile(lang.SourceFile.IN_MEMORY_FILE, |
| 361 "myNs|*.foobar")); |
| 362 |
| 363 exprTree = parser.preprocess(); |
| 364 Expect.isNotNull(exprTree); |
| 365 Expect.equals(exprTree.length, 1); |
| 366 for (selectorGroup in exprTree) { |
| 367 var idx = 0; |
| 368 for (selector in selectorGroup.selectors) { |
| 369 Expect.equals(selectorGroup.selectors.length, 2); |
| 370 if (idx == 0) { |
| 371 Expect.isTrue(selector is NamespaceSelector); |
| 372 Expect.isTrue(selector.isCombinatorNone()); |
| 373 Expect.isFalse(selector.isNamespaceWildcard()); |
| 374 ElementSelector elementSelector = selector.nameAsSimpleSelector; |
| 375 Expect.equals("myNs", selector.namespace); |
| 376 Expect.isTrue(elementSelector.isWildcard()); |
| 377 Expect.equals("*", elementSelector.name); |
| 378 } else if (idx == 1) { |
| 379 Expect.isTrue(selector is ClassSelector); |
| 380 Expect.isTrue(selector.isCombinatorNone()); |
| 381 Expect.equals("foobar", selector.name); |
| 382 } else { |
| 383 Expect.fail("unexpected expression"); |
| 384 } |
| 385 |
| 386 idx++; |
| 387 } |
| 388 } |
| 389 |
| 390 parser = new Parser(new lang.SourceFile(lang.SourceFile.IN_MEMORY_FILE, |
| 391 "*|*.foobar")); |
| 392 |
| 393 exprTree = parser.preprocess(); |
| 394 Expect.isNotNull(exprTree); |
| 395 Expect.equals(exprTree.length, 1); |
| 396 for (selectorGroup in exprTree) { |
| 397 var idx = 0; |
| 398 for (selector in selectorGroup.selectors) { |
| 399 Expect.equals(selectorGroup.selectors.length, 2); |
| 400 if (idx == 0) { |
| 401 Expect.isTrue(selector is NamespaceSelector); |
| 402 Expect.isTrue(selector.isCombinatorNone()); |
| 403 Expect.isTrue(selector.isNamespaceWildcard()); |
| 404 Expect.equals("*", selector.namespace); |
| 405 ElementSelector elementSelector = selector.nameAsSimpleSelector; |
| 406 Expect.isTrue(elementSelector.isWildcard()); |
| 407 Expect.equals("*", elementSelector.name); |
| 408 } else if (idx == 1) { |
| 409 Expect.isTrue(selector is ClassSelector); |
| 410 Expect.isTrue(selector.isCombinatorNone()); |
| 411 Expect.equals("foobar", selector.name); |
| 412 } else { |
| 413 Expect.fail("unexpected expression"); |
| 414 } |
| 415 |
| 416 idx++; |
| 417 } |
| 418 } |
| 419 |
| 420 } |
| 421 |
| 422 static void testPseudo() { |
| 423 // TODO(terry): Implement |
| 424 } |
| 425 |
| 426 static void testAttribute() { |
| 427 // TODO(terry): Implement |
| 428 } |
| 429 |
| 430 static void testNegation() { |
| 431 // TODO(terry): Implement |
| 432 } |
| 433 |
| 434 } |
| 435 |
| 436 |
| 437 main() { |
| 438 SelectorLiteralTest.testMain(); |
| 439 } |
OLD | NEW |