Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(411)

Side by Side Diff: lib/unittest/unittest.dart

Issue 10662022: Unit test filtering. (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 8 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « lib/unittest/config.dart ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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) {
Siggi Cherem (dart-lang) 2012/06/25 21:40:59 use a setter instead: String set filter(String va
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() {
Siggi Cherem (dart-lang) 2012/06/25 21:40:59 use a getter instead: String get filter => _filte
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 379 matching lines...) Expand 10 before | Expand all | Expand 10 after
634 }); 656 });
635 port.toSendPort().send(null, null); 657 port.toSendPort().send(null, null);
636 } 658 }
637 659
638 /** Runs all queued tests, one at a time. */ 660 /** Runs all queued tests, one at a time. */
639 _runTests() { 661 _runTests() {
640 // If we are soloing a test, remove all the others. 662 // If we are soloing a test, remove all the others.
641 if (_soloTest != null) { 663 if (_soloTest != null) {
642 _tests = _tests.filter((t) => t == _soloTest); 664 _tests = _tests.filter((t) => t == _soloTest);
643 } 665 }
666 if (_filter != null) {
Siggi Cherem (dart-lang) 2012/06/25 21:40:59 To simplify the API, I'm inclined to just make _fi
667 RegExp re = new RegExp(_filter);
668 _tests = _tests.filter((t) => re.hasMatch(t.description));
669 }
644 670
645 _config.onStart(); 671 _config.onStart();
646 672
647 _defer(() { 673 _defer(() {
648 assert (_currentTest == 0); 674 assert (_currentTest == 0);
649 _testRunner(); 675 _testRunner();
650 }); 676 });
651 } 677 }
652 678
653 /** 679 /**
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after
756 _config.onInit(); 782 _config.onInit();
757 783
758 // Immediately queue the suite up. It will run after a timeout (i.e. after 784 // Immediately queue the suite up. It will run after a timeout (i.e. after
759 // main() has returned). 785 // main() has returned).
760 _defer(_runTests); 786 _defer(_runTests);
761 } 787 }
762 788
763 /** Signature for a test function. */ 789 /** Signature for a test function. */
764 typedef void TestFunction(); 790 typedef void TestFunction();
765 791
OLDNEW
« no previous file with comments | « lib/unittest/config.dart ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698