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 library web_components.test.build.script_compactor_test; |
| 5 |
| 6 import 'package:code_transformers/tests.dart'; |
| 7 import 'package:web_components/build/messages.dart'; |
| 8 import 'package:web_components/build/script_compactor.dart'; |
| 9 import 'package:unittest/compact_vm_config.dart'; |
| 10 import 'package:unittest/unittest.dart'; |
| 11 |
| 12 var transformer = new ScriptCompactorTransformer(); |
| 13 var phases = [[transformer]]; |
| 14 |
| 15 main() { |
| 16 useCompactVMConfiguration(); |
| 17 |
| 18 group('basic', basicTests); |
| 19 group('code extraction tests', codeExtractorTests); |
| 20 group('fixes import/export/part URIs', dartUriTests); |
| 21 group('validates script-tag URIs', validateUriTests); |
| 22 } |
| 23 |
| 24 void basicTests() { |
| 25 testPhases('single script', phases, { |
| 26 'a|web/index.html': ''' |
| 27 <!DOCTYPE html><html><head></head><body> |
| 28 <script type="application/dart" src="index.dart"></script> |
| 29 </body></html>''', |
| 30 'a|web/index.dart': ''' |
| 31 library a.index; |
| 32 main(){}''', |
| 33 }, { |
| 34 'a|web/index.html': ''' |
| 35 <!DOCTYPE html><html><head></head><body> |
| 36 <script type="application/dart" src="index.bootstrap.dart"></script> |
| 37 </body></html>''', |
| 38 'a|web/index.bootstrap.dart': ''' |
| 39 library a.web.index_bootstrap_dart; |
| 40 |
| 41 import 'index.dart' as i0; |
| 42 |
| 43 main() => i0.main();''', |
| 44 'a|web/index.dart': ''' |
| 45 library a.index; |
| 46 main(){}''', |
| 47 }, [], StringFormatter.noNewlinesOrSurroundingWhitespace); |
| 48 |
| 49 testPhases('multiple scripts from nested html import', phases, { |
| 50 'a|web/index.html': ''' |
| 51 <!DOCTYPE html><html> |
| 52 <head> |
| 53 <link rel="import" href="packages/b/a.html"> |
| 54 </head> |
| 55 <body> |
| 56 <script type="application/dart" src="index.dart"></script> |
| 57 </body> |
| 58 </body></html>''', |
| 59 'a|web/index.dart': ''' |
| 60 library a.index; |
| 61 main(){}''', |
| 62 'b|lib/a.html': ''' |
| 63 <link rel="import" href="b/b.html"> |
| 64 <link rel="import" href="../../packages/c/c.html"> |
| 65 <script type="application/dart" src="a.dart"></script>''', |
| 66 'b|lib/b/b.html': '<script type="application/dart" src="b.dart"></script>', |
| 67 'b|lib/a.dart': 'library b.a;', |
| 68 'b|lib/b/b.dart': 'library b.b.b;', |
| 69 'c|lib/c.html': '<script type="application/dart" src="c.dart"></script>', |
| 70 'c|lib/c.dart': 'library c.c;', |
| 71 }, { |
| 72 'a|web/index.html': ''' |
| 73 <!DOCTYPE html><html> |
| 74 <head> |
| 75 <link rel="import" href="packages/b/a.html"> |
| 76 </head> |
| 77 <body> |
| 78 <script type="application/dart" src="index.bootstrap.dart"></script> |
| 79 </body></html>''', |
| 80 'a|web/index.bootstrap.dart': ''' |
| 81 library a.web.index_bootstrap_dart; |
| 82 |
| 83 import 'package:b/b/b.dart' as i0; |
| 84 import 'package:c/c.dart' as i1; |
| 85 import 'package:b/a.dart' as i2; |
| 86 import 'index.dart' as i3; |
| 87 |
| 88 main() => i3.main();''', |
| 89 'b|lib/a.html': ''' |
| 90 <link rel="import" href="b/b.html"> |
| 91 <link rel="import" href="../../packages/c/c.html"> |
| 92 <script type="application/dart" src="a.dart"></script>''', |
| 93 }, [], StringFormatter.noNewlinesOrSurroundingWhitespace); |
| 94 |
| 95 testPhases('inline scripts', phases, { |
| 96 'a|web/index.html': ''' |
| 97 <!DOCTYPE html> |
| 98 <html> |
| 99 <head> |
| 100 <link rel="import" href="packages/a/foo.html"> |
| 101 </head> |
| 102 <body> |
| 103 <script type="application/dart"> |
| 104 library a.index; |
| 105 main(){} |
| 106 </script> |
| 107 </body> |
| 108 </html>''', |
| 109 'a|lib/foo.html': ''' |
| 110 <script type="application/dart"> |
| 111 library a.foo; |
| 112 |
| 113 import 'bar.dart'; |
| 114 </script>''', |
| 115 }, { |
| 116 'a|web/index.html': ''' |
| 117 <!DOCTYPE html> |
| 118 <html> |
| 119 <head> |
| 120 <link rel="import" href="packages/a/foo.html"> |
| 121 </head> |
| 122 <body> |
| 123 <script type="application/dart" src="index.bootstrap.dart"></script> |
| 124 </body> |
| 125 </html>''', |
| 126 'a|web/index.html.1.dart': ''' |
| 127 library a.index; |
| 128 main(){}''', |
| 129 'a|web/index.html.0.dart': ''' |
| 130 library a.foo; |
| 131 |
| 132 import 'package:a/bar.dart';''', |
| 133 'a|web/index.bootstrap.dart': ''' |
| 134 library a.web.index_bootstrap_dart; |
| 135 |
| 136 import 'index.html.0.dart' as i0; |
| 137 import 'index.html.1.dart' as i1; |
| 138 |
| 139 main() => i1.main();''', |
| 140 }, [], StringFormatter.noNewlinesOrSurroundingWhitespace); |
| 141 |
| 142 testPhases('Cleans library names generated from file paths.', phases, { |
| 143 'a|web/01_test.html': ''' |
| 144 <!DOCTYPE html><html><head> |
| 145 <script type="application/dart">/*1*/</script> |
| 146 </head></html>''', |
| 147 'a|web/foo_02_test.html': ''' |
| 148 <!DOCTYPE html><html><head> |
| 149 <script type="application/dart">/*2*/</script> |
| 150 </head></html>''', |
| 151 'a|web/test_03.html': ''' |
| 152 <!DOCTYPE html><html><head> |
| 153 <script type="application/dart">/*3*/</script> |
| 154 </head></html>''', |
| 155 'a|web/*test_%foo_04!.html': ''' |
| 156 <!DOCTYPE html><html><head> |
| 157 <script type="application/dart">/*4*/</script> |
| 158 </head></html>''', |
| 159 'a|web/%05_test.html': ''' |
| 160 <!DOCTYPE html><html><head> |
| 161 <script type="application/dart">/*5*/</script> |
| 162 </head></html>''', |
| 163 }, { |
| 164 // Appends an _ if it starts with a number. |
| 165 'a|web/01_test.html.0.dart': 'library a.web._01_test_html_0;\n/*1*/', |
| 166 // Allows numbers in the middle. |
| 167 'a|web/foo_02_test.html.0.dart': 'library a.web.foo_02_test_html_0;\n/*2*/', |
| 168 // Allows numbers at the end. |
| 169 'a|web/test_03.html.0.dart': 'library a.web.test_03_html_0;\n/*3*/', |
| 170 // Replaces invalid characters with _. |
| 171 'a|web/*test_%foo_04!.html.0.dart': |
| 172 'library a.web._test__foo_04__html_0;\n/*4*/', |
| 173 // Replace invalid character followed by number. |
| 174 'a|web/%05_test.html.0.dart': 'library a.web._05_test_html_0;\n/*5*/', |
| 175 }, [], StringFormatter.noNewlinesOrSurroundingWhitespace); |
| 176 } |
| 177 |
| 178 void codeExtractorTests() { |
| 179 testPhases('no dart script', phases, { |
| 180 'a|web/test.html': '<!DOCTYPE html><html></html>', |
| 181 }, {}, [ |
| 182 'error: Found either zero or multiple dart scripts in the entry point ' |
| 183 '`web/test.html`. Exactly one was expected.', |
| 184 ], StringFormatter.noNewlinesOrSurroundingWhitespace); |
| 185 |
| 186 testPhases('single script, no library in script', phases, { |
| 187 'a|web/test.html': ''' |
| 188 <!DOCTYPE html><html><head> |
| 189 <script type="application/dart">main() { }</script>''', |
| 190 }, { |
| 191 'a|web/test.html': ''' |
| 192 <!DOCTYPE html><html><head> |
| 193 <script type="application/dart" src="test.bootstrap.dart"> |
| 194 </script> |
| 195 </head><body></body></html>''', |
| 196 'a|web/test.html.0.dart': ''' |
| 197 library a.web.test_html_0; |
| 198 main() { }''', |
| 199 'a|web/test.bootstrap.dart': ''' |
| 200 library a.web.test_bootstrap_dart; |
| 201 |
| 202 import 'test.html.0.dart' as i0; |
| 203 |
| 204 main() => i0.main();''', |
| 205 }, [], StringFormatter.noNewlinesOrSurroundingWhitespace); |
| 206 |
| 207 testPhases('single script, with library', phases, { |
| 208 'a|web/test.html': ''' |
| 209 <!DOCTYPE html><html><head> |
| 210 <script type="application/dart"> |
| 211 library f; |
| 212 main() { } |
| 213 </script>''', |
| 214 }, { |
| 215 'a|web/test.html': ''' |
| 216 <!DOCTYPE html><html><head> |
| 217 <script type="application/dart" src="test.bootstrap.dart"> |
| 218 </script> |
| 219 </head><body></body></html>''', |
| 220 'a|web/test.html.0.dart': ''' |
| 221 library f; |
| 222 main() { }''', |
| 223 'a|web/test.bootstrap.dart': ''' |
| 224 library a.web.test_bootstrap_dart; |
| 225 |
| 226 import 'test.html.0.dart' as i0; |
| 227 |
| 228 main() => i0.main();''', |
| 229 }, [], StringFormatter.noNewlinesOrSurroundingWhitespace); |
| 230 |
| 231 testPhases('under lib/ directory not transformed', phases, { |
| 232 'a|lib/test.html': ''' |
| 233 <!DOCTYPE html><html><head> |
| 234 <script type="application/dart"> |
| 235 library f; |
| 236 main() { } |
| 237 </script>''', |
| 238 }, { |
| 239 'a|lib/test.html': ''' |
| 240 <!DOCTYPE html><html><head> |
| 241 <script type="application/dart"> |
| 242 library f; |
| 243 main() { } |
| 244 </script>''', |
| 245 }, [], StringFormatter.noNewlinesOrSurroundingWhitespace); |
| 246 |
| 247 testPhases('multiple scripts - error', phases, { |
| 248 'a|web/test.html': ''' |
| 249 <!DOCTYPE html><html><head> |
| 250 <script type="application/dart"> |
| 251 library a1; |
| 252 main1() { } |
| 253 </script> |
| 254 <script type="application/dart">library a2;\nmain2() { }</script>''', |
| 255 }, {}, [ |
| 256 'error: Found either zero or multiple dart scripts in the entry point ' |
| 257 '`web/test.html`. Exactly one was expected.', |
| 258 ], StringFormatter.noNewlinesOrSurroundingWhitespace); |
| 259 |
| 260 testPhases('multiple imported scripts', phases, { |
| 261 'a|web/test.html': ''' |
| 262 <link rel="import" href="test2.html"> |
| 263 <link rel="import" href="bar/test.html"> |
| 264 <link rel="import" href="packages/a/foo/test.html"> |
| 265 <link rel="import" href="packages/b/test.html"> |
| 266 <script type="application/dart" src="test.dart"></script>''', |
| 267 'a|web/test.dart': 'library a.test;', |
| 268 'a|web/test2.html': '<script type="application/dart">main1() { }', |
| 269 'a|web/bar/test.html': '<script type="application/dart">main2() { }', |
| 270 'a|lib/foo/test.html': '<script type="application/dart">main3() { }', |
| 271 'b|lib/test.html': '<script type="application/dart">main4() { }', |
| 272 }, { |
| 273 'a|web/test.html': ''' |
| 274 <html> |
| 275 <head> |
| 276 <link rel="import" href="test2.html"> |
| 277 <link rel="import" href="bar/test.html"> |
| 278 <link rel="import" href="packages/a/foo/test.html"> |
| 279 <link rel="import" href="packages/b/test.html"> |
| 280 <script type="application/dart" src="test.bootstrap.dart"></script> |
| 281 </head><body></body></html>''', |
| 282 'a|web/test.bootstrap.dart': ''' |
| 283 library a.web.test_bootstrap_dart; |
| 284 import 'test.html.0.dart' as i0; |
| 285 import 'test.html.1.dart' as i1; |
| 286 import 'test.html.2.dart' as i2; |
| 287 import 'test.html.3.dart' as i3; |
| 288 import 'test.dart' as i4; |
| 289 |
| 290 main() => i4.main(); |
| 291 ''', |
| 292 'a|web/test.html.0.dart': ''' |
| 293 library a.web.test_html_0; |
| 294 main1() { }''', |
| 295 'a|web/test.html.1.dart': ''' |
| 296 library a.web.test_html_1; |
| 297 main2() { }''', |
| 298 'a|web/test.html.2.dart': ''' |
| 299 library a.web.test_html_2; |
| 300 main3() { }''', |
| 301 'a|web/test.html.3.dart': ''' |
| 302 library a.web.test_html_3; |
| 303 main4() { }''', |
| 304 }, [], StringFormatter.noNewlinesOrSurroundingWhitespace); |
| 305 } |
| 306 |
| 307 dartUriTests() { |
| 308 testPhases('from web folder', phases, { |
| 309 'a|web/test.html': ''' |
| 310 <!DOCTYPE html><html><head> |
| 311 <link rel="import" href="test2/foo.html"> |
| 312 <script type="application/dart" src="test.dart"></script> |
| 313 </head><body></body></html>''', |
| 314 'a|web/test.dart': 'library a.test;', |
| 315 'a|web/test2/foo.html': ''' |
| 316 <!DOCTYPE html><html><head></head><body> |
| 317 <script type="application/dart"> |
| 318 import 'package:qux/qux.dart'; |
| 319 import 'foo.dart'; |
| 320 export 'bar.dart'; |
| 321 part 'baz.dart'; |
| 322 </script> |
| 323 </body></html>''', |
| 324 }, { |
| 325 'a|web/test.html': ''' |
| 326 <!DOCTYPE html><html><head> |
| 327 <link rel="import" href="test2/foo.html"> |
| 328 <script type="application/dart" src="test.bootstrap.dart"></script> |
| 329 </head><body></body></html>''', |
| 330 'a|web/test.html.0.dart': ''' |
| 331 library a.web.test_html_0; |
| 332 |
| 333 import 'package:qux/qux.dart'; |
| 334 import 'test2/foo.dart'; |
| 335 export 'test2/bar.dart'; |
| 336 part 'test2/baz.dart';''', |
| 337 'a|web/test2/foo.html': ''' |
| 338 <!DOCTYPE html><html><head></head><body> |
| 339 <script type="application/dart" src="foo.bootstrap.dart"> |
| 340 </script> |
| 341 </body></html>''', |
| 342 'a|web/test2/foo.html.0.dart': ''' |
| 343 library a.web.test2.foo_html_0; |
| 344 |
| 345 import 'package:qux/qux.dart'; |
| 346 import 'foo.dart'; |
| 347 export 'bar.dart'; |
| 348 part 'baz.dart';''', |
| 349 }, [], StringFormatter.noNewlinesOrSurroundingWhitespace); |
| 350 |
| 351 testPhases('from lib folder', phases, { |
| 352 'a|web/test.html': ''' |
| 353 <!DOCTYPE html><html><head> |
| 354 <link rel="import" href="packages/a/test2/foo.html"> |
| 355 <script type="application/dart" src="test.dart"></script> |
| 356 </head><body></body></html>''', |
| 357 'a|web/test.dart': 'library a.test;', |
| 358 'a|lib/test2/foo.html': ''' |
| 359 <!DOCTYPE html><html><head></head><body> |
| 360 <script type="application/dart"> |
| 361 import 'package:qux/qux.dart'; |
| 362 import 'foo.dart'; |
| 363 export 'bar.dart'; |
| 364 part 'baz.dart'; |
| 365 </script> |
| 366 </body></html>''', |
| 367 }, { |
| 368 'a|web/test.html': ''' |
| 369 <!DOCTYPE html><html><head> |
| 370 <link rel="import" href="packages/a/test2/foo.html"> |
| 371 <script type="application/dart" src="test.bootstrap.dart"></script> |
| 372 </head><body></body></html>''', |
| 373 'a|web/test.html.0.dart': ''' |
| 374 library a.web.test_html_0; |
| 375 |
| 376 import 'package:qux/qux.dart'; |
| 377 import 'package:a/test2/foo.dart'; |
| 378 export 'package:a/test2/bar.dart'; |
| 379 part 'package:a/test2/baz.dart';''', |
| 380 'a|lib/test2/foo.html': ''' |
| 381 <!DOCTYPE html><html><head></head><body> |
| 382 <script type="application/dart"> |
| 383 import 'package:qux/qux.dart'; |
| 384 import 'foo.dart'; |
| 385 export 'bar.dart'; |
| 386 part 'baz.dart'; |
| 387 </script> |
| 388 </body></html>''', |
| 389 }, [], StringFormatter.noNewlinesOrSurroundingWhitespace); |
| 390 |
| 391 testPhases('from another pkg', phases, { |
| 392 'a|web/test.html': ''' |
| 393 <!DOCTYPE html><html><head> |
| 394 <link rel="import" href="packages/b/test2/foo.html"> |
| 395 <script type="application/dart" src="test.dart"></script> |
| 396 </head><body></body></html>''', |
| 397 'a|web/test.dart': 'library a.test;', |
| 398 'b|lib/test2/foo.html': ''' |
| 399 <!DOCTYPE html><html><head></head><body> |
| 400 <script type="application/dart"> |
| 401 import 'package:qux/qux.dart'; |
| 402 import 'foo.dart'; |
| 403 export 'bar.dart'; |
| 404 part 'baz.dart'; |
| 405 </script> |
| 406 </body></html>''', |
| 407 }, { |
| 408 'a|web/test.html': ''' |
| 409 <!DOCTYPE html><html><head> |
| 410 <link rel="import" href="packages/b/test2/foo.html"> |
| 411 <script type="application/dart" src="test.bootstrap.dart"></script> |
| 412 </head><body></body></html>''', |
| 413 'a|web/test.html.0.dart': ''' |
| 414 library a.web.test_html_0; |
| 415 |
| 416 import 'package:qux/qux.dart'; |
| 417 import 'package:b/test2/foo.dart'; |
| 418 export 'package:b/test2/bar.dart'; |
| 419 part 'package:b/test2/baz.dart';''', |
| 420 }, [], StringFormatter.noNewlinesOrSurroundingWhitespace); |
| 421 } |
| 422 |
| 423 validateUriTests() { |
| 424 testPhases('script src is invalid', phases, { |
| 425 'a|web/test.html': ''' |
| 426 <!DOCTYPE html><html><body> |
| 427 <script type="application/dart" src="a.dart"></script> |
| 428 </body></html>''', |
| 429 }, {}, [ |
| 430 'warning: ${scriptFileNotFound.create({'url': 'a|web/a.dart'}).snippet} ' |
| 431 '(web/test.html 1 8)', |
| 432 ]); |
| 433 } |
OLD | NEW |