Chromium Code Reviews| Index: lib/unittest/unittest.dart |
| =================================================================== |
| --- lib/unittest/unittest.dart (revision 8913) |
| +++ lib/unittest/unittest.dart (working copy) |
| @@ -155,9 +155,14 @@ |
| /** [Configuration] used by the unittest library. */ |
| Configuration _config = null; |
| -/** Set the [Configuration] used by the unittest library. */ |
| -void configure(Configuration config) { |
| +/** |
| + * Set the [Configuration] used by the unittest library. Returns any |
| + * previous configuration. |
| + */ |
| +Configuration configure(Configuration config) { |
| + Configuration _oldConfig = _config; |
| _config = config; |
| + return _oldConfig; |
| } |
| /** |
| @@ -175,6 +180,12 @@ |
| */ |
| Function _testRunner; |
| +/** Setup function called before each test in a group */ |
| +Function _testSetup; |
| + |
| +/** Teardown function called after each test in a group */ |
| +Function _testTeardown; |
| + |
| /** Current test being executed. */ |
| int _currentTest = 0; |
| @@ -504,11 +515,12 @@ |
| * Creates a new named group of tests. Calls to group() or test() within the |
| * body of the function passed to this will inherit this group's description. |
| */ |
| -void group(String description, void body()) { |
| +void group(String description, void body(), |
| + [Function setupTest, Function teardownTest]) { |
|
Bob Nystrom
2012/06/20 20:21:22
Since you added setUp() and tearDown(), do you sti
gram
2012/06/20 21:11:47
Done.
|
| ensureInitialized(); |
| // Concatenate the new group. |
| - final oldGroup = _currentGroup; |
| + final parentGroup = _currentGroup; |
| if (_currentGroup != '') { |
| // Add a space. |
| _currentGroup = '$_currentGroup $description'; |
| @@ -517,14 +529,47 @@ |
| _currentGroup = description; |
| } |
| + // Groups can be nested, so we need to preserve the current |
| + // settings for test setup/teardown. |
| + Function parentSetup = _testSetup; |
| + Function parentTeardown = _testTeardown; |
| + |
| try { |
| + _testSetup = null; |
| + _testTeardown = null; |
| body(); |
| } finally { |
| // Now that the group is over, restore the previous one. |
| - _currentGroup = oldGroup; |
| + _currentGroup = parentGroup; |
| + _testSetup = parentSetup; |
| + _testTeardown = parentTeardown; |
| } |
| } |
| +/** |
| + * Register a [setUp] function for a test [group]. This function will |
| + * be called before each test in the group is run. Note that if groups |
| + * are nested only the most locally scoped [setUp] function will be run. |
|
Bob Nystrom
2012/06/20 20:21:22
It might be useful at some point to actually allow
gram
2012/06/20 21:11:47
I think this could be tricky. There may be cases w
|
| + * [setUp] and [tearDown] should be called within the [group] before any |
| + * calls to [test]. |
| + */ |
| + |
| +void setUp(Function setupTest) { |
|
Bob Nystrom
2012/06/20 20:21:22
Remove blank line above this.
gram
2012/06/20 21:11:47
Done.
|
| + _testSetup = setupTest; |
| +} |
| + |
| +/** |
| + * Register a [tearDown] function for a test [group]. This function will |
| + * be called after each test in the group is run. Note that if groups |
| + * are nested only the most locally scoped [tearDown] function will be run. |
| + * [setUp] and [tearDown] should be called within the [group] before any |
| + * calls to [test]. |
| + */ |
| + |
| +void tearDown(Function teardownTest) { |
|
Bob Nystrom
2012/06/20 20:21:22
Remove blank line above this.
gram
2012/06/20 21:11:47
Done.
|
| + _testTeardown = teardownTest; |
| +} |
| + |
| /** Called by subclasses to indicate that an asynchronous test completed. */ |
| void _handleAllCallbacksDone() { |
| // TODO (gram): we defer this to give the nextBatch recursive |
| @@ -648,7 +693,7 @@ |
| guardAsync(() { |
| _callbacksCalled = 0; |
| _state = _RUNNING_TEST; |
| - testCase.test(); |
| + testCase.run(); |
| if (_state != _UNCAUGHT_ERROR) { |
| if (testCase.callbacks == _callbacksCalled) { |