OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2015, 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 file. |
| 4 |
| 5 import 'dart:io'; |
| 6 |
| 7 import 'package:path/path.dart' as p; |
| 8 import 'package:unittest/src/io.dart'; |
| 9 import 'package:unittest/unittest.dart'; |
| 10 |
| 11 import 'io.dart'; |
| 12 |
| 13 String _sandbox; |
| 14 |
| 15 void main() { |
| 16 test("reports when no tests are run", () { |
| 17 return withTempDir((path) { |
| 18 new File(p.join(path, "test.dart")).writeAsStringSync("void main() {}"); |
| 19 var result = runUnittest(["test.dart"], workingDirectory: path); |
| 20 expect(result.stdout, equals("No tests ran.\n")); |
| 21 }); |
| 22 }); |
| 23 |
| 24 test("runs several successful tests and reports when each completes", () { |
| 25 _expectReport(""" |
| 26 declarer.test('success 1', () {}); |
| 27 declarer.test('success 2', () {}); |
| 28 declarer.test('success 3', () {});""", |
| 29 """ |
| 30 +0: success 1 |
| 31 +1: success 1 |
| 32 +1: success 2 |
| 33 +2: success 2 |
| 34 +2: success 3 |
| 35 +3: success 3 |
| 36 +3: All tests passed!"""); |
| 37 }); |
| 38 |
| 39 test("runs several failing tests and reports when each fails", () { |
| 40 _expectReport(""" |
| 41 declarer.test('failure 1', () => throw new TestFailure('oh no')); |
| 42 declarer.test('failure 2', () => throw new TestFailure('oh no')); |
| 43 declarer.test('failure 3', () => throw new TestFailure('oh no'));""", |
| 44 """ |
| 45 +0: failure 1 |
| 46 +0 -1: failure 1 |
| 47 oh no |
| 48 test.dart 7:42 main.<fn> |
| 49 dart:isolate _RawReceivePortImpl._handleMessage |
| 50 |
| 51 |
| 52 +0 -1: failure 2 |
| 53 +0 -2: failure 2 |
| 54 oh no |
| 55 test.dart 8:42 main.<fn> |
| 56 dart:isolate _RawReceivePortImpl._handleMessage |
| 57 |
| 58 |
| 59 +0 -2: failure 3 |
| 60 +0 -3: failure 3 |
| 61 oh no |
| 62 test.dart 9:42 main.<fn> |
| 63 dart:isolate _RawReceivePortImpl._handleMessage |
| 64 |
| 65 |
| 66 +0 -3: Some tests failed."""); |
| 67 }); |
| 68 |
| 69 test("runs failing tests along with successful tests", () { |
| 70 _expectReport(""" |
| 71 declarer.test('failure 1', () => throw new TestFailure('oh no')); |
| 72 declarer.test('success 1', () {}); |
| 73 declarer.test('failure 2', () => throw new TestFailure('oh no')); |
| 74 declarer.test('success 2', () {});""", |
| 75 """ |
| 76 +0: failure 1 |
| 77 +0 -1: failure 1 |
| 78 oh no |
| 79 test.dart 7:42 main.<fn> |
| 80 dart:isolate _RawReceivePortImpl._handleMessage |
| 81 |
| 82 |
| 83 +0 -1: success 1 |
| 84 +1 -1: success 1 |
| 85 +1 -1: failure 2 |
| 86 +1 -2: failure 2 |
| 87 oh no |
| 88 test.dart 9:42 main.<fn> |
| 89 dart:isolate _RawReceivePortImpl._handleMessage |
| 90 |
| 91 |
| 92 +1 -2: success 2 |
| 93 +2 -2: success 2 |
| 94 +2 -2: Some tests failed."""); |
| 95 }); |
| 96 |
| 97 test("gracefully handles multiple test failures in a row", () { |
| 98 _expectReport(""" |
| 99 // This completer ensures that the test isolate isn't killed until all |
| 100 // errors have been thrown. |
| 101 var completer = new Completer(); |
| 102 declarer.test('failures', () { |
| 103 new Future.microtask(() => throw 'first error'); |
| 104 new Future.microtask(() => throw 'second error'); |
| 105 new Future.microtask(() => throw 'third error'); |
| 106 new Future.microtask(completer.complete); |
| 107 }); |
| 108 declarer.test('wait', () => completer.future);""", |
| 109 """ |
| 110 +0: failures |
| 111 +0 -1: failures |
| 112 first error |
| 113 test.dart 11:38 main.<fn>.<fn> |
| 114 dart:isolate _RawReceivePortImpl._handleMessage |
| 115 ===== asynchronous gap =========================== |
| 116 dart:async Future.Future.microtask |
| 117 test.dart 11:15 main.<fn> |
| 118 dart:isolate _RawReceivePortImpl._handleMessage |
| 119 |
| 120 |
| 121 second error |
| 122 test.dart 12:38 main.<fn>.<fn> |
| 123 dart:isolate _RawReceivePortImpl._handleMessage |
| 124 ===== asynchronous gap =========================== |
| 125 dart:async Future.Future.microtask |
| 126 test.dart 12:15 main.<fn> |
| 127 dart:isolate _RawReceivePortImpl._handleMessage |
| 128 |
| 129 |
| 130 third error |
| 131 test.dart 13:38 main.<fn>.<fn> |
| 132 dart:isolate _RawReceivePortImpl._handleMessage |
| 133 ===== asynchronous gap =========================== |
| 134 dart:async Future.Future.microtask |
| 135 test.dart 13:15 main.<fn> |
| 136 dart:isolate _RawReceivePortImpl._handleMessage |
| 137 |
| 138 |
| 139 +0 -1: wait |
| 140 +1 -1: wait |
| 141 +1 -1: Some tests failed."""); |
| 142 }); |
| 143 } |
| 144 |
| 145 final _prefixLength = "XX:XX ".length; |
| 146 |
| 147 void _expectReport(String tests, String expected) { |
| 148 var dart = """ |
| 149 import 'dart:async'; |
| 150 |
| 151 import 'package:unittest/unittest.dart'; |
| 152 |
| 153 void main() { |
| 154 var declarer = Zone.current[#unittest.declarer]; |
| 155 $tests |
| 156 } |
| 157 """; |
| 158 |
| 159 expect(withTempDir((path) { |
| 160 new File(p.join(path, "test.dart")).writeAsStringSync(dart); |
| 161 var result = runUnittest(["test.dart"], workingDirectory: path); |
| 162 |
| 163 // Convert CRs into newlines, remove excess trailing whitespace, and trim |
| 164 // off timestamps. |
| 165 var actual = result.stdout.trim().split(new RegExp(r"[\r\n]")).map((line) { |
| 166 if (line.startsWith(" ") || line.isEmpty) return line.trimRight(); |
| 167 return line.trim().substring(_prefixLength); |
| 168 }).join("\n"); |
| 169 |
| 170 // Un-indent the expected string. |
| 171 var indentation = expected.indexOf(new RegExp("[^ ]")); |
| 172 expected = expected.split("\n").map((line) { |
| 173 if (line.isEmpty) return line; |
| 174 return line.substring(indentation); |
| 175 }).join("\n"); |
| 176 |
| 177 expect(actual, equals(expected)); |
| 178 }), completes); |
| 179 } |
OLD | NEW |