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 MJSToString(value) { | |
Kevin Millikin (Chromium)
2011/03/21 11:25:39
You should adopt the same naming convention as the
Lasse Reichstein
2011/03/21 13:17:58
Renamed to MjsUnitToString.
| |
45 switch (typeof value) { | |
46 case "string": return JSON.stringify(value); | |
47 case "number": | |
48 if (value === 0 && 1/value < 0) return "-0"; | |
49 case "boolean": | |
50 case "null": | |
51 case "undefined": | |
52 case "function": | |
53 return String(value); | |
54 case "object": | |
55 if (value === null) return "null"; | |
56 var clazz = Object.prototype.toString.call(value); | |
57 clazz = clazz.substring(8, clazz.length - 1); | |
58 switch (clazz) { | |
59 case "Number": | |
60 case "String": | |
61 case "Boolean": | |
62 case "Date": | |
63 return clazz + "(" + MJSToString(value.valueOf()) + ")"; | |
64 case "RegExp": | |
65 return value.toString(); | |
66 case "Array": | |
67 return "[" + value.map(MJSToString).join("\n") + "]"; | |
68 case "Object": | |
69 break; | |
70 default: | |
71 return clazz + "()"; | |
72 } | |
73 // [[Class]] is "Object". | |
74 var constructor = value.constructor.name; | |
75 if (name) return name + "()"; | |
76 return "Object()"; | |
77 } | |
78 } | |
79 | |
80 | |
44 function fail(expected, found, name_opt) { | 81 function fail(expected, found, name_opt) { |
45 var start; | 82 var start; |
Kevin Millikin (Chromium)
2011/03/21 11:25:39
I can't figure out why this function is so convolu
Lasse Reichstein
2011/03/21 13:17:58
And it doesn't have to be.
I added the second case
Kevin Millikin (Chromium)
2011/03/21 13:37:36
Here's where we need printf :)
Lasse Reichstein
2011/03/21 14:18:26
Indeed! :)
| |
46 if (name_opt) { | 83 if (name_opt) { |
47 // Fix this when we ditch the old test runner. | 84 // Fix this when we ditch the old test runner. |
48 start = "Fail" + "ure (" + name_opt + "): "; | 85 start = "Fail" + "ure (" + name_opt + "): "; |
49 } else { | 86 } else { |
50 start = "Fail" + "ure:"; | 87 start = "Fail" + "ure:"; |
51 } | 88 } |
52 throw new MjsUnitAssertionError(start + " expected <" + expected + "> found <" + found + ">"); | 89 var message; |
90 var expected_string = MJSToString(expected); | |
91 var found_string = MJSToString(found); | |
92 if (typeof expected === typeof found) { | |
93 message = | |
94 " expected <" + expected_string + "> found <" + found_string + ">"; | |
95 } else { | |
96 message = " expected <" + expected_string + " (" + | |
97 typeof expected + ")> found <" + | |
98 found_string + " (" + typeof found + ")>"; | |
99 } | |
100 throw new MjsUnitAssertionError(start + message); | |
53 } | 101 } |
54 | 102 |
55 | 103 |
56 function deepObjectEquals(a, b) { | 104 function deepObjectEquals(a, b) { |
57 var aProps = []; | 105 var aProps = []; |
58 for (var key in a) | 106 for (var key in a) |
59 aProps.push(key); | 107 aProps.push(key); |
60 var bProps = []; | 108 var bProps = []; |
61 for (var key in b) | 109 for (var key in b) |
62 bProps.push(key); | 110 bProps.push(key); |
63 aProps.sort(); | 111 aProps.sort(); |
64 bProps.sort(); | 112 bProps.sort(); |
65 if (!deepEquals(aProps, bProps)) | 113 if (!deepEquals(aProps, bProps)) |
66 return false; | 114 return false; |
67 for (var i = 0; i < aProps.length; i++) { | 115 for (var i = 0; i < aProps.length; i++) { |
68 if (!deepEquals(a[aProps[i]], b[aProps[i]])) | 116 if (!deepEquals(a[aProps[i]], b[aProps[i]])) |
69 return false; | 117 return false; |
70 } | 118 } |
71 return true; | 119 return true; |
72 } | 120 } |
73 | 121 |
74 | 122 |
75 function deepEquals(a, b) { | 123 function deepEquals(a, b) { |
76 if (a == b) return true; | 124 if (a == b) { |
125 // Check for -0. | |
126 if (a === 0 && b === 0) return (1 / a) === (1 / b); | |
127 return true; | |
128 } | |
77 if (typeof a == "number" && typeof b == "number" && isNaN(a) && isNaN(b)) { | 129 if (typeof a == "number" && typeof b == "number" && isNaN(a) && isNaN(b)) { |
78 return true; | 130 return true; |
79 } | 131 } |
80 if (a == null || b == null) return false; | 132 if (a == null || b == null) return false; |
81 if (a.constructor === RegExp || b.constructor === RegExp) { | 133 if (a.constructor === RegExp || b.constructor === RegExp) { |
82 return (a.constructor === b.constructor) && (a.toString === b.toString); | 134 return (a.constructor === b.constructor) && (a.toString === b.toString); |
83 } | 135 } |
84 if ((typeof a) !== 'object' || (typeof b) !== 'object' || | 136 if ((typeof a) !== 'object' || (typeof b) !== 'object' || |
85 (a === null) || (b === null)) | 137 (a === null) || (b === null)) |
86 return false; | 138 return false; |
(...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
204 | 256 |
205 | 257 |
206 function assertUnreachable(name_opt) { | 258 function assertUnreachable(name_opt) { |
207 // Fix this when we ditch the old test runner. | 259 // Fix this when we ditch the old test runner. |
208 var message = "Fail" + "ure: unreachable" | 260 var message = "Fail" + "ure: unreachable" |
209 if (name_opt) { | 261 if (name_opt) { |
210 message += " - " + name_opt; | 262 message += " - " + name_opt; |
211 } | 263 } |
212 throw new MjsUnitAssertionError(message); | 264 throw new MjsUnitAssertionError(message); |
213 } | 265 } |
OLD | NEW |