123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480 |
- goog.setTestOnly('goog.testing.TestRunner');
- goog.provide('goog.testing.TestRunner');
- goog.require('goog.dom');
- goog.require('goog.dom.TagName');
- goog.require('goog.testing.TestCase');
- goog.testing.TestRunner = function() {
-
- this.errors = [];
-
- this.testCase = null;
-
- this.initialized = false;
-
- this.logEl_ = null;
-
- this.errorFilter_ = null;
-
- this.strict_ = true;
-
- this.uniqueId_ = Math.random() + '';
- };
- goog.testing.TestRunner.prototype.getSearchString = function() {
- return window.location.search;
- };
- goog.testing.TestRunner.prototype.getUniqueId = function() {
- return this.uniqueId_;
- };
- goog.testing.TestRunner.prototype.initialize = function(testCase) {
- if (this.testCase && this.testCase.running) {
- throw Error('The test runner is already waiting for a test to complete');
- }
- this.testCase = testCase;
- this.initialized = true;
- };
- goog.testing.TestRunner.prototype.setStrict = function(strict) {
- this.strict_ = strict;
- };
- goog.testing.TestRunner.prototype.isStrict = function() {
- return this.strict_;
- };
- goog.testing.TestRunner.prototype.isInitialized = function() {
- return this.initialized;
- };
- goog.testing.TestRunner.prototype.isFinished = function() {
- return this.errors.length > 0 ||
- this.initialized && !!this.testCase && this.testCase.started &&
- !this.testCase.running;
- };
- goog.testing.TestRunner.prototype.isSuccess = function() {
- return !this.hasErrors() && !!this.testCase && this.testCase.isSuccess();
- };
- goog.testing.TestRunner.prototype.hasErrors = function() {
- return this.errors.length > 0;
- };
- goog.testing.TestRunner.prototype.logError = function(msg) {
- if (!this.errorFilter_ || this.errorFilter_.call(null, msg)) {
- this.errors.push(msg);
- }
- };
- goog.testing.TestRunner.prototype.logTestFailure = function(ex) {
- var testName = (goog.testing.TestCase.currentTestName);
- if (this.testCase) {
- this.testCase.logError(testName, ex);
- } else {
-
- throw new Error(
- 'Test runner not initialized with a test case. Original ' +
- 'exception: ' + ex.message);
- }
- };
- goog.testing.TestRunner.prototype.setErrorFilter = function(fn) {
- this.errorFilter_ = fn;
- };
- goog.testing.TestRunner.prototype.getReport = function(opt_verbose) {
- var report = [];
- if (this.testCase) {
- report.push(this.testCase.getReport(opt_verbose));
- }
- if (this.errors.length > 0) {
- report.push('JavaScript errors detected by test runner:');
- report.push.apply(report, this.errors);
- report.push('\n');
- }
- return report.join('\n');
- };
- goog.testing.TestRunner.prototype.getRunTime = function() {
- return this.testCase ? this.testCase.getRunTime() : 0;
- };
- goog.testing.TestRunner.prototype.getNumFilesLoaded = function() {
- return this.testCase ? this.testCase.getNumFilesLoaded() : 0;
- };
- goog.testing.TestRunner.prototype.execute = function() {
- if (!this.testCase) {
- throw Error(
- 'The test runner must be initialized with a test case ' +
- 'before execute can be called.');
- }
- if (this.strict_ && this.testCase.getCount() == 0) {
- throw Error(
- 'No tests found in given test case: ' + this.testCase.getName() + '. ' +
- 'By default, the test runner fails if a test case has no tests. ' +
- 'To modify this behavior, see goog.testing.TestRunner\'s ' +
- 'setStrict() method, or G_testRunner.setStrict()');
- }
- this.testCase.setCompletedCallback(goog.bind(this.onComplete_, this));
- if (goog.testing.TestRunner.shouldUsePromises_(this.testCase)) {
- this.testCase.runTestsReturningPromise();
- } else {
- this.testCase.runTests();
- }
- };
- goog.testing.TestRunner.shouldUsePromises_ = function(testCase) {
- return testCase.constructor === goog.testing.TestCase;
- };
- goog.testing.TestRunner.TEST_LOG_ID = 'closureTestRunnerLog';
- goog.testing.TestRunner.prototype.onComplete_ = function() {
- var log = this.testCase.getReport(true);
- if (this.errors.length > 0) {
- log += '\n' + this.errors.join('\n');
- }
- if (!this.logEl_) {
- var el = document.getElementById(goog.testing.TestRunner.TEST_LOG_ID);
- if (el == null) {
- el = goog.dom.createElement(goog.dom.TagName.DIV);
- el.id = goog.testing.TestRunner.TEST_LOG_ID;
- document.body.appendChild(el);
- }
- this.logEl_ = el;
- }
-
- this.writeLog(log);
-
- var runAgainLink = goog.dom.createElement(goog.dom.TagName.A);
- runAgainLink.style.display = 'inline-block';
- runAgainLink.style.fontSize = 'small';
- runAgainLink.style.marginBottom = '16px';
- runAgainLink.href = '';
- runAgainLink.onclick = goog.bind(function() {
- this.execute();
- return false;
- }, this);
- runAgainLink.innerHTML = 'Run again without reloading';
- this.logEl_.appendChild(runAgainLink);
- };
- goog.testing.TestRunner.prototype.writeLog = function(log) {
- var lines = log.split('\n');
- for (var i = 0; i < lines.length; i++) {
- var line = lines[i];
- var color;
- var isPassed = /PASSED/.test(line);
- var isFailOrError =
- /FAILED/.test(line) || /ERROR/.test(line) || /NO TESTS RUN/.test(line);
- if (isPassed) {
- color = 'darkgreen';
- } else if (isFailOrError) {
- color = 'darkred';
- } else {
- color = '#333';
- }
- var div = goog.dom.createElement(goog.dom.TagName.DIV);
- if (line.substr(0, 2) == '> ') {
-
- div.innerHTML = line;
- } else {
- div.appendChild(document.createTextNode(line));
- }
- var testNameMatch = /(\S+) (\[[^\]]*] )?: (FAILED|ERROR|PASSED)/.exec(line);
- if (testNameMatch) {
-
-
-
-
- var newSearch = 'runTests=' + testNameMatch[1];
- var search = window.location.search;
- if (search) {
- var oldTests = /runTests=([^&]*)/.exec(search);
- if (oldTests) {
- newSearch = search.substr(0, oldTests.index) + newSearch +
- search.substr(oldTests.index + oldTests[0].length);
- } else {
- newSearch = search + '&' + newSearch;
- }
- } else {
- newSearch = '?' + newSearch;
- }
- var href = window.location.href;
- var hash = window.location.hash;
- if (hash && hash.charAt(0) != '#') {
- hash = '#' + hash;
- }
- href = href.split('#')[0].split('?')[0] + newSearch + hash;
-
- var a = goog.dom.createElement(goog.dom.TagName.A);
- a.innerHTML = '(run individually)';
- a.style.fontSize = '0.8em';
- a.style.color = '#888';
- a.href = href;
- div.appendChild(document.createTextNode(' '));
- div.appendChild(a);
- }
- div.style.color = color;
- div.style.font = 'normal 100% monospace';
- div.style.wordWrap = 'break-word';
- if (i == 0) {
-
- div.style.padding = '20px';
- div.style.marginBottom = '10px';
- if (isPassed) {
- div.style.border = '1px solid ' + color;
- div.style.backgroundColor = '#eeffee';
- } else if (isFailOrError) {
- div.style.border = '5px solid ' + color;
- div.style.backgroundColor = '#ffeeee';
- } else {
- div.style.border = '1px solid black';
- div.style.backgroundColor = '#eeeeee';
- }
- }
- try {
- div.style.whiteSpace = 'pre-wrap';
- } catch (e) {
-
-
-
- }
- if (i < 2) {
- div.style.fontWeight = 'bold';
- }
- this.logEl_.appendChild(div);
- }
- };
- goog.testing.TestRunner.prototype.log = function(s) {
- if (this.testCase) {
- this.testCase.log(s);
- }
- };
- goog.testing.TestRunner.prototype.getTestResults = function() {
- if (this.testCase) {
- return this.testCase.getTestResults();
- }
- return null;
- };
- goog.testing.TestRunner.prototype.getTestResultsAsJson = function() {
- if (this.testCase) {
- return this.testCase.getTestResultsAsJson();
- }
- return null;
- };
|