OLD | NEW |
| (Empty) |
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 } | |
OLD | NEW |