1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 'use strict'; 6 7 base.require('base.unittest.test_error'); 8 base.exportTo('base.unittest', function() { 9 function forAllAssertMethodsIn_(prototype, fn) { 10 for (var fieldName in prototype) { 11 if (fieldName.indexOf('assert') != 0) 12 continue; 13 var fieldValue = prototype[fieldName]; 14 if (typeof fieldValue != 'function') 15 continue; 16 fn(fieldName, fieldValue); 17 } 18 } 19 20 var Assertions = {}; 21 Assertions.prototype = { 22 assertTrue: function(a, opt_message) { 23 if (a) 24 return; 25 var message = opt_message || 'Expected true, got ' + a; 26 throw new base.unittest.TestError(message); 27 }, 28 29 assertFalse: function(a, opt_message) { 30 if (!a) 31 return; 32 var message = opt_message || 'Expected false, got ' + a; 33 throw new base.unittest.TestError(message); 34 }, 35 36 assertUndefined: function(a, opt_message) { 37 if (a === undefined) 38 return; 39 var message = opt_message || 'Expected undefined, got ' + a; 40 throw new base.unittest.TestError(message); 41 }, 42 43 assertNotUndefined: function(a, opt_message) { 44 if (a !== undefined) 45 return; 46 var message = opt_message || 'Expected not undefined, got ' + a; 47 throw new base.unittest.TestError(message); 48 }, 49 50 assertNull: function(a, opt_message) { 51 if (a === null) 52 return; 53 var message = opt_message || 'Expected null, got ' + a; 54 throw new base.unittest.TestError(message); 55 }, 56 57 assertNotNull: function(a, opt_message) { 58 if (a !== null) 59 return; 60 var message = opt_message || 'Expected non-null, got ' + a; 61 throw new base.unittest.TestError(message); 62 }, 63 64 assertEquals: function(a, b, opt_message) { 65 if (a === b) 66 return; 67 var message = opt_message || 'Expected\n"' + JSON.stringify(a) + 68 '"\ngot\n"' + JSON.stringify(b) + '"'; 69 throw new base.unittest.TestError(message); 70 }, 71 72 assertNotEquals: function(a, b, opt_message) { 73 if (a !== b) 74 return; 75 var message = opt_message || 'Expected something not equal to ' + b; 76 throw new base.unittest.TestError(message); 77 }, 78 79 assertArrayEquals: function(a, b, opt_message) { 80 if (a.length === b.length) { 81 var ok = true; 82 for (var i = 0; i < a.length; i++) { 83 ok &= (a[i] === b[i]); 84 } 85 if (ok) 86 return; 87 } 88 89 var message = opt_message || 'Expected array ' + a + ', got array ' + b; 90 throw new base.unittest.TestError(message); 91 }, 92 93 assertArrayShallowEquals: function(a, b, opt_message) { 94 if (a.length === b.length) { 95 var ok = true; 96 for (var i = 0; i < a.length; i++) { 97 ok &= (a[i] === b[i]); 98 } 99 if (ok) 100 return; 101 } 102 103 var message = opt_message || 'Expected array ' + b + ', got array ' + a; 104 throw new base.unittest.TestError(message); 105 }, 106 107 assertAlmostEquals: function(a, b, opt_message) { 108 if (Math.abs(a - b) < 0.00001) 109 return; 110 var message = opt_message || 'Expected almost ' + a + ', got ' + b; 111 throw new base.unittest.TestError(message); 112 }, 113 114 assertVec2Equals: function(a, b, opt_message) { 115 if (a[0] === b[0] && 116 a[1] === b[1]) 117 return; 118 var message = opt_message || 'Expected (' + a[0] + ',' + a[1] + 119 ') but got (' + b[0] + ',' + b[1] + ')'; 120 throw new base.unittest.TestError(message); 121 }, 122 123 assertVec3Equals: function(a, b, opt_message) { 124 if (a[0] === b[0] && 125 a[1] === b[1] && 126 a[2] === b[2]) 127 return; 128 var message = opt_message || 'Expected ' + vec3.toString(a) + 129 ' but got ' + vec3.toString(b); 130 throw new base.unittest.TestError(message); 131 }, 132 133 assertQuadEquals: function(a, b, opt_message) { 134 var ok = true; 135 ok &= a.p1[0] === b.p1[0] && a.p1[1] === b.p1[1]; 136 ok &= a.p2[0] === b.p2[0] && a.p2[1] === b.p2[1]; 137 ok &= a.p3[0] === b.p3[0] && a.p3[1] === b.p3[1]; 138 ok &= a.p4[0] === b.p4[0] && a.p4[1] === b.p4[1]; 139 if (ok) 140 return; 141 var message = opt_message || 'Expected "' + a.toString() + 142 '", got "' + b.toString() + '"'; 143 throw new base.unittest.TestError(message); 144 }, 145 146 assertRectEquals: function(a, b, opt_message) { 147 var ok = true; 148 if (a.x === b.x && a.y === b.y && 149 a.width === b.width && a.height === b.height) { 150 return; 151 } 152 153 var message = opt_message || 'Expected "' + a.toString() + 154 '", got "' + b.toString() + '"'; 155 throw new base.unittest.TestError(message); 156 }, 157 158 assertObjectEquals: function(a, b, opt_message) { 159 var a_json = JSON.stringify(a); 160 var b_json = JSON.stringify(b); 161 if (a_json === b_json) 162 return; 163 var message = opt_message || 'Expected ' + a_json + ', got ' + b_json; 164 throw new base.unittest.TestError(message); 165 }, 166 167 assertThrows: function(fn, opt_message) { 168 try { 169 fn(); 170 } catch (e) { 171 return; 172 } 173 var message = opt_message || 'Expected throw from ' + fn; 174 throw new base.unittest.TestError(message); 175 }, 176 177 assertDoesNotThrow: function(fn, opt_message) { 178 try { 179 fn(); 180 } catch (e) { 181 var message = opt_message || 'Expected to not throw from ' + fn + 182 ' but got: ' + e; 183 throw new TestError(message); 184 } 185 }, 186 187 assertApproxEquals: function(a, b, opt_epsilon, opt_message) { 188 if (a === b) 189 return; 190 var epsilon = opt_epsilon || 0.000001; // 6 digits. 191 a = Math.abs(a); 192 b = Math.abs(b); 193 var relative_error = Math.abs(a - b) / (a + b); 194 if (relative_error < epsilon) 195 return; 196 var message = opt_message || 'Expect ' + a + ' and ' + b + 197 ' to be within ' + epsilon + ' was ' + relative_error; 198 throw new base.unittest.TestError(message); 199 }, 200 201 assertVisible: function(elt) { 202 if (!elt.offsetHeight || !elt.offsetWidth) 203 throw new base.unittest.TestError('Expected element to be visible'); 204 } 205 }; 206 207 function bindGlobals_() { 208 forAllAssertMethodsIn_(Assertions.prototype, 209 function(fieldName, fieldValue) { 210 global[fieldName] = fieldValue.bind(this); 211 }); 212 }; 213 bindGlobals_(); 214 215 return { 216 Assertions: Assertions 217 }; 218 }); 219 220