Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2011, 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 /** | 5 /** |
| 6 * A library for writing dart unit tests. | 6 * A library for writing dart unit tests. |
| 7 * | 7 * |
| 8 * To import this library, specify the relative path to | 8 * To import this library, specify the relative path to |
| 9 * lib/unittest/unittest.dart. | 9 * lib/unittest/unittest.dart. |
| 10 * | 10 * |
| (...skipping 195 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 206 int _state = _UNINITIALIZED; | 206 int _state = _UNINITIALIZED; |
| 207 String _uncaughtErrorMessage = null; | 207 String _uncaughtErrorMessage = null; |
| 208 | 208 |
| 209 final _PASS = 'pass'; | 209 final _PASS = 'pass'; |
| 210 final _FAIL = 'fail'; | 210 final _FAIL = 'fail'; |
| 211 final _ERROR = 'error'; | 211 final _ERROR = 'error'; |
| 212 | 212 |
| 213 /** If set, then all other test cases will be ignored. */ | 213 /** If set, then all other test cases will be ignored. */ |
| 214 TestCase _soloTest; | 214 TestCase _soloTest; |
| 215 | 215 |
| 216 /** If set, then only tests whose names match this pattern will be run. */ | |
|
eub
2012/06/21 20:58:45
s/pattern/regexp/ to be clear.
eub
2012/06/21 20:58:45
Also, s/name/description/ globally unless that's a
gram
2012/06/21 21:18:03
Done.
| |
| 217 String _filter = null; | |
| 218 | |
| 216 /** | 219 /** |
| 217 * (Deprecated) Evaluates the [function] and validates that it throws an | 220 * (Deprecated) Evaluates the [function] and validates that it throws an |
| 218 * exception. If [callback] is provided, then it will be invoked with the | 221 * exception. If [callback] is provided, then it will be invoked with the |
| 219 * thrown exception. The callback may do any validation it wants. In addition, | 222 * thrown exception. The callback may do any validation it wants. In addition, |
| 220 * if it returns `false`, that also indicates an expectation failure. | 223 * if it returns `false`, that also indicates an expectation failure. |
| 221 */ | 224 */ |
| 222 void expectThrow(function, [bool callback(exception)]) { | 225 void expectThrow(function, [bool callback(exception)]) { |
| 223 bool threw = false; | 226 bool threw = false; |
| 224 try { | 227 try { |
| 225 function(); | 228 function(); |
| 226 } catch (var e) { | 229 } catch (var e) { |
| 227 threw = true; | 230 threw = true; |
| 228 | 231 |
| 229 // Also let the callback look at it. | 232 // Also let the callback look at it. |
| 230 if (callback != null) { | 233 if (callback != null) { |
| 231 var result = callback(e); | 234 var result = callback(e); |
| 232 | 235 |
| 233 // If the callback explicitly returned false, treat that like an | 236 // If the callback explicitly returned false, treat that like an |
| 234 // expectation too. (If it returns null, though, don't.) | 237 // expectation too. (If it returns null, though, don't.) |
| 235 if (result == false) { | 238 if (result == false) { |
| 236 _fail('Exception:\n$e\ndid not match expectation.'); | 239 _fail('Exception:\n$e\ndid not match expectation.'); |
| 237 } | 240 } |
| 238 } | 241 } |
| 239 } | 242 } |
| 240 | 243 |
| 241 if (threw != true) _fail('An expected exception was not thrown.'); | 244 if (threw != true) _fail('An expected exception was not thrown.'); |
| 242 } | 245 } |
| 243 | 246 |
| 247 /** Sets the filter which constrains which tests to run based on their names. */ | |
| 248 void setFilter(String filter) { | |
| 249 if (filter == '') filter = null; | |
| 250 _filter = filter; | |
| 251 } | |
| 252 | |
| 253 /** Gets the filter which constrains which tests to run based on their names. */ | |
| 254 String getFilter() { | |
| 255 return _filter; | |
| 256 } | |
| 244 /** | 257 /** |
| 245 * Creates a new test case with the given description and body. The | 258 * Creates a new test case with the given description and body. The |
| 246 * description will include the descriptions of any surrounding group() | 259 * description will include the descriptions of any surrounding group() |
| 247 * calls. | 260 * calls. |
| 248 */ | 261 */ |
| 249 void test(String spec, TestFunction body) { | 262 void test(String spec, TestFunction body) { |
| 250 ensureInitialized(); | 263 ensureInitialized(); |
| 251 | 264 |
| 252 _tests.add(new TestCase(_tests.length + 1, _fullSpec(spec), body, 0)); | 265 _tests.add(new TestCase(_tests.length + 1, _fullSpec(spec), body, 0)); |
| 253 } | 266 } |
| (...skipping 380 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 634 }); | 647 }); |
| 635 port.toSendPort().send(null, null); | 648 port.toSendPort().send(null, null); |
| 636 } | 649 } |
| 637 | 650 |
| 638 /** Runs all queued tests, one at a time. */ | 651 /** Runs all queued tests, one at a time. */ |
| 639 _runTests() { | 652 _runTests() { |
| 640 // If we are soloing a test, remove all the others. | 653 // If we are soloing a test, remove all the others. |
| 641 if (_soloTest != null) { | 654 if (_soloTest != null) { |
| 642 _tests = _tests.filter((t) => t == _soloTest); | 655 _tests = _tests.filter((t) => t == _soloTest); |
| 643 } | 656 } |
| 657 if (_filter == null) { // check arguments for a --filter spec. | |
| 658 var options = new Options().arguments; | |
| 659 if (options != null) { | |
| 660 for (var i = 0; i < options.length; i++) { | |
| 661 if (options[i].startsWith('--filter=')) { | |
|
eub
2012/06/21 20:58:45
Another one for the Great Flag Parsing Unification
| |
| 662 var filter = options[i].substring(10); | |
|
eub
2012/06/21 20:58:45
This chops off the first character, or did I misco
gram
2012/06/21 21:18:03
Correct, I was assuming this is quoted. But you're
| |
| 663 filter = filter.substring(0, filter.length-1); | |
|
eub
2012/06/21 20:58:45
And this chops off the last character, so we're co
| |
| 664 setFilter(filter); | |
| 665 break; | |
| 666 } | |
| 667 } | |
| 668 } | |
| 669 } | |
| 670 if (_filter != null) { | |
| 671 RegExp re = new RegExp(_filter); | |
| 672 _tests = _tests.filter((t) => re.hasMatch(t.description)); | |
| 673 } | |
| 644 | 674 |
| 645 _config.onStart(); | 675 _config.onStart(); |
| 646 | 676 |
| 647 _defer(() { | 677 _defer(() { |
| 648 assert (_currentTest == 0); | 678 assert (_currentTest == 0); |
| 649 _testRunner(); | 679 _testRunner(); |
| 650 }); | 680 }); |
| 651 } | 681 } |
| 652 | 682 |
| 653 /** | 683 /** |
| (...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 756 _config.onInit(); | 786 _config.onInit(); |
| 757 | 787 |
| 758 // Immediately queue the suite up. It will run after a timeout (i.e. after | 788 // Immediately queue the suite up. It will run after a timeout (i.e. after |
| 759 // main() has returned). | 789 // main() has returned). |
| 760 _defer(_runTests); | 790 _defer(_runTests); |
| 761 } | 791 } |
| 762 | 792 |
| 763 /** Signature for a test function. */ | 793 /** Signature for a test function. */ |
| 764 typedef void TestFunction(); | 794 typedef void TestFunction(); |
| 765 | 795 |
| OLD | NEW |