| 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.group; | 5 library test.backend.group; |
| 6 | 6 |
| 7 import 'dart:async'; | 7 import 'dart:collection'; |
| 8 | 8 |
| 9 import '../utils.dart'; | |
| 10 import 'invoker.dart'; | |
| 11 import 'metadata.dart'; | 9 import 'metadata.dart'; |
| 10 import 'operating_system.dart'; |
| 11 import 'suite_entry.dart'; |
| 12 import 'test.dart'; |
| 13 import 'test_platform.dart'; |
| 12 | 14 |
| 13 /// A group contains multiple tests and subgroups. | 15 /// A group contains one or more tests and subgroups. |
| 14 /// | 16 /// |
| 15 /// A group has a description that is prepended to that of all nested tests and | 17 /// It includes metadata that applies to all contained tests. |
| 16 /// subgroups. It also has [setUp] and [tearDown] functions which are scoped to | 18 class Group implements SuiteEntry { |
| 17 /// the tests and groups it contains. | 19 final String name; |
| 18 class Group { | |
| 19 /// The parent group, or `null` if this is the root group. | |
| 20 final Group parent; | |
| 21 | 20 |
| 22 /// The description of the current test group, or `null` if this is the root | 21 final Metadata metadata; |
| 23 /// group. | |
| 24 final String _description; | |
| 25 | 22 |
| 26 /// The metadata for this group, including the metadata of any parent groups. | 23 /// The children of this group. |
| 27 Metadata get metadata { | 24 final List<SuiteEntry> entries; |
| 28 if (parent == null) return _metadata; | |
| 29 return parent.metadata.merge(_metadata); | |
| 30 } | |
| 31 final Metadata _metadata; | |
| 32 | 25 |
| 33 /// The set-up functions for this group. | 26 Group(this.name, this.metadata, Iterable<SuiteEntry> entries) |
| 34 final setUps = new List<AsyncFunction>(); | 27 : entries = new UnmodifiableListView<SuiteEntry>(entries.toList()); |
| 35 | 28 |
| 36 /// The tear-down functions for this group. | 29 Group forPlatform(TestPlatform platform, {OperatingSystem os}) { |
| 37 final tearDowns = new List<AsyncFunction>(); | 30 if (!metadata.testOn.evaluate(platform, os: os)) return null; |
| 38 | 31 var newMetadata = metadata.forPlatform(platform, os: os); |
| 39 /// Returns the description for this group, including the description of any | 32 var filtered = _map((entry) => entry.forPlatform(platform, os: os)); |
| 40 /// parent groups. | 33 if (filtered.isEmpty) return null; |
| 41 /// | 34 return new Group(name, newMetadata, filtered); |
| 42 /// If this is the root group, returns `null`. | |
| 43 String get description { | |
| 44 if (parent == null || parent.description == null) return _description; | |
| 45 return "${parent.description} $_description"; | |
| 46 } | 35 } |
| 47 | 36 |
| 48 /// Creates a new root group. | 37 Group filter(bool callback(Test test)) { |
| 49 /// | 38 var filtered = _map((entry) => entry.filter(callback)); |
| 50 /// This is the implicit group that exists outside of any calls to `group()`. | 39 if (filtered.isEmpty) return null; |
| 51 Group.root() | 40 return new Group(name, metadata, filtered); |
| 52 : this(null, null, new Metadata()); | |
| 53 | |
| 54 Group(this.parent, this._description, this._metadata); | |
| 55 | |
| 56 /// Run the set-up functions for this and any parent groups. | |
| 57 /// | |
| 58 /// If no set-up functions are declared, this returns a [Future] that | |
| 59 /// completes immediately. | |
| 60 Future runSetUps() { | |
| 61 // TODO(nweiz): Use async/await here once issue 23497 has been fixed in two | |
| 62 // stable versions. | |
| 63 if (parent != null) { | |
| 64 return parent.runSetUps().then((_) { | |
| 65 return Future.forEach(setUps, (setUp) => setUp()); | |
| 66 }); | |
| 67 } | |
| 68 | |
| 69 return Future.forEach(setUps, (setUp) => setUp()); | |
| 70 } | 41 } |
| 71 | 42 |
| 72 /// Run the tear-up functions for this and any parent groups. | 43 /// Returns the entries of this group mapped using [callback]. |
| 73 /// | 44 /// |
| 74 /// If no set-up functions are declared, this returns a [Future] that | 45 /// Any `null` values returned by [callback] will be removed. |
| 75 /// completes immediately. | 46 List<SuiteEntry> _map(SuiteEntry callback(SuiteEntry entry)) { |
| 76 Future runTearDowns() { | 47 return entries |
| 77 return Invoker.current.unclosable(() { | 48 .map((entry) => callback(entry)) |
| 78 var tearDowns = []; | 49 .where((entry) => entry != null) |
| 79 for (var group = this; group != null; group = group.parent) { | 50 .toList(); |
| 80 tearDowns.addAll(group.tearDowns.reversed); | |
| 81 } | |
| 82 | |
| 83 return Future.forEach(tearDowns, _errorsDontStopTest); | |
| 84 }); | |
| 85 } | |
| 86 | |
| 87 /// Runs [body] with special error-handling behavior. | |
| 88 /// | |
| 89 /// Errors emitted [body] will still cause be the test to fail, but they won't | |
| 90 /// cause it to *stop*. In particular, they won't remove any outstanding | |
| 91 /// callbacks registered outside of [body]. | |
| 92 Future _errorsDontStopTest(body()) { | |
| 93 var completer = new Completer(); | |
| 94 | |
| 95 Invoker.current.addOutstandingCallback(); | |
| 96 Invoker.current.waitForOutstandingCallbacks(() { | |
| 97 new Future.sync(body).whenComplete(completer.complete); | |
| 98 }).then((_) => Invoker.current.removeOutstandingCallback()); | |
| 99 | |
| 100 return completer.future; | |
| 101 } | 51 } |
| 102 } | 52 } |
| OLD | NEW |