1 <!DOCTYPE html> 2 <html> 3 <!-- 4 Copyright (c) 2012 The Chromium Authors. All rights reserved. 5 Use of this source code is governed by a BSD-style license that can be 6 found in the LICENSE file. 7 --> 8 <head> 9 <title>Unittest Tests</title> 10 <script src="base.js"></script> 11 <script> 12 base.require('unittest'); 13 base.require('test_utils'); 14 </script> 15 </head> 16 <body> 17 18 <script> 19 'use strict'; 20 21 22 var TestCase = unittest.TestCase; 23 var HTMLTestRunner = unittest.HTMLTestRunner; 24 25 function rawAssertTrue(a) { 26 if (a) return; 27 throw new Error('Expected true, got ' + a); 28 } 29 30 function rawAssertFalse(a) { 31 if (!a) return; 32 throw new Error('Expected true, got ' + a); 33 } 34 35 function rawAssertEquals(a, b) { 36 if (a == b) return; 37 throw new Error('Expected <' + a + '>, got <' + b + '>'); 38 } 39 40 function rawAssertThrows(fn) { 41 try { 42 fn(); 43 } catch (e) { 44 return; 45 } 46 throw new Error('Expected <' + fn + '> to throw'); 47 } 48 49 function rawAssertNotThrows(fn) { 50 try { 51 fn(); 52 } catch (e) { 53 throw new Error('Expected <' + fn + '> to not throw'); 54 } 55 } 56 57 /** 58 * Runs all methods in the window that start with test_. 59 * @return {undefined} Nothing. 60 */ 61 function runAllTests() { 62 var testMethodNames = []; 63 for (var name in tests) { 64 if (name.indexOf('test') != 0) 65 continue; 66 var value = tests[name]; 67 if (typeof value != 'function') 68 continue; 69 testMethodNames.push(name); 70 } 71 var resultsEl = document.createElement('unittest-test-runner'); 72 document.body.appendChild(resultsEl); 73 74 var numFailures = 0; 75 for (var i = 0; i < testMethodNames.length; i++) { 76 var testMethodName = testMethodNames[i]; 77 var testMethod = tests[testMethodName]; 78 var testCaseEl = unittest.createTestCaseDiv_(testMethodName); 79 resultsEl.appendChild(testCaseEl); 80 testCaseEl.status = 'RUNNING'; 81 82 var testStub = { 83 get testName() { 84 return testMethodName; 85 }, 86 87 addHTMLOutput: function(opt_title, opt_element) { 88 return testCaseEl.addHTMLOutput(opt_title, opt_element); 89 } 90 }; 91 92 try { 93 testMethod.call(testStub); 94 testCaseEl.status = 'PASSED'; 95 } catch (e) { 96 testCaseEl.status = 'FAILED'; 97 e.testName = testMethodName; 98 testCaseEl.addError(testStub, e); 99 numFailures += 1; 100 } 101 } 102 window.G_testRunner = { 103 computeResultStats: function() { 104 return { 105 get numTestsRun() { 106 return testMethodName.length; 107 }, 108 get numTestsPassed() { 109 return this.numTestsRun - numFailures; 110 }, 111 get numTestsWithErrors() { 112 return numFailures; 113 } 114 }; 115 }, 116 117 get done() { 118 return true; 119 } 120 }; 121 } 122 123 // Simple assertions for use in testing the test harness. :) 124 function FakeTest(testMethod, opt_testMethodName) { 125 TestCase.call(this, testMethod, opt_testMethodName); 126 } 127 128 FakeTest.prototype = { 129 __proto__: TestCase.prototype, 130 131 setUp: function() { 132 this.didSetUp = true; 133 }, 134 135 tearDown: function() { 136 this.didTearDown = true; 137 } 138 }; 139 140 141 function FakeResults() { 142 } 143 144 FakeResults.prototype = { 145 willRunTest: function(test) { 146 this.didWillRunTest = true; 147 this.willRunTestArg1 = test; 148 }, 149 150 addError: function(e) { 151 this.didAddError = true; 152 this.addErrorArg1 = e; 153 }, 154 155 didRunTest: function(test) { 156 this.didDidRunTest = true; 157 this.didRunTestArg1 = test; 158 } 159 }; 160 161 function testTestCaseRun() { 162 var didRunTestMethod = false; 163 var test = new FakeTest(function() { 164 didRunTestMethod = true; 165 }, 'test_testcase_run'); 166 var results = new FakeResults(); 167 test.run(results); 168 169 rawAssertTrue(results.didWillRunTest); 170 rawAssertEquals(test, results.willRunTestArg1); 171 172 rawAssertTrue(test.didSetUp); 173 rawAssertTrue(didRunTestMethod); 174 175 rawAssertFalse(results.didAddError); 176 rawAssertTrue(test.didTearDown); 177 178 rawAssertTrue(results.didDidRunTest); 179 rawAssertEquals(test, results.didRunTestArg1); 180 181 rawAssertTrue(window.assertEquals == undefined); 182 } 183 184 function testTestCaseRun_TestThatFails() { 185 var didRunTestMethod = false; 186 var test = new FakeTest(function() { 187 didRunTestMethod = true; 188 assertTrue(false); 189 }, 'test_testcase_run_test_that_fails'); 190 var results = new FakeResults(); 191 test.run(results); 192 193 rawAssertTrue(results.didWillRunTest); 194 rawAssertEquals(test, results.willRunTestArg1); 195 196 rawAssertTrue(test.didSetUp); 197 rawAssertTrue(didRunTestMethod); 198 rawAssertTrue(results.didAddError); 199 rawAssertTrue(results.addErrorArg1 instanceof unittest.TestError); 200 rawAssertTrue(test.didTearDown); 201 202 rawAssertTrue(results.didDidRunTest); 203 rawAssertEquals(test, results.didRunTestArg1); 204 205 rawAssertTrue(window.assertEquals == undefined); 206 } 207 208 function testObjectThatHasErrorInSetUp() { 209 var didRunTestMethod = false; 210 var test = new FakeTest(function() { 211 didRunTestMethod = true; 212 }, 'test_object_that_has_error_in_setUp'); 213 test.setUp = function() { 214 throw Error('Intentional setUp error'); 215 }; 216 217 var results = new FakeResults(); 218 test.run(results); 219 220 rawAssertTrue(results.didWillRunTest); 221 rawAssertEquals(test, results.willRunTestArg1); 222 223 rawAssertFalse(didRunTestMethod); 224 rawAssertTrue(results.didAddError); 225 rawAssertTrue(results.addErrorArg1 instanceof Error); 226 rawAssertFalse(test.didTearDown); 227 228 rawAssertTrue(results.didDidRunTest); 229 rawAssertEquals(test, results.didRunTestArg1); 230 231 rawAssertTrue(window.assertEquals == undefined); 232 } 233 234 function testObjectThatThrowsInCleanup() { 235 var didRunTestMethod = false; 236 var test = new FakeTest(function() { 237 didRunTestMethod = true; 238 }, 'test_object_that_throws_in_cleanup'); 239 test.setUp = function() { 240 throw Error('Intentional setUp error'); 241 }; 242 243 var results = new FakeResults(); 244 test.run(results); 245 246 rawAssertTrue(results.didWillRunTest); 247 rawAssertEquals(test, results.willRunTestArg1); 248 249 rawAssertFalse(didRunTestMethod); 250 rawAssertTrue(results.didAddError); 251 rawAssertTrue(results.addErrorArg1 instanceof Error); 252 rawAssertFalse(test.didTearDown); 253 254 rawAssertTrue(results.didDidRunTest); 255 rawAssertEquals(test, results.didRunTestArg1); 256 257 rawAssertTrue(window.assertEquals == undefined); 258 } 259 260 function testAssertTrue() { 261 var test = new TestCase(function() { 262 rawAssertThrows(function() { 263 assertTrue(false); 264 }); 265 rawAssertNotThrows(function() { 266 assertTrue(true); 267 }); 268 }, 'test_assert_true'); 269 var results = new FakeResults(); 270 test.run(results); 271 } 272 273 function testAssertThrows() { 274 var test = new TestCase(function() { 275 rawAssertThrows(function() { 276 assertThrows(function() { 277 }); 278 }); 279 rawAssertNotThrows(function() { 280 assertThrows(function() { 281 throw new Error('expected_error'); 282 }); 283 }); 284 }, 'test_assert_throws'); 285 var results = new FakeResults(); 286 test.run(results); 287 } 288 289 function testTestRunnerAllPassed() { 290 var runner = new HTMLTestRunner('foo'); 291 var test1 = new TestCase(function() { 292 assertTrue(true); 293 }, 'test1'); 294 var test2 = new TestCase(function() { 295 assertTrue(true); 296 }, 'test2'); 297 298 var testRunnerEl = this.addHTMLOutput('Hypothetical result'); 299 testRunnerEl.appendChild(runner); 300 runner.run([test1, test2]); 301 302 var stats = runner.computeResultStats(); 303 rawAssertEquals(2, stats.numTestsRun); 304 rawAssertEquals(2, stats.numTestsPassed); 305 rawAssertEquals(0, stats.numTestsWithErrors); 306 } 307 308 function testTestErrorConstruction() { 309 var err = new unittest.TestError(); 310 rawAssertTrue(err instanceof unittest.TestError); 311 rawAssertTrue(err instanceof Error); 312 rawAssertTrue(err.stack); 313 314 var err = new unittest.TestError('foo'); 315 rawAssertTrue(err instanceof unittest.TestError); 316 rawAssertTrue(err instanceof Error); 317 rawAssertTrue(err.stack); 318 rawAssertEquals('foo', err.message); 319 } 320 321 function testTestRunnerOneFailed() { 322 var runner = new HTMLTestRunner('foo', ''); 323 var test1 = new TestCase(function() { 324 assertTrue(true); 325 }, 'test1'); 326 var test2 = new TestCase(function() { 327 assertTrue(false); 328 }, 'test2'); 329 330 var testRunnerEl = this.addHTMLOutput('Hypothetical result:'); 331 testRunnerEl.appendChild(runner); 332 runner.run([test1, test2]); 333 334 var stats = runner.computeResultStats(); 335 rawAssertEquals(2, stats.numTestsRun); 336 rawAssertEquals(1, stats.numTestsPassed); 337 rawAssertEquals(1, stats.numTestsWithErrors); 338 } 339 340 function testTestRunnerOneFailedWithHashFilter() { 341 var runner = new HTMLTestRunner('foo', '#test2'); 342 343 var test1 = new TestCase(function() { 344 assertTrue(true); 345 }, 'test1'); 346 var test2 = new TestCase(function() { 347 assertTrue(false); 348 }, 'test2'); 349 350 var testRunnerEl = this.addHTMLOutput('Hypothetical result:'); 351 testRunnerEl.appendChild(runner); 352 runner.run([test1, test2]); 353 354 var stats = runner.computeResultStats(); 355 rawAssertEquals(1, stats.numTestsRun); 356 rawAssertEquals(0, stats.numTestsPassed); 357 rawAssertEquals(1, stats.numTestsWithErrors); 358 } 359 360 function testTestDiscovery() { 361 var object = { 362 test1: function() { 363 }, 364 test_2: function() { 365 }, 366 notATest: function() { 367 }, 368 alsoNotATest: 3 369 }; 370 var tests = unittest.discoverTests(object, undefined); 371 rawAssertEquals(2, tests.length); 372 rawAssertEquals('test_2', tests[0].testName); 373 rawAssertEquals('test1', tests[1].testName); 374 } 375 376 function testTestRunnerFilter() { 377 var object = { 378 test1: function() { 379 }, 380 test_2: function() { 381 } 382 }; 383 var tests = unittest.discoverTests(object, /test_2/); 384 rawAssertEquals(1, tests.length); 385 rawAssertEquals('test_2', tests[0].testName); 386 } 387 388 </script> 389 </body> 390 </html> 391