Chromium Code Reviews| Index: chrome/test/base/js2gtest.js |
| diff --git a/chrome/test/base/js2gtest.js b/chrome/test/base/js2gtest.js |
| index 8d0f7e734bb1eeb2a92629c2d24246998e86e7de..5c1f3c5321dea3d72a300be69251a2a91a1b6398 100644 |
| --- a/chrome/test/base/js2gtest.js |
| +++ b/chrome/test/base/js2gtest.js |
| @@ -10,6 +10,7 @@ |
| * @see WebUI testing: http://goo.gl/ZWFXF |
| * @see gtest documentation: http://goo.gl/Ujj3H |
| * @see chrome/chrome_tests.gypi |
| + * @see chrome/test/base/js2gtest.js |
| * @see tools/gypv8sh.py |
| */ |
| @@ -99,6 +100,20 @@ var needGenHeader = true; |
| */ |
| var argHint = '// ' + this['arguments'].join(' '); |
| +/** |
| + * @type {Array<string>} |
| + */ |
| +var pendingOutput = ''; |
| + |
| +/** |
| + * Adds a string followed by a newline to the pending output. |
| + * @param {string=} opt_string |
| + */ |
| +function output(opt_string) { |
| + if (opt_string !== undefined) |
| + pendingOutput += opt_string; |
| + pendingOutput += '\n'; |
| +} |
| /** |
| * Generates the header of the cc file to stdout. |
| @@ -108,10 +123,10 @@ function maybeGenHeader(testFixture) { |
| if (!needGenHeader) |
| return; |
| needGenHeader = false; |
| - print('// GENERATED FILE'); |
| - print(argHint); |
| - print('// PLEASE DO NOT HAND EDIT!'); |
| - print(); |
| + output('// GENERATED FILE'); |
| + output(argHint); |
| + output('// PLEASE DO NOT HAND EDIT!'); |
| + output(); |
| // Output some C++ headers based upon the |testType|. |
| // |
| @@ -122,31 +137,31 @@ function maybeGenHeader(testFixture) { |
| // 'webui' - browser_tests harness, js2webui rule, WebUIBrowserTest |
| // superclass. |
| if (testType === 'extension') { |
| - print('#include "chrome/test/base/extension_js_browser_test.h"'); |
| + output('#include "chrome/test/base/extension_js_browser_test.h"'); |
| testing.Test.prototype.typedefCppFixture = 'ExtensionJSBrowserTest'; |
| addSetPreloadInfo = false; |
| testF = 'IN_PROC_BROWSER_TEST_F'; |
| } else if (testType === 'unit') { |
| - print('#include "chrome/test/base/v8_unit_test.h"'); |
| + output('#include "chrome/test/base/v8_unit_test.h"'); |
| testing.Test.prototype.typedefCppFixture = 'V8UnitTest'; |
| testF = 'TEST_F'; |
| addSetPreloadInfo = false; |
| } else { |
| - print('#include "chrome/test/base/web_ui_browser_test.h"'); |
| + output('#include "chrome/test/base/web_ui_browser_test.h"'); |
| testing.Test.prototype.typedefCppFixture = 'WebUIBrowserTest'; |
| testF = 'IN_PROC_BROWSER_TEST_F'; |
| addSetPreloadInfo = true; |
| } |
| - print('#include "url/gurl.h"'); |
| - print('#include "testing/gtest/include/gtest/gtest.h"'); |
| + output('#include "url/gurl.h"'); |
| + output('#include "testing/gtest/include/gtest/gtest.h"'); |
| // Add includes specified by test fixture. |
| if (testFixture) { |
| if (this[testFixture].prototype.testGenCppIncludes) |
| this[testFixture].prototype.testGenCppIncludes(); |
| if (this[testFixture].prototype.commandLineSwitches) |
| - print('#include "base/command_line.h"'); |
| + output('#include "base/command_line.h"'); |
| } |
| - print(); |
| + output(); |
| } |
| @@ -172,6 +187,16 @@ function includeFileToPaths(includeFile) { |
| }; |
| } |
| +/** |
| + * Returns the content of a javascript file with a sourceURL comment |
| + * appended to facilitate better stack traces. |
| + * @param {string} path Relative path name. |
| + * return {string} |
| + */ |
| +function readJsFile(path) { |
| + return read(path) + '//# sourceURL=' + path; |
| +} |
| + |
| /** |
| * Maps object names to the path to the file that provides them. |
| @@ -205,7 +230,7 @@ if (depsFile) { |
| // Read and eval the deps file. It should only contain goog.addDependency |
| // calls. |
| - eval(read(depsFile)); |
| + eval(readJsFile(depsFile)); |
| } |
| /** |
| @@ -272,7 +297,7 @@ function resolveClosureModuleDeps(deps) { |
| */ |
| function GEN(code) { |
| maybeGenHeader(null); |
| - print(code); |
| + output(code); |
| } |
| /** |
| @@ -301,7 +326,7 @@ function GEN_BLOCK(commentEncodedCode) { |
| function GEN_INCLUDE(includes) { |
| for (var i = 0; i < includes.length; i++) { |
| var includePaths = includeFileToPaths(includes[i]); |
| - var js = read(includePaths.path); |
| + var js = readJsFile(includePaths.path); |
| pathStack.push(includePaths); |
| ('global', eval)(js); |
| pathStack.pop(); |
| @@ -337,55 +362,56 @@ function TEST_F(testFixture, testFunction, testBody) { |
| if (typedefCppFixture && !(testFixture in typedeffedCppFixtures)) { |
| var switches = this[testFixture].prototype.commandLineSwitches; |
| if (!switches || !switches.length || typedefCppFixture == 'V8UnitTest') { |
| - print('typedef ' + typedefCppFixture + ' ' + testFixture + ';'); |
| + output('typedef ' + typedefCppFixture + ' ' + testFixture + ';'); |
| } else { |
| // Make the testFixture a class inheriting from the base fixture. |
| - print('class ' + testFixture + ' : public ' + typedefCppFixture + ' {'); |
| - print(' private:'); |
| + output('class ' + testFixture + ' : public ' + typedefCppFixture + ' {'); |
| + output(' private:'); |
| // Override SetUpCommandLine and add each switch. |
| - print(' void'); |
| - print(' SetUpCommandLine(base::CommandLine* command_line) override {'); |
| + output(' void'); |
| + output(' SetUpCommandLine(base::CommandLine* command_line) override {'); |
| for (var i = 0; i < switches.length; i++) { |
| - print(' command_line->AppendSwitchASCII('); |
| - print(' "' + switches[i].switchName + '",'); |
| - print(' "' + (switches[i].switchValue || '') + '");'); |
| + output(' command_line->AppendSwitchASCII('); |
| + output(' "' + switches[i].switchName + '",'); |
| + output(' "' + (switches[i].switchValue || '') + '");'); |
| } |
| - print(' }'); |
| - print('};'); |
| + output(' }'); |
| + output('};'); |
| } |
| typedeffedCppFixtures[testFixture] = typedefCppFixture; |
| } |
| - print(testF + '(' + testFixture + ', ' + testFunction + ') {'); |
| + output(testF + '(' + testFixture + ', ' + testFunction + ') {'); |
| for (var i = 0; i < extraLibraries.length; i++) { |
| - print(' AddLibrary(base::FilePath(FILE_PATH_LITERAL("' + |
| + output(' AddLibrary(base::FilePath(FILE_PATH_LITERAL("' + |
| extraLibraries[i].replace(/\\/g, '/') + '")));'); |
| } |
| - print(' AddLibrary(base::FilePath(FILE_PATH_LITERAL("' + |
| + output(' AddLibrary(base::FilePath(FILE_PATH_LITERAL("' + |
| jsFileBase.replace(/\\/g, '/') + '")));'); |
| if (addSetPreloadInfo) { |
| - print(' set_preload_test_fixture("' + testFixture + '");'); |
| - print(' set_preload_test_name("' + testFunction + '");'); |
| + output(' set_preload_test_fixture("' + testFixture + '");'); |
| + output(' set_preload_test_name("' + testFunction + '");'); |
| } |
| if (testGenPreamble) |
| testGenPreamble(testFixture, testFunction); |
| if (browsePreload) |
| - print(' BrowsePreload(GURL("' + browsePreload + '"));'); |
| + output(' BrowsePreload(GURL("' + browsePreload + '"));'); |
| if (browsePrintPreload) { |
| - print(' BrowsePrintPreload(GURL(WebUITestDataPathToURL(\n' + |
| + output(' BrowsePrintPreload(GURL(WebUITestDataPathToURL(\n' + |
| ' FILE_PATH_LITERAL("' + browsePrintPreload + '"))));'); |
| } |
| - print(' ' + testPredicate + '(RunJavascriptTestF(' + isAsyncParam + |
| + output(' ' + testPredicate + '(RunJavascriptTestF(' + isAsyncParam + |
| '"' + testFixture + '", ' + |
| '"' + testFunction + '"));'); |
| if (testGenPostamble) |
| testGenPostamble(testFixture, testFunction); |
| - print('}'); |
| - print(); |
| + output('}'); |
| + output(); |
|
David Tseng
2015/08/26 23:18:00
Optional: want to use the function() {/*!
...
|
| } |
| // Now that generation functions are defined, load in |jsFile|. |
| -var js = read(jsFile); |
| +var js = readJsFile(jsFile); |
| pathStack.push({path: jsFile, base: jsFileBase}); |
| eval(js); |
| pathStack.pop(); |
| +print(pendingOutput); |