Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(766)

Side by Side Diff: test/mjsunit/mjsunit.js

Issue 93066: Built-in JSON support (Closed)
Patch Set: Created 11 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
44 if (name_opt) { 44 if (name_opt) {
45 // Fix this when we ditch the old test runner. 45 // Fix this when we ditch the old test runner.
46 start = "Fail" + "ure (" + name_opt + "): "; 46 start = "Fail" + "ure (" + name_opt + "): ";
47 } else { 47 } else {
48 start = "Fail" + "ure:"; 48 start = "Fail" + "ure:";
49 } 49 }
50 throw new MjsUnitAssertionError(start + " expected <" + expected + "> found <" + found + ">"); 50 throw new MjsUnitAssertionError(start + " expected <" + expected + "> found <" + found + ">");
51 } 51 }
52 52
53 53
54 function deepObjectEquals(a, b) {
55 var aProps = [];
56 for (var key in a)
57 aProps.push(key);
58 var bProps = [];
59 for (var key in b)
60 bProps.push(key);
61 aProps.sort();
62 bProps.sort();
63 if (!deepEquals(aProps, bProps))
64 return false;
65 for (var i = 0; i < aProps.length; i++) {
66 if (!deepEquals(a[aProps[i]], b[aProps[i]]))
67 return false;
68 }
69 return true;
70 }
71
72
54 function deepEquals(a, b) { 73 function deepEquals(a, b) {
55 if (a == b) return true; 74 if (a == b) return true;
56 if (typeof a == "number" && typeof b == "number" && isNaN(a) && isNaN(b)) { 75 if (typeof a == "number" && typeof b == "number" && isNaN(a) && isNaN(b)) {
57 return true; 76 return true;
58 } 77 }
59 if ((typeof a) !== 'object' || (typeof b) !== 'object' || 78 if ((typeof a) !== 'object' || (typeof b) !== 'object' ||
60 (a === null) || (b === null)) 79 (a === null) || (b === null))
61 return false; 80 return false;
62 if (a.constructor === Array) { 81 if (a.constructor === Array) {
63 if (b.constructor !== Array) 82 if (b.constructor !== Array)
64 return false; 83 return false;
65 if (a.length != b.length) 84 if (a.length != b.length)
66 return false; 85 return false;
67 for (var i = 0; i < a.length; i++) { 86 for (var i = 0; i < a.length; i++) {
68 if (i in a) { 87 if (i in a) {
69 if (!(i in b) || !(deepEquals(a[i], b[i]))) 88 if (!(i in b) || !(deepEquals(a[i], b[i])))
70 return false; 89 return false;
71 } else if (i in b) { 90 } else if (i in b) {
72 return false; 91 return false;
73 } 92 }
74 } 93 }
75 return true; 94 return true;
95 } else {
96 return deepObjectEquals(a, b);
76 } 97 }
77 return false;
78 } 98 }
79 99
80 100
81 function assertEquals(expected, found, name_opt) { 101 function assertEquals(expected, found, name_opt) {
82 if (!deepEquals(found, expected)) { 102 if (!deepEquals(found, expected)) {
83 fail(expected, found, name_opt); 103 fail(expected, found, name_opt);
84 } 104 }
85 } 105 }
86 106
87 107
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
123 } 143 }
124 144
125 145
126 function assertNotNull(value, name_opt) { 146 function assertNotNull(value, name_opt) {
127 if (value === null) { 147 if (value === null) {
128 fail("not null", value, name_opt); 148 fail("not null", value, name_opt);
129 } 149 }
130 } 150 }
131 151
132 152
133 function assertThrows(code) { 153 function assertThrows(code, type_opt, cause_opt) {
134 var threwException = true; 154 var threwException = true;
135 try { 155 try {
136 eval(code); 156 if (typeof code == 'function') {
157 code();
158 } else {
159 eval(code);
160 }
137 threwException = false; 161 threwException = false;
138 } catch (e) { 162 } catch (e) {
163 if (typeof type_opt == 'function')
164 assertInstanceof(e, type_opt);
165 if (arguments.length >= 3)
166 assertEquals(e.type, cause_opt);
139 // Do nothing. 167 // Do nothing.
140 } 168 }
141 if (!threwException) assertTrue(false, "did not throw exception"); 169 if (!threwException) assertTrue(false, "did not throw exception");
142 } 170 }
143 171
144 172
145 function assertInstanceof(obj, type) { 173 function assertInstanceof(obj, type) {
146 if (!(obj instanceof type)) { 174 if (!(obj instanceof type)) {
147 assertTrue(false, "Object <" + obj + "> is not an instance of <" + type + "> "); 175 assertTrue(false, "Object <" + obj + "> is not an instance of <" + type + "> ");
148 } 176 }
(...skipping 10 matching lines...) Expand all
159 187
160 188
161 function assertUnreachable(name_opt) { 189 function assertUnreachable(name_opt) {
162 // Fix this when we ditch the old test runner. 190 // Fix this when we ditch the old test runner.
163 var message = "Fail" + "ure: unreachable" 191 var message = "Fail" + "ure: unreachable"
164 if (name_opt) { 192 if (name_opt) {
165 message += " - " + name_opt; 193 message += " - " + name_opt;
166 } 194 }
167 throw new MjsUnitAssertionError(message); 195 throw new MjsUnitAssertionError(message);
168 } 196 }
OLDNEW
« src/objects.h ('K') | « test/mjsunit/json.js ('k') | tools/visual_studio/js2c.cmd » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698