OLD | NEW |
| (Empty) |
1 // Copyright (c) 2011, 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 /** | |
6 * testcase.dart: this file is sourced by unittest.dart. It defines [TestCase] | |
7 * and assumes unittest defines the type [TestFunction]. | |
8 */ | |
9 | |
10 /** Summarizes information about a single test case. */ | |
11 | |
12 part of unittest; | |
13 | |
14 class TestCase { | |
15 /** Identifier for this test. */ | |
16 final int id; | |
17 | |
18 /** A description of what the test is specifying. */ | |
19 final String description; | |
20 | |
21 /** The setup function to call before the test, if any. */ | |
22 Function _setUp; | |
23 | |
24 Function get setUp => _setUp; | |
25 set setUp(Function value) => _setUp = value; | |
26 | |
27 /** The teardown function to call after the test, if any. */ | |
28 Function _tearDown; | |
29 | |
30 Function get tearDown => _tearDown; | |
31 set tearDown(Function value) => _tearDown = value; | |
32 | |
33 /** The body of the test case. */ | |
34 TestFunction test; | |
35 | |
36 /** | |
37 * Remaining number of callbacks functions that must reach a 'done' state | |
38 * to wait for before the test completes. | |
39 */ | |
40 int callbackFunctionsOutstanding; | |
41 | |
42 /** Error or failure message. */ | |
43 String message = ''; | |
44 | |
45 /** | |
46 * One of [PASS], [FAIL], [ERROR], or [null] if the test hasn't run yet. | |
47 */ | |
48 String result; | |
49 | |
50 /** Stack trace associated with this test, or null if it succeeded. */ | |
51 String stackTrace; | |
52 | |
53 /** The group (or groups) under which this test is running. */ | |
54 final String currentGroup; | |
55 | |
56 Date startTime; | |
57 | |
58 Duration runningTime; | |
59 | |
60 bool enabled = true; | |
61 | |
62 bool _doneTeardown = false; | |
63 | |
64 TestCase(this.id, this.description, this.test, | |
65 this.callbackFunctionsOutstanding) | |
66 : currentGroup = _currentGroup, | |
67 _setUp = _testSetup, | |
68 _tearDown = _testTeardown; | |
69 | |
70 bool get isComplete => !enabled || result != null; | |
71 | |
72 void run() { | |
73 if (enabled) { | |
74 result = stackTrace = null; | |
75 message = ''; | |
76 _doneTeardown = false; | |
77 if (_setUp != null) { | |
78 _setUp(); | |
79 } | |
80 _config.onTestStart(this); | |
81 startTime = new Date.now(); | |
82 runningTime = null; | |
83 test(); | |
84 } | |
85 } | |
86 | |
87 void _complete() { | |
88 if (runningTime == null) { | |
89 // TODO(gram): currently the duration measurement code is blocked | |
90 // by issue 4437. When that is fixed replace the line below with: | |
91 // runningTime = new Date.now().difference(startTime); | |
92 runningTime = new Duration(milliseconds: 0); | |
93 } | |
94 if (!_doneTeardown) { | |
95 if (_tearDown != null) { | |
96 _tearDown(); | |
97 } | |
98 _doneTeardown = true; | |
99 } | |
100 _config.onTestResult(this); | |
101 } | |
102 | |
103 void pass() { | |
104 result = PASS; | |
105 _complete(); | |
106 } | |
107 | |
108 void fail(String messageText, String stack) { | |
109 if (result != null) { | |
110 if (result == PASS) { | |
111 error('Test failed after initially passing: $messageText', stack); | |
112 } else if (result == FAIL) { | |
113 error('Test failed more than once: $messageText', stack); | |
114 } | |
115 } else { | |
116 result = FAIL; | |
117 message = messageText; | |
118 stackTrace = stack; | |
119 _complete(); | |
120 } | |
121 } | |
122 | |
123 void error(String messageText, String stack) { | |
124 result = ERROR; | |
125 message = messageText; | |
126 stackTrace = stack; | |
127 _complete(); | |
128 } | |
129 } | |
OLD | NEW |