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

Side by Side Diff: chrome/test/base/js2gtest.js

Issue 1299893002: Improve error handling for js2gtest C++ generation. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Add missing newline. Created 5 years, 3 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 | « chrome/test/base/OWNERS ('k') | tools/gypv8sh.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 /** 5 /**
6 * @fileoverview Generator script for creating gtest-style JavaScript 6 * @fileoverview Generator script for creating gtest-style JavaScript
7 * tests for extensions, WebUI and unit tests. Generates C++ gtest wrappers 7 * tests for extensions, WebUI and unit tests. Generates C++ gtest wrappers
8 * which will invoke the appropriate JavaScript for each test. 8 * which will invoke the appropriate JavaScript for each test.
9 * @author scr@chromium.org (Sheridan Rawlins) 9 * @author scr@chromium.org (Sheridan Rawlins)
10 * @see WebUI testing: http://goo.gl/ZWFXF 10 * @see WebUI testing: http://goo.gl/ZWFXF
11 * @see gtest documentation: http://goo.gl/Ujj3H 11 * @see gtest documentation: http://goo.gl/Ujj3H
12 * @see chrome/chrome_tests.gypi 12 * @see chrome/chrome_tests.gypi
13 * @see chrome/test/base/js2gtest.js
13 * @see tools/gypv8sh.py 14 * @see tools/gypv8sh.py
14 */ 15 */
15 16
16 // Arguments from rules in chrome_tests.gypi are passed in through 17 // Arguments from rules in chrome_tests.gypi are passed in through
17 // python script gypv8sh.py. 18 // python script gypv8sh.py.
18 if (arguments.length != 6) { 19 if (arguments.length != 6) {
19 print('usage: ' + 20 print('usage: ' +
20 arguments[0] + 21 arguments[0] +
21 ' path-to-testfile.js testfile.js path_to_deps.js output.cc test-type'); 22 ' path-to-testfile.js testfile.js path_to_deps.js output.cc test-type');
22 quit(-1); 23 quit(-1);
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
92 * @type {boolean} 93 * @type {boolean}
93 */ 94 */
94 var needGenHeader = true; 95 var needGenHeader = true;
95 96
96 /** 97 /**
97 * Helpful hint pointing back to the source js. 98 * Helpful hint pointing back to the source js.
98 * @type {string} 99 * @type {string}
99 */ 100 */
100 var argHint = '// ' + this['arguments'].join(' '); 101 var argHint = '// ' + this['arguments'].join(' ');
101 102
103 /**
104 * @type {Array<string>}
105 */
106 var pendingOutput = '';
107
108 /**
109 * Adds a string followed by a newline to the pending output.
110 * If present, an initial newline character is stripped from the string.
111 * @param {string=} opt_string
112 */
113 function output(opt_string) {
114 opt_string = opt_string || '';
115 if (opt_string[0] == '\n')
116 opt_string = opt_string.substring(1);
117 pendingOutput += opt_string;
118 pendingOutput += '\n';
119 }
102 120
103 /** 121 /**
104 * Generates the header of the cc file to stdout. 122 * Generates the header of the cc file to stdout.
105 * @param {string?} testFixture Name of test fixture. 123 * @param {string?} testFixture Name of test fixture.
106 */ 124 */
107 function maybeGenHeader(testFixture) { 125 function maybeGenHeader(testFixture) {
108 if (!needGenHeader) 126 if (!needGenHeader)
109 return; 127 return;
110 needGenHeader = false; 128 needGenHeader = false;
111 print('// GENERATED FILE'); 129 output(`
112 print(argHint); 130 // GENERATED FILE'
113 print('// PLEASE DO NOT HAND EDIT!'); 131 ${argHint}
114 print(); 132 // PLEASE DO NOT HAND EDIT!
133 `);
115 134
116 // Output some C++ headers based upon the |testType|. 135 // Output some C++ headers based upon the |testType|.
117 // 136 //
118 // Currently supports: 137 // Currently supports:
119 // 'extension' - browser_tests harness, js2extension rule, 138 // 'extension' - browser_tests harness, js2extension rule,
120 // ExtensionJSBrowserTest superclass. 139 // ExtensionJSBrowserTest superclass.
121 // 'unit' - unit_tests harness, js2unit rule, V8UnitTest superclass. 140 // 'unit' - unit_tests harness, js2unit rule, V8UnitTest superclass.
122 // 'webui' - browser_tests harness, js2webui rule, WebUIBrowserTest 141 // 'webui' - browser_tests harness, js2webui rule, WebUIBrowserTest
123 // superclass. 142 // superclass.
124 if (testType === 'extension') { 143 if (testType === 'extension') {
125 print('#include "chrome/test/base/extension_js_browser_test.h"'); 144 output('#include "chrome/test/base/extension_js_browser_test.h"');
126 testing.Test.prototype.typedefCppFixture = 'ExtensionJSBrowserTest'; 145 testing.Test.prototype.typedefCppFixture = 'ExtensionJSBrowserTest';
127 addSetPreloadInfo = false; 146 addSetPreloadInfo = false;
128 testF = 'IN_PROC_BROWSER_TEST_F'; 147 testF = 'IN_PROC_BROWSER_TEST_F';
129 } else if (testType === 'unit') { 148 } else if (testType === 'unit') {
130 print('#include "chrome/test/base/v8_unit_test.h"'); 149 output('#include "chrome/test/base/v8_unit_test.h"');
131 testing.Test.prototype.typedefCppFixture = 'V8UnitTest'; 150 testing.Test.prototype.typedefCppFixture = 'V8UnitTest';
132 testF = 'TEST_F'; 151 testF = 'TEST_F';
133 addSetPreloadInfo = false; 152 addSetPreloadInfo = false;
134 } else { 153 } else {
135 print('#include "chrome/test/base/web_ui_browser_test.h"'); 154 output('#include "chrome/test/base/web_ui_browser_test.h"');
136 testing.Test.prototype.typedefCppFixture = 'WebUIBrowserTest'; 155 testing.Test.prototype.typedefCppFixture = 'WebUIBrowserTest';
137 testF = 'IN_PROC_BROWSER_TEST_F'; 156 testF = 'IN_PROC_BROWSER_TEST_F';
138 addSetPreloadInfo = true; 157 addSetPreloadInfo = true;
139 } 158 }
140 print('#include "url/gurl.h"'); 159 output(`
141 print('#include "testing/gtest/include/gtest/gtest.h"'); 160 #include "url/gurl.h"
161 #include "testing/gtest/include/gtest/gtest.h"`);
142 // Add includes specified by test fixture. 162 // Add includes specified by test fixture.
143 if (testFixture) { 163 if (testFixture) {
144 if (this[testFixture].prototype.testGenCppIncludes) 164 if (this[testFixture].prototype.testGenCppIncludes)
145 this[testFixture].prototype.testGenCppIncludes(); 165 this[testFixture].prototype.testGenCppIncludes();
146 if (this[testFixture].prototype.commandLineSwitches) 166 if (this[testFixture].prototype.commandLineSwitches)
147 print('#include "base/command_line.h"'); 167 output('#include "base/command_line.h"');
148 } 168 }
149 print(); 169 output();
150 } 170 }
151 171
152 172
153 /** 173 /**
154 * @type {Array<{path: string, base: string>} 174 * @type {Array<{path: string, base: string>}
155 */ 175 */
156 var pathStack = []; 176 var pathStack = [];
157 177
158 178
159 /** 179 /**
160 * Convert the |includeFile| to paths appropriate for immediate 180 * Convert the |includeFile| to paths appropriate for immediate
161 * inclusion (path) and runtime inclusion (base). 181 * inclusion (path) and runtime inclusion (base).
162 * @param {string} includeFile The file to include. 182 * @param {string} includeFile The file to include.
163 * @return {{path: string, base: string}} Object describing the paths 183 * @return {{path: string, base: string}} Object describing the paths
164 * for |includeFile|. |path| is relative to cwd; |base| is relative to 184 * for |includeFile|. |path| is relative to cwd; |base| is relative to
165 * source root. 185 * source root.
166 */ 186 */
167 function includeFileToPaths(includeFile) { 187 function includeFileToPaths(includeFile) {
168 paths = pathStack[pathStack.length - 1]; 188 paths = pathStack[pathStack.length - 1];
169 return { 189 return {
170 path: paths.path.replace(/[^\/\\]+$/, includeFile), 190 path: paths.path.replace(/[^\/\\]+$/, includeFile),
171 base: paths.base.replace(/[^\/\\]+$/, includeFile), 191 base: paths.base.replace(/[^\/\\]+$/, includeFile),
172 }; 192 };
173 } 193 }
174 194
195 /**
196 * Returns the content of a javascript file with a sourceURL comment
197 * appended to facilitate better stack traces.
198 * @param {string} path Relative path name.
199 * return {string}
200 */
201 function readJsFile(path) {
202 return read(path) + '\n//# sourceURL=' + path;
203 }
204
175 205
176 /** 206 /**
177 * Maps object names to the path to the file that provides them. 207 * Maps object names to the path to the file that provides them.
178 * Populated from the |depsFile| if any. 208 * Populated from the |depsFile| if any.
179 * @type {Object<string>} 209 * @type {Object<string>}
180 */ 210 */
181 var dependencyProvidesToPaths = {}; 211 var dependencyProvidesToPaths = {};
182 212
183 /** 213 /**
184 * Maps dependency path names to object names required by the file. 214 * Maps dependency path names to object names required by the file.
(...skipping 13 matching lines...) Expand all
198 */ 228 */
199 goog.addDependency = function(path, provides, requires) { 229 goog.addDependency = function(path, provides, requires) {
200 provides.forEach(function(provide) { 230 provides.forEach(function(provide) {
201 dependencyProvidesToPaths[provide] = path; 231 dependencyProvidesToPaths[provide] = path;
202 }); 232 });
203 dependencyPathsToRequires[path] = requires; 233 dependencyPathsToRequires[path] = requires;
204 }; 234 };
205 235
206 // Read and eval the deps file. It should only contain goog.addDependency 236 // Read and eval the deps file. It should only contain goog.addDependency
207 // calls. 237 // calls.
208 eval(read(depsFile)); 238 eval(readJsFile(depsFile));
209 } 239 }
210 240
211 /** 241 /**
212 * Resolves a list of libraries to an ordered list of paths to load by the 242 * Resolves a list of libraries to an ordered list of paths to load by the
213 * generated C++. The input should contain object names provided 243 * generated C++. The input should contain object names provided
214 * by the deps file. Dependencies will be resolved and included in the 244 * by the deps file. Dependencies will be resolved and included in the
215 * correct order, meaning that the returned array may contain more entries 245 * correct order, meaning that the returned array may contain more entries
216 * than the input. 246 * than the input.
217 * @param {Array<string>} deps List of dependencies. 247 * @param {Array<string>} deps List of dependencies.
218 * @return {Array<string>} List of paths to load. 248 * @return {Array<string>} List of paths to load.
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
265 295
266 return resultPaths; 296 return resultPaths;
267 } 297 }
268 298
269 /** 299 /**
270 * Output |code| verbatim. 300 * Output |code| verbatim.
271 * @param {string} code The code to output. 301 * @param {string} code The code to output.
272 */ 302 */
273 function GEN(code) { 303 function GEN(code) {
274 maybeGenHeader(null); 304 maybeGenHeader(null);
275 print(code); 305 output(code);
276 } 306 }
277 307
278 /** 308 /**
279 * Outputs |commentEncodedCode|, converting comment to enclosed C++ code. 309 * Outputs |commentEncodedCode|, converting comment to enclosed C++ code.
280 * @param {function} commentEncodedCode A function in the following format (note 310 * @param {function} commentEncodedCode A function in the following format (note
281 * the space in '/ *' and '* /' should be removed to form a comment delimiter): 311 * the space in '/ *' and '* /' should be removed to form a comment delimiter):
282 * function() {/ *! my_cpp_code.DoSomething(); * / 312 * function() {/ *! my_cpp_code.DoSomething(); * /
283 * Code between / *! and * / will be extracted and written to stdout. 313 * Code between / *! and * / will be extracted and written to stdout.
284 */ 314 */
285 function GEN_BLOCK(commentEncodedCode) { 315 function GEN_BLOCK(commentEncodedCode) {
286 var code = commentEncodedCode.toString(). 316 var code = commentEncodedCode.toString().
287 replace(/^[^\/]+\/\*!?/, ''). 317 replace(/^[^\/]+\/\*!?/, '').
288 replace(/\*\/[^\/]+$/, ''). 318 replace(/\*\/[^\/]+$/, '').
289 replace(/^\n|\n$/, ''). 319 replace(/^\n|\n$/, '').
290 replace(/\s+$/, ''); 320 replace(/\s+$/, '');
291 GEN(code); 321 GEN(code);
292 } 322 }
293 323
294 /** 324 /**
295 * Generate includes for the current |jsFile| by including them 325 * Generate includes for the current |jsFile| by including them
296 * immediately and at runtime. 326 * immediately and at runtime.
297 * The paths must be relative to the directory of the current file. 327 * The paths must be relative to the directory of the current file.
298 * @param {Array<string>} includes Paths to JavaScript files to 328 * @param {Array<string>} includes Paths to JavaScript files to
299 * include immediately and at runtime. 329 * include immediately and at runtime.
300 */ 330 */
301 function GEN_INCLUDE(includes) { 331 function GEN_INCLUDE(includes) {
302 for (var i = 0; i < includes.length; i++) { 332 for (var i = 0; i < includes.length; i++) {
303 var includePaths = includeFileToPaths(includes[i]); 333 var includePaths = includeFileToPaths(includes[i]);
304 var js = read(includePaths.path); 334 var js = readJsFile(includePaths.path);
305 pathStack.push(includePaths); 335 pathStack.push(includePaths);
306 ('global', eval)(js); 336 ('global', eval)(js);
307 pathStack.pop(); 337 pathStack.pop();
308 genIncludes.push(includePaths.base); 338 genIncludes.push(includePaths.base);
309 } 339 }
310 } 340 }
311 341
312 /** 342 /**
313 * Generate gtest-style TEST_F definitions for C++ with a body that 343 * Generate gtest-style TEST_F definitions for C++ with a body that
314 * will invoke the |testBody| for |testFixture|.|testFunction|. 344 * will invoke the |testBody| for |testFixture|.|testFunction|.
315 * @param {string} testFixture The name of this test's fixture. 345 * @param {string} testFixture The name of this test's fixture.
316 * @param {string} testFunction The name of this test's function. 346 * @param {string} testFunction The name of this test's function.
317 * @param {Function} testBody The function body to execute for this test. 347 * @param {Function} testBody The function body to execute for this test.
318 */ 348 */
319 function TEST_F(testFixture, testFunction, testBody) { 349 function TEST_F(testFixture, testFunction, testBody) {
320 maybeGenHeader(testFixture); 350 maybeGenHeader(testFixture);
321 var browsePreload = this[testFixture].prototype.browsePreload; 351 var browsePreload = this[testFixture].prototype.browsePreload;
322 var browsePrintPreload = this[testFixture].prototype.browsePrintPreload; 352 var browsePrintPreload = this[testFixture].prototype.browsePrintPreload;
323 var testGenPreamble = this[testFixture].prototype.testGenPreamble; 353 var testGenPreamble = this[testFixture].prototype.testGenPreamble;
324 var testGenPostamble = this[testFixture].prototype.testGenPostamble; 354 var testGenPostamble = this[testFixture].prototype.testGenPostamble;
325 var typedefCppFixture = this[testFixture].prototype.typedefCppFixture; 355 var typedefCppFixture = this[testFixture].prototype.typedefCppFixture;
326 var isAsyncParam = testType === 'unit' ? '' : 356 var isAsyncParam = testType === 'unit' ? '' :
327 this[testFixture].prototype.isAsync + ', '; 357 this[testFixture].prototype.isAsync + ',\n ';
328 var testShouldFail = this[testFixture].prototype.testShouldFail; 358 var testShouldFail = this[testFixture].prototype.testShouldFail;
329 var testPredicate = testShouldFail ? 'ASSERT_FALSE' : 'ASSERT_TRUE'; 359 var testPredicate = testShouldFail ? 'ASSERT_FALSE' : 'ASSERT_TRUE';
330 var extraLibraries = genIncludes.concat( 360 var extraLibraries = genIncludes.concat(
331 this[testFixture].prototype.extraLibraries.map( 361 this[testFixture].prototype.extraLibraries.map(
332 function(includeFile) { 362 function(includeFile) {
333 return includeFileToPaths(includeFile).base; 363 return includeFileToPaths(includeFile).base;
334 }), 364 }),
335 resolveClosureModuleDeps(this[testFixture].prototype.closureModuleDeps)); 365 resolveClosureModuleDeps(this[testFixture].prototype.closureModuleDeps));
336 366
337 if (typedefCppFixture && !(testFixture in typedeffedCppFixtures)) { 367 if (typedefCppFixture && !(testFixture in typedeffedCppFixtures)) {
338 var switches = this[testFixture].prototype.commandLineSwitches; 368 var switches = this[testFixture].prototype.commandLineSwitches;
339 if (!switches || !switches.length || typedefCppFixture == 'V8UnitTest') { 369 if (!switches || !switches.length || typedefCppFixture == 'V8UnitTest') {
340 print('typedef ' + typedefCppFixture + ' ' + testFixture + ';'); 370 output(`
371 typedef ${typedefCppFixture} ${testFixture};
372 `);
341 } else { 373 } else {
342 // Make the testFixture a class inheriting from the base fixture. 374 // Make the testFixture a class inheriting from the base fixture.
343 print('class ' + testFixture + ' : public ' + typedefCppFixture + ' {'); 375 output(`
344 print(' private:'); 376 class ${testFixture} : public ${typedefCppFixture} {
377 private:`);
345 // Override SetUpCommandLine and add each switch. 378 // Override SetUpCommandLine and add each switch.
346 print(' void'); 379 output(`
347 print(' SetUpCommandLine(base::CommandLine* command_line) override {'); 380 void SetUpCommandLine(base::CommandLine* command_line) override {`);
348 for (var i = 0; i < switches.length; i++) { 381 for (var i = 0; i < switches.length; i++) {
349 print(' command_line->AppendSwitchASCII('); 382 output(`
350 print(' "' + switches[i].switchName + '",'); 383 command_line->AppendSwitchASCII(
351 print(' "' + (switches[i].switchValue || '') + '");'); 384 "${switches[i].switchName}",
385 "${(switches[i].switchValue || '')}");`);
352 } 386 }
353 print(' }'); 387 output(`
354 print('};'); 388 }
389 };
390 `);
355 } 391 }
356 typedeffedCppFixtures[testFixture] = typedefCppFixture; 392 typedeffedCppFixtures[testFixture] = typedefCppFixture;
357 } 393 }
358 394
359 print(testF + '(' + testFixture + ', ' + testFunction + ') {'); 395 output(`${testF}(${testFixture}, ${testFunction}) {`);
360 for (var i = 0; i < extraLibraries.length; i++) { 396 for (var i = 0; i < extraLibraries.length; i++) {
361 print(' AddLibrary(base::FilePath(FILE_PATH_LITERAL("' + 397 var libraryName = extraLibraries[i].replace(/\\/g, '/');
362 extraLibraries[i].replace(/\\/g, '/') + '")));'); 398 output(`
399 AddLibrary(base::FilePath(FILE_PATH_LITERAL(
400 "${libraryName}")));`);
363 } 401 }
364 print(' AddLibrary(base::FilePath(FILE_PATH_LITERAL("' + 402 output(`
365 jsFileBase.replace(/\\/g, '/') + '")));'); 403 AddLibrary(base::FilePath(FILE_PATH_LITERAL(
404 "${jsFileBase.replace(/\\/g, '/')}")));`);
366 if (addSetPreloadInfo) { 405 if (addSetPreloadInfo) {
367 print(' set_preload_test_fixture("' + testFixture + '");'); 406 output(`
368 print(' set_preload_test_name("' + testFunction + '");'); 407 set_preload_test_fixture("${testFixture}");
408 set_preload_test_name("${testFunction}");`);
369 } 409 }
370 if (testGenPreamble) 410 if (testGenPreamble)
371 testGenPreamble(testFixture, testFunction); 411 testGenPreamble(testFixture, testFunction);
372 if (browsePreload) 412 if (browsePreload)
373 print(' BrowsePreload(GURL("' + browsePreload + '"));'); 413 output(` BrowsePreload(GURL("${browsePreload}"));`);
374 if (browsePrintPreload) { 414 if (browsePrintPreload) {
375 print(' BrowsePrintPreload(GURL(WebUITestDataPathToURL(\n' + 415 output(`
376 ' FILE_PATH_LITERAL("' + browsePrintPreload + '"))));'); 416 BrowsePrintPreload(GURL(WebUITestDataPathToURL(
417 FILE_PATH_LITERAL("${browsePrintPreload}"))));`);
377 } 418 }
378 print(' ' + testPredicate + '(RunJavascriptTestF(' + isAsyncParam + 419 output(`
379 '"' + testFixture + '", ' + 420 ${testPredicate}(
380 '"' + testFunction + '"));'); 421 RunJavascriptTestF(
422 ${isAsyncParam}"${testFixture}",
423 "${testFunction}"));`);
381 if (testGenPostamble) 424 if (testGenPostamble)
382 testGenPostamble(testFixture, testFunction); 425 testGenPostamble(testFixture, testFunction);
383 print('}'); 426 output('}\n');
384 print();
385 } 427 }
386 428
387 // Now that generation functions are defined, load in |jsFile|. 429 // Now that generation functions are defined, load in |jsFile|.
388 var js = read(jsFile); 430 var js = readJsFile(jsFile);
389 pathStack.push({path: jsFile, base: jsFileBase}); 431 pathStack.push({path: jsFile, base: jsFileBase});
390 eval(js); 432 eval(js);
391 pathStack.pop(); 433 pathStack.pop();
434 print(pendingOutput);
OLDNEW
« no previous file with comments | « chrome/test/base/OWNERS ('k') | tools/gypv8sh.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698