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

Unified Diff: lib/unittest/unittest.dart

Issue 10335007: First pass at a generic reusable command line option parser. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 8 years, 8 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « lib/args/utils.dart ('k') | tests/lib/args/args_test.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: lib/unittest/unittest.dart
diff --git a/lib/unittest/unittest.dart b/lib/unittest/unittest.dart
index 67c1fcee8e4b12d6fa30055b48a0803524adff1e..65670a48025c4608ff1f480b1f2172a7ceaf54f4 100644
--- a/lib/unittest/unittest.dart
+++ b/lib/unittest/unittest.dart
@@ -120,6 +120,7 @@
#library('unittest');
#import('dart:isolate');
+#import('../args/args.dart');
Bob Nystrom 2012/05/02 21:48:55 Oops! This is temp code. Don't worry about anythin
Bob Nystrom 2012/05/02 21:52:42 And, actually now that I look at it, the rest of t
#source('config.dart');
#source('expectation.dart');
@@ -172,18 +173,33 @@ final _PASS = 'pass';
final _FAIL = 'fail';
final _ERROR = 'error';
+/** If set, then all other test cases will be ignored. */
+TestCase _soloTest;
+
/** Creates an expectation for the given value. */
Expectation expect(value) => new Expectation(value);
/** Evaluates the given function and validates that it throws an exception. */
-void expectThrow(function) {
+void expectThrow(function, [bool callback(exception)]) {
bool threw = false;
try {
function();
} catch (var e) {
threw = true;
+
+ // Also let the callback look at it.
+ if (callback != null) {
+ var result = callback(e);
+
+ // If the callback explicitly returned false, treat that like an
+ // expectation too. (If it returns null, though, don't.)
+ if (result == false) {
+ _fail('Exception:\n$e\ndid not match expectation.');
+ }
+ }
}
- Expect.equals(true, threw, 'Expected exception but none was thrown.');
+
+ if (threw != true) _fail('An expected exception was not thrown.');
}
/**
@@ -216,6 +232,23 @@ void asyncTest(String spec, int callbacks, TestFunction body) {
}
}
+/**
+ * Creates a new test case with the given description and body. The
+ * description will include the descriptions of any surrounding group()
+ * calls.
+ *
+ * "solo_" means that this will be the only test that is run. All other tests
+ * will be skipped. This is a convenience function to let you quickly isolate
+ * a single test by adding "solo_" before it to temporarily disable all other
+ * tests.
+ */
+ void solo_test(String spec, TestFunction body) {
+ ensureInitialized();
+
+ _soloTest = new TestCase(_tests.length + 1, _fullSpec(spec), body, 0);
+ _tests.add(_soloTest);
+}
+
/** Sentinel value for [_SpreadArgsHelper]. */
class _Sentinel {
const _Sentinel();
@@ -408,6 +441,11 @@ _defer(void callback()) {
/** Runs all queued tests, one at a time. */
_runTests() {
+ // If we are soloing a test, remove all the others.
+ if (_soloTest != null) {
+ _tests = _tests.filter((t) => t == _soloTest);
+ }
+
_config.onStart();
_defer(() {
@@ -429,7 +467,13 @@ _guard(tryBody, [finallyBody]) {
trace == null ? '' : trace.toString());
}
} catch (var e, var trace) {
- if (_state != _UNCAUGHT_ERROR) {
+ if (_state == _RUNNING_TEST) {
+ // If a random exception is thrown from within a test, we consider that
+ // a test failure too. A test case implicitly has an expectation that it
+ // will run to completion without an uncaught exception being thrown.
+ _tests[_currentTest].fail('Caught $e',
+ trace == null ? '' : trace.toString());
+ } else if (_state != _UNCAUGHT_ERROR) {
_tests[_currentTest].error('Caught $e',
trace == null ? '' : trace.toString());
}
@@ -493,6 +537,10 @@ String _fullSpec(String spec) {
return _currentGroup != '' ? '$_currentGroup $spec' : spec;
}
+void _fail(String message) {
+ throw new ExpectException(message);
+}
+
/**
* Lazily initializes the test library if not already initialized.
*/
« no previous file with comments | « lib/args/utils.dart ('k') | tests/lib/args/args_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698