1 jls.loader.provide('jls.jsunit.TestCase'); 2 3 jls.loader.require('jls.lang.AssertionError'); 4 jls.loader.require('jls.jsunit.TestResult'); 5 jls.loader.require('jls.jsunit.Assert'); 6 7 /** 8 * @namespace Provides JSUnit core classes. 9 * @see jls.jsunit.TestCase 10 * @name jls.jsunit 11 */ 12 13 jls.jsunit.TestCase = jls.lang.Class.create( /** @lends jls.jsunit.TestCase.prototype */ 14 { 15 /** 16 * Constructs a test case with the given name. 17 * 18 * @param {String} name The name of the test case. 19 * @class A test case defines the fixture to run multiple tests. 20 * The test cases can be launched by using the jls.jsunit.TestRunner class. 21 * @constructs 22 */ 23 initialize : function(name) { 24 this.setName(name); 25 }, 26 /** 27 * Sets this test case name. 28 * 29 * @param {String} name The name of the test case. 30 * @returns {jls.jsunit.TestCase} This test case. 31 */ 32 setName : function(name) { 33 this._name = name || ''; 34 }, 35 /** 36 * Returns this test case name. 37 * 38 * @returns {String} The name of the test case. 39 */ 40 getName : function() { 41 return this._name; 42 }, 43 /** 44 * Sets up the fixture. This method is called before a test is executed. 45 * 46 */ 47 setUp : function() { 48 }, 49 /** 50 * Tears down the fixture. This method is called after a test is executed. 51 * 52 */ 53 tearDown : function() { 54 }, 55 /** 56 * Returns the test case count. 57 * 58 * @returns {Number} The test case count. 59 */ 60 countTestCases : function() { 61 return 1; 62 }, 63 runBare : function() { 64 var exception = null; 65 this.setUp(); 66 try { 67 this.runTest(); 68 } 69 catch (e) { 70 exception = e; 71 } 72 try { 73 this.tearDown(); 74 } 75 catch (e) { 76 if (exception == null) { 77 exception = e; 78 } 79 } 80 if (exception != null) { 81 throw exception; 82 } 83 }, 84 /** 85 * Override to run the test and assert its state. 86 */ 87 runTest : function() { 88 jls.jsunit.Assert.assertNotNull(this.getName(), 'TestCase.name cannot be null'); 89 jls.jsunit.Assert.assertTrue((this.getName() in this) && (typeof this[this.getName()] == 'function'), 'Function "' + this.getName() + '" not found'); 90 this[this.getName()](); 91 } 92 }); 93 94