OLD | NEW |
(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 nested_test; |
| 6 |
| 7 import 'dart:utf'; |
| 8 import 'package:unittest/unittest.dart'; |
| 9 import 'package:csslib/parser.dart'; |
| 10 import 'package:csslib/visitor.dart'; |
| 11 import 'testing.dart'; |
| 12 |
| 13 List optionsCss = ['--no-colors', 'memory']; |
| 14 |
| 15 compileAndValidate(String input, String generated) { |
| 16 var errors = []; |
| 17 var stylesheet = compileCss(input, errors: errors, opts: optionsCss); |
| 18 expect(stylesheet != null, true); |
| 19 expect(errors.isEmpty, true, reason: errors.toString()); |
| 20 expect(prettyPrint(stylesheet), generated); |
| 21 } |
| 22 |
| 23 selectorVariations() { |
| 24 final input1 = r'''html { color: red; }'''; |
| 25 final generated1 = r'''html { |
| 26 color: #f00; |
| 27 }'''; |
| 28 compileAndValidate(input1, generated1); |
| 29 |
| 30 final input2 = r'''button { span { height: 200 } }'''; |
| 31 final generated2 = r'''button { |
| 32 } |
| 33 button span { |
| 34 height: 200; |
| 35 }'''; |
| 36 compileAndValidate(input2, generated2); |
| 37 |
| 38 final input3 = r'''div { color: red; } button { span { height: 200 } }'''; |
| 39 final generated3 = r'''div { |
| 40 color: #f00; |
| 41 } |
| 42 button { |
| 43 } |
| 44 button span { |
| 45 height: 200; |
| 46 }'''; |
| 47 compileAndValidate(input3, generated3); |
| 48 |
| 49 final input4 = r'''#header { color: red; h1 { font-size: 26px; } }'''; |
| 50 final generated4 = r'''#header { |
| 51 color: #f00; |
| 52 } |
| 53 #header h1 { |
| 54 font-size: 26px; |
| 55 }'''; |
| 56 compileAndValidate(input4, generated4); |
| 57 |
| 58 final input5 = r''' |
| 59 #header { |
| 60 color: red; |
| 61 h1 { font-size: 26px; } |
| 62 background-color: blue; |
| 63 }'''; |
| 64 final generated5 = r'''#header { |
| 65 color: #f00; |
| 66 background-color: #00f; |
| 67 } |
| 68 #header h1 { |
| 69 font-size: 26px; |
| 70 }'''; |
| 71 compileAndValidate(input5, generated5); |
| 72 |
| 73 final input6 = r'''html { body {color: red; }}'''; |
| 74 final generated6 = r'''html { |
| 75 } |
| 76 html body { |
| 77 color: #f00; |
| 78 }'''; |
| 79 compileAndValidate(input6, generated6); |
| 80 |
| 81 final input7 = r'''html body {color: red; }'''; |
| 82 final generated7 = r'''html body { |
| 83 color: #f00; |
| 84 }'''; |
| 85 compileAndValidate(input7, generated7); |
| 86 |
| 87 final input8 = r''' |
| 88 html, body { color: red; } |
| 89 button { height: 200 } |
| 90 body { width: 300px; }'''; |
| 91 final generated8 = r'''html, body { |
| 92 color: #f00; |
| 93 } |
| 94 button { |
| 95 height: 200; |
| 96 } |
| 97 body { |
| 98 width: 300px; |
| 99 }'''; |
| 100 compileAndValidate(input8, generated8); |
| 101 |
| 102 final input9 = ''' |
| 103 html, body { |
| 104 color: red; |
| 105 button { height: 200 } |
| 106 div { width: 300px; } |
| 107 }'''; |
| 108 final generated9 = r'''html, body { |
| 109 color: #f00; |
| 110 } |
| 111 html button, body button { |
| 112 height: 200; |
| 113 } |
| 114 html div, body div { |
| 115 width: 300px; |
| 116 }'''; |
| 117 compileAndValidate(input9, generated9); |
| 118 |
| 119 final input10 = ''' |
| 120 html { |
| 121 color: red; |
| 122 button, div { height: 200 } |
| 123 body { width: 300px; } |
| 124 }'''; |
| 125 final generated10 = r'''html { |
| 126 color: #f00; |
| 127 } |
| 128 html button, html div { |
| 129 height: 200; |
| 130 } |
| 131 html body { |
| 132 width: 300px; |
| 133 }'''; |
| 134 compileAndValidate(input10, generated10); |
| 135 |
| 136 final input11 = ''' |
| 137 html, body { |
| 138 color: red; |
| 139 button, div { height: 200 } |
| 140 table { width: 300px; } |
| 141 }'''; |
| 142 final generated11 = r'''html, body { |
| 143 color: #f00; |
| 144 } |
| 145 html button, body button, html div, body div { |
| 146 height: 200; |
| 147 } |
| 148 html table, body table { |
| 149 width: 300px; |
| 150 }'''; |
| 151 compileAndValidate(input11, generated11); |
| 152 |
| 153 final input12 = ''' |
| 154 html, body { |
| 155 color: red; |
| 156 button, div { |
| 157 span, a, ul { height: 200 } |
| 158 } |
| 159 table { width: 300px; } |
| 160 }'''; |
| 161 final generated12 = r'''html, body { |
| 162 color: #f00; |
| 163 } |
| 164 ''' |
| 165 'html button span, body button span, html div span, body div span, ' |
| 166 'html button a, body button a, html div a, body div a, html button ul, ' |
| 167 r'''body button ul, html div ul, body div ul { |
| 168 height: 200; |
| 169 } |
| 170 html table, body table { |
| 171 width: 300px; |
| 172 }'''; |
| 173 compileAndValidate(input12, generated12); |
| 174 |
| 175 final input13 = r''' |
| 176 #header { |
| 177 div { |
| 178 width: 100px; |
| 179 a { height: 200px; } |
| 180 } |
| 181 color: blue; |
| 182 } |
| 183 span { color: #1f1f1f; } |
| 184 '''; |
| 185 final generated13 = r'''#header { |
| 186 color: #00f; |
| 187 } |
| 188 #header div { |
| 189 width: 100px; |
| 190 } |
| 191 #header div a { |
| 192 height: 200px; |
| 193 } |
| 194 span { |
| 195 color: #1f1f1f; |
| 196 }'''; |
| 197 compileAndValidate(input13, generated13); |
| 198 } |
| 199 |
| 200 void simpleNest() { |
| 201 final errors = []; |
| 202 final input = ''' |
| 203 div span { color: green; } |
| 204 #header { |
| 205 color: red; |
| 206 h1 { |
| 207 font-size: 26px; |
| 208 font-weight: bold; |
| 209 } |
| 210 p { |
| 211 font-size: 12px; |
| 212 a { |
| 213 text-decoration: none; |
| 214 } |
| 215 } |
| 216 background-color: blue; |
| 217 } |
| 218 div > span[attr="foo"] { color: yellow; } |
| 219 '''; |
| 220 |
| 221 final generated = |
| 222 r'''div span { |
| 223 color: #008000; |
| 224 } |
| 225 #header { |
| 226 color: #f00; |
| 227 background-color: #00f; |
| 228 } |
| 229 #header h1 { |
| 230 font-size: 26px; |
| 231 font-weight: bold; |
| 232 } |
| 233 #header p { |
| 234 font-size: 12px; |
| 235 } |
| 236 #header p a { |
| 237 text-decoration: none; |
| 238 } |
| 239 div > span[attr="foo"] { |
| 240 color: #ff0; |
| 241 }'''; |
| 242 compileAndValidate(input, generated); |
| 243 } |
| 244 |
| 245 void complexNest() { |
| 246 final errors = []; |
| 247 final input = ''' |
| 248 @font-face { font-family: arial; } |
| 249 div { color: #f0f0f0; } |
| 250 #header + div { |
| 251 color: url(abc.png); |
| 252 *[attr="bar"] { |
| 253 font-size: 26px; |
| 254 font-weight: bold; |
| 255 } |
| 256 p~ul { |
| 257 font-size: 12px; |
| 258 :not(p) { |
| 259 text-decoration: none; |
| 260 div > span[attr="foo"] { color: yellow; } |
| 261 } |
| 262 } |
| 263 background-color: blue; |
| 264 span { |
| 265 color: red; |
| 266 .one { color: blue; } |
| 267 .two { color: green; } |
| 268 .three { color: yellow; } |
| 269 .four { |
| 270 .four-1 { background-color: #00000f; } |
| 271 .four-2 { background-color: #0000ff; } |
| 272 .four-3 { background-color: #000fff; } |
| 273 .four-4 { |
| 274 height: 44px; |
| 275 .four-4-1 { height: 10px; } |
| 276 .four-4-2 { height: 20px; } |
| 277 .four-4-3 { height: 30px; } |
| 278 width: 44px; |
| 279 } |
| 280 } |
| 281 } |
| 282 } |
| 283 span { color: #1f1f2f; } |
| 284 '''; |
| 285 |
| 286 final generated = r'''@font-face { |
| 287 font-family: arial; |
| 288 } |
| 289 div { |
| 290 color: #f0f0f0; |
| 291 } |
| 292 #header + div { |
| 293 color: url("abc.png"); |
| 294 background-color: #00f; |
| 295 } |
| 296 #header + div *[attr="bar"] { |
| 297 font-size: 26px; |
| 298 font-weight: bold; |
| 299 } |
| 300 #header + div p ~ ul { |
| 301 font-size: 12px; |
| 302 } |
| 303 #header + div p ~ ul :not(p) { |
| 304 text-decoration: none; |
| 305 } |
| 306 #header + div p ~ ul :not(p) div > span[attr="foo"] { |
| 307 color: #ff0; |
| 308 } |
| 309 #header + div span { |
| 310 color: #f00; |
| 311 } |
| 312 #header + div span .one { |
| 313 color: #00f; |
| 314 } |
| 315 #header + div span .two { |
| 316 color: #008000; |
| 317 } |
| 318 #header + div span .three { |
| 319 color: #ff0; |
| 320 } |
| 321 #header + div span .four .four-1 { |
| 322 background-color: #00000f; |
| 323 } |
| 324 #header + div span .four .four-2 { |
| 325 background-color: #00f; |
| 326 } |
| 327 #header + div span .four .four-3 { |
| 328 background-color: #000fff; |
| 329 } |
| 330 #header + div span .four .four-4 { |
| 331 height: 44px; |
| 332 width: 44px; |
| 333 } |
| 334 #header + div span .four .four-4 .four-4-1 { |
| 335 height: 10px; |
| 336 } |
| 337 #header + div span .four .four-4 .four-4-2 { |
| 338 height: 20px; |
| 339 } |
| 340 #header + div span .four .four-4 .four-4-3 { |
| 341 height: 30px; |
| 342 } |
| 343 span { |
| 344 color: #1f1f2f; |
| 345 }'''; |
| 346 |
| 347 compileAndValidate(input, generated); |
| 348 } |
| 349 |
| 350 void mediaNesting() { |
| 351 var errors = []; |
| 352 |
| 353 final input = r''' |
| 354 @media screen and (-webkit-min-device-pixel-ratio:0) { |
| 355 #toggle-all { |
| 356 image: url(test.jpb); |
| 357 div, table { |
| 358 background: none; |
| 359 a { width: 100px; } |
| 360 } |
| 361 color: red; |
| 362 } |
| 363 } |
| 364 '''; |
| 365 final generated = r'''@media screen AND (-webkit-min-device-pixel-ratio:0) { |
| 366 #toggle-all { |
| 367 image: url("test.jpb"); |
| 368 color: #f00; |
| 369 } |
| 370 #toggle-all div, #toggle-all table { |
| 371 background: none; |
| 372 } |
| 373 #toggle-all div a, #toggle-all table a { |
| 374 width: 100px; |
| 375 } |
| 376 }'''; |
| 377 |
| 378 compileAndValidate(input, generated); |
| 379 } |
| 380 |
| 381 void simpleThis() { |
| 382 final errors = []; |
| 383 final input = '''#header { |
| 384 h1 { |
| 385 font-size: 26px; |
| 386 font-weight: bold; |
| 387 } |
| 388 p { font-size: 12px; |
| 389 a { text-decoration: none; |
| 390 &:hover { border-width: 1px } |
| 391 } |
| 392 } |
| 393 } |
| 394 '''; |
| 395 |
| 396 final generated = r'''#header { |
| 397 } |
| 398 #header h1 { |
| 399 font-size: 26px; |
| 400 font-weight: bold; |
| 401 } |
| 402 #header p { |
| 403 font-size: 12px; |
| 404 } |
| 405 #header p a { |
| 406 text-decoration: none; |
| 407 } |
| 408 #header p a:hover { |
| 409 border-width: 1px; |
| 410 }'''; |
| 411 |
| 412 compileAndValidate(input, generated); |
| 413 } |
| 414 |
| 415 void complexThis() { |
| 416 var errors = []; |
| 417 |
| 418 final input1 = r''' |
| 419 .light { |
| 420 .leftCol { |
| 421 .textLink { |
| 422 color: fooL1; |
| 423 &:hover { color: barL1;} |
| 424 } |
| 425 .picLink { |
| 426 background-image: url(/fooL1.jpg); |
| 427 &:hover { background-image: url(/barL1.jpg);} |
| 428 } |
| 429 .textWithIconLink { |
| 430 color: fooL2; |
| 431 background-image: url(/fooL2.jpg); |
| 432 &:hover { color: barL2; background-image: url(/barL2.jpg);} |
| 433 } |
| 434 } |
| 435 }'''; |
| 436 |
| 437 final generated1 = r'''.light { |
| 438 } |
| 439 .light .leftCol .textLink { |
| 440 color: fooL1; |
| 441 } |
| 442 .light .leftCol .textLink:hover { |
| 443 color: barL1; |
| 444 } |
| 445 .light .leftCol .picLink { |
| 446 background-image: url("/fooL1.jpg"); |
| 447 } |
| 448 .light .leftCol .picLink:hover { |
| 449 background-image: url("/barL1.jpg"); |
| 450 } |
| 451 .light .leftCol .textWithIconLink { |
| 452 color: fooL2; |
| 453 background-image: url("/fooL2.jpg"); |
| 454 } |
| 455 .light .leftCol .textWithIconLink:hover { |
| 456 color: barL2; |
| 457 background-image: url("/barL2.jpg"); |
| 458 }'''; |
| 459 |
| 460 compileAndValidate(input1, generated1); |
| 461 |
| 462 final input2 = r''' |
| 463 .textLink { |
| 464 .light .leftCol & { |
| 465 color: fooL1; |
| 466 &:hover { color: barL1; } |
| 467 } |
| 468 .light .rightCol & { |
| 469 color: fooL3; |
| 470 &:hover { color: barL3; } |
| 471 } |
| 472 }'''; |
| 473 |
| 474 final generated2 = r''' |
| 475 .textLink { |
| 476 } |
| 477 .light .leftCol .textLink { |
| 478 color: fooL1; |
| 479 } |
| 480 .light .leftCol .textLink:hover { |
| 481 color: barL1; |
| 482 } |
| 483 .light .rightCol .textLink { |
| 484 color: fooL3; |
| 485 } |
| 486 .light .rightCol .textLink:hover { |
| 487 color: barL3; |
| 488 }'''; |
| 489 |
| 490 compileAndValidate(input2, generated2); |
| 491 } |
| 492 |
| 493 variationsThis() { |
| 494 var errors = []; |
| 495 |
| 496 final input1 = r''' |
| 497 .textLink { |
| 498 a { |
| 499 light .leftCol & { |
| 500 color: red; |
| 501 } |
| 502 } |
| 503 }'''; |
| 504 final generated1 = r'''.textLink { |
| 505 } |
| 506 light .leftCol .textLink a { |
| 507 color: #f00; |
| 508 }'''; |
| 509 |
| 510 compileAndValidate(input1, generated1); |
| 511 |
| 512 final input2 = r'''.textLink { |
| 513 a { |
| 514 & light .leftCol & { |
| 515 color: red; |
| 516 } |
| 517 } |
| 518 }'''; |
| 519 final generated2 = r'''.textLink { |
| 520 } |
| 521 .textLink a light .leftCol .textLink a { |
| 522 color: #f00; |
| 523 }'''; |
| 524 compileAndValidate(input2, generated2); |
| 525 |
| 526 final input3 = r''' |
| 527 .textLink { |
| 528 a { |
| 529 & light .leftCol { color: red; } |
| 530 } |
| 531 }'''; |
| 532 final generated3 = r'''.textLink { |
| 533 } |
| 534 .textLink a light .leftCol { |
| 535 color: #f00; |
| 536 }'''; |
| 537 compileAndValidate(input3, generated3); |
| 538 |
| 539 final input4 = r''' |
| 540 .textLink { |
| 541 a { |
| 542 & light .leftCol { color: red; } |
| 543 &:hover { width: 100px; } |
| 544 } |
| 545 }'''; |
| 546 final generated4 = r'''.textLink { |
| 547 } |
| 548 .textLink a light .leftCol { |
| 549 color: #f00; |
| 550 } |
| 551 .textLink a:hover { |
| 552 width: 100px; |
| 553 }'''; |
| 554 compileAndValidate(input4, generated4); |
| 555 |
| 556 final input5 = r'''.textLink { a { &:hover { color: red; } } }'''; |
| 557 final generated5 = r'''.textLink { |
| 558 } |
| 559 .textLink a:hover { |
| 560 color: #f00; |
| 561 }'''; |
| 562 |
| 563 compileAndValidate(input5, generated5); |
| 564 |
| 565 final input6 = r'''.textLink { &:hover { color: red; } }'''; |
| 566 final generated6 = r'''.textLink { |
| 567 } |
| 568 .textLink:hover { |
| 569 color: #f00; |
| 570 }'''; |
| 571 compileAndValidate(input6, generated6); |
| 572 |
| 573 final input7 = r'''.textLink { a { & + & { color: red; } } }'''; |
| 574 final generated7 = r'''.textLink { |
| 575 } |
| 576 .textLink a + .textLink a { |
| 577 color: #f00; |
| 578 }'''; |
| 579 compileAndValidate(input7, generated7); |
| 580 |
| 581 final input8 = r'''.textLink { a { & { color: red; } } }'''; |
| 582 final generated8 = r'''.textLink { |
| 583 } |
| 584 .textLink a { |
| 585 color: #f00; |
| 586 }'''; |
| 587 compileAndValidate(input8, generated8); |
| 588 |
| 589 final input9 = r'''.textLink { a { & ~ & { color: red; } } }'''; |
| 590 final generated9 = r'''.textLink { |
| 591 } |
| 592 .textLink a ~ .textLink a { |
| 593 color: #f00; |
| 594 }'''; |
| 595 compileAndValidate(input9, generated9); |
| 596 |
| 597 final input10 = r'''.textLink { a { & & { color: red; } } }'''; |
| 598 final generated10 = r'''.textLink { |
| 599 } |
| 600 .textLink a .textLink a { |
| 601 color: #f00; |
| 602 }'''; |
| 603 compileAndValidate(input10, generated10); |
| 604 } |
| 605 |
| 606 main() { |
| 607 test('Selector and Nested Variations', selectorVariations); |
| 608 test('Simple nesting', simpleNest); |
| 609 test('Complex nesting', complexNest); |
| 610 test('@media nesting', mediaNesting); |
| 611 test('Simple &', simpleThis); |
| 612 test("Variations &", variationsThis); |
| 613 test('Complex &', complexThis); |
| 614 } |
OLD | NEW |