OLD | NEW |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 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 | 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 dart2js produces the expected static type warnings to ensures that | 5 // Test that dart2js produces the expected static type warnings to ensures that |
6 // the analyzer and dart2js agrees on the tests. | 6 // the analyzer and dart2js agrees on the tests. |
7 | 7 |
8 import 'dart:async'; | 8 import 'dart:async'; |
9 import 'dart:io'; | 9 import 'dart:io'; |
10 import 'package:expect/expect.dart'; | 10 import 'package:expect/expect.dart'; |
11 import 'package:async_helper/async_helper.dart'; | 11 import 'package:async_helper/async_helper.dart'; |
12 import 'memory_compiler.dart'; | 12 import 'memory_compiler.dart'; |
13 import 'package:compiler/src/commandline_options.dart'; | 13 import 'package:compiler/src/commandline_options.dart'; |
14 import 'package:compiler/src/filenames.dart'; | 14 import 'package:compiler/src/filenames.dart'; |
15 import 'package:compiler/src/io/source_file.dart'; | 15 import 'package:compiler/src/io/source_file.dart'; |
16 import 'package:compiler/src/source_file_provider.dart'; | 16 import 'package:compiler/src/source_file_provider.dart'; |
17 import 'package:compiler/src/util/uri_extras.dart'; | 17 import 'package:compiler/src/util/uri_extras.dart'; |
18 import 'dart:convert'; | 18 import 'dart:convert'; |
19 | 19 |
20 void checkWarnings(Map<String, dynamic> tests, [List<String> arguments]) { | 20 void checkWarnings(Map<String, dynamic> tests, [List<String> arguments]) { |
21 bool isWindows = Platform.isWindows; | 21 bool isWindows = Platform.isWindows; |
22 Uri script = currentDirectory.resolveUri(Platform.script); | 22 Uri script = currentDirectory.resolveUri(Platform.script); |
23 bool warningsMismatch = false; | 23 bool warningsMismatch = false; |
24 bool verbose = arguments != null && arguments.contains('-v'); | 24 bool verbose = arguments != null && arguments.contains('-v'); |
25 asyncTest(() => Future.forEach(tests.keys, (String test) async { | 25 asyncTest(() => Future.forEach(tests.keys, (String test) async { |
26 Uri uri = script.resolve('../../$test'); | 26 Uri uri = script.resolve('../../$test'); |
27 String source = UTF8.decode(readAll(uriPathToNative(uri.path))); | 27 String source = UTF8.decode(readAll(uriPathToNative(uri.path))); |
28 SourceFile file = new StringSourceFile( | 28 SourceFile file = new StringSourceFile( |
29 uri, relativize(currentDirectory, uri, isWindows), source); | 29 uri, relativize(currentDirectory, uri, isWindows), source); |
30 Map<int,String> expectedWarnings = {}; | 30 Map<int, String> expectedWarnings = {}; |
31 int lineNo = 0; | 31 int lineNo = 0; |
32 for (String line in source.split('\n')) { | 32 for (String line in source.split('\n')) { |
33 if (line.contains('///') && | 33 if (line.contains('///') && |
34 (line.contains('static type warning') || | 34 (line.contains('static type warning') || |
35 line.contains('static warning'))) { | 35 line.contains('static warning'))) { |
36 expectedWarnings[lineNo] = line; | 36 expectedWarnings[lineNo] = line; |
37 } | 37 } |
38 lineNo++; | 38 lineNo++; |
39 } | |
40 Set<int> unseenWarnings = new Set<int>.from(expectedWarnings.keys); | |
41 DiagnosticCollector collector = new DiagnosticCollector(); | |
42 await runCompiler( | |
43 entryPoint: uri, | |
44 diagnosticHandler: collector, | |
45 options: [Flags.analyzeOnly], | |
46 showDiagnostics: verbose); | |
47 Map<String, List<int>> statusMap = tests[test]; | |
48 // Line numbers with known unexpected warnings. | |
49 List<int> unexpectedStatus = []; | |
50 if (statusMap != null && statusMap.containsKey('unexpected')) { | |
51 unexpectedStatus = statusMap['unexpected']; | |
52 } | |
53 // Line numbers with known missing warnings. | |
54 List<int> missingStatus = []; | |
55 if (statusMap != null && statusMap.containsKey('missing')) { | |
56 missingStatus = statusMap['missing']; | |
57 } | |
58 for (CollectedMessage message in collector.warnings) { | |
59 Expect.equals(uri, message.uri); | |
60 int lineNo = file.getLine(message.begin); | |
61 if (expectedWarnings.containsKey(lineNo)) { | |
62 unseenWarnings.remove(lineNo); | |
63 } else if (!unexpectedStatus.contains(lineNo+1)) { | |
64 warningsMismatch = true; | |
65 print(file.getLocationMessage( | |
66 'Unexpected warning: ${message.message}', | |
67 message.begin, message.end)); | |
68 } | |
69 } | |
70 if (!unseenWarnings.isEmpty) { | |
71 for (int lineNo in unseenWarnings) { | |
72 if (!missingStatus.contains(lineNo+1)) { | |
73 warningsMismatch = true; | |
74 String line = expectedWarnings[lineNo]; | |
75 print('$uri [${lineNo+1}]: Missing static type warning.'); | |
76 print(line); | |
77 } | 39 } |
78 } | 40 Set<int> unseenWarnings = new Set<int>.from(expectedWarnings.keys); |
79 } | 41 DiagnosticCollector collector = new DiagnosticCollector(); |
80 }).then((_) { | 42 await runCompiler( |
81 Expect.isFalse(warningsMismatch); | 43 entryPoint: uri, |
82 })); | 44 diagnosticHandler: collector, |
| 45 options: [Flags.analyzeOnly], |
| 46 showDiagnostics: verbose); |
| 47 Map<String, List<int>> statusMap = tests[test]; |
| 48 // Line numbers with known unexpected warnings. |
| 49 List<int> unexpectedStatus = []; |
| 50 if (statusMap != null && statusMap.containsKey('unexpected')) { |
| 51 unexpectedStatus = statusMap['unexpected']; |
| 52 } |
| 53 // Line numbers with known missing warnings. |
| 54 List<int> missingStatus = []; |
| 55 if (statusMap != null && statusMap.containsKey('missing')) { |
| 56 missingStatus = statusMap['missing']; |
| 57 } |
| 58 for (CollectedMessage message in collector.warnings) { |
| 59 Expect.equals(uri, message.uri); |
| 60 int lineNo = file.getLine(message.begin); |
| 61 if (expectedWarnings.containsKey(lineNo)) { |
| 62 unseenWarnings.remove(lineNo); |
| 63 } else if (!unexpectedStatus.contains(lineNo + 1)) { |
| 64 warningsMismatch = true; |
| 65 print(file.getLocationMessage( |
| 66 'Unexpected warning: ${message.message}', |
| 67 message.begin, |
| 68 message.end)); |
| 69 } |
| 70 } |
| 71 if (!unseenWarnings.isEmpty) { |
| 72 for (int lineNo in unseenWarnings) { |
| 73 if (!missingStatus.contains(lineNo + 1)) { |
| 74 warningsMismatch = true; |
| 75 String line = expectedWarnings[lineNo]; |
| 76 print('$uri [${lineNo+1}]: Missing static type warning.'); |
| 77 print(line); |
| 78 } |
| 79 } |
| 80 } |
| 81 }).then((_) { |
| 82 Expect.isFalse(warningsMismatch); |
| 83 })); |
83 } | 84 } |
OLD | NEW |