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 * Tests that an observation matches the expected value. | 6 * Tests that an observation matches the expected value. |
7 * @param {Object} expected The expected value. | 7 * @param {Object} expected The expected value. |
8 * @param {Object} observed The actual value. | 8 * @param {Object} observed The actual value. |
9 * @param {string=} opt_message Optional message to include with a test | 9 * @param {string=} opt_message Optional message to include with a test |
10 * failure. | 10 * failure. |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
50 if (observed === reference) { | 50 if (observed === reference) { |
51 var message = 'Assertion Failed\n Observed: ' + observed + | 51 var message = 'Assertion Failed\n Observed: ' + observed + |
52 '\n Reference: ' + reference; | 52 '\n Reference: ' + reference; |
53 if (opt_message) | 53 if (opt_message) |
54 message = message + '\n ' + opt_message; | 54 message = message + '\n ' + opt_message; |
55 throw new Error(message); | 55 throw new Error(message); |
56 } | 56 } |
57 } | 57 } |
58 | 58 |
59 /** | 59 /** |
60 * Verifies that a test evaluation results in an assertion failure. | 60 * Verifies that a test evaluation results in an exception. |
61 * @param {!Function} f The test function. | 61 * @param {!Function} f The test function. |
62 */ | 62 */ |
63 function assertThrows(f) { | 63 function assertThrows(f) { |
64 var triggeredError = false; | 64 var triggeredError = false; |
65 try { | 65 try { |
66 f(); | 66 f(); |
67 } catch(err) { | 67 } catch(err) { |
68 triggeredError = true; | 68 triggeredError = true; |
69 } | 69 } |
70 if (!triggeredError) | 70 if (!triggeredError) |
71 throw new Error('Assertion Failed: throw expected.'); | 71 throw new Error('Assertion Failed: throw expected.'); |
72 } | 72 } |
73 | 73 |
74 /** | 74 /** |
75 * Verifies that a function evaluation does not throw an exception. | |
76 * @param {!Function} The test function. | |
77 */ | |
arv (Not doing code reviews)
2013/06/20 14:26:24
This is generally not a useful assertion function
kevers
2013/06/20 14:59:28
Removed.
| |
78 function assertNotThrows(f) { | |
79 var triggeredError = false; | |
80 try { | |
81 f(); | |
82 } catch(err) { | |
83 triggeredError = true; | |
84 console.log(err.stack); | |
85 } | |
86 if (triggeredError) | |
87 throw new Error('Assertion Failed: exception thrown.'); | |
88 } | |
89 | |
90 /** | |
91 * Verifies that the contents of the expected and observed arrays match. | |
92 * @param {Array.<Object>} expected The expected result. | |
arv (Not doing code reviews)
2013/06/20 14:26:24
{Array} since the array can contain any value
kevers
2013/06/20 14:59:28
Done.
| |
93 * @param {Array.<Object>} observed The actual result. | |
94 */ | |
95 function assertArrayEquals(expected, observed) { | |
96 var v1 = Array.prototype.concat.call(expected); | |
97 var v2 = Array.prototype.concat.call(observed); | |
arv (Not doing code reviews)
2013/06/20 14:26:24
not concat... use slice. slice is generic, concat
kevers
2013/06/20 14:59:28
Done.
| |
98 var equal = v1.length == v2.length; | |
99 if (equal) { | |
100 for (var i = 0; i < v1.length; i++) { | |
101 if (v1[i] !== v2[i]) { | |
102 equal = false; | |
103 break; | |
104 } | |
105 } | |
106 } | |
107 if (!equal) { | |
108 var message = 'Assertion Failed\n Observed: ' + v2 + | |
109 '\n Expected: ' + v1; | |
110 throw new Error(message); | |
111 } | |
112 } | |
113 | |
114 /** | |
75 * Runs all functions starting with test and reports success or | 115 * Runs all functions starting with test and reports success or |
76 * failure of the test suite. | 116 * failure of the test suite. |
77 */ | 117 */ |
78 function runTests() { | 118 function runTests() { |
79 var tests = []; | 119 var tests = []; |
80 | |
81 if (window.setUp) | |
82 window.setUp(); | |
83 | |
84 var success = true; | 120 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 tests.push(name); |
88 } | 124 } |
89 if (!tests.length) { | 125 if (!tests.length) { |
90 console.error('\nFailed to find test cases.'); | 126 console.error('\nFailed to find test cases.'); |
91 success = false; | 127 success = false; |
92 } | 128 } |
93 for (var i = 0; i < tests.length; i++) { | 129 for (var i = 0; i < tests.length; i++) { |
94 try { | 130 try { |
131 if (window.setUp) | |
132 window.setUp(); | |
95 window[tests[i]](); | 133 window[tests[i]](); |
96 } catch (err) { | 134 } catch (err) { |
97 console.error('Failure in test ' + tests[i] + '\n' + err); | 135 console.error('Failure in test ' + tests[i] + '\n' + err); |
98 console.log(err.stack); | 136 console.log(err.stack); |
99 success = false; | 137 success = false; |
100 } | 138 } |
139 if (window.tearDown) | |
140 window.tearDown(); | |
101 } | 141 } |
102 | |
103 if (window.tearDown) | |
104 window.tearDown(); | |
105 | |
106 endTests(success); | 142 endTests(success); |
107 } | 143 } |
108 | 144 |
109 /** | 145 /** |
110 * Signals completion of a test. | 146 * Signals completion of a test. |
111 * @param {boolean} success Indicates if the test completed successfully. | 147 * @param {boolean} success Indicates if the test completed successfully. |
112 */ | 148 */ |
113 function endTests(success) { | 149 function endTests(success) { |
114 domAutomationController.setAutomationId(1); | 150 domAutomationController.setAutomationId(1); |
115 domAutomationController.send(success ? 'SUCCESS' : 'FAILURE'); | 151 domAutomationController.send(success ? 'SUCCESS' : 'FAILURE'); |
116 } | 152 } |
117 | 153 |
118 window.onerror = function() { | 154 window.onerror = function() { |
119 endTests(false); | 155 endTests(false); |
120 }; | 156 }; |
OLD | NEW |