| OLD | NEW |
| 1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2016, 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.md file. | 3 // BSD-style license that can be found in the LICENSE.md file. |
| 4 | 4 |
| 5 library testing.multitest; | 5 library testing.multitest; |
| 6 | 6 |
| 7 import 'dart:async' show | 7 import 'dart:async' show Stream, StreamTransformer; |
| 8 Stream, | |
| 9 StreamTransformer; | |
| 10 | 8 |
| 11 import 'dart:io' show | 9 import 'dart:io' show Directory, File; |
| 12 Directory, | |
| 13 File; | |
| 14 | 10 |
| 15 import 'log.dart' show | 11 import 'log.dart' show splitLines; |
| 16 splitLines; | |
| 17 | 12 |
| 18 import 'test_description.dart' show | 13 import 'test_description.dart' show TestDescription; |
| 19 TestDescription; | |
| 20 | 14 |
| 21 bool isError(Set<String> expectations) { | 15 bool isError(Set<String> expectations) { |
| 22 if (expectations.contains("compile-time error")) return true; | 16 if (expectations.contains("compile-time error")) return true; |
| 23 if (expectations.contains("runtime error")) return true; | 17 if (expectations.contains("runtime error")) return true; |
| 24 return false; | 18 return false; |
| 25 } | 19 } |
| 26 | 20 |
| 27 bool isCheckedModeError(Set<String> expectations) { | 21 bool isCheckedModeError(Set<String> expectations) { |
| 28 if (expectations.contains("checked mode compile-time error")) return true; | 22 if (expectations.contains("checked mode compile-time error")) return true; |
| 29 if (expectations.contains("dynamic type error")) return true; | 23 if (expectations.contains("dynamic type error")) return true; |
| 30 return isError(expectations); | 24 return isError(expectations); |
| 31 } | 25 } |
| 32 | 26 |
| 33 class MultitestTransformer | 27 class MultitestTransformer |
| 34 implements StreamTransformer<TestDescription, TestDescription> { | 28 implements StreamTransformer<TestDescription, TestDescription> { |
| 35 static const String multitestMarker = "///"; | 29 static const String multitestMarker = "///"; |
| 36 | 30 |
| 37 static const List<String> validOutcomesList = const <String>[ | 31 static const List<String> validOutcomesList = const <String>[ |
| 38 "ok", | 32 "ok", |
| 39 "compile-time error", | 33 "compile-time error", |
| 40 "runtime error", | 34 "runtime error", |
| 41 "static type warning", | 35 "static type warning", |
| 42 "dynamic type error", | 36 "dynamic type error", |
| 43 "checked mode compile-time error", | 37 "checked mode compile-time error", |
| 44 ]; | 38 ]; |
| 45 | 39 |
| 46 static final Set<String> validOutcomes = | 40 static final Set<String> validOutcomes = |
| 47 new Set<String>.from(validOutcomesList); | 41 new Set<String>.from(validOutcomesList); |
| 48 | 42 |
| 49 Stream<TestDescription> bind(Stream<TestDescription> stream) async* { | 43 Stream<TestDescription> bind(Stream<TestDescription> stream) async* { |
| 50 List<String> errors = <String>[]; | 44 List<String> errors = <String>[]; |
| 51 reportError(String error) { | 45 reportError(String error) { |
| 52 errors.add(error); | 46 errors.add(error); |
| 53 print(error); | 47 print(error); |
| 54 } | 48 } |
| 55 nextTest: await for (TestDescription test in stream) { | 49 |
| 50 nextTest: |
| 51 await for (TestDescription test in stream) { |
| 56 String contents = await test.file.readAsString(); | 52 String contents = await test.file.readAsString(); |
| 57 if (!contents.contains(multitestMarker)) { | 53 if (!contents.contains(multitestMarker)) { |
| 58 yield test; | 54 yield test; |
| 59 continue nextTest; | 55 continue nextTest; |
| 60 } | 56 } |
| 61 // Note: this is modified in the loop below. | 57 // Note: this is modified in the loop below. |
| 62 List<String> linesWithoutAnnotations = <String>[]; | 58 List<String> linesWithoutAnnotations = <String>[]; |
| 63 Map<String, List<String>> testsAsLines = <String, List<String>>{ | 59 Map<String, List<String>> testsAsLines = <String, List<String>>{ |
| 64 "none": linesWithoutAnnotations, | 60 "none": linesWithoutAnnotations, |
| 65 }; | 61 }; |
| 66 Map<String, Set<String>> outcomes = <String, Set<String>>{ | 62 Map<String, Set<String>> outcomes = <String, Set<String>>{ |
| 67 "none": new Set<String>(), | 63 "none": new Set<String>(), |
| 68 }; | 64 }; |
| 69 int lineNumber = 0; | 65 int lineNumber = 0; |
| 70 for (String line in splitLines(contents)) { | 66 for (String line in splitLines(contents)) { |
| 71 lineNumber++; | 67 lineNumber++; |
| 72 int index = line.indexOf(multitestMarker); | 68 int index = line.indexOf(multitestMarker); |
| 73 String subtestName; | 69 String subtestName; |
| 74 List<String> subtestOutcomesList; | 70 List<String> subtestOutcomesList; |
| 75 if (index != -1) { | 71 if (index != -1) { |
| 76 String annotationText = | 72 String annotationText = |
| 77 line.substring(index + multitestMarker.length).trim(); | 73 line.substring(index + multitestMarker.length).trim(); |
| 78 index = annotationText.indexOf(":"); | 74 index = annotationText.indexOf(":"); |
| 79 if (index != -1) { | 75 if (index != -1) { |
| 80 subtestName = annotationText.substring(0, index).trim(); | 76 subtestName = annotationText.substring(0, index).trim(); |
| 81 subtestOutcomesList = annotationText.substring(index + 1).split(",") | 77 subtestOutcomesList = annotationText |
| 82 .map((s) => s.trim()).toList(); | 78 .substring(index + 1) |
| 79 .split(",") |
| 80 .map((s) => s.trim()) |
| 81 .toList(); |
| 83 if (subtestName == "none") { | 82 if (subtestName == "none") { |
| 84 reportError(test.formatError( | 83 reportError(test.formatError( |
| 85 "$lineNumber: $subtestName can't be used as test name.")); | 84 "$lineNumber: $subtestName can't be used as test name.")); |
| 86 continue nextTest; | 85 continue nextTest; |
| 87 } | 86 } |
| 88 if (subtestOutcomesList.isEmpty) { | 87 if (subtestOutcomesList.isEmpty) { |
| 89 reportError(test.formatError( | 88 reportError(test |
| 90 "$lineNumber: Expected <testname>:<outcomes>")); | 89 .formatError("$lineNumber: Expected <testname>:<outcomes>")); |
| 91 continue nextTest; | 90 continue nextTest; |
| 92 } | 91 } |
| 93 } | 92 } |
| 94 } | 93 } |
| 95 if (subtestName != null) { | 94 if (subtestName != null) { |
| 96 List<String> lines = testsAsLines.putIfAbsent(subtestName, | 95 List<String> lines = testsAsLines.putIfAbsent(subtestName, |
| 97 () => new List<String>.from(linesWithoutAnnotations)); | 96 () => new List<String>.from(linesWithoutAnnotations)); |
| 98 lines.add(line); | 97 lines.add(line); |
| 99 Set<String> subtestOutcomes = outcomes.putIfAbsent(subtestName, | 98 Set<String> subtestOutcomes = |
| 100 () => new Set<String>()); | 99 outcomes.putIfAbsent(subtestName, () => new Set<String>()); |
| 101 if (subtestOutcomesList.length != 1 || | 100 if (subtestOutcomesList.length != 1 || |
| 102 subtestOutcomesList.single != "continued") { | 101 subtestOutcomesList.single != "continued") { |
| 103 for (String outcome in subtestOutcomesList) { | 102 for (String outcome in subtestOutcomesList) { |
| 104 if (validOutcomes.contains(outcome)) { | 103 if (validOutcomes.contains(outcome)) { |
| 105 subtestOutcomes.add(outcome); | 104 subtestOutcomes.add(outcome); |
| 106 } else { | 105 } else { |
| 107 reportError(test.formatError( | 106 reportError(test.formatError( |
| 108 "$lineNumber: '$outcome' isn't a recognized outcome.")); | 107 "$lineNumber: '$outcome' isn't a recognized outcome.")); |
| 109 continue nextTest; | 108 continue nextTest; |
| 110 } | 109 } |
| 111 } | 110 } |
| 112 } | 111 } |
| 113 } else { | 112 } else { |
| 114 for (List<String> lines in testsAsLines.values) { | 113 for (List<String> lines in testsAsLines.values) { |
| 115 // This will also modify [linesWithoutAnnotations]. | 114 // This will also modify [linesWithoutAnnotations]. |
| 116 lines.add(line); | 115 lines.add(line); |
| 117 } | 116 } |
| 118 } | 117 } |
| 119 } | 118 } |
| 120 Uri root = Uri.base.resolve("generated/"); | 119 Uri root = Uri.base.resolve("generated/"); |
| 121 Directory generated = new Directory.fromUri(root.resolve(test.shortName)); | 120 Directory generated = new Directory.fromUri(root.resolve(test.shortName)); |
| 122 generated = await generated.create(recursive: true); | 121 generated = await generated.create(recursive: true); |
| 123 for (String name in testsAsLines.keys) { | 122 for (String name in testsAsLines.keys) { |
| 124 List<String> lines = testsAsLines[name]; | 123 List<String> lines = testsAsLines[name]; |
| 125 Uri uri = generated.uri.resolve("${name}_generated.dart"); | 124 Uri uri = generated.uri.resolve("${name}_generated.dart"); |
| 126 TestDescription subtest = | 125 TestDescription subtest = |
| 127 new TestDescription(root, new File.fromUri(uri)); | 126 new TestDescription(root, new File.fromUri(uri)); |
| 128 subtest.multitestExpectations = outcomes[name]; | 127 subtest.multitestExpectations = outcomes[name]; |
| 129 await subtest.file.writeAsString(lines.join("")); | 128 await subtest.file.writeAsString(lines.join("")); |
| 130 yield subtest; | 129 yield subtest; |
| 131 } | 130 } |
| 132 } | 131 } |
| 133 if (errors.isNotEmpty) { | 132 if (errors.isNotEmpty) { |
| 134 throw "Error: ${errors.join("\n")}"; | 133 throw "Error: ${errors.join("\n")}"; |
| 135 } | 134 } |
| 136 } | 135 } |
| 137 } | 136 } |
| OLD | NEW |