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

Side by Side Diff: test/intl/assert.js

Issue 1866693002: Allow to pass a user message to assert functions (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: Created 4 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
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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 + ')' : "") +
JF 2016/04/06 17:00:20 Mixing single and double quotes is odd :-)
Clemens Hammacher 2016/04/07 07:42:52 Done.
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 * Returns if the code throws the specified exception, throws otherwise.
titzer 2016/04/06 18:22:10 You can just reword to "Runs {code} and asserts th
Clemens Hammacher 2016/04/07 07:42:52 Done.
130 */ 129 */
131 function assertThrows(code, type_opt, cause_opt) { 130 function assertThrows(code, type_opt, cause_opt) {
132 var threwException = true; 131 var threwException = true;
titzer 2016/04/06 18:22:10 I just noticed this variable is dead.
Clemens Hammacher 2016/04/07 07:42:52 Right, I will remove it.
133 try { 132 try {
134 if (typeof code == 'function') { 133 if (typeof code == 'function') {
135 code(); 134 code();
136 } else { 135 } else {
137 eval(code); 136 eval(code);
138 } 137 }
139 threwException = false; 138 threwException = false;
140 } catch (e) { 139 } catch (e) {
141 if (typeof type_opt == 'function') { 140 if (typeof type_opt == 'function') {
142 assertInstanceof(e, type_opt); 141 assertInstanceof(e, type_opt);
143 } 142 }
144 if (arguments.length >= 3) { 143 if (arguments.length >= 3) {
145 assertEquals(e.type, cause_opt); 144 assertEquals(cause_opt, e.type, 'thrown exception type mismatch');
146 } 145 }
147 // Success. 146 // Success.
148 return; 147 return;
149 } 148 }
150 throw new Error("Did not throw exception"); 149 var expected = arguments.length >= 3 ? cause_opt :
150 typeof type_opt == 'function' ? type_opt : 'any exception';
151 fail(expected, "no exception", "expected thrown exception");
151 } 152 }
152 153
153 154
154 /** 155 /**
155 * Throws an exception if code throws. 156 * Throws an exception if code throws.
titzer 2016/04/06 18:22:10 You can reword this similarly. "Runs {code} and as
Clemens Hammacher 2016/04/07 07:42:52 Done.
156 */ 157 */
157 function assertDoesNotThrow(code, name_opt) { 158 function assertDoesNotThrow(code, user_message = '') {
158 try { 159 try {
159 if (typeof code == 'function') { 160 if (typeof code == 'function') {
160 code(); 161 code();
161 } else { 162 } else {
162 eval(code); 163 eval(code);
163 } 164 }
164 } catch (e) { 165 } catch (e) {
165 fail("threw an exception: ", e.message || e, name_opt); 166 fail("no expection", "exception: " + String(e), user_message);
166 } 167 }
167 } 168 }
168 169
169 170
170 /** 171 /**
171 * Throws if obj is not of given type. 172 * Throws if obj is not of given type.
172 */ 173 */
173 function assertInstanceof(obj, type) { 174 function assertInstanceof(obj, type) {
174 if (!(obj instanceof type)) { 175 if (!(obj instanceof type)) {
175 var actualTypeName = null; 176 var actualTypeName = null;
176 var actualConstructor = Object.prototypeOf(obj).constructor; 177 var actualConstructor = Object.prototypeOf(obj).constructor;
177 if (typeof actualConstructor == "function") { 178 if (typeof actualConstructor == "function") {
178 actualTypeName = actualConstructor.name || String(actualConstructor); 179 actualTypeName = actualConstructor.name || String(actualConstructor);
179 } 180 }
180 throw new Error('Object <' + obj + '> is not an instance of <' + 181 throw new Error('Object <' + obj + '> is not an instance of <' +
181 (type.name || type) + '>' + 182 (type.name || type) + '>' +
182 (actualTypeName ? ' but of < ' + actualTypeName + '>' : '')); 183 (actualTypeName ? ' but of < ' + actualTypeName + '>' : ''));
183 } 184 }
184 } 185 }
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698