Skip to content

Commit

Permalink
fork testsuite, jsunit and testrunner from com_google_javascript_clos…
Browse files Browse the repository at this point in the history
…ure_library (#627)

* fork testsuite, jsunit and testrunner from com_google_javascript_closure_library

* fork testsuite, jsunit and testrunner from com_google_javascript_closure_library

* fork testsuite, jsunit and testrunner from com_google_javascript_closure_library

* fork testsuite, jsunit and testrunner from com_google_javascript_closure_library

* fork testsuite, jsunit and testrunner from com_google_javascript_closure_library

* fork testsuite, jsunit and testrunner from com_google_javascript_closure_library

* fork testsuite, jsunit and testrunner from com_google_javascript_closure_library

* fork testsuite, jsunit and testrunner from com_google_javascript_closure_library

Co-authored-by: Goktug Gokdogan <[email protected]>

---------

Co-authored-by: Goktug Gokdogan <[email protected]>
  • Loading branch information
mollyibot and gkdn authored Nov 27, 2024
1 parent 3e28627 commit 5c122e7
Show file tree
Hide file tree
Showing 9 changed files with 898 additions and 19 deletions.
4 changes: 2 additions & 2 deletions closure/compiler/test/goog_es6_interop/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
# See the License for the specific language governing permissions and
# limitations under the License.


load("//closure/compiler:closure_js_library.bzl", "closure_js_library")
load("//closure/testing:closure_js_test.bzl", "closure_js_test")

Expand Down Expand Up @@ -49,6 +48,7 @@ closure_js_test(
],
deps = [
":person_factory",
"@com_google_javascript_closure_library//closure/goog/testing:testsuite",
"@com_google_javascript_closure_library//closure/goog/testing:asserts",
"//closure/testing/library:testsuite",
],
)
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ goog.module('rulesClosure.googEs6Interop.PersonFactoryTest');

const testSuite = goog.require('goog.testing.testSuite');
const {createPerson} = goog.require('rulesClosure.googEs6Interop.personFactory');

goog.require('goog.testing.asserts');


class PersonFactoryTest {
Expand Down
60 changes: 60 additions & 0 deletions closure/testing/library/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
load("//closure:defs.bzl", "closure_js_binary", "closure_js_library")

# Copyright 2024 The Closure Rules Authors. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
package(
default_testonly = True,
default_visibility = ["//visibility:public"],
)

licenses(["notice"])

closure_js_library(
name = "testsuite",
srcs = ["testsuite.js"],
suppress = [
"useOfGoogProvide",
],
deps = [
"@com_google_javascript_closure_library//closure/goog/testing:testcase",
":jsunit",
],
)

closure_js_library(
name = "jsunit",
srcs = ["jsunit.js"],
suppress = [
"useOfGoogProvide",
"reportUnknownTypes",
],
deps = [
"@com_google_javascript_closure_library//closure/goog/testing:asserts",
"@com_google_javascript_closure_library//closure/goog/testing:testcase",
":testrunner",
],
)

closure_js_library(
name = "testrunner",
srcs = ["testrunner.js"],
suppress = [
"useOfGoogProvide",
"reportUnknownTypes",
"visibility",
],
deps = [
"@com_google_javascript_closure_library//closure/goog/testing:testcase",
],
)
184 changes: 184 additions & 0 deletions closure/testing/library/jsunit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
/**
* @license
* Copyright The Closure Library Authors.
* SPDX-License-Identifier: Apache-2.0
*/

/**
* @fileoverview Utilities for working with JsUnit. Writes out the JsUnit file
* that needs to be included in every unit test.
*
* Testing code should not have dependencies outside of goog.testing so as to
* reduce the chance of masking missing dependencies.
*/

goog.setTestOnly('goog.testing.jsunit');
goog.provide('goog.testing.jsunit');

goog.require('goog.testing.TestCase');
goog.require('goog.testing.TestRunner');
goog.require('goog.testing.asserts');


/**
* @define {boolean} If this code is being parsed by JsTestC, we let it disable
* the onload handler to avoid running the test in JsTestC.
*/
goog.testing.jsunit.AUTO_RUN_ONLOAD =
goog.define('goog.testing.jsunit.AUTO_RUN_ONLOAD', true);


/**
* @define {number} Sets a delay in milliseconds after the window onload event
* and running the tests. See goog.testing.jsunit.AUTO_RUN_DELAY_IN_MS.
*/
goog.testing.jsunit.AUTO_RUN_DELAY_IN_MS_DEFAULT =
goog.define('goog.testing.jsunit.AUTO_RUN_DELAY_IN_MS_DEFAULT', 0);

/**
* @type {number} Sets a delay in milliseconds after the window onload event
* and running the tests. Used as a workaround for IE failing to report load
* event if the page has iframes. The appropriate value is zero;
* maximum should be 500. Do not use this value to support asynchronous tests.
*/
goog.testing.jsunit.AUTO_RUN_DELAY_IN_MS =
goog.testing.jsunit.AUTO_RUN_DELAY_IN_MS_DEFAULT;


(function() {
'use strict';
// Only allow one global test runner to be created on a page.
if (goog.global['G_testRunner'] instanceof goog.testing.TestRunner) {
return;
}

// Increases the maximum number of stack frames in Google Chrome from the
// default 10 to 50 to get more useful stack traces.
Error.stackTraceLimit = 50;

// Store a reference to the window's timeout so that it can't be overridden
// by tests.
/** @type {!Function} */
var realTimeout = window.setTimeout;

// Create a test runner.
var tr = new goog.testing.TestRunner();

// Export it so that it can be queried by Selenium and tests that use a
// compiled test runner.
goog.exportSymbol('G_testRunner', tr);
goog.exportSymbol('G_testRunner.initialize', tr.initialize);
goog.exportSymbol('G_testRunner.isInitialized', tr.isInitialized);
goog.exportSymbol('G_testRunner.isFinished', tr.isFinished);
goog.exportSymbol('G_testRunner.getUniqueId', tr.getUniqueId);
goog.exportSymbol('G_testRunner.isSuccess', tr.isSuccess);
goog.exportSymbol('G_testRunner.getReport', tr.getReport);
goog.exportSymbol('G_testRunner.getRunTime', tr.getRunTime);
goog.exportSymbol('G_testRunner.getNumFilesLoaded', tr.getNumFilesLoaded);
goog.exportSymbol('G_testRunner.setStrict', tr.setStrict);
goog.exportSymbol('G_testRunner.logTestFailure', tr.logTestFailure);
goog.exportSymbol('G_testRunner.getTestResults', tr.getTestResults);
goog.exportSymbol('G_testRunner.getTestResultsAsJson', tr.getTestResultsAsJson);

// Export debug as a global function for JSUnit compatibility. This just
// calls log on the current test case.
if (!goog.global['debug']) {
goog.exportSymbol('debug', goog.bind(tr.log, tr));
}

// If the application has defined a global error filter, set it now. This
// allows users who use a base test include to set the error filter before
// the testing code is loaded.
if (goog.global['G_errorFilter']) {
tr.setErrorFilter(goog.global['G_errorFilter']);
}

var maybeGetStack = function(error) {
'use strict';
var stack = error && error.stack;
return typeof stack === 'string' ? stack : '';
};

// Add an error handler to report errors that may occur during
// initialization of the page.
var onerror = window.onerror;
window.onerror = function(messageOrEvent, url, line, colno, errObj) {
'use strict';
// Call any existing onerror handlers, except our boot handler.
if (onerror && onerror != window['__onerror_at_boot']) {
onerror.apply(window, arguments);
}
var stack = maybeGetStack(errObj || messageOrEvent);
if (stack) {
tr.logError(String(messageOrEvent) + '\n' + stack);
} else if (typeof messageOrEvent == 'object') {
var error = /** @type {{target: ?}} */ (messageOrEvent);
// Some older webkit browsers pass an event object as the only argument
// to window.onerror. It doesn't contain an error message, url or line
// number. We therefore log as much info as we can.
if (error.target && error.target.tagName == 'SCRIPT') {
tr.logError('UNKNOWN ERROR: Script ' + error.target.src);
} else {
tr.logError('UNKNOWN ERROR: No error information available.');
}
} else {
// Add the column if it is available, older browsers won't have it.
var colstr = colno != null ? '\nColumn: ' + colno : '';
tr.logError(
'JS ERROR: ' + messageOrEvent + '\nURL: ' + url + '\nLine: ' + line +
colstr);
}
};

/**
* The onerror handler that may have been set by the test runner.
* @type {?function(string, string=, number=, number=, Object=)}
*/
window['__onerror_at_boot'] = window['__onerror_at_boot'] || null;
/**
* The arguments for any call to window.onerror occuring before this point.
* @type {?Array<!Array<?>>}
*/
window['__errors_since_boot'] = window['__errors_since_boot'] || null;

if (window['__onerror_at_boot']) {
if (window['__errors_since_boot']) {
for (var i = 0; i < window['__errors_since_boot'].length; i++) {
var args = window['__errors_since_boot'][i];
window.onerror.apply(window, args);
}
}
// http://perfectionkills.com/understanding-delete/#ie_bugs
window['__onerror_at_boot'] = null;
}

// Create an onload handler, if the test runner hasn't been initialized then
// no test has been registered with the test runner by the test file. We
// then create a new test case and auto discover any tests in the global
// scope. If this code is being parsed by JsTestC, we let it disable the
// onload handler to avoid running the test in JsTestC.
if (goog.testing.jsunit.AUTO_RUN_ONLOAD) {
var onload = window.onload;
window.onload = function(e) {
'use strict';
// Call any existing onload handlers.
if (onload) {
onload(e);
}
// Execute the test on the next turn, to allow the WebDriver.get()
// operation to return to the test runner and begin polling.
var executionDelayAfterLoad = goog.testing.jsunit.AUTO_RUN_DELAY_IN_MS;

realTimeout(function() {
'use strict';
if (!tr.initialized) {
var testCase = new goog.testing.TestCase(document.title);
goog.testing.TestCase.initializeTestCase(testCase);
tr.initialize(testCase);
}
tr.execute();
}, executionDelayAfterLoad);
window.onload = null;
};
}
})();
Loading

0 comments on commit 5c122e7

Please sign in to comment.