OLD | NEW |
(Empty) | |
| 1 // Copyright 2013 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 // limitations under the License. |
| 28 |
| 29 // Some methods are taken from v8/test/mjsunit/mjsunit.js |
| 30 |
| 31 /** |
| 32 * Compares two objects for key/value equality. |
| 33 * Returns true if they are equal, false otherwise. |
| 34 */ |
| 35 function deepObjectEquals(a, b) { |
| 36 var aProps = Object.keys(a); |
| 37 aProps.sort(); |
| 38 var bProps = Object.keys(b); |
| 39 bProps.sort(); |
| 40 if (!deepEquals(aProps, bProps)) { |
| 41 return false; |
| 42 } |
| 43 for (var i = 0; i < aProps.length; i++) { |
| 44 if (!deepEquals(a[aProps[i]], b[aProps[i]])) { |
| 45 return false; |
| 46 } |
| 47 } |
| 48 return true; |
| 49 } |
| 50 |
| 51 |
| 52 /** |
| 53 * Compares two JavaScript values for type and value equality. |
| 54 * It checks internals of arrays and objects. |
| 55 */ |
| 56 function deepEquals(a, b) { |
| 57 if (a === b) { |
| 58 // Check for -0. |
| 59 if (a === 0) return (1 / a) === (1 / b); |
| 60 return true; |
| 61 } |
| 62 if (typeof a != typeof b) return false; |
| 63 if (typeof a == 'number') return isNaN(a) && isNaN(b); |
| 64 if (typeof a !== 'object' && typeof a !== 'function') return false; |
| 65 // Neither a nor b is primitive. |
| 66 var objectClass = classOf(a); |
| 67 if (objectClass !== classOf(b)) return false; |
| 68 if (objectClass === 'RegExp') { |
| 69 // For RegExp, just compare pattern and flags using its toString. |
| 70 return (a.toString() === b.toString()); |
| 71 } |
| 72 // Functions are only identical to themselves. |
| 73 if (objectClass === 'Function') return false; |
| 74 if (objectClass === 'Array') { |
| 75 var elementCount = 0; |
| 76 if (a.length != b.length) { |
| 77 return false; |
| 78 } |
| 79 for (var i = 0; i < a.length; i++) { |
| 80 if (!deepEquals(a[i], b[i])) return false; |
| 81 } |
| 82 return true; |
| 83 } |
| 84 if (objectClass == 'String' || objectClass == 'Number' || |
| 85 objectClass == 'Boolean' || objectClass == 'Date') { |
| 86 if (a.valueOf() !== b.valueOf()) return false; |
| 87 } |
| 88 return deepObjectEquals(a, b); |
| 89 } |
| 90 |
| 91 |
| 92 /** |
| 93 * Throws an exception, and prints the values in case of error. |
| 94 */ |
| 95 function fail(expected, found) { |
| 96 // TODO(cira): Replace String with PrettyPrint for objects and arrays. |
| 97 var message = 'Failure: expected <' + String(expected) + '>, found <' + |
| 98 String(found) + '>.'; |
| 99 throw new Error(message); |
| 100 } |
| 101 |
| 102 |
| 103 /** |
| 104 * Throws if two variables have different types or values. |
| 105 */ |
| 106 function assertEquals(expected, found) { |
| 107 if (!deepEquals(expected, found)) { |
| 108 fail(expected, found); |
| 109 } |
| 110 } |
| 111 |
| 112 |
| 113 /** |
| 114 * Throws if value is false. |
| 115 */ |
| 116 function assertTrue(value) { |
| 117 assertEquals(true, value) |
| 118 } |
| 119 |
| 120 |
| 121 /** |
| 122 * Throws if value is true. |
| 123 */ |
| 124 function assertFalse(value) { |
| 125 assertEquals(false, value); |
| 126 } |
| 127 |
| 128 |
| 129 /** |
| 130 * Returns true if code throws specified exception. |
| 131 */ |
| 132 function assertThrows(code, type_opt, cause_opt) { |
| 133 var threwException = true; |
| 134 try { |
| 135 if (typeof code == 'function') { |
| 136 code(); |
| 137 } else { |
| 138 eval(code); |
| 139 } |
| 140 threwException = false; |
| 141 } catch (e) { |
| 142 if (typeof type_opt == 'function') { |
| 143 assertInstanceof(e, type_opt); |
| 144 } |
| 145 if (arguments.length >= 3) { |
| 146 assertEquals(e.type, cause_opt); |
| 147 } |
| 148 // Success. |
| 149 return; |
| 150 } |
| 151 throw new Error("Did not throw exception"); |
| 152 } |
| 153 |
| 154 |
| 155 /** |
| 156 * Throws an exception if code throws. |
| 157 */ |
| 158 function assertDoesNotThrow(code, name_opt) { |
| 159 try { |
| 160 if (typeof code == 'function') { |
| 161 code(); |
| 162 } else { |
| 163 eval(code); |
| 164 } |
| 165 } catch (e) { |
| 166 fail("threw an exception: ", e.message || e, name_opt); |
| 167 } |
| 168 } |
| 169 |
| 170 |
| 171 /** |
| 172 * Throws if obj is not of given type. |
| 173 */ |
| 174 function assertInstanceof(obj, type) { |
| 175 if (!(obj instanceof type)) { |
| 176 var actualTypeName = null; |
| 177 var actualConstructor = Object.prototypeOf(obj).constructor; |
| 178 if (typeof actualConstructor == "function") { |
| 179 actualTypeName = actualConstructor.name || String(actualConstructor); |
| 180 } |
| 181 throw new Error('Object <' + obj + '> is not an instance of <' + |
| 182 (type.name || type) + '>' + |
| 183 (actualTypeName ? ' but of < ' + actualTypeName + '>' : '')); |
| 184 } |
| 185 } |
OLD | NEW |