| 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 /** |
| 217 * (Deprecated) Evaluates the [function] and validates that it throws an | 223 * (Deprecated) Evaluates the [function] and validates that it throws an |
| 218 * exception. If [callback] is provided, then it will be invoked with the | 224 * 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, | 225 * thrown exception. The callback may do any validation it wants. In addition, |
| 220 * if it returns `false`, that also indicates an expectation failure. | 226 * if it returns `false`, that also indicates an expectation failure. |
| 221 */ | 227 */ |
| 222 void expectThrow(function, [bool callback(exception)]) { | 228 void expectThrow(function, [bool callback(exception)]) { |
| 223 bool threw = false; | 229 bool threw = false; |
| 224 try { | 230 try { |
| 225 function(); | 231 function(); |
| 226 } catch (var e) { | 232 } catch (var e) { |
| 227 threw = true; | 233 threw = true; |
| 228 | 234 |
| 229 // Also let the callback look at it. | 235 // Also let the callback look at it. |
| 230 if (callback != null) { | 236 if (callback != null) { |
| 231 var result = callback(e); | 237 var result = callback(e); |
| 232 | 238 |
| 233 // If the callback explicitly returned false, treat that like an | 239 // If the callback explicitly returned false, treat that like an |
| 234 // expectation too. (If it returns null, though, don't.) | 240 // expectation too. (If it returns null, though, don't.) |
| 235 if (result == false) { | 241 if (result == false) { |
| 236 _fail('Exception:\n$e\ndid not match expectation.'); | 242 _fail('Exception:\n$e\ndid not match expectation.'); |
| 237 } | 243 } |
| 238 } | 244 } |
| 239 } | 245 } |
| 240 | 246 |
| 241 if (threw != true) _fail('An expected exception was not thrown.'); | 247 if (threw != true) _fail('An expected exception was not thrown.'); |
| 242 } | 248 } |
| 243 | 249 |
| 244 /** | 250 /** |
| 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 /** |
| 245 * Creates a new test case with the given description and body. The | 267 * Creates a new test case with the given description and body. The |
| 246 * description will include the descriptions of any surrounding group() | 268 * description will include the descriptions of any surrounding group() |
| 247 * calls. | 269 * calls. |
| 248 */ | 270 */ |
| 249 void test(String spec, TestFunction body) { | 271 void test(String spec, TestFunction body) { |
| 250 ensureInitialized(); | 272 ensureInitialized(); |
| 251 | 273 |
| 252 _tests.add(new TestCase(_tests.length + 1, _fullSpec(spec), body, 0)); | 274 _tests.add(new TestCase(_tests.length + 1, _fullSpec(spec), body, 0)); |
| 253 } | 275 } |
| 254 | 276 |
| (...skipping 373 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 628 // message at the end of the event loop. | 650 // message at the end of the event loop. |
| 629 // TODO(sigmund): expose this functionality somewhere in our libraries. | 651 // TODO(sigmund): expose this functionality somewhere in our libraries. |
| 630 final port = new ReceivePort(); | 652 final port = new ReceivePort(); |
| 631 port.receive((msg, reply) { | 653 port.receive((msg, reply) { |
| 632 callback(); | 654 callback(); |
| 633 port.close(); | 655 port.close(); |
| 634 }); | 656 }); |
| 635 port.toSendPort().send(null, null); | 657 port.toSendPort().send(null, null); |
| 636 } | 658 } |
| 637 | 659 |
| 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 |
| 638 /** Runs all queued tests, one at a time. */ | 684 /** Runs all queued tests, one at a time. */ |
| 639 _runTests() { | 685 _runTests() { |
| 640 // If we are soloing a test, remove all the others. | 686 // If we are soloing a test, remove all the others. |
| 641 if (_soloTest != null) { | 687 if (_soloTest != null) { |
| 642 _tests = _tests.filter((t) => t == _soloTest); | 688 _tests = _tests.filter((t) => t == _soloTest); |
| 643 } | 689 } |
| 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 } |
| 644 | 697 |
| 645 _config.onStart(); | 698 _config.onStart(); |
| 646 | 699 |
| 647 _defer(() { | 700 _defer(() { |
| 648 assert (_currentTest == 0); | 701 assert (_currentTest == 0); |
| 649 _testRunner(); | 702 _testRunner(); |
| 650 }); | 703 }); |
| 651 } | 704 } |
| 652 | 705 |
| 653 /** | 706 /** |
| (...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 756 _config.onInit(); | 809 _config.onInit(); |
| 757 | 810 |
| 758 // Immediately queue the suite up. It will run after a timeout (i.e. after | 811 // Immediately queue the suite up. It will run after a timeout (i.e. after |
| 759 // main() has returned). | 812 // main() has returned). |
| 760 _defer(_runTests); | 813 _defer(_runTests); |
| 761 } | 814 } |
| 762 | 815 |
| 763 /** Signature for a test function. */ | 816 /** Signature for a test function. */ |
| 764 typedef void TestFunction(); | 817 typedef void TestFunction(); |
| 765 | 818 |
| OLD | NEW |