Andrew Top | ef837fa | 2017-10-04 22:44:25 -0700 | [diff] [blame] | 1 | // Copyright 2013 the V8 project authors. All rights reserved. |
| 2 | // Copyright (C) 2005, 2006, 2007, 2008, 2009 Apple Inc. All rights reserved. |
| 3 | // |
| 4 | // Redistribution and use in source and binary forms, with or without |
| 5 | // modification, are permitted provided that the following conditions |
| 6 | // are met: |
| 7 | // 1. Redistributions of source code must retain the above copyright |
| 8 | // notice, this list of conditions and the following disclaimer. |
| 9 | // 2. Redistributions in binary form must reproduce the above copyright |
| 10 | // notice, this list of conditions and the following disclaimer in the |
| 11 | // documentation and/or other materials provided with the distribution. |
| 12 | // |
| 13 | // THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND ANY |
| 14 | // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 15 | // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 16 | // DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY |
| 17 | // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 18 | // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 19 | // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON |
| 20 | // ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 21 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 22 | // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 23 | |
| 24 | var wasPostTestScriptParsed = false; |
| 25 | |
| 26 | function removeLink(text) |
| 27 | { |
| 28 | return text.replace(/<a[^>]*>/g, "").replace(/<\/a>/g, ""); |
| 29 | } |
| 30 | |
| 31 | function description(msg) |
| 32 | { |
| 33 | print(removeLink(msg)); |
| 34 | print("\nOn success, you will see a series of \"PASS\" messages, followed by \"TEST COMPLETE\".\n"); |
| 35 | print(); |
| 36 | } |
| 37 | |
| 38 | function debug(msg) |
| 39 | { |
| 40 | print(msg); |
| 41 | } |
| 42 | |
| 43 | function escapeString(text) |
| 44 | { |
| 45 | return text.replace(/\0/g, ""); |
| 46 | } |
| 47 | |
| 48 | function testPassed(msg) |
| 49 | { |
| 50 | print("PASS", escapeString(msg)); |
| 51 | } |
| 52 | |
| 53 | function testFailed(msg) |
| 54 | { |
| 55 | print("FAIL", escapeString(msg)); |
| 56 | } |
| 57 | |
| 58 | function areArraysEqual(_a, _b) |
| 59 | { |
| 60 | if (Object.prototype.toString.call(_a) != Object.prototype.toString.call([])) |
| 61 | return false; |
| 62 | if (_a.length !== _b.length) |
| 63 | return false; |
| 64 | for (var i = 0; i < _a.length; i++) |
| 65 | if (_a[i] !== _b[i]) |
| 66 | return false; |
| 67 | return true; |
| 68 | } |
| 69 | |
| 70 | function isMinusZero(n) |
| 71 | { |
| 72 | // the only way to tell 0 from -0 in JS is the fact that 1/-0 is |
| 73 | // -Infinity instead of Infinity |
| 74 | return n === 0 && 1/n < 0; |
| 75 | } |
| 76 | |
| 77 | function isResultCorrect(_actual, _expected) |
| 78 | { |
| 79 | if (_expected === 0) |
| 80 | return _actual === _expected && (1/_actual) === (1/_expected); |
| 81 | if (_actual === _expected) |
| 82 | return true; |
| 83 | if (typeof(_expected) == "number" && isNaN(_expected)) |
| 84 | return typeof(_actual) == "number" && isNaN(_actual); |
| 85 | if (Object.prototype.toString.call(_expected) == Object.prototype.toString.call([])) |
| 86 | return areArraysEqual(_actual, _expected); |
| 87 | return false; |
| 88 | } |
| 89 | |
| 90 | function stringify(v) |
| 91 | { |
| 92 | if (v) |
| 93 | return v.toString(); |
| 94 | if (v === 0 && 1/v < 0) |
| 95 | return "-0"; |
| 96 | else |
| 97 | return "" + v; |
| 98 | } |
| 99 | |
| 100 | function shouldBe(_a, _b) |
| 101 | { |
| 102 | if (typeof _a != "string" || typeof _b != "string") |
| 103 | debug("WARN: shouldBe() expects string arguments"); |
| 104 | var exception; |
| 105 | var _av; |
| 106 | try { |
| 107 | _av = eval(_a); |
| 108 | } catch (e) { |
| 109 | exception = e; |
| 110 | } |
| 111 | var _bv = eval(_b); |
| 112 | |
| 113 | if (exception) |
| 114 | testFailed(_a + " should be " + _bv + ". Threw exception " + exception); |
| 115 | else if (isResultCorrect(_av, _bv)) |
| 116 | testPassed(_a + " is " + _b); |
| 117 | else if (typeof(_av) == typeof(_bv)) |
| 118 | testFailed(_a + " should be " + _bv + ". Was " + stringify(_av) + "."); |
| 119 | else |
| 120 | testFailed(_a + " should be " + _bv + " (of type " + typeof _bv + "). Was " + stringify(_av) + " (of type " + typeof _av + ")."); |
| 121 | } |
| 122 | |
| 123 | function shouldBeTrue(_a) { shouldBe(_a, "true"); } |
| 124 | function shouldBeFalse(_a) { shouldBe(_a, "false"); } |
| 125 | function shouldBeNaN(_a) { shouldBe(_a, "NaN"); } |
| 126 | function shouldBeNull(_a) { shouldBe(_a, "null"); } |
| 127 | |
| 128 | function shouldBeEqualToString(a, b) |
| 129 | { |
| 130 | if (typeof a !== "string" || typeof b !== "string") |
| 131 | debug("WARN: shouldBeEqualToString() expects string arguments"); |
| 132 | var unevaledString = JSON.stringify(b); |
| 133 | shouldBe(a, unevaledString); |
| 134 | } |
| 135 | |
| 136 | function shouldBeUndefined(_a) |
| 137 | { |
| 138 | var exception; |
| 139 | var _av; |
| 140 | try { |
| 141 | _av = eval(_a); |
| 142 | } catch (e) { |
| 143 | exception = e; |
| 144 | } |
| 145 | |
| 146 | if (exception) |
| 147 | testFailed(_a + " should be undefined. Threw exception " + exception); |
| 148 | else if (typeof _av == "undefined") |
| 149 | testPassed(_a + " is undefined."); |
| 150 | else |
| 151 | testFailed(_a + " should be undefined. Was " + _av); |
| 152 | } |
| 153 | |
| 154 | |
| 155 | function shouldThrow(_a, _e) |
| 156 | { |
| 157 | var exception; |
| 158 | var _av; |
| 159 | try { |
| 160 | _av = eval(_a); |
| 161 | } catch (e) { |
| 162 | exception = e; |
| 163 | } |
| 164 | |
| 165 | var _ev; |
| 166 | if (_e) |
| 167 | _ev = eval(_e); |
| 168 | |
| 169 | if (exception) { |
| 170 | if (typeof _e == "undefined" || exception == _ev) |
| 171 | testPassed(_a + " threw exception " + exception + "."); |
| 172 | else |
| 173 | testFailed(_a + " should throw " + (typeof _e == "undefined" ? "an exception" : _ev) + ". Threw exception " + exception + "."); |
| 174 | } else if (typeof _av == "undefined") |
| 175 | testFailed(_a + " should throw " + (typeof _e == "undefined" ? "an exception" : _ev) + ". Was undefined."); |
| 176 | else |
| 177 | testFailed(_a + " should throw " + (typeof _e == "undefined" ? "an exception" : _ev) + ". Was " + stringify(_av) + "."); |
| 178 | } |
| 179 | |
| 180 | |
| 181 | function shouldNotThrow(_a) |
| 182 | { |
| 183 | try { |
| 184 | eval(_a); |
| 185 | testPassed(_a + " did not throw exception."); |
| 186 | } catch (e) { |
| 187 | testFailed(_a + " should not throw exception. Threw exception " + e + "."); |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | function isSuccessfullyParsed() |
| 192 | { |
| 193 | successfullyParsed = true; |
| 194 | shouldBeTrue("successfullyParsed"); |
| 195 | debug("\nTEST COMPLETE\n"); |
| 196 | } |
| 197 | |
| 198 | // It's possible for an async test to call finishJSTest() before js-test-post.js |
| 199 | // has been parsed. |
| 200 | function finishJSTest() |
| 201 | { |
| 202 | wasFinishJSTestCalled = true; |
| 203 | if (!wasPostTestScriptParsed) |
| 204 | return; |
| 205 | isSuccessfullyParsed(); |
| 206 | } |