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