Skip to content

Commit 8ad00bd

Browse files
committed
Add Declarer and Group classes.
These provide the core test-declaration API. [email protected] See #2 Review URL: https://codereview.chromium.org//917783003
1 parent 8537739 commit 8ad00bd

3 files changed

Lines changed: 496 additions & 0 deletions

File tree

‎lib/src/declarer.dart‎

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
// Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file
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.
4+
5+
library unittest.declarer;
6+
7+
import 'dart:collection';
8+
9+
import 'group.dart';
10+
import 'invoker.dart';
11+
import 'test.dart';
12+
13+
/// A class that manages the state of tests as they're declared.
14+
///
15+
/// This is in charge of tracking the current group, set-up, and tear-down
16+
/// functions. It produces a list of runnable [tests].
17+
class Declarer {
18+
/// The current group.
19+
var _group = new Group.root();
20+
21+
/// The list of tests that have been defined.
22+
List<Test> get tests => new UnmodifiableListView<Test>(_tests);
23+
final _tests = new List<Test>();
24+
25+
Declarer();
26+
27+
/// Defines a test case with the given description and body.
28+
///
29+
/// The description will be added to the descriptions of any surrounding
30+
/// [group]s.
31+
void test(String description, body()) {
32+
// TODO(nweiz): Once tests have begun running, throw an error if [test] is
33+
// called.
34+
var prefix = _group.description;
35+
if (prefix != null) description = "$prefix $description";
36+
37+
var group = _group;
38+
_tests.add(new LocalTest(description, () {
39+
// TODO(nweiz): It might be useful to throw an error here if a test starts
40+
// running while other tests from the same declarer are also running,
41+
// since they might share closurized state.
42+
return group.runSetUp().then((_) => body());
43+
}, tearDown: group.runTearDown));
44+
}
45+
46+
/// Creates a group of tests.
47+
///
48+
/// A group's description is included in the descriptions of any tests or
49+
/// sub-groups it contains. [setUp] and [tearDown] are also scoped to the
50+
/// containing group.
51+
void group(String description, void body()) {
52+
var oldGroup = _group;
53+
_group = new Group(oldGroup, description);
54+
try {
55+
body();
56+
} finally {
57+
_group = oldGroup;
58+
}
59+
}
60+
61+
/// Registers a function to be run before tests.
62+
///
63+
/// This function will be called before each test is run. [callback] may be
64+
/// asynchronous; if so, it must return a [Future].
65+
///
66+
/// If this is called within a [group], it applies only to tests in that
67+
/// group. [callback] will be run after any set-up callbacks in parent groups
68+
/// or at the top level.
69+
void setUp(callback()) {
70+
if (_group.setUp != null) {
71+
throw new StateError("setUp() may not be called multiple times for the "
72+
"same group.");
73+
}
74+
75+
_group.setUp = callback;
76+
}
77+
78+
/// Registers a function to be run after tests.
79+
///
80+
/// This function will be called after each test is run. [callback] may be
81+
/// asynchronous; if so, it must return a [Future].
82+
///
83+
/// If this is called within a [group], it applies only to tests in that
84+
/// group. [callback] will be run before any tear-down callbacks in parent
85+
/// groups or at the top level.
86+
void tearDown(callback()) {
87+
if (_group.tearDown != null) {
88+
throw new StateError("tearDown() may not be called multiple times for "
89+
"the same group.");
90+
}
91+
92+
_group.tearDown = callback;
93+
}
94+
}

‎lib/src/group.dart‎

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
// Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file
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.
4+
5+
library unittest.group;
6+
7+
import 'dart:async';
8+
9+
import 'utils.dart';
10+
11+
/// A group contains multiple tests and subgroups.
12+
///
13+
/// A group has a description that is prepended to that of all nested tests and
14+
/// subgroups. It also has [setUp] and [tearDown] functions which are scoped to
15+
/// the tests and groups it contains.
16+
class Group {
17+
/// The parent group, or `null` if this is the root group.
18+
final Group parent;
19+
20+
/// The description of the current test group, or `null` if this is the root
21+
/// group.
22+
final String _description;
23+
24+
/// The set-up function for this group, or `null`.
25+
AsyncFunction setUp;
26+
27+
/// The tear-down function for this group, or `null`.
28+
AsyncFunction tearDown;
29+
30+
/// Returns the description for this group, including the description of any
31+
/// parent groups.
32+
///
33+
/// If this is the root group, returns `null`.
34+
String get description {
35+
if (parent == null || parent.description == null) return _description;
36+
return "${parent.description} $_description";
37+
}
38+
39+
/// Creates a new root group.
40+
///
41+
/// This is the implicit group that exists outside of any calls to `group()`.
42+
Group.root()
43+
: this(null, null);
44+
45+
Group(this.parent, this._description);
46+
47+
/// Run the set-up functions for this and any parent groups.
48+
///
49+
/// If no set-up functions are declared, this returns a [Future] that
50+
/// completes immediately.
51+
Future runSetUp() {
52+
if (parent != null) {
53+
return parent.runSetUp().then((_) {
54+
if (setUp != null) return setUp();
55+
});
56+
}
57+
58+
if (setUp != null) return new Future.sync(setUp);
59+
return new Future.value();
60+
}
61+
62+
/// Run the tear-up functions for this and any parent groups.
63+
///
64+
/// If no set-up functions are declared, this returns a [Future] that
65+
/// completes immediately.
66+
Future runTearDown() {
67+
if (parent != null) {
68+
return new Future.sync(() {
69+
if (tearDown != null) return tearDown();
70+
}).then((_) => parent.runTearDown());
71+
}
72+
73+
if (tearDown != null) return new Future.sync(tearDown);
74+
return new Future.value();
75+
}
76+
}

0 commit comments

Comments
 (0)