| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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 library multitest; | 5 library multitest; |
| 6 | 6 |
| 7 import "dart:async"; | 7 import "dart:async"; |
| 8 import "dart:io"; | 8 import "dart:io"; |
| 9 | 9 |
| 10 import "path.dart"; | 10 import "path.dart"; |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 76 : '\r\n'; | 76 : '\r\n'; |
| 77 List<String> lines = contents.split(line_separator); | 77 List<String> lines = contents.split(line_separator); |
| 78 if (lines.last == '') lines.removeLast(); | 78 if (lines.last == '') lines.removeLast(); |
| 79 bytes = null; | 79 bytes = null; |
| 80 contents = null; | 80 contents = null; |
| 81 Set<String> validMultitestOutcomes = new Set<String>.from( | 81 Set<String> validMultitestOutcomes = new Set<String>.from( |
| 82 ['ok', 'compile-time error', 'runtime error', | 82 ['ok', 'compile-time error', 'runtime error', |
| 83 'static type warning', 'dynamic type error', | 83 'static type warning', 'dynamic type error', |
| 84 'checked mode compile-time error']); | 84 'checked mode compile-time error']); |
| 85 | 85 |
| 86 List<String> testTemplate = new List<String>(); | |
| 87 testTemplate.add( | |
| 88 '// Test created from multitest named ${filePath.toNativePath()}.'); | |
| 89 // Create the set of multitests, which will have a new test added each | 86 // Create the set of multitests, which will have a new test added each |
| 90 // time we see a multitest line with a new key. | 87 // time we see a multitest line with a new key. |
| 91 Map<String, List<String>> testsAsLines = new Map<String, List<String>>(); | 88 Map<String, List<String>> testsAsLines = new Map<String, List<String>>(); |
| 92 | 89 |
| 90 // Add the default case with key "none". |
| 91 testsAsLines['none'] = new List<String>(); |
| 92 outcomes['none'] = new Set<String>(); |
| 93 |
| 93 int lineCount = 0; | 94 int lineCount = 0; |
| 94 for (String line in lines) { | 95 for (String line in lines) { |
| 95 lineCount++; | 96 lineCount++; |
| 96 var annotation = new _Annotation.from(line); | 97 var annotation = new _Annotation.from(line); |
| 97 if (annotation != null) { | 98 if (annotation != null) { |
| 98 testsAsLines.putIfAbsent(annotation.key, | 99 testsAsLines.putIfAbsent(annotation.key, |
| 99 () => new List<String>.from(testTemplate)).add(line); | 100 () => new List<String>.from(testsAsLines["none"])); |
| 100 outcomes.putIfAbsent(annotation.key, | 101 // Add line to test with annotation.key as key, empty line to the rest. |
| 101 () => new Set<String>()); | 102 for (var key in testsAsLines.keys) { |
| 102 if (annotation.rest == 'continued') { | 103 testsAsLines[key].add(annotation.key == key ? line : ""); |
| 103 continue; | 104 } |
| 104 } else { | 105 outcomes.putIfAbsent(annotation.key, () => new Set<String>()); |
| 106 if (annotation.rest != 'continued') { |
| 105 for (String nextOutcome in annotation.outcomesList) { | 107 for (String nextOutcome in annotation.outcomesList) { |
| 106 if (validMultitestOutcomes.contains(nextOutcome)) { | 108 if (validMultitestOutcomes.contains(nextOutcome)) { |
| 107 outcomes[annotation.key].add(nextOutcome); | 109 outcomes[annotation.key].add(nextOutcome); |
| 108 } else { | 110 } else { |
| 109 DebugLogger.warning( | 111 DebugLogger.warning( |
| 110 "Warning: Invalid test directive '$nextOutcome' on line " | 112 "Warning: Invalid test directive '$nextOutcome' on line " |
| 111 "${lineCount}:\n${annotation.rest} "); | 113 "${lineCount}:\n${annotation.rest} "); |
| 112 } | 114 } |
| 113 } | 115 } |
| 114 } | 116 } |
| 115 } else { | 117 } else { |
| 116 testTemplate.add(line); | |
| 117 for (var test in testsAsLines.values) test.add(line); | 118 for (var test in testsAsLines.values) test.add(line); |
| 118 } | 119 } |
| 119 } | 120 } |
| 121 // End marker, has a final line separator so we don't need to add it after |
| 122 // joining the lines. |
| 123 var marker = |
| 124 '// Test created from multitest named ${filePath.toNativePath()}.' |
| 125 '$line_separator'; |
| 126 for (var test in testsAsLines.values) test.add(marker); |
| 120 | 127 |
| 121 var keysToDelete = []; | 128 var keysToDelete = []; |
| 122 // Check that every key (other than the none case) has at least one outcome | 129 // Check that every key (other than the none case) has at least one outcome |
| 123 for (var outcomeKey in outcomes.keys) { | 130 for (var outcomeKey in outcomes.keys) { |
| 124 if (outcomeKey != 'none' && outcomes[outcomeKey].isEmpty) { | 131 if (outcomeKey != 'none' && outcomes[outcomeKey].isEmpty) { |
| 125 DebugLogger.warning( | 132 DebugLogger.warning( |
| 126 "Warning: Test ${outcomeKey} has no valid annotated outcomes.\n" | 133 "Warning: Test ${outcomeKey} has no valid annotated outcomes.\n" |
| 127 "Expected one of: ${validMultitestOutcomes.toString()}"); | 134 "Expected one of: ${validMultitestOutcomes.toString()}"); |
| 128 // If this multitest doesn't have an outcome, mark the multitest for | 135 // If this multitest doesn't have an outcome, mark the multitest for |
| 129 // deletion. | 136 // deletion. |
| 130 keysToDelete.add(outcomeKey); | 137 keysToDelete.add(outcomeKey); |
| 131 } | 138 } |
| 132 } | 139 } |
| 133 // If a key/multitest was marked for deletion, do the necessary cleanup. | 140 // If a key/multitest was marked for deletion, do the necessary cleanup. |
| 134 keysToDelete.forEach((key) => outcomes.remove(key)); | 141 keysToDelete.forEach(outcomes.remove); |
| 135 keysToDelete.forEach((key) => testsAsLines.remove(key)); | 142 keysToDelete.forEach(testsAsLines.remove); |
| 136 | |
| 137 // Add the template, with no multitest lines, as a test with key 'none'. | |
| 138 testsAsLines['none'] = testTemplate; | |
| 139 outcomes['none'] = new Set<String>(); | |
| 140 | 143 |
| 141 // Copy all the tests into the output map tests, as multiline strings. | 144 // Copy all the tests into the output map tests, as multiline strings. |
| 142 for (String key in testsAsLines.keys) { | 145 for (String key in testsAsLines.keys) { |
| 143 tests[key] = testsAsLines[key].join(line_separator) + line_separator; | 146 tests[key] = testsAsLines[key].join(line_separator); |
| 144 } | 147 } |
| 145 } | 148 } |
| 146 | 149 |
| 147 // Represents a mutlitest annotation in the special /// comment. | 150 // Represents a mutlitest annotation in the special /// comment. |
| 148 class _Annotation { | 151 class _Annotation { |
| 149 String key; | 152 String key; |
| 150 String rest; | 153 String rest; |
| 151 List<String> outcomesList; | 154 List<String> outcomesList; |
| 152 _Annotation() {} | 155 _Annotation() {} |
| 153 factory _Annotation.from(String line) { | 156 factory _Annotation.from(String line) { |
| (...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 298 // TestSuite.forDirectory. | 301 // TestSuite.forDirectory. |
| 299 split.removeLast(); | 302 split.removeLast(); |
| 300 } | 303 } |
| 301 String path = '${generatedTestDir.path}/${split.last}'; | 304 String path = '${generatedTestDir.path}/${split.last}'; |
| 302 Directory dir = new Directory(path); | 305 Directory dir = new Directory(path); |
| 303 if (!dir.existsSync()) { | 306 if (!dir.existsSync()) { |
| 304 dir.createSync(); | 307 dir.createSync(); |
| 305 } | 308 } |
| 306 return new Path(new File(path).absolute.path); | 309 return new Path(new File(path).absolute.path); |
| 307 } | 310 } |
| OLD | NEW |