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 test.backend.declarer; |
| 6 |
| 7 import 'dart:collection'; |
| 8 |
| 9 import '../frontend/timeout.dart'; |
| 10 import 'group.dart'; |
| 11 import 'invoker.dart'; |
| 12 import 'metadata.dart'; |
| 13 import 'test.dart'; |
| 14 |
| 15 /// A class that manages the state of tests as they're declared. |
| 16 /// |
| 17 /// This is in charge of tracking the current group, set-up, and tear-down |
| 18 /// functions. It produces a list of runnable [tests]. |
| 19 class Declarer { |
| 20 /// The current group. |
| 21 var _group = new Group.root(); |
| 22 |
| 23 /// The list of tests that have been defined. |
| 24 List<Test> get tests => new UnmodifiableListView<Test>(_tests); |
| 25 final _tests = new List<Test>(); |
| 26 |
| 27 Declarer(); |
| 28 |
| 29 /// Defines a test case with the given description and body. |
| 30 void test(String description, body(), {String testOn, Timeout timeout, |
| 31 skip, Map<String, dynamic> onPlatform}) { |
| 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 metadata = _group.metadata.merge(new Metadata.parse( |
| 38 testOn: testOn, timeout: timeout, skip: skip, onPlatform: onPlatform)); |
| 39 |
| 40 var group = _group; |
| 41 _tests.add(new LocalTest(description, metadata, () { |
| 42 // TODO(nweiz): It might be useful to throw an error here if a test starts |
| 43 // running while other tests from the same declarer are also running, |
| 44 // since they might share closurized state. |
| 45 |
| 46 // TODO(nweiz): Use async/await here once issue 23497 has been fixed in |
| 47 // two stable versions. |
| 48 return group.runSetUp().then((_) => body()); |
| 49 }, tearDown: group.runTearDown)); |
| 50 } |
| 51 |
| 52 /// Creates a group of tests. |
| 53 void group(String description, void body(), {String testOn, |
| 54 Timeout timeout, skip, Map<String, dynamic> onPlatform}) { |
| 55 var oldGroup = _group; |
| 56 |
| 57 var metadata = new Metadata.parse( |
| 58 testOn: testOn, timeout: timeout, skip: skip, onPlatform: onPlatform); |
| 59 |
| 60 // Don' load the tests for a skipped group. |
| 61 if (metadata.skip) { |
| 62 _tests.add(new LocalTest(description, metadata, () {})); |
| 63 return; |
| 64 } |
| 65 |
| 66 _group = new Group(oldGroup, description, metadata); |
| 67 try { |
| 68 body(); |
| 69 } finally { |
| 70 _group = oldGroup; |
| 71 } |
| 72 } |
| 73 |
| 74 /// Registers a function to be run before tests. |
| 75 void setUp(callback()) { |
| 76 if (_group.setUp != null) { |
| 77 throw new StateError("setUp() may not be called multiple times for the " |
| 78 "same group."); |
| 79 } |
| 80 |
| 81 _group.setUp = callback; |
| 82 } |
| 83 |
| 84 /// Registers a function to be run after tests. |
| 85 void tearDown(callback()) { |
| 86 if (_group.tearDown != null) { |
| 87 throw new StateError("tearDown() may not be called multiple times for " |
| 88 "the same group."); |
| 89 } |
| 90 |
| 91 _group.tearDown = callback; |
| 92 } |
| 93 } |
OLD | NEW |