OLD | NEW |
1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 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 | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 library unittest.backend.declarer; | 5 library unittest.backend.declarer; |
6 | 6 |
7 import 'dart:collection'; | 7 import 'dart:collection'; |
8 | 8 |
9 import 'group.dart'; | 9 import 'group.dart'; |
10 import 'invoker.dart'; | 10 import 'invoker.dart'; |
| 11 import 'metadata.dart'; |
11 import 'test.dart'; | 12 import 'test.dart'; |
12 | 13 |
13 /// A class that manages the state of tests as they're declared. | 14 /// A class that manages the state of tests as they're declared. |
14 /// | 15 /// |
15 /// This is in charge of tracking the current group, set-up, and tear-down | 16 /// This is in charge of tracking the current group, set-up, and tear-down |
16 /// functions. It produces a list of runnable [tests]. | 17 /// functions. It produces a list of runnable [tests]. |
17 class Declarer { | 18 class Declarer { |
18 /// The current group. | 19 /// The current group. |
19 var _group = new Group.root(); | 20 var _group = new Group.root(); |
20 | 21 |
21 /// The list of tests that have been defined. | 22 /// The list of tests that have been defined. |
22 List<Test> get tests => new UnmodifiableListView<Test>(_tests); | 23 List<Test> get tests => new UnmodifiableListView<Test>(_tests); |
23 final _tests = new List<Test>(); | 24 final _tests = new List<Test>(); |
24 | 25 |
25 Declarer(); | 26 Declarer(); |
26 | 27 |
27 /// Defines a test case with the given description and body. | 28 /// Defines a test case with the given description and body. |
28 /// | 29 /// |
29 /// The description will be added to the descriptions of any surrounding | 30 /// The description will be added to the descriptions of any surrounding |
30 /// [group]s. | 31 /// [group]s. |
31 void test(String description, body()) { | 32 /// |
| 33 /// If [testOn] is passed, it's parsed as a [PlatformSelector], and the test |
| 34 /// will only be run on matching platforms. |
| 35 void test(String description, body(), {String testOn}) { |
32 // TODO(nweiz): Once tests have begun running, throw an error if [test] is | 36 // TODO(nweiz): Once tests have begun running, throw an error if [test] is |
33 // called. | 37 // called. |
34 var prefix = _group.description; | 38 var prefix = _group.description; |
35 if (prefix != null) description = "$prefix $description"; | 39 if (prefix != null) description = "$prefix $description"; |
36 | 40 |
| 41 var metadata = _group.metadata.merge(new Metadata.parse(testOn: testOn)); |
37 var group = _group; | 42 var group = _group; |
38 _tests.add(new LocalTest(description, () { | 43 _tests.add(new LocalTest(description, metadata, () { |
39 // TODO(nweiz): It might be useful to throw an error here if a test starts | 44 // 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, | 45 // running while other tests from the same declarer are also running, |
41 // since they might share closurized state. | 46 // since they might share closurized state. |
42 return group.runSetUp().then((_) => body()); | 47 return group.runSetUp().then((_) => body()); |
43 }, tearDown: group.runTearDown)); | 48 }, tearDown: group.runTearDown)); |
44 } | 49 } |
45 | 50 |
46 /// Creates a group of tests. | 51 /// Creates a group of tests. |
47 /// | 52 /// |
48 /// A group's description is included in the descriptions of any tests or | 53 /// 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 | 54 /// sub-groups it contains. [setUp] and [tearDown] are also scoped to the |
50 /// containing group. | 55 /// containing group. |
51 void group(String description, void body()) { | 56 /// |
| 57 /// If [testOn] is passed, it's parsed as a [PlatformSelector], and any tests |
| 58 /// in the group will only be run on matching platforms. |
| 59 void group(String description, void body(), {String testOn}) { |
52 var oldGroup = _group; | 60 var oldGroup = _group; |
53 _group = new Group(oldGroup, description); | 61 |
| 62 _group = new Group( |
| 63 oldGroup, description, new Metadata.parse(testOn: testOn)); |
54 try { | 64 try { |
55 body(); | 65 body(); |
56 } finally { | 66 } finally { |
57 _group = oldGroup; | 67 _group = oldGroup; |
58 } | 68 } |
59 } | 69 } |
60 | 70 |
61 /// Registers a function to be run before tests. | 71 /// Registers a function to be run before tests. |
62 /// | 72 /// |
63 /// This function will be called before each test is run. [callback] may be | 73 /// This function will be called before each test is run. [callback] may be |
(...skipping 21 matching lines...) Expand all Loading... |
85 /// groups or at the top level. | 95 /// groups or at the top level. |
86 void tearDown(callback()) { | 96 void tearDown(callback()) { |
87 if (_group.tearDown != null) { | 97 if (_group.tearDown != null) { |
88 throw new StateError("tearDown() may not be called multiple times for " | 98 throw new StateError("tearDown() may not be called multiple times for " |
89 "the same group."); | 99 "the same group."); |
90 } | 100 } |
91 | 101 |
92 _group.tearDown = callback; | 102 _group.tearDown = callback; |
93 } | 103 } |
94 } | 104 } |
OLD | NEW |