OLD | NEW |
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, 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 import 'dart:async'; | 5 import 'dart:async'; |
6 | 6 |
7 import 'package:test/test.dart'; | 7 import 'package:test/test.dart'; |
8 | 8 |
9 import '../../utils.dart'; | 9 import '../../utils.dart'; |
10 | 10 |
11 void main() { | 11 void main() { |
12 group("synchronous", () { | 12 group("synchronous", () { |
13 test("passes with an expected print", () { | 13 test("passes with an expected print", () { |
14 expect(() => print("Hello, world!"), prints("Hello, world!\n")); | 14 expect(() => print("Hello, world!"), prints("Hello, world!\n")); |
15 }); | 15 }); |
16 | 16 |
17 test("combines multiple prints", () { | 17 test("combines multiple prints", () { |
18 expect(() { | 18 expect(() { |
19 print("Hello"); | 19 print("Hello"); |
20 print("World!"); | 20 print("World!"); |
21 }, prints("Hello\nWorld!\n")); | 21 }, prints("Hello\nWorld!\n")); |
22 }); | 22 }); |
23 | 23 |
24 test("works with a Matcher", () { | 24 test("works with a Matcher", () { |
25 expect(() => print("Hello, world!"), prints(contains("Hello"))); | 25 expect(() => print("Hello, world!"), prints(contains("Hello"))); |
26 }); | 26 }); |
27 | 27 |
28 test("describes a failure nicely", () { | 28 test("describes a failure nicely", () { |
29 return runTest(() { | 29 return runTestBody(() { |
30 expect(() => print("Hello, world!"), prints("Goodbye, world!\n")); | 30 expect(() => print("Hello, world!"), prints("Goodbye, world!\n")); |
31 }).then((liveTest) { | 31 }).then((liveTest) { |
32 expectTestFailed(liveTest, | 32 expectTestFailed(liveTest, |
33 "Expected: prints 'Goodbye, world!\\n'\n" | 33 "Expected: prints 'Goodbye, world!\\n'\n" |
34 " ''\n" | 34 " ''\n" |
35 " Actual: <$closureString>\n" | 35 " Actual: <$closureString>\n" |
36 " Which: printed 'Hello, world!\\n'\n" | 36 " Which: printed 'Hello, world!\\n'\n" |
37 " ''\n" | 37 " ''\n" |
38 " Which: is different.\n" | 38 " Which: is different.\n" |
39 "Expected: Goodbye, w ...\n" | 39 "Expected: Goodbye, w ...\n" |
40 " Actual: Hello, wor ...\n" | 40 " Actual: Hello, wor ...\n" |
41 " ^\n" | 41 " ^\n" |
42 " Differ at offset 0\n"); | 42 " Differ at offset 0\n"); |
43 }); | 43 }); |
44 }); | 44 }); |
45 | 45 |
46 test("describes a failure with a non-descriptive Matcher nicely", () { | 46 test("describes a failure with a non-descriptive Matcher nicely", () { |
47 return runTest(() { | 47 return runTestBody(() { |
48 expect(() => print("Hello, world!"), prints(contains("Goodbye"))); | 48 expect(() => print("Hello, world!"), prints(contains("Goodbye"))); |
49 }).then((liveTest) { | 49 }).then((liveTest) { |
50 expectTestFailed(liveTest, | 50 expectTestFailed(liveTest, |
51 "Expected: prints contains 'Goodbye'\n" | 51 "Expected: prints contains 'Goodbye'\n" |
52 " Actual: <$closureString>\n" | 52 " Actual: <$closureString>\n" |
53 " Which: printed 'Hello, world!\\n'\n" | 53 " Which: printed 'Hello, world!\\n'\n" |
54 " ''\n"); | 54 " ''\n"); |
55 }); | 55 }); |
56 }); | 56 }); |
57 | 57 |
58 test("describes a failure with no text nicely", () { | 58 test("describes a failure with no text nicely", () { |
59 return runTest(() { | 59 return runTestBody(() { |
60 expect(() {}, prints(contains("Goodbye"))); | 60 expect(() {}, prints(contains("Goodbye"))); |
61 }).then((liveTest) { | 61 }).then((liveTest) { |
62 expectTestFailed(liveTest, | 62 expectTestFailed(liveTest, |
63 "Expected: prints contains 'Goodbye'\n" | 63 "Expected: prints contains 'Goodbye'\n" |
64 " Actual: <$closureString>\n" | 64 " Actual: <$closureString>\n" |
65 " Which: printed nothing.\n"); | 65 " Which: printed nothing.\n"); |
66 }); | 66 }); |
67 }); | 67 }); |
68 | 68 |
69 test("with a non-function", () { | 69 test("with a non-function", () { |
70 return runTest(() { | 70 return runTestBody(() { |
71 expect(10, prints(contains("Goodbye"))); | 71 expect(10, prints(contains("Goodbye"))); |
72 }).then((liveTest) { | 72 }).then((liveTest) { |
73 expectTestFailed(liveTest, | 73 expectTestFailed(liveTest, |
74 "Expected: prints contains 'Goodbye'\n" | 74 "Expected: prints contains 'Goodbye'\n" |
75 " Actual: <10>\n"); | 75 " Actual: <10>\n"); |
76 }); | 76 }); |
77 }); | 77 }); |
78 }); | 78 }); |
79 | 79 |
80 group('asynchronous', () { | 80 group('asynchronous', () { |
81 test("passes with an expected print", () { | 81 test("passes with an expected print", () { |
82 expect(() => new Future(() => print("Hello, world!")), | 82 expect(() => new Future(() => print("Hello, world!")), |
83 prints("Hello, world!\n")); | 83 prints("Hello, world!\n")); |
84 }); | 84 }); |
85 | 85 |
86 test("combines multiple prints", () { | 86 test("combines multiple prints", () { |
87 expect(() => new Future(() { | 87 expect(() => new Future(() { |
88 print("Hello"); | 88 print("Hello"); |
89 print("World!"); | 89 print("World!"); |
90 }), prints("Hello\nWorld!\n")); | 90 }), prints("Hello\nWorld!\n")); |
91 }); | 91 }); |
92 | 92 |
93 test("works with a Matcher", () { | 93 test("works with a Matcher", () { |
94 expect(() => new Future(() => print("Hello, world!")), | 94 expect(() => new Future(() => print("Hello, world!")), |
95 prints(contains("Hello"))); | 95 prints(contains("Hello"))); |
96 }); | 96 }); |
97 | 97 |
98 test("describes a failure nicely", () { | 98 test("describes a failure nicely", () { |
99 return runTest(() { | 99 return runTestBody(() { |
100 expect(() => new Future(() => print("Hello, world!")), | 100 expect(() => new Future(() => print("Hello, world!")), |
101 prints("Goodbye, world!\n")); | 101 prints("Goodbye, world!\n")); |
102 }).then((liveTest) { | 102 }).then((liveTest) { |
103 expectTestFailed(liveTest, startsWith( | 103 expectTestFailed(liveTest, startsWith( |
104 "Expected: prints 'Goodbye, world!\\n'\n" | 104 "Expected: prints 'Goodbye, world!\\n'\n" |
105 " ''\n" | 105 " ''\n" |
106 " Actual: <$closureString>\n" | 106 " Actual: <$closureString>\n" |
107 " Which: printed 'Hello, world!\\n'\n" | 107 " Which: printed 'Hello, world!\\n'\n" |
108 " ''\n" | 108 " ''\n" |
109 " Which: is different.\n" | 109 " Which: is different.\n" |
110 "Expected: Goodbye, w ...\n" | 110 "Expected: Goodbye, w ...\n" |
111 " Actual: Hello, wor ...\n" | 111 " Actual: Hello, wor ...\n" |
112 " ^\n" | 112 " ^\n" |
113 " Differ at offset 0")); | 113 " Differ at offset 0")); |
114 }); | 114 }); |
115 }); | 115 }); |
116 | 116 |
117 test("describes a failure with a non-descriptive Matcher nicely", () { | 117 test("describes a failure with a non-descriptive Matcher nicely", () { |
118 return runTest(() { | 118 return runTestBody(() { |
119 expect(() => new Future(() => print("Hello, world!")), | 119 expect(() => new Future(() => print("Hello, world!")), |
120 prints(contains("Goodbye"))); | 120 prints(contains("Goodbye"))); |
121 }).then((liveTest) { | 121 }).then((liveTest) { |
122 expectTestFailed(liveTest, startsWith( | 122 expectTestFailed(liveTest, startsWith( |
123 "Expected: prints contains 'Goodbye'\n" | 123 "Expected: prints contains 'Goodbye'\n" |
124 " Actual: <$closureString>\n" | 124 " Actual: <$closureString>\n" |
125 " Which: printed 'Hello, world!\\n'\n" | 125 " Which: printed 'Hello, world!\\n'\n" |
126 " ''")); | 126 " ''")); |
127 }); | 127 }); |
128 }); | 128 }); |
129 | 129 |
130 test("describes a failure with no text nicely", () { | 130 test("describes a failure with no text nicely", () { |
131 return runTest(() { | 131 return runTestBody(() { |
132 expect(() => new Future.value(), prints(contains("Goodbye"))); | 132 expect(() => new Future.value(), prints(contains("Goodbye"))); |
133 }).then((liveTest) { | 133 }).then((liveTest) { |
134 expectTestFailed(liveTest, startsWith( | 134 expectTestFailed(liveTest, startsWith( |
135 "Expected: prints contains 'Goodbye'\n" | 135 "Expected: prints contains 'Goodbye'\n" |
136 " Actual: <$closureString>\n" | 136 " Actual: <$closureString>\n" |
137 " Which: printed nothing.")); | 137 " Which: printed nothing.")); |
138 }); | 138 }); |
139 }); | 139 }); |
140 | 140 |
141 test("won't let the test end until the Future completes", () { | 141 test("won't let the test end until the Future completes", () { |
142 return expectTestBlocks(() { | 142 return expectTestBlocks(() { |
143 var completer = new Completer(); | 143 var completer = new Completer(); |
144 expect(() => completer.future, prints(isEmpty)); | 144 expect(() => completer.future, prints(isEmpty)); |
145 return completer; | 145 return completer; |
146 }, (completer) => completer.complete()); | 146 }, (completer) => completer.complete()); |
147 }); | 147 }); |
148 }); | 148 }); |
149 } | 149 } |
OLD | NEW |