Chromium Code Reviews| Index: lib/src/group.dart |
| diff --git a/lib/src/group.dart b/lib/src/group.dart |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..282078c83287aefd871f15efc39c812a1375e65f |
| --- /dev/null |
| +++ b/lib/src/group.dart |
| @@ -0,0 +1,74 @@ |
| +// Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file |
| +// for details. All rights reserved. Use of this source code is governed by a |
| +// BSD-style license that can be found in the LICENSE file. |
| + |
| +library unittest.group; |
| + |
| +import 'dart:async'; |
| + |
| +/// A group contains multiple tests and subgroups. |
| +/// |
| +/// A group has a description that is prepended to that of all nested tests and |
| +/// subgroups. It also has [setUp] and [tearDown] functions which are scoped to |
| +/// the tests and groups it contains. |
| +class Group { |
| + /// The parent group, or `null` if this is the root group. |
| + final Group parent; |
| + |
| + /// The description of the current test group, or `null` if this is the root |
| + /// group. |
| + final String _description; |
| + |
| + /// The set-up function for this group, or `null`. |
| + Function setUp; |
|
kevmoo
2015/02/11 19:57:31
AsyncFunction?
nweiz
2015/02/11 21:32:54
Done.
|
| + |
| + /// The tear-down function for this group, or `null`. |
| + Function tearDown; |
|
kevmoo
2015/02/11 19:57:31
AsyncFunction?
nweiz
2015/02/11 21:32:54
Done.
|
| + |
| + /// Returns the description for this group, including the description of any |
| + /// parent groups. |
| + /// |
| + /// If this is the root group. returns `null`. |
|
kevmoo
2015/02/11 19:57:31
"group." -> "group,"
nweiz
2015/02/11 21:32:54
Done.
|
| + String get description { |
| + if (parent == null || parent.description == null) return _description; |
| + return "${parent.description} $_description"; |
| + } |
| + |
| + /// Creates a new root group. |
| + /// |
| + /// This is the implicit group that exists outside of any calls to `group()`. |
| + Group.root() |
| + : this(null, null); |
| + |
| + Group(this.parent, this._description); |
| + |
| + /// Run the set-up functions for this and any parent groups. |
| + /// |
| + /// If no set-up functions are declared, this returns a [Future] that |
| + /// completes immediately. |
| + Future runSetUp() { |
| + if (parent != null) { |
| + return parent.runSetUp().then((_) { |
| + if (setUp != null) return setUp(); |
| + }); |
| + } |
| + |
| + if (setUp != null) return new Future.sync(setUp); |
| + return new Future.value(); |
| + } |
| + |
| + /// Run the tear-up functions for this and any parent groups. |
| + /// |
| + /// If no set-up functions are declared, this returns a [Future] that |
| + /// completes immediately. |
| + Future runTearDown() { |
| + if (parent != null) { |
| + return new Future.sync(() { |
| + if (tearDown != null) return tearDown(); |
| + }).then((_) => parent.runTearDown()); |
| + } |
| + |
| + if (tearDown != null) return new Future.sync(tearDown); |
| + return new Future.value(); |
| + } |
| +} |