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