OLD | NEW |
---|---|
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 /** | 5 /** |
6 * List of test cases. | |
7 * @type {Array.<strring>} List of function names for tests to run. | |
arv (Not doing code reviews)
2013/06/20 17:45:15
Do we want to hide these in an IIFE?
kevers
2013/06/20 18:22:12
Done.
| |
8 */ | |
9 var testCases = []; | |
10 | |
11 /** | |
12 * Indicates if all tests have run successfully. | |
13 * @type {boolean} | |
14 */ | |
15 var cleanTestRun = true; | |
16 | |
17 /** | |
18 * Armed during setup of a test to call the matching tear down code. | |
19 * @type {?Function} | |
20 */ | |
21 var pendingTearDown = null; | |
22 | |
23 /** | |
6 * Tests that an observation matches the expected value. | 24 * Tests that an observation matches the expected value. |
7 * @param {Object} expected The expected value. | 25 * @param {Object} expected The expected value. |
8 * @param {Object} observed The actual value. | 26 * @param {Object} observed The actual value. |
9 * @param {string=} opt_message Optional message to include with a test | 27 * @param {string=} opt_message Optional message to include with a test |
10 * failure. | 28 * failure. |
11 */ | 29 */ |
12 function assertEquals(expected, observed, opt_message) { | 30 function assertEquals(expected, observed, opt_message) { |
13 if (observed !== expected) { | 31 if (observed !== expected) { |
14 var message = 'Assertion Failed\n Observed: ' + observed + | 32 var message = 'Assertion Failed\n Observed: ' + observed + |
15 '\n Expected: ' + expected; | 33 '\n Expected: ' + expected; |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
50 if (observed === reference) { | 68 if (observed === reference) { |
51 var message = 'Assertion Failed\n Observed: ' + observed + | 69 var message = 'Assertion Failed\n Observed: ' + observed + |
52 '\n Reference: ' + reference; | 70 '\n Reference: ' + reference; |
53 if (opt_message) | 71 if (opt_message) |
54 message = message + '\n ' + opt_message; | 72 message = message + '\n ' + opt_message; |
55 throw new Error(message); | 73 throw new Error(message); |
56 } | 74 } |
57 } | 75 } |
58 | 76 |
59 /** | 77 /** |
60 * Verifies that a test evaluation results in an assertion failure. | 78 * Verifies that a test evaluation results in an exception. |
61 * @param {!Function} f The test function. | 79 * @param {!Function} f The test function. |
62 */ | 80 */ |
63 function assertThrows(f) { | 81 function assertThrows(f) { |
64 var triggeredError = false; | 82 var triggeredError = false; |
65 try { | 83 try { |
66 f(); | 84 f(); |
67 } catch(err) { | 85 } catch(err) { |
68 triggeredError = true; | 86 triggeredError = true; |
69 } | 87 } |
70 if (!triggeredError) | 88 if (!triggeredError) |
71 throw new Error('Assertion Failed: throw expected.'); | 89 throw new Error('Assertion Failed: throw expected.'); |
72 } | 90 } |
73 | 91 |
74 /** | 92 /** |
93 * Verifies that the contents of the expected and observed arrays match. | |
94 * @param {Array} expected The expected result. | |
95 * @param {Array} observed The actual result. | |
96 */ | |
97 function assertArrayEquals(expected, observed) { | |
98 var v1 = Array.prototype.slice.call(expected); | |
99 var v2 = Array.prototype.slice.call(observed); | |
100 var equal = v1.length == v2.length; | |
101 if (equal) { | |
102 for (var i = 0; i < v1.length; i++) { | |
103 if (v1[i] !== v2[i]) { | |
104 equal = false; | |
105 break; | |
106 } | |
107 } | |
108 } | |
109 if (!equal) { | |
110 var message = 'Assertion Failed\n Observed: ' + v1 + | |
111 '\n Expected: ' + v2; | |
112 throw new Error(message); | |
113 } | |
114 } | |
115 | |
116 /** | |
75 * Runs all functions starting with test and reports success or | 117 * Runs all functions starting with test and reports success or |
76 * failure of the test suite. | 118 * failure of the test suite. |
77 */ | 119 */ |
78 function runTests() { | 120 function runTests() { |
79 var tests = []; | |
80 | |
81 if (window.setUp) | |
82 window.setUp(); | |
83 | |
84 var success = true; | |
85 for (var name in window) { | 121 for (var name in window) { |
86 if (typeof window[name] == 'function' && /^test/.test(name)) | 122 if (typeof window[name] == 'function' && /^test/.test(name)) |
87 tests.push(name); | 123 testCases.push(name); |
88 } | 124 } |
89 if (!tests.length) { | 125 if (!testCases.length) { |
90 console.error('\nFailed to find test cases.'); | 126 console.error('\nFailed to find test cases.'); |
91 success = false; | 127 cleanTestRun = false; |
92 } | 128 } |
93 for (var i = 0; i < tests.length; i++) { | 129 continueTesting(); |
94 try { | |
95 window[tests[i]](); | |
96 } catch (err) { | |
97 console.error('Failure in test ' + tests[i] + '\n' + err); | |
98 console.log(err.stack); | |
99 success = false; | |
100 } | |
101 } | |
102 | |
103 if (window.tearDown) | |
104 window.tearDown(); | |
105 | |
106 endTests(success); | |
107 } | 130 } |
108 | 131 |
109 /** | 132 /** |
133 * Runs the next test in the queue. Reports the test results if the queue is | |
134 * empty. | |
135 */ | |
136 function continueTesting() { | |
arv (Not doing code reviews)
2013/06/20 17:45:15
Does continueTesting need to be a global?
kevers
2013/06/20 18:22:12
Mo longer a global.
| |
137 if (pendingTearDown) { | |
138 pendingTearDown(); | |
139 pendingTearDown = null; | |
140 } | |
141 if(testCases.length > 0) { | |
arv (Not doing code reviews)
2013/06/20 17:45:15
if (
kevers
2013/06/20 18:22:12
Done.
| |
142 var fn = testCases.pop(); | |
143 var isAsyncTest = /Async$/.test(fn); | |
arv (Not doing code reviews)
2013/06/20 17:45:15
I still prefer fn.length instead
kevers
2013/06/20 18:22:12
Done.
| |
144 try { | |
145 if (window.setUp) | |
146 window.setUp(); | |
147 pendingTearDown = window.tearDown; | |
148 window[fn](continueTesting); | |
149 } catch(err) { | |
150 console.error('Failure in test ' + fn + '\n' + err); | |
151 console.log(err.stack); | |
152 cleanTestRun = false; | |
153 } | |
154 // Asynchronous tests must manually call continueTesting when complete. | |
155 if (!isAsyncTest) | |
156 continueTesting(); | |
157 } else { | |
158 endTests(cleanTestRun); | |
159 } | |
160 if (testCases.length) { | |
161 domAutomationController.setAutomationId(1); | |
162 domAutomationController.send('PENDING'); | |
163 } | |
164 } | |
165 | |
166 /** | |
110 * Signals completion of a test. | 167 * Signals completion of a test. |
111 * @param {boolean} success Indicates if the test completed successfully. | 168 * @param {boolean} success Indicates if the test completed successfully. |
112 */ | 169 */ |
113 function endTests(success) { | 170 function endTests(success) { |
114 domAutomationController.setAutomationId(1); | 171 domAutomationController.setAutomationId(1); |
115 domAutomationController.send(success ? 'SUCCESS' : 'FAILURE'); | 172 domAutomationController.send(success ? 'SUCCESS' : 'FAILURE'); |
116 } | 173 } |
117 | 174 |
118 window.onerror = function() { | 175 window.onerror = function() { |
119 endTests(false); | 176 endTests(false); |
120 }; | 177 }; |
OLD | NEW |