Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(103)

Unified Diff: pkg/scheduled_test/test/metatest.dart

Issue 524153002: Sharing metatest logic between unittest and scheduled_test (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: status fixes Created 6 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: pkg/scheduled_test/test/metatest.dart
diff --git a/pkg/scheduled_test/test/metatest.dart b/pkg/scheduled_test/test/metatest.dart
deleted file mode 100644
index 3c0cd9acb39f8189e56dcc284fac685d4ddd79f9..0000000000000000000000000000000000000000
--- a/pkg/scheduled_test/test/metatest.dart
+++ /dev/null
@@ -1,240 +0,0 @@
-// Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file
-// for details. All rights reserved. Use of this source code is governed by a
-// BSD-style license that can be found in the LICENSE file.
-
-/// A test library for testing test libraries? We must go deeper.
-///
-/// Since unit testing code tends to use a lot of global state, it can be tough
-/// to test. This library manages it by running each test case in a child
-/// isolate, then reporting the results back to the parent isolate.
-library metatest;
-
-import 'dart:async';
-import 'dart:io';
-import 'dart:isolate';
-
-import 'package:path/path.dart' as path;
-import 'package:unittest/unittest.dart';
-import 'package:scheduled_test/scheduled_test.dart' as scheduled_test;
-
-import 'utils.dart';
-
-/// Declares a test with the given [description] and [body]. [body] corresponds
-/// to the `main` method of a test file, and will be run in an isolate. By
-/// default, this expects that all tests defined in [body] pass, but if
-/// [passing] is passed, only tests listed there are expected to pass.
-void expectTestsPass(String description, void body(), {List<String> passing}) {
- _setUpTest(description, body, (results) {
- if (_hasError(results)) {
- throw 'Expected all tests to pass, but got error(s):\n'
- '${_summarizeTests(results)}';
- } else if (passing == null) {
- if (results['failed'] != 0) {
- throw 'Expected all tests to pass, but some failed:\n'
- '${_summarizeTests(results)}';
- }
- } else {
- var shouldPass = new Set.from(passing);
- var didPass = new Set.from(results['results']
- .where((t) => t['result'] == 'pass')
- .map((t) => t['description']));
-
- if (!shouldPass.containsAll(didPass) ||
- !didPass.containsAll(shouldPass)) {
- String stringify(Set<String> tests) =>
- '{${tests.map((t) => '"$t"').join(', ')}}';
-
- fail('Expected exactly ${stringify(shouldPass)} to pass, but '
- '${stringify(didPass)} passed.\n'
- '${_summarizeTests(results)}');
- }
- }
- });
-}
-
-/// Declares a test with the given [description] and [body].
-///
-/// [body] corresponds
-/// to the `main` method of a test file, and will be run in an isolate.
-///
-/// All tests must have an expected result in [expectedResults].
-void expectTests(String description, void body(),
- Map<String, String> expectedResults) {
- _setUpTest(description, body, (results) {
- expectedResults = new Map.from(expectedResults);
-
- for (var testResult in results['results']) {
- var description = testResult['description'];
-
- expect(expectedResults, contains(description),
- reason: '"$description" did not have an expected result set.\n'
- '${_summarizeTests(results)}');
-
- var result = testResult['result'];
-
- expect(expectedResults, containsPair(description, result),
- reason: 'The test "$description" not not have the expected result.\n'
- '${_summarizeTests(results)}');
-
- expectedResults.remove(description);
- }
- expect(expectedResults, isEmpty,
- reason: 'Unexpected additional test results\n'
- '${_summarizeTests(results)}');
- });
-}
-
-/// Declares a test with the given [description] and [body]. [body] corresponds
-/// to the `main` method of a test file, and will be run in an isolate. Expects
-/// all tests defined by [body] to fail.
-void expectTestsFail(String description, void body()) {
- _setUpTest(description, body, (results) {
- if (_hasError(results)) {
- throw 'Expected all tests to fail, but got error(s):\n'
- '${_summarizeTests(results)}';
- } else if (results['passed'] != 0) {
- throw 'Expected all tests to fail, but some passed:\n'
- '${_summarizeTests(results)}';
- }
- });
-}
-
-/// Runs [setUpFn] before every metatest. Note that [setUpFn] will be
-/// overwritten if the test itself calls [setUp].
-void metaSetUp(void setUpFn()) {
- if (_inChildIsolate) scheduled_test.setUp(setUpFn);
-}
-
-/// Sets up a test with the given [description] and [body]. After the test runs,
-/// calls [validate] with the result map.
-void _setUpTest(String description, void body(), void validate(Map)) {
- if (_inChildIsolate) {
- _ensureInitialized();
- if (_testToRun == description) body();
- } else {
- test(description, () {
- expect(_runInIsolate(description).then(validate), completes);
- });
- }
-}
-
-/// The description of the test to run in the child isolate.
-///
-/// `null` in the parent isolate.
-String _testToRun;
-
-/// The port with which the child isolate should communicate with the parent
-/// isolate.
-///
-/// `null` in the parent isolate.
-SendPort _replyTo;
-
-/// Whether or not we're running in a child isolate that's supposed to run a
-/// test.
-bool _inChildIsolate;
-
-/// Initialize metatest.
-///
-/// [message] should be the second argument to [main]. It's used to determine
-/// whether this test is in the parent isolate or a child isolate.
-void initMetatest(message) {
- if (message == null) {
- _inChildIsolate = false;
- } else {
- _testToRun = message['testToRun'];
- _replyTo = message['replyTo'];
- _inChildIsolate = true;
- }
-}
-
-/// Runs the test described by [description] in its own isolate. Returns a map
-/// describing the results of that test run.
-Future<Map> _runInIsolate(String description) {
- var replyPort = new ReceivePort();
- // TODO(nweiz): Don't use path here once issue 8440 is fixed.
- var uri = path.toUri(path.absolute(path.fromUri(Platform.script)));
- return Isolate.spawnUri(uri, [], {
- 'testToRun': description,
- 'replyTo': replyPort.sendPort
- }).then((_) {
- // TODO(nweiz): Remove this timeout once issue 8875 is fixed and we can
- // capture top-level exceptions.
- return timeout(replyPort.first, 30 * 1000, () {
- throw 'Timed out waiting for test to complete.';
- });
- });
-}
-
-/// Returns whether [results] (a test result map) describes a test run in which
-/// an error occurred.
-bool _hasError(Map results) {
- return results['errors'] > 0 || results['uncaughtError'] != null ||
- (results['passed'] == 0 && results['failed'] == 0);
-}
-
-/// Returns a string description of the test run descibed by [results].
-String _summarizeTests(Map results) {
- var buffer = new StringBuffer();
- for (var t in results["results"]) {
- buffer.writeln("${t['result'].toUpperCase()}: ${t['description']}");
- if (t['message'] != '') buffer.writeln("${_indent(t['message'])}");
- if (t['stackTrace'] != null && t['stackTrace'] != '') {
- buffer.writeln("${_indent(t['stackTrace'])}");
- }
- }
-
- buffer.writeln();
-
- var success = false;
- if (results['passed'] == 0 && results['failed'] == 0 &&
- results['errors'] == 0 && results['uncaughtError'] == null) {
- buffer.write('No tests found.');
- // This is considered a failure too.
- } else if (results['failed'] == 0 && results['errors'] == 0 &&
- results['uncaughtError'] == null) {
- buffer.write('All ${results['passed']} tests passed.');
- success = true;
- } else {
- if (results['uncaughtError'] != null) {
- buffer.write('Top-level uncaught error: ${results['uncaughtError']}');
- }
- buffer.write('${results['passed']} PASSED, ${results['failed']} FAILED, '
- '${results['errors']} ERRORS');
- }
- return prefixLines(buffer.toString());
-}
-
-/// Indents each line of [str] by two spaces.
-String _indent(String str) {
- return str.replaceAll(new RegExp("^", multiLine: true), " ");
-}
-
-/// Ensure that the metatest configuration is loaded.
-void _ensureInitialized() {
- unittestConfiguration = _singleton;
-}
-
-final _singleton = new _MetaConfiguration();
-
-/// Special test configuration for use within the child isolates. This hides all
-/// output and reports data back to the parent isolate.
-class _MetaConfiguration extends Configuration {
-
- _MetaConfiguration() : super.blank();
-
- void onSummary(int passed, int failed, int errors, List<TestCase> results,
- String uncaughtError) {
- _replyTo.send({
- "passed": passed,
- "failed": failed,
- "errors": errors,
- "uncaughtError": uncaughtError,
- "results": results.map((testCase) => {
- "description": testCase.description,
- "message": testCase.message,
- "result": testCase.result,
- "stackTrace": testCase.stackTrace.toString()
- }).toList()
- });
- }
-}
« no previous file with comments | « pkg/scheduled_test/test/descriptor/pattern_test.dart ('k') | pkg/scheduled_test/test/scheduled_future_matchers_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698