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 unittest.loader; |
| 6 |
| 7 import 'dart:io'; |
| 8 |
| 9 import 'package:path/path.dart' as p; |
| 10 import 'package:unittest/src/loader.dart'; |
| 11 import 'package:unittest/src/state.dart'; |
| 12 import 'package:unittest/unittest.dart'; |
| 13 |
| 14 import 'utils.dart'; |
| 15 |
| 16 Loader _loader; |
| 17 String _sandbox; |
| 18 |
| 19 final _tests = """ |
| 20 import 'dart:async'; |
| 21 |
| 22 import 'package:unittest/unittest.dart'; |
| 23 |
| 24 void main() { |
| 25 var declarer = Zone.current[#unittest.declarer]; |
| 26 declarer.test("success", () {}); |
| 27 declarer.test("failure", () => throw new TestFailure('oh no')); |
| 28 declarer.test("error", () => throw 'oh no'); |
| 29 } |
| 30 """; |
| 31 |
| 32 void main() { |
| 33 setUp(() { |
| 34 _loader = new Loader(packageRoot: p.join(packageDir, 'packages')); |
| 35 _sandbox = Directory.systemTemp.createTempSync('unittest_').path; |
| 36 }); |
| 37 |
| 38 tearDown(() { |
| 39 new Directory(_sandbox).deleteSync(recursive: true); |
| 40 return _loader.close(); |
| 41 }); |
| 42 |
| 43 group(".loadFile()", () { |
| 44 var suite; |
| 45 setUp(() { |
| 46 /// TODO(nweiz): Use scheduled_test for this once it's compatible with |
| 47 /// this version of unittest. |
| 48 new File(p.join(_sandbox, 'a_test.dart')).writeAsStringSync(_tests); |
| 49 return _loader.loadFile(p.join(_sandbox, 'a_test.dart')) |
| 50 .then((suite_) => suite = suite_); |
| 51 }); |
| 52 |
| 53 test("returns a suite with a name matching the file path", () { |
| 54 expect(suite.name, equals(p.join(_sandbox, 'a_test.dart'))); |
| 55 }); |
| 56 |
| 57 test("returns tests with the correct names", () { |
| 58 expect(suite.tests, hasLength(3)); |
| 59 expect(suite.tests[0].name, equals("success")); |
| 60 expect(suite.tests[1].name, equals("failure")); |
| 61 expect(suite.tests[2].name, equals("error")); |
| 62 }); |
| 63 |
| 64 test("can load and run a successful test", () { |
| 65 var liveTest = suite.tests[0].load(suite); |
| 66 |
| 67 expectStates(liveTest, [ |
| 68 const State(Status.running, Result.success), |
| 69 const State(Status.complete, Result.success) |
| 70 ]); |
| 71 expectErrors(liveTest, []); |
| 72 |
| 73 return liveTest.run().whenComplete(() => liveTest.close()); |
| 74 }); |
| 75 |
| 76 test("can load and run a failing test", () { |
| 77 var liveTest = suite.tests[1].load(suite); |
| 78 expectSingleFailure(liveTest); |
| 79 return liveTest.run().whenComplete(() => liveTest.close()); |
| 80 }); |
| 81 |
| 82 test("throws a nice error if the package root doesn't exist", () { |
| 83 var loader = new Loader(); |
| 84 expect(() => loader.loadFile(p.join(_sandbox, 'a_test.dart')), |
| 85 throwsA(isFileSystemException( |
| 86 "Directory ${p.join(_sandbox, 'packages')} does not exist."))); |
| 87 }); |
| 88 }); |
| 89 |
| 90 group(".loadDir()", () { |
| 91 test("ignores non-Dart files", () { |
| 92 new File(p.join(_sandbox, 'a_test.txt')).writeAsStringSync(_tests); |
| 93 expect(_loader.loadDir(_sandbox), completion(isEmpty)); |
| 94 }); |
| 95 |
| 96 test("ignores files in packages/ directories", () { |
| 97 var dir = p.join(_sandbox, 'packages'); |
| 98 new Directory(dir).createSync(); |
| 99 new File(p.join(dir, 'a_test.dart')).writeAsStringSync(_tests); |
| 100 expect(_loader.loadDir(_sandbox), completion(isEmpty)); |
| 101 }); |
| 102 |
| 103 test("ignores files that don't end in _test.dart", () { |
| 104 new File(p.join(_sandbox, 'test.dart')).writeAsStringSync(_tests); |
| 105 expect(_loader.loadDir(_sandbox), completion(isEmpty)); |
| 106 }); |
| 107 |
| 108 group("with suites loaded from a directory", () { |
| 109 var suites; |
| 110 setUp(() { |
| 111 /// TODO(nweiz): Use scheduled_test for this once it's compatible with |
| 112 /// this version of unittest. |
| 113 new File(p.join(_sandbox, 'a_test.dart')).writeAsStringSync(_tests); |
| 114 new File(p.join(_sandbox, 'another_test.dart')) |
| 115 .writeAsStringSync(_tests); |
| 116 new Directory(p.join(_sandbox, 'dir')).createSync(); |
| 117 new File(p.join(_sandbox, 'dir/sub_test.dart')) |
| 118 .writeAsStringSync(_tests); |
| 119 |
| 120 return _loader.loadDir(_sandbox).then((suites_) => suites = suites_); |
| 121 }); |
| 122 |
| 123 test("names those suites after their files", () { |
| 124 expect(suites.map((suite) => suite.name), unorderedEquals([ |
| 125 p.join(_sandbox, 'a_test.dart'), |
| 126 p.join(_sandbox, 'another_test.dart'), |
| 127 p.join(_sandbox, 'dir/sub_test.dart') |
| 128 ])); |
| 129 }); |
| 130 |
| 131 test("can run tests in those suites", () { |
| 132 var suite = suites.firstWhere((suite) => suite.name.contains("a_test")); |
| 133 var liveTest = suite.tests[1].load(suite); |
| 134 expectSingleFailure(liveTest); |
| 135 return liveTest.run().whenComplete(() => liveTest.close()); |
| 136 }); |
| 137 }); |
| 138 }); |
| 139 } |
OLD | NEW |