| 9 | // Transform an array of values into object keys with the same value. |
| 10 | YAHOO.lang.keysToObject = function( array , value ) |
| 11 | { |
| 12 | var obj = {}; |
| 13 | for( var i = 0 ; i < array.length ; i++ ) |
| 14 | { |
| 15 | obj[ array[ i ] ] = value; |
| 16 | } |
| 17 | return i ? obj : null; |
| 18 | }; |
| 19 | |
| 20 | var native = YAHOO.tool.TestCase; |
| 21 | // This new constructor is an adaptor to the original YUI codes. |
| 22 | YAHOO.tool.TestCase = function( template ) |
| 23 | { |
| 24 | // Construct the '_should' instructions object from any methods |
| 25 | // prefix by 'should'. |
| 26 | var nonInstructions = [ '_should', 'name', 'setUp', 'tearDown', /^test/ ], |
| 27 | should = template._should || ( template._should = {} ), |
| 28 | match, |
| 29 | instruction; |
| 30 | for ( var item in template ) |
| 31 | { |
| 32 | if ( match = item.match( /^should(.*)/ ) ) |
| 33 | { |
| 34 | instruction = match[ 1 ]; |
| 35 | should[ instruction.charAt( 0 ).toLowerCase() |
| 36 | + instruction.substr( 1 ) ] = YAHOO.lang.keysToObject( template[ item ], true ); |
| 37 | delete template[ item ]; |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | // Ignore all tests from the template other than the one specified by 'shouldIgnoreAllBut' (#4242). |
| 42 | var preserves; |
| 43 | if( ( preserves = template._should.ignoreAllBut ) ) |
| 44 | { |
| 45 | delete template._should.ignoreAllBut; |
| 46 | var ignores = template._should.ignore = {}, match, test; |
| 47 | for ( test in template ) |
| 48 | if ( ( match = test.match( /^test.*/ ) ) && !( match in preserves ) ) |
| 49 | ignores[ match ] = true; |
| 50 | } |
| 51 | |
| 52 | return native.call( this, template ); |
| 53 | }; |
| 54 | YAHOO.tool.TestCase.prototype = native.prototype; |
| 55 | YAHOO.tool.TestCase.Wait = native.Wait; |
| 56 | |
| 57 | |
| 58 | // Overwrite YAHOO.tool.TestRuner._resumeTest to support optional error catching |
| 59 | // by instruction 'shouldThrows' (#4242). |
| 60 | YAHOO.tool.TestRunner._resumeTest = function (segment /*:Function*/) /*:Void*/ |
| 61 | { |
| 62 | |
| 63 | //get relevant information |
| 64 | var node /*:TestNode*/ = this._cur; |
| 65 | var testName /*:String*/ = node.testObject; |
| 66 | var testCase /*:YAHOO.tool.TestCase*/ = node.parent.testObject; |
| 67 | |
| 68 | //cancel other waits if available |
| 69 | if (testCase.__yui_wait){ |
| 70 | clearTimeout(testCase.__yui_wait); |
| 71 | delete testCase.__yui_wait; |
| 72 | } |
| 73 | |
| 74 | //get the "should" test cases |
| 75 | var shouldFail /*:Object*/ = (testCase._should.fail || {})[testName]; |
| 76 | var shouldError /*:Object*/ = (testCase._should.error || {})[testName]; |
| 77 | var shouldThrow /*:Object*/ = (testCase._should.throws || {})[testName]; |
| 78 | |
| 79 | //variable to hold whether or not the test failed |
| 80 | var failed /*:Boolean*/ = false; |
| 81 | var error /*:Error*/ = null; |
| 82 | |
| 83 | var _resumeTestResult = CKEDITOR.tools.bind( function ( evt ) |
| 84 | { |
| 85 | if( evt ) |
| 86 | YAHOO.util.Event.removeListener( window, "error", arguments.callee ); |
| 87 | //fireEvent appropriate event |
| 88 | if (failed) { |
| 89 | this.fireEvent(this.TEST_FAIL_EVENT, { testCase: testCase, testName: testName, error: error }); |
| 90 | } else { |
| 91 | this.fireEvent(this.TEST_PASS_EVENT, { testCase: testCase, testName: testName }); |
| 92 | } |
| 93 | |
| 94 | //run the tear down |
| 95 | testCase.tearDown(); |
| 96 | |
| 97 | //update results |
| 98 | node.parent.results[testName] = { |
| 99 | result: failed ? "fail" : "pass", |
| 100 | message: error ? error.getMessage() : "Test passed", |
| 101 | type: "test", |
| 102 | name: testName |
| 103 | }; |
| 104 | |
| 105 | if (failed){ |
| 106 | node.parent.results.failed++; |
| 107 | } else { |
| 108 | node.parent.results.passed++; |
| 109 | } |
| 110 | node.parent.results.total++; |
| 111 | |
| 112 | //set timeout not supported in all environments |
| 113 | if (typeof setTimeout != "undefined"){ |
| 114 | setTimeout(function(){ |
| 115 | YAHOO.tool.TestRunner._run(); |
| 116 | }, 0); |
| 117 | } else { |
| 118 | this._run(); |
| 119 | } |
| 120 | |
| 121 | }, this ); |
| 122 | |
| 123 | //try the test |
| 124 | try { |
| 125 | |
| 126 | //run the test |
| 127 | segment.apply(testCase); |
| 128 | |
| 129 | //if it should fail, and it got here, then it's a fail because it didn't |
| 130 | if (shouldFail){ |
| 131 | error = new YAHOO.util.ShouldFail(); |
| 132 | failed = true; |
| 133 | } else if (shouldError){ |
| 134 | error = new YAHOO.util.ShouldError(); |
| 135 | failed = true; |
| 136 | } |
| 137 | |
| 138 | } catch (thrown /*:Error*/){ |
| 139 | if (thrown instanceof YAHOO.tool.TestCase.Wait){ |
| 140 | |
| 141 | if (YAHOO.lang.isFunction(thrown.segment)){ |
| 142 | if (YAHOO.lang.isNumber(thrown.delay)){ |
| 143 | |
| 144 | //some environments don't support setTimeout |
| 145 | if (typeof setTimeout != "undefined"){ |
| 146 | testCase.__yui_wait = setTimeout(function(){ |
| 147 | YAHOO.tool.TestRunner._resumeTest(thrown.segment); |
| 148 | }, thrown.delay); |
| 149 | } else { |
| 150 | throw new Error("Asynchronous tests not supported in this environment."); |
| 151 | } |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | return; |
| 156 | |
| 157 | } |
| 158 | else if (thrown instanceof YAHOO.util.AssertionError) { |
| 159 | if (!shouldFail){ |
| 160 | error = thrown; |
| 161 | failed = true; |
| 162 | } |
| 163 | } else { |
| 164 | //first check to see if it should error |
| 165 | if (!shouldError) { |
| 166 | error = new YAHOO.util.UnexpectedError(thrown); |
| 167 | failed = true; |
| 168 | } else { |
| 169 | //check to see what type of data we have |
| 170 | if (YAHOO.lang.isString(shouldError)){ |
| 171 | |
| 172 | //if it's a string, check the error message |
| 173 | if (thrown.message != shouldError){ |
| 174 | error = new YAHOO.util.UnexpectedError(thrown); |
| 175 | failed = true; |
| 176 | } |
| 177 | } else if (YAHOO.lang.isFunction(shouldError)){ |
| 178 | |
| 179 | //if it's a function, see if the error is an instance of it |
| 180 | if (!(thrown instanceof shouldError)){ |
| 181 | error = new YAHOO.util.UnexpectedError(thrown); |
| 182 | failed = true; |
| 183 | } |
| 184 | |
| 185 | } else if (YAHOO.lang.isObject(shouldError)){ |
| 186 | |
| 187 | //if it's an object, check the instance and message |
| 188 | if (!(thrown instanceof shouldError.constructor) || |
| 189 | thrown.message != shouldError.message){ |
| 190 | error = new YAHOO.util.UnexpectedError(thrown); |
| 191 | failed = true; |
| 192 | } |
| 193 | |
| 194 | } |
| 195 | |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | if( shouldThrow ) |
| 200 | { |
| 201 | YAHOO.util.Event.addListener( window, 'error', _resumeTestResult ); |
| 202 | throw thrown; |
| 203 | } |
| 204 | else |
| 205 | _resumeTestResult(); |
| 206 | |
| 207 | } |
| 208 | |
| 209 | }; |
| 210 | |
| 211 | |
| 212 | } )(); |
| 213 | |
| 214 | |
| 215 | |
| 216 | (function() |
| 217 | { |