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