| 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 196 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 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 /** | 216 /** |
| 217 * If set, then only tests whose descriptions match this regexp pattern | |
| 218 * will be run. | |
| 219 */ | |
| 220 String _filter = null; | |
| 221 | |
| 222 /** | |
| 223 * (Deprecated) Evaluates the [function] and validates that it throws an | 217 * (Deprecated) Evaluates the [function] and validates that it throws an |
| 224 * exception. If [callback] is provided, then it will be invoked with the | 218 * exception. If [callback] is provided, then it will be invoked with the |
| 225 * thrown exception. The callback may do any validation it wants. In addition, | 219 * thrown exception. The callback may do any validation it wants. In addition, |
| 226 * if it returns `false`, that also indicates an expectation failure. | 220 * if it returns `false`, that also indicates an expectation failure. |
| 227 */ | 221 */ |
| 228 void expectThrow(function, [bool callback(exception)]) { | 222 void expectThrow(function, [bool callback(exception)]) { |
| 229 bool threw = false; | 223 bool threw = false; |
| 230 try { | 224 try { |
| 231 function(); | 225 function(); |
| 232 } catch (var e) { | 226 } catch (var e) { |
| 233 threw = true; | 227 threw = true; |
| 234 | 228 |
| 235 // Also let the callback look at it. | 229 // Also let the callback look at it. |
| 236 if (callback != null) { | 230 if (callback != null) { |
| 237 var result = callback(e); | 231 var result = callback(e); |
| 238 | 232 |
| 239 // If the callback explicitly returned false, treat that like an | 233 // If the callback explicitly returned false, treat that like an |
| 240 // expectation too. (If it returns null, though, don't.) | 234 // expectation too. (If it returns null, though, don't.) |
| 241 if (result == false) { | 235 if (result == false) { |
| 242 _fail('Exception:\n$e\ndid not match expectation.'); | 236 _fail('Exception:\n$e\ndid not match expectation.'); |
| 243 } | 237 } |
| 244 } | 238 } |
| 245 } | 239 } |
| 246 | 240 |
| 247 if (threw != true) _fail('An expected exception was not thrown.'); | 241 if (threw != true) _fail('An expected exception was not thrown.'); |
| 248 } | 242 } |
| 249 | 243 |
| 250 /** | 244 /** |
| 251 * Sets the regexp pattern filter which constrains which tests to run | |
| 252 * based on their descriptions. | |
| 253 */ | |
| 254 void setFilter(String filter) { | |
| 255 if (filter == '') filter = null; | |
| 256 _filter = filter; | |
| 257 } | |
| 258 | |
| 259 /** | |
| 260 * Gets the regexp pattern filter which constrains which tests to run | |
| 261 * based on their descriptions. | |
| 262 */ | |
| 263 String getFilter() { | |
| 264 return _filter; | |
| 265 } | |
| 266 /** | |
| 267 * Creates a new test case with the given description and body. The | 245 * Creates a new test case with the given description and body. The |
| 268 * description will include the descriptions of any surrounding group() | 246 * description will include the descriptions of any surrounding group() |
| 269 * calls. | 247 * calls. |
| 270 */ | 248 */ |
| 271 void test(String spec, TestFunction body) { | 249 void test(String spec, TestFunction body) { |
| 272 ensureInitialized(); | 250 ensureInitialized(); |
| 273 | 251 |
| 274 _tests.add(new TestCase(_tests.length + 1, _fullSpec(spec), body, 0)); | 252 _tests.add(new TestCase(_tests.length + 1, _fullSpec(spec), body, 0)); |
| 275 } | 253 } |
| 276 | 254 |
| (...skipping 373 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 650 // message at the end of the event loop. | 628 // message at the end of the event loop. |
| 651 // TODO(sigmund): expose this functionality somewhere in our libraries. | 629 // TODO(sigmund): expose this functionality somewhere in our libraries. |
| 652 final port = new ReceivePort(); | 630 final port = new ReceivePort(); |
| 653 port.receive((msg, reply) { | 631 port.receive((msg, reply) { |
| 654 callback(); | 632 callback(); |
| 655 port.close(); | 633 port.close(); |
| 656 }); | 634 }); |
| 657 port.toSendPort().send(null, null); | 635 port.toSendPort().send(null, null); |
| 658 } | 636 } |
| 659 | 637 |
| 660 /** Check arguments for a --filter spec. */ | |
| 661 _getFilterFromArgs() { | |
| 662 var options = new Options().arguments; | |
| 663 if (options != null) { | |
| 664 for (var i = 0; i < options.length; i++) { | |
| 665 if (options[i].startsWith('--filter=')) { | |
| 666 var filter = options[i].substring(9); | |
| 667 if (filter.length > 0) { | |
| 668 // Strip quotes if present. | |
| 669 var last = filter.length - 1; | |
| 670 if ((filter[0] == "'" && filter[last] == "'") || | |
| 671 (filter[0] == '"' && filter[last] == '"')) { | |
| 672 filter = filter.substring(1, last); | |
| 673 } | |
| 674 if (filter.length > 0) { | |
| 675 setFilter(filter); | |
| 676 } | |
| 677 } | |
| 678 break; | |
| 679 } | |
| 680 } | |
| 681 } | |
| 682 } | |
| 683 | |
| 684 /** Runs all queued tests, one at a time. */ | 638 /** Runs all queued tests, one at a time. */ |
| 685 _runTests() { | 639 _runTests() { |
| 686 // If we are soloing a test, remove all the others. | 640 // If we are soloing a test, remove all the others. |
| 687 if (_soloTest != null) { | 641 if (_soloTest != null) { |
| 688 _tests = _tests.filter((t) => t == _soloTest); | 642 _tests = _tests.filter((t) => t == _soloTest); |
| 689 } | 643 } |
| 690 if (_filter == null) { | |
| 691 _getFilterFromArgs(); | |
| 692 } | |
| 693 if (_filter != null) { | |
| 694 RegExp re = new RegExp(_filter); | |
| 695 _tests = _tests.filter((t) => re.hasMatch(t.description)); | |
| 696 } | |
| 697 | 644 |
| 698 _config.onStart(); | 645 _config.onStart(); |
| 699 | 646 |
| 700 _defer(() { | 647 _defer(() { |
| 701 assert (_currentTest == 0); | 648 assert (_currentTest == 0); |
| 702 _testRunner(); | 649 _testRunner(); |
| 703 }); | 650 }); |
| 704 } | 651 } |
| 705 | 652 |
| 706 /** | 653 /** |
| (...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 809 _config.onInit(); | 756 _config.onInit(); |
| 810 | 757 |
| 811 // Immediately queue the suite up. It will run after a timeout (i.e. after | 758 // Immediately queue the suite up. It will run after a timeout (i.e. after |
| 812 // main() has returned). | 759 // main() has returned). |
| 813 _defer(_runTests); | 760 _defer(_runTests); |
| 814 } | 761 } |
| 815 | 762 |
| 816 /** Signature for a test function. */ | 763 /** Signature for a test function. */ |
| 817 typedef void TestFunction(); | 764 typedef void TestFunction(); |
| 818 | 765 |
| OLD | NEW |