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

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: Created 5 years, 4 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 | 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 * @param {string=} opt_string
111 */
112 function output(opt_string) {
113 if (opt_string !== undefined)
114 pendingOutput += opt_string;
115 pendingOutput += '\n';
116 }
102 117
103 /** 118 /**
104 * Generates the header of the cc file to stdout. 119 * Generates the header of the cc file to stdout.
105 * @param {string?} testFixture Name of test fixture. 120 * @param {string?} testFixture Name of test fixture.
106 */ 121 */
107 function maybeGenHeader(testFixture) { 122 function maybeGenHeader(testFixture) {
108 if (!needGenHeader) 123 if (!needGenHeader)
109 return; 124 return;
110 needGenHeader = false; 125 needGenHeader = false;
111 print('// GENERATED FILE'); 126 output('// GENERATED FILE');
112 print(argHint); 127 output(argHint);
113 print('// PLEASE DO NOT HAND EDIT!'); 128 output('// PLEASE DO NOT HAND EDIT!');
114 print(); 129 output();
115 130
116 // Output some C++ headers based upon the |testType|. 131 // Output some C++ headers based upon the |testType|.
117 // 132 //
118 // Currently supports: 133 // Currently supports:
119 // 'extension' - browser_tests harness, js2extension rule, 134 // 'extension' - browser_tests harness, js2extension rule,
120 // ExtensionJSBrowserTest superclass. 135 // ExtensionJSBrowserTest superclass.
121 // 'unit' - unit_tests harness, js2unit rule, V8UnitTest superclass. 136 // 'unit' - unit_tests harness, js2unit rule, V8UnitTest superclass.
122 // 'webui' - browser_tests harness, js2webui rule, WebUIBrowserTest 137 // 'webui' - browser_tests harness, js2webui rule, WebUIBrowserTest
123 // superclass. 138 // superclass.
124 if (testType === 'extension') { 139 if (testType === 'extension') {
125 print('#include "chrome/test/base/extension_js_browser_test.h"'); 140 output('#include "chrome/test/base/extension_js_browser_test.h"');
126 testing.Test.prototype.typedefCppFixture = 'ExtensionJSBrowserTest'; 141 testing.Test.prototype.typedefCppFixture = 'ExtensionJSBrowserTest';
127 addSetPreloadInfo = false; 142 addSetPreloadInfo = false;
128 testF = 'IN_PROC_BROWSER_TEST_F'; 143 testF = 'IN_PROC_BROWSER_TEST_F';
129 } else if (testType === 'unit') { 144 } else if (testType === 'unit') {
130 print('#include "chrome/test/base/v8_unit_test.h"'); 145 output('#include "chrome/test/base/v8_unit_test.h"');
131 testing.Test.prototype.typedefCppFixture = 'V8UnitTest'; 146 testing.Test.prototype.typedefCppFixture = 'V8UnitTest';
132 testF = 'TEST_F'; 147 testF = 'TEST_F';
133 addSetPreloadInfo = false; 148 addSetPreloadInfo = false;
134 } else { 149 } else {
135 print('#include "chrome/test/base/web_ui_browser_test.h"'); 150 output('#include "chrome/test/base/web_ui_browser_test.h"');
136 testing.Test.prototype.typedefCppFixture = 'WebUIBrowserTest'; 151 testing.Test.prototype.typedefCppFixture = 'WebUIBrowserTest';
137 testF = 'IN_PROC_BROWSER_TEST_F'; 152 testF = 'IN_PROC_BROWSER_TEST_F';
138 addSetPreloadInfo = true; 153 addSetPreloadInfo = true;
139 } 154 }
140 print('#include "url/gurl.h"'); 155 output('#include "url/gurl.h"');
141 print('#include "testing/gtest/include/gtest/gtest.h"'); 156 output('#include "testing/gtest/include/gtest/gtest.h"');
142 // Add includes specified by test fixture. 157 // Add includes specified by test fixture.
143 if (testFixture) { 158 if (testFixture) {
144 if (this[testFixture].prototype.testGenCppIncludes) 159 if (this[testFixture].prototype.testGenCppIncludes)
145 this[testFixture].prototype.testGenCppIncludes(); 160 this[testFixture].prototype.testGenCppIncludes();
146 if (this[testFixture].prototype.commandLineSwitches) 161 if (this[testFixture].prototype.commandLineSwitches)
147 print('#include "base/command_line.h"'); 162 output('#include "base/command_line.h"');
148 } 163 }
149 print(); 164 output();
150 } 165 }
151 166
152 167
153 /** 168 /**
154 * @type {Array<{path: string, base: string>} 169 * @type {Array<{path: string, base: string>}
155 */ 170 */
156 var pathStack = []; 171 var pathStack = [];
157 172
158 173
159 /** 174 /**
160 * Convert the |includeFile| to paths appropriate for immediate 175 * Convert the |includeFile| to paths appropriate for immediate
161 * inclusion (path) and runtime inclusion (base). 176 * inclusion (path) and runtime inclusion (base).
162 * @param {string} includeFile The file to include. 177 * @param {string} includeFile The file to include.
163 * @return {{path: string, base: string}} Object describing the paths 178 * @return {{path: string, base: string}} Object describing the paths
164 * for |includeFile|. |path| is relative to cwd; |base| is relative to 179 * for |includeFile|. |path| is relative to cwd; |base| is relative to
165 * source root. 180 * source root.
166 */ 181 */
167 function includeFileToPaths(includeFile) { 182 function includeFileToPaths(includeFile) {
168 paths = pathStack[pathStack.length - 1]; 183 paths = pathStack[pathStack.length - 1];
169 return { 184 return {
170 path: paths.path.replace(/[^\/\\]+$/, includeFile), 185 path: paths.path.replace(/[^\/\\]+$/, includeFile),
171 base: paths.base.replace(/[^\/\\]+$/, includeFile), 186 base: paths.base.replace(/[^\/\\]+$/, includeFile),
172 }; 187 };
173 } 188 }
174 189
190 /**
191 * Returns the content of a javascript file with a sourceURL comment
192 * appended to facilitate better stack traces.
193 * @param {string} path Relative path name.
194 * return {string}
195 */
196 function readJsFile(path) {
197 return read(path) + '//# sourceURL=' + path;
198 }
199
175 200
176 /** 201 /**
177 * Maps object names to the path to the file that provides them. 202 * Maps object names to the path to the file that provides them.
178 * Populated from the |depsFile| if any. 203 * Populated from the |depsFile| if any.
179 * @type {Object<string>} 204 * @type {Object<string>}
180 */ 205 */
181 var dependencyProvidesToPaths = {}; 206 var dependencyProvidesToPaths = {};
182 207
183 /** 208 /**
184 * Maps dependency path names to object names required by the file. 209 * Maps dependency path names to object names required by the file.
(...skipping 13 matching lines...) Expand all
198 */ 223 */
199 goog.addDependency = function(path, provides, requires) { 224 goog.addDependency = function(path, provides, requires) {
200 provides.forEach(function(provide) { 225 provides.forEach(function(provide) {
201 dependencyProvidesToPaths[provide] = path; 226 dependencyProvidesToPaths[provide] = path;
202 }); 227 });
203 dependencyPathsToRequires[path] = requires; 228 dependencyPathsToRequires[path] = requires;
204 }; 229 };
205 230
206 // Read and eval the deps file. It should only contain goog.addDependency 231 // Read and eval the deps file. It should only contain goog.addDependency
207 // calls. 232 // calls.
208 eval(read(depsFile)); 233 eval(readJsFile(depsFile));
209 } 234 }
210 235
211 /** 236 /**
212 * Resolves a list of libraries to an ordered list of paths to load by the 237 * 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 238 * generated C++. The input should contain object names provided
214 * by the deps file. Dependencies will be resolved and included in the 239 * by the deps file. Dependencies will be resolved and included in the
215 * correct order, meaning that the returned array may contain more entries 240 * correct order, meaning that the returned array may contain more entries
216 * than the input. 241 * than the input.
217 * @param {Array<string>} deps List of dependencies. 242 * @param {Array<string>} deps List of dependencies.
218 * @return {Array<string>} List of paths to load. 243 * @return {Array<string>} List of paths to load.
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
265 290
266 return resultPaths; 291 return resultPaths;
267 } 292 }
268 293
269 /** 294 /**
270 * Output |code| verbatim. 295 * Output |code| verbatim.
271 * @param {string} code The code to output. 296 * @param {string} code The code to output.
272 */ 297 */
273 function GEN(code) { 298 function GEN(code) {
274 maybeGenHeader(null); 299 maybeGenHeader(null);
275 print(code); 300 output(code);
276 } 301 }
277 302
278 /** 303 /**
279 * Outputs |commentEncodedCode|, converting comment to enclosed C++ code. 304 * Outputs |commentEncodedCode|, converting comment to enclosed C++ code.
280 * @param {function} commentEncodedCode A function in the following format (note 305 * @param {function} commentEncodedCode A function in the following format (note
281 * the space in '/ *' and '* /' should be removed to form a comment delimiter): 306 * the space in '/ *' and '* /' should be removed to form a comment delimiter):
282 * function() {/ *! my_cpp_code.DoSomething(); * / 307 * function() {/ *! my_cpp_code.DoSomething(); * /
283 * Code between / *! and * / will be extracted and written to stdout. 308 * Code between / *! and * / will be extracted and written to stdout.
284 */ 309 */
285 function GEN_BLOCK(commentEncodedCode) { 310 function GEN_BLOCK(commentEncodedCode) {
286 var code = commentEncodedCode.toString(). 311 var code = commentEncodedCode.toString().
287 replace(/^[^\/]+\/\*!?/, ''). 312 replace(/^[^\/]+\/\*!?/, '').
288 replace(/\*\/[^\/]+$/, ''). 313 replace(/\*\/[^\/]+$/, '').
289 replace(/^\n|\n$/, ''). 314 replace(/^\n|\n$/, '').
290 replace(/\s+$/, ''); 315 replace(/\s+$/, '');
291 GEN(code); 316 GEN(code);
292 } 317 }
293 318
294 /** 319 /**
295 * Generate includes for the current |jsFile| by including them 320 * Generate includes for the current |jsFile| by including them
296 * immediately and at runtime. 321 * immediately and at runtime.
297 * The paths must be relative to the directory of the current file. 322 * The paths must be relative to the directory of the current file.
298 * @param {Array<string>} includes Paths to JavaScript files to 323 * @param {Array<string>} includes Paths to JavaScript files to
299 * include immediately and at runtime. 324 * include immediately and at runtime.
300 */ 325 */
301 function GEN_INCLUDE(includes) { 326 function GEN_INCLUDE(includes) {
302 for (var i = 0; i < includes.length; i++) { 327 for (var i = 0; i < includes.length; i++) {
303 var includePaths = includeFileToPaths(includes[i]); 328 var includePaths = includeFileToPaths(includes[i]);
304 var js = read(includePaths.path); 329 var js = readJsFile(includePaths.path);
305 pathStack.push(includePaths); 330 pathStack.push(includePaths);
306 ('global', eval)(js); 331 ('global', eval)(js);
307 pathStack.pop(); 332 pathStack.pop();
308 genIncludes.push(includePaths.base); 333 genIncludes.push(includePaths.base);
309 } 334 }
310 } 335 }
311 336
312 /** 337 /**
313 * Generate gtest-style TEST_F definitions for C++ with a body that 338 * Generate gtest-style TEST_F definitions for C++ with a body that
314 * will invoke the |testBody| for |testFixture|.|testFunction|. 339 * will invoke the |testBody| for |testFixture|.|testFunction|.
(...skipping 15 matching lines...) Expand all
330 var extraLibraries = genIncludes.concat( 355 var extraLibraries = genIncludes.concat(
331 this[testFixture].prototype.extraLibraries.map( 356 this[testFixture].prototype.extraLibraries.map(
332 function(includeFile) { 357 function(includeFile) {
333 return includeFileToPaths(includeFile).base; 358 return includeFileToPaths(includeFile).base;
334 }), 359 }),
335 resolveClosureModuleDeps(this[testFixture].prototype.closureModuleDeps)); 360 resolveClosureModuleDeps(this[testFixture].prototype.closureModuleDeps));
336 361
337 if (typedefCppFixture && !(testFixture in typedeffedCppFixtures)) { 362 if (typedefCppFixture && !(testFixture in typedeffedCppFixtures)) {
338 var switches = this[testFixture].prototype.commandLineSwitches; 363 var switches = this[testFixture].prototype.commandLineSwitches;
339 if (!switches || !switches.length || typedefCppFixture == 'V8UnitTest') { 364 if (!switches || !switches.length || typedefCppFixture == 'V8UnitTest') {
340 print('typedef ' + typedefCppFixture + ' ' + testFixture + ';'); 365 output('typedef ' + typedefCppFixture + ' ' + testFixture + ';');
341 } else { 366 } else {
342 // Make the testFixture a class inheriting from the base fixture. 367 // Make the testFixture a class inheriting from the base fixture.
343 print('class ' + testFixture + ' : public ' + typedefCppFixture + ' {'); 368 output('class ' + testFixture + ' : public ' + typedefCppFixture + ' {');
344 print(' private:'); 369 output(' private:');
345 // Override SetUpCommandLine and add each switch. 370 // Override SetUpCommandLine and add each switch.
346 print(' void'); 371 output(' void');
347 print(' SetUpCommandLine(base::CommandLine* command_line) override {'); 372 output(' SetUpCommandLine(base::CommandLine* command_line) override {');
348 for (var i = 0; i < switches.length; i++) { 373 for (var i = 0; i < switches.length; i++) {
349 print(' command_line->AppendSwitchASCII('); 374 output(' command_line->AppendSwitchASCII(');
350 print(' "' + switches[i].switchName + '",'); 375 output(' "' + switches[i].switchName + '",');
351 print(' "' + (switches[i].switchValue || '') + '");'); 376 output(' "' + (switches[i].switchValue || '') + '");');
352 } 377 }
353 print(' }'); 378 output(' }');
354 print('};'); 379 output('};');
355 } 380 }
356 typedeffedCppFixtures[testFixture] = typedefCppFixture; 381 typedeffedCppFixtures[testFixture] = typedefCppFixture;
357 } 382 }
358 383
359 print(testF + '(' + testFixture + ', ' + testFunction + ') {'); 384 output(testF + '(' + testFixture + ', ' + testFunction + ') {');
360 for (var i = 0; i < extraLibraries.length; i++) { 385 for (var i = 0; i < extraLibraries.length; i++) {
361 print(' AddLibrary(base::FilePath(FILE_PATH_LITERAL("' + 386 output(' AddLibrary(base::FilePath(FILE_PATH_LITERAL("' +
362 extraLibraries[i].replace(/\\/g, '/') + '")));'); 387 extraLibraries[i].replace(/\\/g, '/') + '")));');
363 } 388 }
364 print(' AddLibrary(base::FilePath(FILE_PATH_LITERAL("' + 389 output(' AddLibrary(base::FilePath(FILE_PATH_LITERAL("' +
365 jsFileBase.replace(/\\/g, '/') + '")));'); 390 jsFileBase.replace(/\\/g, '/') + '")));');
366 if (addSetPreloadInfo) { 391 if (addSetPreloadInfo) {
367 print(' set_preload_test_fixture("' + testFixture + '");'); 392 output(' set_preload_test_fixture("' + testFixture + '");');
368 print(' set_preload_test_name("' + testFunction + '");'); 393 output(' set_preload_test_name("' + testFunction + '");');
369 } 394 }
370 if (testGenPreamble) 395 if (testGenPreamble)
371 testGenPreamble(testFixture, testFunction); 396 testGenPreamble(testFixture, testFunction);
372 if (browsePreload) 397 if (browsePreload)
373 print(' BrowsePreload(GURL("' + browsePreload + '"));'); 398 output(' BrowsePreload(GURL("' + browsePreload + '"));');
374 if (browsePrintPreload) { 399 if (browsePrintPreload) {
375 print(' BrowsePrintPreload(GURL(WebUITestDataPathToURL(\n' + 400 output(' BrowsePrintPreload(GURL(WebUITestDataPathToURL(\n' +
376 ' FILE_PATH_LITERAL("' + browsePrintPreload + '"))));'); 401 ' FILE_PATH_LITERAL("' + browsePrintPreload + '"))));');
377 } 402 }
378 print(' ' + testPredicate + '(RunJavascriptTestF(' + isAsyncParam + 403 output(' ' + testPredicate + '(RunJavascriptTestF(' + isAsyncParam +
379 '"' + testFixture + '", ' + 404 '"' + testFixture + '", ' +
380 '"' + testFunction + '"));'); 405 '"' + testFunction + '"));');
381 if (testGenPostamble) 406 if (testGenPostamble)
382 testGenPostamble(testFixture, testFunction); 407 testGenPostamble(testFixture, testFunction);
383 print('}'); 408 output('}');
384 print(); 409 output();
David Tseng 2015/08/26 23:18:00 Optional: want to use the function() {/*! ...
385 } 410 }
386 411
387 // Now that generation functions are defined, load in |jsFile|. 412 // Now that generation functions are defined, load in |jsFile|.
388 var js = read(jsFile); 413 var js = readJsFile(jsFile);
389 pathStack.push({path: jsFile, base: jsFileBase}); 414 pathStack.push({path: jsFile, base: jsFileBase});
390 eval(js); 415 eval(js);
391 pathStack.pop(); 416 pathStack.pop();
417 print(pendingOutput);
OLDNEW
« no previous file with comments | « no previous file | tools/gypv8sh.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698