| OLD | NEW |
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, 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 /// A test library for testing test libraries? We must go deeper. | 5 /// A test library for testing test libraries? We must go deeper. |
| 6 /// | 6 /// |
| 7 /// Since unit testing code tends to use a lot of global state, it can be tough | 7 /// Since unit testing code tends to use a lot of global state, it can be tough |
| 8 /// to test. This library manages it by running each test case in a child | 8 /// to test. This library manages it by running each test case in a child |
| 9 /// isolate, then reporting the results back to the parent isolate. | 9 /// isolate, then reporting the results back to the parent isolate. |
| 10 library metatest; | 10 library metatest; |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 45 '{${tests.map((t) => '"$t"').join(', ')}}'; | 45 '{${tests.map((t) => '"$t"').join(', ')}}'; |
| 46 | 46 |
| 47 fail('Expected exactly ${stringify(shouldPass)} to pass, but ' | 47 fail('Expected exactly ${stringify(shouldPass)} to pass, but ' |
| 48 '${stringify(didPass)} passed.\n' | 48 '${stringify(didPass)} passed.\n' |
| 49 '${_summarizeTests(results)}'); | 49 '${_summarizeTests(results)}'); |
| 50 } | 50 } |
| 51 } | 51 } |
| 52 }); | 52 }); |
| 53 } | 53 } |
| 54 | 54 |
| 55 /// Declares a test with the given [description] and [body]. |
| 56 /// |
| 57 /// [body] corresponds |
| 58 /// to the `main` method of a test file, and will be run in an isolate. |
| 59 /// |
| 60 /// All tests must have an expected result in [expectedResults]. |
| 61 void expectTests(String description, void body(), |
| 62 Map<String, String> expectedResults) { |
| 63 _setUpTest(description, body, (results) { |
| 64 expectedResults = new Map.from(expectedResults); |
| 65 |
| 66 for (var testResult in results['results']) { |
| 67 var description = testResult['description']; |
| 68 |
| 69 expect(expectedResults, contains(description), |
| 70 reason: '"$description" did not have an expected result set.\n' |
| 71 '${_summarizeTests(results)}'); |
| 72 |
| 73 var result = testResult['result']; |
| 74 |
| 75 expect(expectedResults, containsPair(description, result), |
| 76 reason: 'The test "$description" not not have the expected result.\n' |
| 77 '${_summarizeTests(results)}'); |
| 78 |
| 79 expectedResults.remove(description); |
| 80 } |
| 81 expect(expectedResults, isEmpty, |
| 82 reason: 'Unexpected additional test results\n' |
| 83 '${_summarizeTests(results)}'); |
| 84 }); |
| 85 } |
| 86 |
| 55 /// Declares a test with the given [description] and [body]. [body] corresponds | 87 /// Declares a test with the given [description] and [body]. [body] corresponds |
| 56 /// to the `main` method of a test file, and will be run in an isolate. Expects | 88 /// to the `main` method of a test file, and will be run in an isolate. Expects |
| 57 /// all tests defined by [body] to fail. | 89 /// all tests defined by [body] to fail. |
| 58 void expectTestsFail(String description, void body()) { | 90 void expectTestsFail(String description, void body()) { |
| 59 _setUpTest(description, body, (results) { | 91 _setUpTest(description, body, (results) { |
| 60 if (_hasError(results)) { | 92 if (_hasError(results)) { |
| 61 throw 'Expected all tests to fail, but got error(s):\n' | 93 throw 'Expected all tests to fail, but got error(s):\n' |
| 62 '${_summarizeTests(results)}'; | 94 '${_summarizeTests(results)}'; |
| 63 } else if (results['passed'] != 0) { | 95 } else if (results['passed'] != 0) { |
| 64 throw 'Expected all tests to fail, but some passed:\n' | 96 throw 'Expected all tests to fail, but some passed:\n' |
| (...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 202 "uncaughtError": uncaughtError, | 234 "uncaughtError": uncaughtError, |
| 203 "results": results.map((testCase) => { | 235 "results": results.map((testCase) => { |
| 204 "description": testCase.description, | 236 "description": testCase.description, |
| 205 "message": testCase.message, | 237 "message": testCase.message, |
| 206 "result": testCase.result, | 238 "result": testCase.result, |
| 207 "stackTrace": testCase.stackTrace.toString() | 239 "stackTrace": testCase.stackTrace.toString() |
| 208 }).toList() | 240 }).toList() |
| 209 }); | 241 }); |
| 210 } | 242 } |
| 211 } | 243 } |
| OLD | NEW |