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 23 matching lines...) Expand all Loading... |
34 MjsUnitAssertionError.prototype.toString = function () { | 34 MjsUnitAssertionError.prototype.toString = function () { |
35 return this.message; | 35 return this.message; |
36 } | 36 } |
37 | 37 |
38 /* | 38 /* |
39 * This file is included in all mini jsunit test cases. The test | 39 * This file is included in all mini jsunit test cases. The test |
40 * framework expects lines that signal failed tests to start with | 40 * framework expects lines that signal failed tests to start with |
41 * the f-word and ignore all other lines. | 41 * the f-word and ignore all other lines. |
42 */ | 42 */ |
43 | 43 |
44 function fail(expected, found, name_opt) { | 44 function MjsUnitToString(value) { |
45 var start; | 45 switch (typeof value) { |
46 if (name_opt) { | 46 case "string": |
47 // Fix this when we ditch the old test runner. | 47 return JSON.stringify(value); |
48 start = "Fail" + "ure (" + name_opt + "): "; | 48 case "number": |
49 } else { | 49 if (value === 0 && (1 / value) < 0) return "-0"; |
50 start = "Fail" + "ure:"; | 50 case "boolean": |
| 51 case "null": |
| 52 case "undefined": |
| 53 case "function": |
| 54 return String(value); |
| 55 case "object": |
| 56 if (value === null) return "null"; |
| 57 var clazz = Object.prototype.toString.call(value); |
| 58 clazz = clazz.substring(8, clazz.length - 1); |
| 59 switch (clazz) { |
| 60 case "Number": |
| 61 case "String": |
| 62 case "Boolean": |
| 63 case "Date": |
| 64 return clazz + "(" + MjsUnitToString(value.valueOf()) + ")"; |
| 65 case "RegExp": |
| 66 return value.toString(); |
| 67 case "Array": |
| 68 return "[" + value.map(MjsUnitArrayElementToString).join(",") + "]"; |
| 69 case "Object": |
| 70 break; |
| 71 default: |
| 72 return clazz + "()"; |
| 73 } |
| 74 // [[Class]] is "Object". |
| 75 var constructor = value.constructor.name; |
| 76 if (name) return name + "()"; |
| 77 return "Object()"; |
| 78 default: |
| 79 return "-- unknown value --"; |
51 } | 80 } |
52 throw new MjsUnitAssertionError(start + " expected <" + expected + "> found <"
+ found + ">"); | |
53 } | 81 } |
54 | 82 |
55 | 83 |
| 84 function MjsUnitArrayElementToString(value, index, array) { |
| 85 if (value === undefined && !(index in array)) return ""; |
| 86 return MjsUnitToString(value); |
| 87 } |
| 88 |
| 89 |
| 90 function fail(expected, found, name_opt) { |
| 91 var message = "Fail" + "ure"; |
| 92 if (name_opt) { |
| 93 // Fix this when we ditch the old test runner. |
| 94 message += " (" + name_opt + ")"; |
| 95 } |
| 96 |
| 97 message += ": expected <" + MjsUnitToString(expected) + |
| 98 "> found <" + MjsUnitToString(found) + ">"; |
| 99 throw new MjsUnitAssertionError(message); |
| 100 } |
| 101 |
| 102 |
56 function deepObjectEquals(a, b) { | 103 function deepObjectEquals(a, b) { |
57 var aProps = []; | 104 var aProps = []; |
58 for (var key in a) | 105 for (var key in a) |
59 aProps.push(key); | 106 aProps.push(key); |
60 var bProps = []; | 107 var bProps = []; |
61 for (var key in b) | 108 for (var key in b) |
62 bProps.push(key); | 109 bProps.push(key); |
63 aProps.sort(); | 110 aProps.sort(); |
64 bProps.sort(); | 111 bProps.sort(); |
65 if (!deepEquals(aProps, bProps)) | 112 if (!deepEquals(aProps, bProps)) |
66 return false; | 113 return false; |
67 for (var i = 0; i < aProps.length; i++) { | 114 for (var i = 0; i < aProps.length; i++) { |
68 if (!deepEquals(a[aProps[i]], b[aProps[i]])) | 115 if (!deepEquals(a[aProps[i]], b[aProps[i]])) |
69 return false; | 116 return false; |
70 } | 117 } |
71 return true; | 118 return true; |
72 } | 119 } |
73 | 120 |
74 | 121 |
75 function deepEquals(a, b) { | 122 function deepEquals(a, b) { |
76 if (a == b) return true; | 123 if (a == b) { |
| 124 // Check for -0. |
| 125 if (a === 0 && b === 0) return (1 / a) === (1 / b); |
| 126 return true; |
| 127 } |
77 if (typeof a == "number" && typeof b == "number" && isNaN(a) && isNaN(b)) { | 128 if (typeof a == "number" && typeof b == "number" && isNaN(a) && isNaN(b)) { |
78 return true; | 129 return true; |
79 } | 130 } |
80 if (a == null || b == null) return false; | 131 if (a == null || b == null) return false; |
81 if (a.constructor === RegExp || b.constructor === RegExp) { | 132 if (a.constructor === RegExp || b.constructor === RegExp) { |
82 return (a.constructor === b.constructor) && (a.toString === b.toString); | 133 return (a.constructor === b.constructor) && (a.toString() === b.toString()); |
83 } | 134 } |
84 if ((typeof a) !== 'object' || (typeof b) !== 'object' || | 135 if ((typeof a) !== 'object' || (typeof b) !== 'object' || |
85 (a === null) || (b === null)) | 136 (a === null) || (b === null)) |
86 return false; | 137 return false; |
87 if (a.constructor === Array) { | 138 if (a.constructor === Array) { |
88 if (b.constructor !== Array) | 139 if (b.constructor !== Array) |
89 return false; | 140 return false; |
90 if (a.length != b.length) | 141 if (a.length != b.length) |
91 return false; | 142 return false; |
92 for (var i = 0; i < a.length; i++) { | 143 for (var i = 0; i < a.length; i++) { |
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
198 eval(code); | 249 eval(code); |
199 } | 250 } |
200 } catch (e) { | 251 } catch (e) { |
201 assertTrue(false, "threw an exception: " + (e.message || e)); | 252 assertTrue(false, "threw an exception: " + (e.message || e)); |
202 } | 253 } |
203 } | 254 } |
204 | 255 |
205 | 256 |
206 function assertUnreachable(name_opt) { | 257 function assertUnreachable(name_opt) { |
207 // Fix this when we ditch the old test runner. | 258 // Fix this when we ditch the old test runner. |
208 var message = "Fail" + "ure: unreachable" | 259 var message = "Fail" + "ure: unreachable"; |
209 if (name_opt) { | 260 if (name_opt) { |
210 message += " - " + name_opt; | 261 message += " - " + name_opt; |
211 } | 262 } |
212 throw new MjsUnitAssertionError(message); | 263 throw new MjsUnitAssertionError(message); |
213 } | 264 } |
OLD | NEW |