Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1 | // Copyright 2008 the V8 project authors. All rights reserved. |
| 2 | // Redistribution and use in source and binary forms, with or without |
| 3 | // modification, are permitted provided that the following conditions are |
| 4 | // met: |
| 5 | // |
| 6 | // * Redistributions of source code must retain the above copyright |
| 7 | // notice, this list of conditions and the following disclaimer. |
| 8 | // * Redistributions in binary form must reproduce the above |
| 9 | // copyright notice, this list of conditions and the following |
| 10 | // disclaimer in the documentation and/or other materials provided |
| 11 | // with the distribution. |
| 12 | // * Neither the name of Google Inc. nor the names of its |
| 13 | // contributors may be used to endorse or promote products derived |
| 14 | // from this software without specific prior written permission. |
| 15 | // |
| 16 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 17 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 18 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 19 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 20 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 21 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 22 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 26 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | |
| 28 | function MjsUnitAssertionError(message) { |
| 29 | this.message = message; |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 30 | // This allows fetching the stack trace using TryCatch::StackTrace. |
| 31 | this.stack = new Error("").stack; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | MjsUnitAssertionError.prototype.toString = function () { |
| 35 | return this.message; |
| 36 | } |
| 37 | |
| 38 | /* |
| 39 | * This file is included in all mini jsunit test cases. The test |
| 40 | * framework expects lines that signal failed tests to start with |
| 41 | * the f-word and ignore all other lines. |
| 42 | */ |
| 43 | |
| 44 | function fail(expected, found, name_opt) { |
| 45 | var start; |
| 46 | if (name_opt) { |
| 47 | // Fix this when we ditch the old test runner. |
| 48 | start = "Fail" + "ure (" + name_opt + "): "; |
| 49 | } else { |
| 50 | start = "Fail" + "ure:"; |
| 51 | } |
| 52 | throw new MjsUnitAssertionError(start + " expected <" + expected + "> found <" + found + ">"); |
| 53 | } |
| 54 | |
| 55 | |
| 56 | function deepObjectEquals(a, b) { |
| 57 | var aProps = []; |
| 58 | for (var key in a) |
| 59 | aProps.push(key); |
| 60 | var bProps = []; |
| 61 | for (var key in b) |
| 62 | bProps.push(key); |
| 63 | aProps.sort(); |
| 64 | bProps.sort(); |
| 65 | if (!deepEquals(aProps, bProps)) |
| 66 | return false; |
| 67 | for (var i = 0; i < aProps.length; i++) { |
| 68 | if (!deepEquals(a[aProps[i]], b[aProps[i]])) |
| 69 | return false; |
| 70 | } |
| 71 | return true; |
| 72 | } |
| 73 | |
| 74 | |
| 75 | function deepEquals(a, b) { |
| 76 | if (a == b) return true; |
| 77 | if (typeof a == "number" && typeof b == "number" && isNaN(a) && isNaN(b)) { |
| 78 | return true; |
| 79 | } |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 80 | if (a == null || b == null) return false; |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 81 | if (a.constructor === RegExp || b.constructor === RegExp) { |
| 82 | return (a.constructor === b.constructor) && (a.toString === b.toString); |
| 83 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 84 | if ((typeof a) !== 'object' || (typeof b) !== 'object' || |
| 85 | (a === null) || (b === null)) |
| 86 | return false; |
| 87 | if (a.constructor === Array) { |
| 88 | if (b.constructor !== Array) |
| 89 | return false; |
| 90 | if (a.length != b.length) |
| 91 | return false; |
| 92 | for (var i = 0; i < a.length; i++) { |
| 93 | if (i in a) { |
| 94 | if (!(i in b) || !(deepEquals(a[i], b[i]))) |
| 95 | return false; |
| 96 | } else if (i in b) { |
| 97 | return false; |
| 98 | } |
| 99 | } |
| 100 | return true; |
| 101 | } else { |
| 102 | return deepObjectEquals(a, b); |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame^] | 107 | function assertSame(expected, found, name_opt) { |
| 108 | if (found !== expected) { |
| 109 | fail(expected, found, name_opt); |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 114 | function assertEquals(expected, found, name_opt) { |
| 115 | if (!deepEquals(found, expected)) { |
| 116 | fail(expected, found, name_opt); |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | |
| 121 | function assertArrayEquals(expected, found, name_opt) { |
| 122 | var start = ""; |
| 123 | if (name_opt) { |
| 124 | start = name_opt + " - "; |
| 125 | } |
| 126 | assertEquals(expected.length, found.length, start + "array length"); |
| 127 | if (expected.length == found.length) { |
| 128 | for (var i = 0; i < expected.length; ++i) { |
| 129 | assertEquals(expected[i], found[i], start + "array element at index " + i); |
| 130 | } |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | |
| 135 | function assertTrue(value, name_opt) { |
| 136 | assertEquals(true, value, name_opt); |
| 137 | } |
| 138 | |
| 139 | |
| 140 | function assertFalse(value, name_opt) { |
| 141 | assertEquals(false, value, name_opt); |
| 142 | } |
| 143 | |
| 144 | |
| 145 | function assertNaN(value, name_opt) { |
| 146 | if (!isNaN(value)) { |
| 147 | fail("NaN", value, name_opt); |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | |
| 152 | function assertNull(value, name_opt) { |
| 153 | if (value !== null) { |
| 154 | fail("null", value, name_opt); |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | |
| 159 | function assertNotNull(value, name_opt) { |
| 160 | if (value === null) { |
| 161 | fail("not null", value, name_opt); |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | |
| 166 | function assertThrows(code, type_opt, cause_opt) { |
| 167 | var threwException = true; |
| 168 | try { |
| 169 | if (typeof code == 'function') { |
| 170 | code(); |
| 171 | } else { |
| 172 | eval(code); |
| 173 | } |
| 174 | threwException = false; |
| 175 | } catch (e) { |
| 176 | if (typeof type_opt == 'function') |
| 177 | assertInstanceof(e, type_opt); |
| 178 | if (arguments.length >= 3) |
| 179 | assertEquals(e.type, cause_opt); |
| 180 | // Do nothing. |
| 181 | } |
| 182 | if (!threwException) assertTrue(false, "did not throw exception"); |
| 183 | } |
| 184 | |
| 185 | |
| 186 | function assertInstanceof(obj, type) { |
| 187 | if (!(obj instanceof type)) { |
| 188 | assertTrue(false, "Object <" + obj + "> is not an instance of <" + type + ">"); |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | |
| 193 | function assertDoesNotThrow(code) { |
| 194 | try { |
| 195 | if (typeof code == 'function') { |
| 196 | code(); |
| 197 | } else { |
| 198 | eval(code); |
| 199 | } |
| 200 | } catch (e) { |
| 201 | assertTrue(false, "threw an exception: " + (e.message || e)); |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | |
| 206 | function assertUnreachable(name_opt) { |
| 207 | // Fix this when we ditch the old test runner. |
| 208 | var message = "Fail" + "ure: unreachable" |
| 209 | if (name_opt) { |
| 210 | message += " - " + name_opt; |
| 211 | } |
| 212 | throw new MjsUnitAssertionError(message); |
| 213 | } |