| 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 test.backend.declarer; | 5 library test.backend.declarer; |
| 6 | 6 |
| 7 import 'dart:collection'; | 7 import 'dart:collection'; |
| 8 | 8 |
| 9 import '../frontend/timeout.dart'; |
| 9 import 'group.dart'; | 10 import 'group.dart'; |
| 10 import 'invoker.dart'; | 11 import 'invoker.dart'; |
| 11 import 'metadata.dart'; | 12 import 'metadata.dart'; |
| 12 import 'test.dart'; | 13 import 'test.dart'; |
| 13 | 14 |
| 14 /// A class that manages the state of tests as they're declared. | 15 /// A class that manages the state of tests as they're declared. |
| 15 /// | 16 /// |
| 16 /// This is in charge of tracking the current group, set-up, and tear-down | 17 /// This is in charge of tracking the current group, set-up, and tear-down |
| 17 /// functions. It produces a list of runnable [tests]. | 18 /// functions. It produces a list of runnable [tests]. |
| 18 class Declarer { | 19 class Declarer { |
| 19 /// The current group. | 20 /// The current group. |
| 20 var _group = new Group.root(); | 21 var _group = new Group.root(); |
| 21 | 22 |
| 22 /// The list of tests that have been defined. | 23 /// The list of tests that have been defined. |
| 23 List<Test> get tests => new UnmodifiableListView<Test>(_tests); | 24 List<Test> get tests => new UnmodifiableListView<Test>(_tests); |
| 24 final _tests = new List<Test>(); | 25 final _tests = new List<Test>(); |
| 25 | 26 |
| 26 Declarer(); | 27 Declarer(); |
| 27 | 28 |
| 28 /// Defines a test case with the given description and body. | 29 /// Defines a test case with the given description and body. |
| 29 /// | 30 /// |
| 30 /// The description will be added to the descriptions of any surrounding | 31 /// The description will be added to the descriptions of any surrounding |
| 31 /// [group]s. | 32 /// [group]s. |
| 32 /// | 33 /// |
| 33 /// If [testOn] is passed, it's parsed as a [PlatformSelector], and the test | 34 /// If [testOn] is passed, it's parsed as a [PlatformSelector], and the test |
| 34 /// will only be run on matching platforms. | 35 /// will only be run on matching platforms. |
| 35 void test(String description, body(), {String testOn}) { | 36 /// |
| 37 /// If [timeout] is passed, it's used to modify or replace the default timeout |
| 38 /// of 30 seconds. Timeout modifications take precedence in suite-group-test |
| 39 /// order, so [timeout] will also modify any timeouts set on the group or |
| 40 /// suite. |
| 41 void test(String description, body(), {String testOn, Timeout timeout}) { |
| 36 // TODO(nweiz): Once tests have begun running, throw an error if [test] is | 42 // TODO(nweiz): Once tests have begun running, throw an error if [test] is |
| 37 // called. | 43 // called. |
| 38 var prefix = _group.description; | 44 var prefix = _group.description; |
| 39 if (prefix != null) description = "$prefix $description"; | 45 if (prefix != null) description = "$prefix $description"; |
| 40 | 46 |
| 41 var metadata = _group.metadata.merge(new Metadata.parse(testOn: testOn)); | 47 var metadata = _group.metadata.merge( |
| 48 new Metadata.parse(testOn: testOn, timeout: timeout)); |
| 42 var group = _group; | 49 var group = _group; |
| 43 _tests.add(new LocalTest(description, metadata, () { | 50 _tests.add(new LocalTest(description, metadata, () { |
| 44 // TODO(nweiz): It might be useful to throw an error here if a test starts | 51 // TODO(nweiz): It might be useful to throw an error here if a test starts |
| 45 // running while other tests from the same declarer are also running, | 52 // running while other tests from the same declarer are also running, |
| 46 // since they might share closurized state. | 53 // since they might share closurized state. |
| 47 return group.runSetUp().then((_) => body()); | 54 return group.runSetUp().then((_) => body()); |
| 48 }, tearDown: group.runTearDown)); | 55 }, tearDown: group.runTearDown)); |
| 49 } | 56 } |
| 50 | 57 |
| 51 /// Creates a group of tests. | 58 /// Creates a group of tests. |
| 52 /// | 59 /// |
| 53 /// A group's description is included in the descriptions of any tests or | 60 /// A group's description is included in the descriptions of any tests or |
| 54 /// sub-groups it contains. [setUp] and [tearDown] are also scoped to the | 61 /// sub-groups it contains. [setUp] and [tearDown] are also scoped to the |
| 55 /// containing group. | 62 /// containing group. |
| 56 /// | 63 /// |
| 57 /// If [testOn] is passed, it's parsed as a [PlatformSelector], and any tests | 64 /// If [testOn] is passed, it's parsed as a [PlatformSelector], and any tests |
| 58 /// in the group will only be run on matching platforms. | 65 /// in the group will only be run on matching platforms. |
| 59 void group(String description, void body(), {String testOn}) { | 66 /// |
| 67 /// If [timeout] is passed, it's used to modify or replace the default timeout |
| 68 /// of 30 seconds. Timeout modifications take precedence in suite-group-test |
| 69 /// order, so [timeout] will also modify any timeouts set on the group or |
| 70 /// suite. |
| 71 void group(String description, void body(), {String testOn, |
| 72 Timeout timeout}) { |
| 60 var oldGroup = _group; | 73 var oldGroup = _group; |
| 61 | 74 |
| 62 _group = new Group( | 75 var metadata = new Metadata.parse(testOn: testOn, timeout: timeout); |
| 63 oldGroup, description, new Metadata.parse(testOn: testOn)); | 76 _group = new Group(oldGroup, description, metadata); |
| 64 try { | 77 try { |
| 65 body(); | 78 body(); |
| 66 } finally { | 79 } finally { |
| 67 _group = oldGroup; | 80 _group = oldGroup; |
| 68 } | 81 } |
| 69 } | 82 } |
| 70 | 83 |
| 71 /// Registers a function to be run before tests. | 84 /// Registers a function to be run before tests. |
| 72 /// | 85 /// |
| 73 /// This function will be called before each test is run. [callback] may be | 86 /// This function will be called before each test is run. [callback] may be |
| (...skipping 21 matching lines...) Expand all Loading... |
| 95 /// groups or at the top level. | 108 /// groups or at the top level. |
| 96 void tearDown(callback()) { | 109 void tearDown(callback()) { |
| 97 if (_group.tearDown != null) { | 110 if (_group.tearDown != null) { |
| 98 throw new StateError("tearDown() may not be called multiple times for " | 111 throw new StateError("tearDown() may not be called multiple times for " |
| 99 "the same group."); | 112 "the same group."); |
| 100 } | 113 } |
| 101 | 114 |
| 102 _group.tearDown = callback; | 115 _group.tearDown = callback; |
| 103 } | 116 } |
| 104 } | 117 } |
| OLD | NEW |