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