|
| 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 | +} |
0 commit comments