OLD | NEW |
1 // Copyright 2008 the V8 project authors. All rights reserved. | 1 // Copyright 2008 the V8 project authors. All rights reserved. |
2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
4 // met: | 4 // met: |
5 // | 5 // |
6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
(...skipping 13 matching lines...) Expand all Loading... |
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 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. | 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 | 27 |
28 function MjsUnitAssertionError(message) { | 28 function MjsUnitAssertionError(message) { |
29 this.message = message; | 29 this.message = message; |
30 // This allows fetching the stack trace using TryCatch::StackTrace. | 30 // This allows fetching the stack trace using TryCatch::StackTrace. |
31 this.stack = new Error("").stack; | 31 this.stack = new Error("").stack; |
32 } | 32 } |
33 | 33 |
34 MjsUnitAssertionError.prototype.toString = function () { | |
35 return this.message; | |
36 } | |
37 | |
38 /* | 34 /* |
39 * This file is included in all mini jsunit test cases. The test | 35 * This file is included in all mini jsunit test cases. The test |
40 * framework expects lines that signal failed tests to start with | 36 * framework expects lines that signal failed tests to start with |
41 * the f-word and ignore all other lines. | 37 * the f-word and ignore all other lines. |
42 */ | 38 */ |
43 | 39 |
| 40 |
| 41 MjsUnitAssertionError.prototype.toString = function () { |
| 42 return this.message; |
| 43 }; |
| 44 |
| 45 |
| 46 function classOf(object) { |
| 47 var string = Object.prototype.toString.call(object); |
| 48 // String has format [object <ClassName>]. |
| 49 return string.substring(8, string.length - 1); |
| 50 } |
| 51 |
| 52 |
44 function MjsUnitToString(value) { | 53 function MjsUnitToString(value) { |
45 switch (typeof value) { | 54 switch (typeof value) { |
46 case "string": | 55 case "string": |
47 return JSON.stringify(value); | 56 return JSON.stringify(value); |
48 case "number": | 57 case "number": |
49 if (value === 0 && (1 / value) < 0) return "-0"; | 58 if (value === 0 && (1 / value) < 0) return "-0"; |
| 59 // FALLTHROUGH. |
50 case "boolean": | 60 case "boolean": |
51 case "null": | |
52 case "undefined": | 61 case "undefined": |
53 case "function": | 62 case "function": |
54 return String(value); | 63 return String(value); |
55 case "object": | 64 case "object": |
56 if (value === null) return "null"; | 65 if (value === null) return "null"; |
57 var clazz = Object.prototype.toString.call(value); | 66 var objectClass = classOf(value); |
58 clazz = clazz.substring(8, clazz.length - 1); | 67 switch (objectClass) { |
59 switch (clazz) { | |
60 case "Number": | 68 case "Number": |
61 case "String": | 69 case "String": |
62 case "Boolean": | 70 case "Boolean": |
63 case "Date": | 71 case "Date": |
64 return clazz + "(" + MjsUnitToString(value.valueOf()) + ")"; | 72 return objectClass + "(" + MjsUnitToString(value.valueOf()) + ")"; |
65 case "RegExp": | 73 case "RegExp": |
66 return value.toString(); | 74 return value.toString(); |
67 case "Array": | 75 case "Array": |
68 return "[" + value.map(MjsUnitArrayElementToString).join(",") + "]"; | 76 return "[" + value.map(MjsUnitArrayElementToString).join(",") + "]"; |
69 case "Object": | 77 case "Object": |
70 break; | 78 break; |
71 default: | 79 default: |
72 return clazz + "()"; | 80 return objectClass + "()"; |
73 } | 81 } |
74 // [[Class]] is "Object". | 82 // [[Class]] is "Object". |
75 var constructor = value.constructor.name; | 83 var constructor = value.constructor.name; |
76 if (name) return name + "()"; | 84 if (name) return name + "()"; |
77 return "Object()"; | 85 return "Object()"; |
78 default: | 86 default: |
79 return "-- unknown value --"; | 87 return "-- unknown value --"; |
80 } | 88 } |
81 } | 89 } |
82 | 90 |
(...skipping 12 matching lines...) Expand all Loading... |
95 } | 103 } |
96 | 104 |
97 message += ": expected <" + MjsUnitToString(expected) + | 105 message += ": expected <" + MjsUnitToString(expected) + |
98 "> found <" + MjsUnitToString(found) + ">"; | 106 "> found <" + MjsUnitToString(found) + ">"; |
99 throw new MjsUnitAssertionError(message); | 107 throw new MjsUnitAssertionError(message); |
100 } | 108 } |
101 | 109 |
102 | 110 |
103 function deepObjectEquals(a, b) { | 111 function deepObjectEquals(a, b) { |
104 var aProps = []; | 112 var aProps = []; |
105 for (var key in a) | 113 for (var key in a) { |
106 aProps.push(key); | 114 aProps.push(key); |
| 115 } |
107 var bProps = []; | 116 var bProps = []; |
108 for (var key in b) | 117 for (key in b) { |
109 bProps.push(key); | 118 bProps.push(key); |
| 119 } |
110 aProps.sort(); | 120 aProps.sort(); |
111 bProps.sort(); | 121 bProps.sort(); |
112 if (!deepEquals(aProps, bProps)) | 122 if (!deepEquals(aProps, bProps)) |
113 return false; | 123 return false; |
114 for (var i = 0; i < aProps.length; i++) { | 124 for (var i = 0; i < aProps.length; i++) { |
115 if (!deepEquals(a[aProps[i]], b[aProps[i]])) | 125 if (!deepEquals(a[aProps[i]], b[aProps[i]])) |
116 return false; | 126 return false; |
117 } | 127 } |
118 return true; | 128 return true; |
119 } | 129 } |
120 | 130 |
121 | 131 |
122 function deepEquals(a, b) { | 132 function deepEquals(a, b) { |
123 if (a == b) { | 133 if (a == b) { |
124 // Check for -0. | 134 // Check for -0. |
125 if (a === 0 && b === 0) return (1 / a) === (1 / b); | 135 if (a === 0 && b === 0) return (1 / a) === (1 / b); |
126 return true; | 136 return true; |
127 } | 137 } |
128 if (typeof a == "number" && typeof b == "number" && isNaN(a) && isNaN(b)) { | 138 if (typeof a == "number" && typeof b == "number" && isNaN(a) && isNaN(b)) { |
129 return true; | 139 return true; |
130 } | 140 } |
131 if (a == null || b == null) return false; | 141 if (a == null || b == null) return false; |
132 if (a.constructor === RegExp || b.constructor === RegExp) { | 142 var aClass = classOf(a); |
133 return (a.constructor === b.constructor) && (a.toString() === b.toString()); | 143 var bClass = classOf(b); |
| 144 if (aClass === "RegExp" || bClass === "RegExp") { |
| 145 return (aClass === bClass) && (a.toString() === b.toString()); |
134 } | 146 } |
135 if ((typeof a) !== 'object' || (typeof b) !== 'object' || | 147 if ((typeof a) !== 'object' || (typeof b) !== 'object' || |
136 (a === null) || (b === null)) | 148 (a === null) || (b === null)) |
137 return false; | 149 return false; |
138 if (a.constructor === Array) { | 150 if (aClass === "Array") { |
139 if (b.constructor !== Array) | 151 if (bClass !== "Array") |
140 return false; | 152 return false; |
141 if (a.length != b.length) | 153 if (a.length != b.length) |
142 return false; | 154 return false; |
143 for (var i = 0; i < a.length; i++) { | 155 for (var i = 0; i < a.length; i++) { |
144 if (i in a) { | 156 if (i in a) { |
145 if (!(i in b) || !(deepEquals(a[i], b[i]))) | 157 if (!(i in b) || !(deepEquals(a[i], b[i]))) |
146 return false; | 158 return false; |
147 } else if (i in b) { | 159 } else if (i in b) { |
148 return false; | 160 return false; |
149 } | 161 } |
150 } | 162 } |
151 return true; | 163 return true; |
| 164 } else if (bClass == "Array") { |
| 165 return false; |
152 } else { | 166 } else { |
153 return deepObjectEquals(a, b); | 167 return deepObjectEquals(a, b); |
154 } | 168 } |
155 } | 169 } |
156 | 170 |
157 | 171 |
158 function assertSame(expected, found, name_opt) { | 172 function assertSame(expected, found, name_opt) { |
159 if (found !== expected) { | 173 if (found !== expected) { |
160 fail(expected, found, name_opt); | 174 fail(expected, found, name_opt); |
161 } | 175 } |
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
255 | 269 |
256 | 270 |
257 function assertUnreachable(name_opt) { | 271 function assertUnreachable(name_opt) { |
258 // Fix this when we ditch the old test runner. | 272 // Fix this when we ditch the old test runner. |
259 var message = "Fail" + "ure: unreachable"; | 273 var message = "Fail" + "ure: unreachable"; |
260 if (name_opt) { | 274 if (name_opt) { |
261 message += " - " + name_opt; | 275 message += " - " + name_opt; |
262 } | 276 } |
263 throw new MjsUnitAssertionError(message); | 277 throw new MjsUnitAssertionError(message); |
264 } | 278 } |
OLD | NEW |