OLD | NEW |
1 // Copyright 2013 the V8 project authors. All rights reserved. | 1 // Copyright 2013 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 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
80 } | 80 } |
81 return true; | 81 return true; |
82 } | 82 } |
83 if (objectClass == 'String' || objectClass == 'Number' || | 83 if (objectClass == 'String' || objectClass == 'Number' || |
84 objectClass == 'Boolean' || objectClass == 'Date') { | 84 objectClass == 'Boolean' || objectClass == 'Date') { |
85 if (a.valueOf() !== b.valueOf()) return false; | 85 if (a.valueOf() !== b.valueOf()) return false; |
86 } | 86 } |
87 return deepObjectEquals(a, b); | 87 return deepObjectEquals(a, b); |
88 } | 88 } |
89 | 89 |
90 | |
91 /** | 90 /** |
92 * Throws an exception, and prints the values in case of error. | 91 * Throws an exception containing the user_message (if any) and the values. |
93 */ | 92 */ |
94 function fail(expected, found) { | 93 function fail(expected, found, user_message = '') { |
95 // TODO(cira): Replace String with PrettyPrint for objects and arrays. | 94 // TODO(cira): Replace String with PrettyPrint for objects and arrays. |
96 var message = 'Failure: expected <' + String(expected) + '>, found <' + | 95 var message = 'Failure' + (user_message ? ' (' + user_message + ')' : '') + |
97 String(found) + '>.'; | 96 ': expected <' + String(expected) + '>, found <' + String(found) + '>.'; |
98 throw new Error(message); | 97 throw new Error(message); |
99 } | 98 } |
100 | 99 |
101 | 100 |
102 /** | 101 /** |
103 * Throws if two variables have different types or values. | 102 * Throws if two variables have different types or values. |
104 */ | 103 */ |
105 function assertEquals(expected, found) { | 104 function assertEquals(expected, found, user_message = '') { |
106 if (!deepEquals(expected, found)) { | 105 if (!deepEquals(expected, found)) { |
107 fail(expected, found); | 106 fail(expected, found, user_message); |
108 } | 107 } |
109 } | 108 } |
110 | 109 |
111 | 110 |
112 /** | 111 /** |
113 * Throws if value is false. | 112 * Throws if value is false. |
114 */ | 113 */ |
115 function assertTrue(value) { | 114 function assertTrue(value, user_message = '') { |
116 assertEquals(true, value) | 115 assertEquals(true, value, user_message); |
117 } | 116 } |
118 | 117 |
119 | 118 |
120 /** | 119 /** |
121 * Throws if value is true. | 120 * Throws if value is true. |
122 */ | 121 */ |
123 function assertFalse(value) { | 122 function assertFalse(value, user_message = '') { |
124 assertEquals(false, value); | 123 assertEquals(false, value, user_message); |
125 } | 124 } |
126 | 125 |
127 | 126 |
128 /** | 127 /** |
129 * Returns true if code throws specified exception. | 128 * Runs code() and asserts that it throws the specified exception. |
130 */ | 129 */ |
131 function assertThrows(code, type_opt, cause_opt) { | 130 function assertThrows(code, type_opt, cause_opt) { |
132 var threwException = true; | |
133 try { | 131 try { |
134 if (typeof code == 'function') { | 132 if (typeof code == 'function') { |
135 code(); | 133 code(); |
136 } else { | 134 } else { |
137 eval(code); | 135 eval(code); |
138 } | 136 } |
139 threwException = false; | |
140 } catch (e) { | 137 } catch (e) { |
141 if (typeof type_opt == 'function') { | 138 if (typeof type_opt == 'function') { |
142 assertInstanceof(e, type_opt); | 139 assertInstanceof(e, type_opt); |
143 } | 140 } |
144 if (arguments.length >= 3) { | 141 if (arguments.length >= 3) { |
145 assertEquals(e.type, cause_opt); | 142 assertEquals(cause_opt, e.type, 'thrown exception type mismatch'); |
146 } | 143 } |
147 // Success. | 144 // Success. |
148 return; | 145 return; |
149 } | 146 } |
150 throw new Error("Did not throw exception"); | 147 var expected = arguments.length >= 3 ? cause_opt : |
| 148 typeof type_opt == 'function' ? type_opt : 'any exception'; |
| 149 fail(expected, 'no exception', 'expected thrown exception'); |
151 } | 150 } |
152 | 151 |
153 | 152 |
154 /** | 153 /** |
155 * Throws an exception if code throws. | 154 * Runs code() and asserts that it does now throw any exception. |
156 */ | 155 */ |
157 function assertDoesNotThrow(code, name_opt) { | 156 function assertDoesNotThrow(code, user_message = '') { |
158 try { | 157 try { |
159 if (typeof code == 'function') { | 158 if (typeof code == 'function') { |
160 code(); | 159 code(); |
161 } else { | 160 } else { |
162 eval(code); | 161 eval(code); |
163 } | 162 } |
164 } catch (e) { | 163 } catch (e) { |
165 fail("threw an exception: ", e.message || e, name_opt); | 164 fail("no expection", "exception: " + String(e), user_message); |
166 } | 165 } |
167 } | 166 } |
168 | 167 |
169 | 168 |
170 /** | 169 /** |
171 * Throws if obj is not of given type. | 170 * Throws if obj is not of given type. |
172 */ | 171 */ |
173 function assertInstanceof(obj, type) { | 172 function assertInstanceof(obj, type) { |
174 if (!(obj instanceof type)) { | 173 if (!(obj instanceof type)) { |
175 var actualTypeName = null; | 174 var actualTypeName = null; |
176 var actualConstructor = Object.prototypeOf(obj).constructor; | 175 var actualConstructor = Object.prototypeOf(obj).constructor; |
177 if (typeof actualConstructor == "function") { | 176 if (typeof actualConstructor == "function") { |
178 actualTypeName = actualConstructor.name || String(actualConstructor); | 177 actualTypeName = actualConstructor.name || String(actualConstructor); |
179 } | 178 } |
180 throw new Error('Object <' + obj + '> is not an instance of <' + | 179 throw new Error('Object <' + obj + '> is not an instance of <' + |
181 (type.name || type) + '>' + | 180 (type.name || type) + '>' + |
182 (actualTypeName ? ' but of < ' + actualTypeName + '>' : '')); | 181 (actualTypeName ? ' but of < ' + actualTypeName + '>' : '')); |
183 } | 182 } |
184 } | 183 } |
OLD | NEW |