| OLD | NEW |
| 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 // Test that duplicate library names result in different messages depending | 5 // Test that duplicate library names result in different messages depending |
| 6 // on whether the libraries are based on the same resource. | 6 // on whether the libraries are based on the same resource. |
| 7 | 7 |
| 8 import 'dart:async'; |
| 8 import 'package:async_helper/async_helper.dart'; | 9 import 'package:async_helper/async_helper.dart'; |
| 9 import 'package:expect/expect.dart'; | 10 import 'package:expect/expect.dart'; |
| 10 | 11 import 'package:compiler/src/dart2jslib.dart' show MessageKind; |
| 11 import 'memory_compiler.dart'; | 12 import 'memory_compiler.dart'; |
| 12 | 13 |
| 13 void check(String kind, | 14 void check(String kind, |
| 14 Iterable<DiagnosticMessage> messages, | 15 Iterable<DiagnosticMessage> messages, |
| 15 List<String> prefixes) { | 16 List<MessageKind> expectedMessageKinds) { |
| 16 Expect.equals(messages.length, prefixes.length, | 17 Expect.equals(messages.length, expectedMessageKinds.length, |
| 17 "Unexpected $kind count: $messages"); | 18 "Unexpected $kind count: $messages"); |
| 18 int i = 0; | 19 int i = 0; |
| 19 messages.forEach((DiagnosticMessage message) { | 20 messages.forEach((DiagnosticMessage message) { |
| 20 Expect.isTrue(message.message.startsWith(prefixes[i++])); | 21 Expect.equals(expectedMessageKinds[i++], message.message.kind); |
| 21 }); | 22 }); |
| 22 } | 23 } |
| 23 | 24 |
| 24 void test(Map<String, String> source, | 25 Future test(Map<String, String> source, |
| 25 {List<String> warnings: const <String>[], | 26 {List<MessageKind> warnings: const <MessageKind>[], |
| 26 List<String> hints: const <String>[]}) { | 27 List<MessageKind> hints: const <MessageKind>[]}) async { |
| 27 DiagnosticCollector collector = new DiagnosticCollector(); | 28 DiagnosticCollector collector = new DiagnosticCollector(); |
| 28 var compiler = compilerFor(source, | 29 await runCompiler( |
| 29 diagnosticHandler: collector, | 30 memorySourceFiles: source, |
| 30 showDiagnostics: true, | 31 diagnosticHandler: collector, |
| 31 options: ['--analyze-only', '--analyze-all'], | 32 showDiagnostics: true, |
| 32 packageRoot: Uri.parse('memory:pkg/')); | 33 options: ['--analyze-only', '--analyze-all'], |
| 33 asyncTest(() => compiler.run(Uri.parse('memory:main.dart')).then((_) { | 34 packageRoot: Uri.parse('memory:pkg/')); |
| 34 Expect.isTrue(collector.errors.isEmpty); | 35 |
| 35 check('warning', collector.warnings, warnings); | 36 Expect.isTrue(collector.errors.isEmpty); |
| 36 check('hint', collector.hints, hints); | 37 check('warning', collector.warnings, warnings); |
| 37 Expect.isTrue(collector.infos.isEmpty); | 38 check('hint', collector.hints, hints); |
| 38 })); | 39 Expect.isTrue(collector.infos.isEmpty); |
| 39 } | 40 } |
| 40 | 41 |
| 41 void main() { | 42 void main() { |
| 42 test({ | 43 asyncTest(runTests); |
| 44 } |
| 45 |
| 46 Future runTests() async { |
| 47 await test({ |
| 43 'main.dart': """ | 48 'main.dart': """ |
| 44 library main; | 49 library main; |
| 45 | 50 |
| 46 import 'package:lib/foo.dart'; | 51 import 'package:lib/foo.dart'; |
| 47 import 'pkg/lib/foo.dart'; | 52 import 'pkg/lib/foo.dart'; |
| 48 """, | 53 """, |
| 49 'pkg/lib/foo.dart': """ | 54 'pkg/lib/foo.dart': """ |
| 50 library lib.foo; | 55 library lib.foo; |
| 51 """}, | 56 """}, |
| 52 warnings: ["The library 'lib.foo' in 'memory:pkg/lib/foo.dart' is loaded"]); | 57 warnings: [MessageKind.DUPLICATED_LIBRARY_RESOURCE]); |
| 53 | 58 |
| 54 test({ | 59 await test({ |
| 55 'main.dart': """ | 60 'main.dart': """ |
| 56 library main; | 61 library main; |
| 57 | 62 |
| 58 import 'package:lib/bar.dart'; | 63 import 'package:lib/bar.dart'; |
| 59 import 'pkg/foo.dart'; | 64 import 'pkg/foo.dart'; |
| 60 """, | 65 """, |
| 61 'pkg/foo.dart': """ | 66 'pkg/foo.dart': """ |
| 62 library foo; | 67 library foo; |
| 63 | 68 |
| 64 import 'lib/bar.dart'; | 69 import 'lib/bar.dart'; |
| 65 """, | 70 """, |
| 66 'pkg/lib/bar.dart': """ | 71 'pkg/lib/bar.dart': """ |
| 67 library lib.bar; | 72 library lib.bar; |
| 68 """}, | 73 """}, |
| 69 warnings: ["The library 'lib.bar' in 'memory:pkg/lib/bar.dart' is loaded"]); | 74 warnings: [MessageKind.DUPLICATED_LIBRARY_RESOURCE]); |
| 70 | 75 |
| 71 test({ | 76 await test({ |
| 72 'main.dart': """ | 77 'main.dart': """ |
| 73 library main; | 78 library main; |
| 74 | 79 |
| 75 import 'foo.dart'; | 80 import 'foo.dart'; |
| 76 import 'pkg/lib/baz.dart'; | 81 import 'pkg/lib/baz.dart'; |
| 77 """, | 82 """, |
| 78 'foo.dart': """ | 83 'foo.dart': """ |
| 79 library foo; | 84 library foo; |
| 80 | 85 |
| 81 import 'package:lib/baz.dart'; | 86 import 'package:lib/baz.dart'; |
| 82 """, | 87 """, |
| 83 'pkg/lib/baz.dart': """ | 88 'pkg/lib/baz.dart': """ |
| 84 library lib.baz; | 89 library lib.baz; |
| 85 """}, | 90 """}, |
| 86 warnings: ["The library 'lib.baz' in 'memory:pkg/lib/baz.dart' is loaded"]); | 91 warnings: [MessageKind.DUPLICATED_LIBRARY_RESOURCE]); |
| 87 | 92 |
| 88 test({ | 93 await test({ |
| 89 'main.dart': """ | 94 'main.dart': """ |
| 90 library main; | 95 library main; |
| 91 | 96 |
| 92 import 'foo.dart'; | 97 import 'foo.dart'; |
| 93 import 'pkg/bar.dart'; | 98 import 'pkg/bar.dart'; |
| 94 """, | 99 """, |
| 95 'foo.dart': """ | 100 'foo.dart': """ |
| 96 library foo; | 101 library foo; |
| 97 | 102 |
| 98 import 'package:lib/boz.dart'; | 103 import 'package:lib/boz.dart'; |
| 99 """, | 104 """, |
| 100 'pkg/bar.dart': """ | 105 'pkg/bar.dart': """ |
| 101 library bar; | 106 library bar; |
| 102 | 107 |
| 103 import 'lib/boz.dart'; | 108 import 'lib/boz.dart'; |
| 104 """, | 109 """, |
| 105 'pkg/lib/boz.dart': """ | 110 'pkg/lib/boz.dart': """ |
| 106 library lib.boz; | 111 library lib.boz; |
| 107 """}, | 112 """}, |
| 108 warnings: ["The library 'lib.boz' in 'memory:pkg/lib/boz.dart' is loaded"]); | 113 warnings: [MessageKind.DUPLICATED_LIBRARY_RESOURCE]); |
| 109 | 114 |
| 110 test({ | 115 await test({ |
| 111 'main.dart': """ | 116 'main.dart': """ |
| 112 library main; | 117 library main; |
| 113 | 118 |
| 114 import 'package:lib/qux.dart'; | 119 import 'package:lib/qux.dart'; |
| 115 import 'pkg/lib/qux.dart'; | 120 import 'pkg/lib/qux.dart'; |
| 116 """, | 121 """, |
| 117 'pkg/lib/qux.dart': """ | 122 'pkg/lib/qux.dart': """ |
| 118 // No library tag. | 123 // No library tag. |
| 119 """}, | 124 """}, |
| 120 hints: ["The resource 'memory:pkg/lib/qux.dart' is loaded"]); | 125 hints: [MessageKind.DUPLICATED_RESOURCE]); |
| 121 | 126 |
| 122 test({ | 127 await test({ |
| 123 'main.dart': """ | 128 'main.dart': """ |
| 124 library main; | 129 library main; |
| 125 | 130 |
| 126 import 'foo.dart'; | 131 import 'foo.dart'; |
| 127 import 'bar.dart'; | 132 import 'bar.dart'; |
| 128 """, | 133 """, |
| 129 'foo.dart': """ | 134 'foo.dart': """ |
| 130 library lib; | 135 library lib; |
| 131 """, | 136 """, |
| 132 'bar.dart': """ | 137 'bar.dart': """ |
| 133 library lib; | 138 library lib; |
| 134 """}, | 139 """}, |
| 135 warnings: ["Duplicated library name 'lib'.", | 140 warnings: [MessageKind.DUPLICATED_LIBRARY_NAME, |
| 136 "Duplicated library name 'lib'."]); | 141 MessageKind.DUPLICATED_LIBRARY_NAME]); |
| 137 } | 142 } |
| 138 | 143 |
| OLD | NEW |