1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969 |
- goog.setTestOnly('goog.testing.TestCase');
- goog.provide('goog.testing.TestCase');
- goog.provide('goog.testing.TestCase.Error');
- goog.provide('goog.testing.TestCase.Order');
- goog.provide('goog.testing.TestCase.Result');
- goog.provide('goog.testing.TestCase.Test');
- goog.require('goog.Promise');
- goog.require('goog.Thenable');
- goog.require('goog.array');
- goog.require('goog.asserts');
- goog.require('goog.dom');
- goog.require('goog.dom.TagName');
- goog.require('goog.json');
- goog.require('goog.object');
- goog.require('goog.testing.JsUnitException');
- goog.require('goog.testing.asserts');
- goog.testing.TestCase = function(opt_name) {
-
- this.name_ = opt_name || 'Untitled Test Case';
-
- this.tests_ = [];
-
- this.testsToRun_ = null;
-
- this.testDone_ = null;
-
- this.order = goog.testing.TestCase.Order.SORTED;
-
- this.runNextTestCallback_ = goog.nullFunction;
-
- this.curTest_ = null;
-
- this.result_ = new goog.testing.TestCase.Result(this);
-
- this.thrownAssertionExceptions_ = [];
-
- this.failOnUnreportedAsserts = true;
-
- this.promiseTimeout = 1000;
- };
- goog.testing.TestCase.Order = {
-
- NATURAL: 'natural',
-
- RANDOM: 'random',
-
- SORTED: 'sorted'
- };
- goog.testing.TestCase.prototype.getName = function() {
- return this.name_;
- };
- goog.testing.TestCase.maxRunTime = 200;
- goog.testing.TestCase.protectedSetTimeout_ = goog.global.setTimeout;
- goog.testing.TestCase.protectedClearTimeout_ = goog.global.clearTimeout;
- goog.testing.TestCase.protectedDate_ = Date;
- goog.testing.TestCase.protectedPerformance_ =
- window.performance && window.performance.now ? performance : null;
- goog.testing.TestCase.currentTestName = null;
- goog.testing.TestCase.IS_IE = typeof opera == 'undefined' &&
- !!goog.global.navigator &&
- goog.global.navigator.userAgent.indexOf('MSIE') != -1;
- goog.testing.TestCase.prototype.exceptionBeforeTest;
- goog.testing.TestCase.prototype.started = false;
- goog.testing.TestCase.prototype.running = false;
- goog.testing.TestCase.prototype.startTime_ = 0;
- goog.testing.TestCase.prototype.batchTime_ = 0;
- goog.testing.TestCase.prototype.currentTestPointer_ = 0;
- goog.testing.TestCase.prototype.onCompleteCallback_ = null;
- goog.testing.TestCase.prototype.add = function(test) {
- goog.asserts.assert(test);
- if (this.started) {
- throw Error(
- 'Tests cannot be added after execute() has been called. ' +
- 'Test: ' + test.name);
- }
- this.tests_.push(test);
- };
- goog.testing.TestCase.prototype.addNewTest = function(name, ref, opt_scope) {
- var test = new goog.testing.TestCase.Test(name, ref, opt_scope || this);
- this.add(test);
- };
- goog.testing.TestCase.prototype.setTests = function(tests) {
- this.tests_ = tests;
- };
- goog.testing.TestCase.prototype.getTests = function() {
- return this.tests_;
- };
- goog.testing.TestCase.prototype.getCount = function() {
- return this.tests_.length;
- };
- goog.testing.TestCase.prototype.getActuallyRunCount = function() {
- return this.testsToRun_ ? goog.object.getCount(this.testsToRun_) : 0;
- };
- goog.testing.TestCase.prototype.next = function() {
- var test;
- while ((test = this.tests_[this.currentTestPointer_++])) {
- if (!this.testsToRun_ || this.testsToRun_[test.name] ||
- this.testsToRun_[this.currentTestPointer_ - 1]) {
- return test;
- }
- }
- return null;
- };
- goog.testing.TestCase.prototype.reset = function() {
- this.currentTestPointer_ = 0;
- this.result_ = new goog.testing.TestCase.Result(this);
- };
- goog.testing.TestCase.prototype.setCompletedCallback = function(fn) {
- this.onCompleteCallback_ = fn;
- };
- goog.testing.TestCase.prototype.setOrder = function(order) {
- this.order = order;
- };
- goog.testing.TestCase.prototype.setTestsToRun = function(testsToRun) {
- this.testsToRun_ = testsToRun;
- };
- goog.testing.TestCase.prototype.shouldRunTests = function() {
- return true;
- };
- goog.testing.TestCase.prototype.execute = function() {
- if (!this.prepareForRun_()) {
- return;
- }
- this.log('Starting tests: ' + this.name_);
- this.cycleTests();
- };
- goog.testing.TestCase.prototype.prepareForRun_ = function() {
- this.started = true;
- this.reset();
- this.startTime_ = this.now();
- this.running = true;
- this.result_.totalCount = this.getCount();
- if (!this.shouldRunTests()) {
- this.log('shouldRunTests() returned false, skipping these tests.');
- this.result_.testSuppressed = true;
- this.finalize();
- return false;
- }
- return true;
- };
- goog.testing.TestCase.prototype.finalize = function() {
- this.saveMessage('Done');
- this.tearDownPage();
- this.endTime_ = this.now();
- this.running = false;
- this.result_.runTime = this.endTime_ - this.startTime_;
- this.result_.numFilesLoaded = this.countNumFilesLoaded_();
- this.result_.complete = true;
- this.log(this.result_.getSummary());
- if (this.result_.isSuccess()) {
- this.log('Tests complete');
- } else {
- this.log('Tests Failed');
- }
- if (this.onCompleteCallback_) {
- var fn = this.onCompleteCallback_;
-
- fn();
- this.onCompleteCallback_ = null;
- }
- };
- goog.testing.TestCase.prototype.saveMessage = function(message) {
- this.result_.messages.push(this.getTimeStamp_() + ' ' + message);
- };
- goog.testing.TestCase.prototype.isInsideMultiTestRunner = function() {
- var top = goog.global['top'];
- return top && typeof top['_allTests'] != 'undefined';
- };
- goog.testing.TestCase.prototype.log = function(val) {
- if (!this.isInsideMultiTestRunner() && goog.global.console) {
- if (typeof val == 'string') {
- val = this.getTimeStamp_() + ' : ' + val;
- }
- if (val instanceof Error && val.stack) {
-
-
-
-
- goog.global.console.log(val, val.message, val.stack);
-
-
- } else {
- goog.global.console.log(val);
- }
- }
- };
- goog.testing.TestCase.prototype.isSuccess = function() {
- return !!this.result_ && this.result_.isSuccess();
- };
- goog.testing.TestCase.prototype.getReport = function(opt_verbose) {
- var rv = [];
- if (this.running) {
- rv.push(this.name_ + ' [RUNNING]');
- } else if (this.result_.runCount == 0) {
- rv.push(this.name_ + ' [NO TESTS RUN]');
- } else {
- var label = this.result_.isSuccess() ? 'PASSED' : 'FAILED';
- rv.push(this.name_ + ' [' + label + ']');
- }
- if (goog.global.location) {
- rv.push(this.trimPath_(goog.global.location.href));
- }
- rv.push(this.result_.getSummary());
- if (opt_verbose) {
- rv.push('.', this.result_.messages.join('\n'));
- } else if (!this.result_.isSuccess()) {
- rv.push(this.result_.errors.join('\n'));
- }
- rv.push(' ');
- return rv.join('\n');
- };
- goog.testing.TestCase.prototype.getResult = function() {
- return this.result_;
- };
- goog.testing.TestCase.prototype.getRunTime = function() {
- return this.result_.runTime;
- };
- goog.testing.TestCase.prototype.getNumFilesLoaded = function() {
- return this.result_.numFilesLoaded;
- };
- goog.testing.TestCase.IResult;
- goog.testing.TestCase.prototype.getTestResults = function() {
- var map = {};
- goog.object.forEach(this.result_.resultsByName, function(resultArray, key) {
-
- if (!Object.prototype.hasOwnProperty.call(
- this.result_.resultsByName, key)) {
- return;
- }
- map[key] = [];
- for (var j = 0; j < resultArray.length; j++) {
- map[key].push(resultArray[j].toObject_());
- }
- }, this);
- return map;
- };
- goog.testing.TestCase.prototype.getTestResultsAsJson = function() {
- return goog.json.serialize(this.getTestResults());
- };
- goog.testing.TestCase.prototype.runTests = function() {
- goog.testing.Continuation_.run(this.runSetUpPage_(this.execute));
- };
- goog.testing.TestCase.prototype.runTestsReturningPromise = function() {
- return new goog.Promise(function(resolve) {
- goog.testing.Continuation_.run(this.runSetUpPage_(function() {
- if (!this.prepareForRun_()) {
- resolve(this.result_);
- return;
- }
- this.log('Starting tests: ' + this.name_);
- this.saveMessage('Start');
- this.batchTime_ = this.now();
- this.runNextTestCallback_ = resolve;
- goog.testing.Continuation_.run(this.runNextTest_());
- }));
- }, this);
- };
- goog.testing.TestCase.prototype.runSetUpPage_ = function(runTestsFn) {
- return this.invokeTestFunction_(this.setUpPage, runTestsFn, function(e) {
- this.exceptionBeforeTest = e;
- runTestsFn.call(this);
- }, 'setUpPage');
- };
- goog.testing.TestCase.prototype.runNextTest_ = function() {
- this.curTest_ = this.next();
- if (!this.curTest_ || !this.running) {
- this.finalize();
- return new goog.testing.Continuation_(
- goog.bind(this.runNextTestCallback_, this, this.result_));
- }
- this.curTest_.started();
- this.result_.runCount++;
- this.log('Running test: ' + this.curTest_.name);
- if (this.maybeFailTestEarly(this.curTest_)) {
- return new goog.testing.Continuation_(
- goog.bind(this.finishTestInvocation_, this));
- }
- goog.testing.TestCase.currentTestName = this.curTest_.name;
- return this.invokeTestFunction_(
- this.setUp, this.safeRunTest_, this.safeTearDown_, 'setUp');
- };
- goog.testing.TestCase.prototype.safeRunTest_ = function() {
- return this.invokeTestFunction_(
- goog.bind(this.curTest_.ref, this.curTest_.scope), this.safeTearDown_,
- this.safeTearDown_, this.curTest_.name);
- };
- goog.testing.TestCase.prototype.safeTearDown_ = function(opt_error) {
- if (arguments.length == 1) {
- this.doError(this.curTest_, opt_error);
- }
- return this.invokeTestFunction_(
- this.tearDown, this.finishTestInvocation_, this.finishTestInvocation_,
- 'tearDown');
- };
- goog.testing.TestCase.prototype.invokeTestFunction_ = function(
- fn, onSuccess, onFailure, fnName) {
- var testCase = this;
- this.thrownAssertionExceptions_ = [];
- try {
- var retval = fn.call(this);
- if (goog.Thenable.isImplementedBy(retval) ||
- goog.isFunction(retval && retval['then'])) {
-
- var promise = goog.Promise.resolve(retval);
- var self = this;
- promise = this.rejectIfPromiseTimesOut_(
- promise, self.promiseTimeout,
- 'Timed out while waiting for a promise returned from ' + fnName +
- ' to resolve. Set goog.testing.TestCase.getActiveTestCase()' +
- '.promiseTimeout to adjust the timeout.');
- promise.then(
- function() {
- self.resetBatchTimeAfterPromise_();
- if (testCase.thrownAssertionExceptions_.length == 0) {
- goog.testing.Continuation_.run(onSuccess.call(self));
- } else {
- goog.testing.Continuation_.run(onFailure.call(
- self,
- testCase.reportUnpropagatedAssertionExceptions_(fnName)));
- }
- },
- function(e) {
- self.resetBatchTimeAfterPromise_();
- goog.testing.Continuation_.run(onFailure.call(self, e));
- });
- return null;
- } else {
- if (this.thrownAssertionExceptions_.length == 0) {
- return new goog.testing.Continuation_(goog.bind(onSuccess, this));
- } else {
- return new goog.testing.Continuation_(goog.bind(
- onFailure, this,
- this.reportUnpropagatedAssertionExceptions_(fnName)));
- }
- }
- } catch (e) {
- return new goog.testing.Continuation_(goog.bind(onFailure, this, e));
- }
- };
- goog.testing.TestCase.prototype.reportUnpropagatedAssertionExceptions_ =
- function(testName) {
- var numExceptions = this.thrownAssertionExceptions_.length;
- for (var i = 0; i < numExceptions; i++) {
- this.recordError_(testName, this.thrownAssertionExceptions_[i]);
- }
- return new goog.testing.JsUnitException(
- 'One or more assertions were raised but not caught by the testing ' +
- 'framework. These assertions may have been unintentionally captured ' +
- 'by a catch block or a thenCatch resolution of a Promise.');
- };
- goog.testing.TestCase.prototype.resetBatchTimeAfterPromise_ = function() {
- this.batchTime_ = this.now();
- };
- goog.testing.TestCase.prototype.finishTestInvocation_ = function(opt_error) {
- if (arguments.length == 1) {
- this.doError(this.curTest_, opt_error);
- }
-
- if (!(this.curTest_.name in this.result_.resultsByName) ||
- !this.result_.resultsByName[this.curTest_.name].length) {
- this.doSuccess(this.curTest_);
- }
- goog.testing.TestCase.currentTestName = null;
-
-
- if (this.now() - this.batchTime_ > goog.testing.TestCase.maxRunTime) {
- this.saveMessage('Breaking async');
- this.timeout(goog.bind(this.startNextBatch_, this), 0);
- return null;
- } else {
- return new goog.testing.Continuation_(goog.bind(this.runNextTest_, this));
- }
- };
- goog.testing.TestCase.prototype.startNextBatch_ = function() {
- this.batchTime_ = this.now();
- goog.testing.Continuation_.run(this.runNextTest_());
- };
- goog.testing.TestCase.prototype.orderTests_ = function() {
- switch (this.order) {
- case goog.testing.TestCase.Order.RANDOM:
-
- var i = this.tests_.length;
- while (i > 1) {
-
- var j = Math.floor(Math.random() * i);
- i--;
- var tmp = this.tests_[i];
- this.tests_[i] = this.tests_[j];
- this.tests_[j] = tmp;
- }
- break;
- case goog.testing.TestCase.Order.SORTED:
- this.tests_.sort(function(t1, t2) {
- if (t1.name == t2.name) {
- return 0;
- }
- return t1.name < t2.name ? -1 : 1;
- });
- break;
-
- }
- };
- goog.testing.TestCase.prototype.getGlobals = function(opt_prefix) {
- return goog.testing.TestCase.getGlobals(opt_prefix);
- };
- goog.testing.TestCase.getGlobals = function(opt_prefix) {
-
-
-
-
- return typeof goog.global['RuntimeObject'] != 'undefined' ?
- [goog.global['RuntimeObject']((opt_prefix || '') + '*'), goog.global] :
- [goog.global];
- };
- goog.testing.TestCase.activeTestCase_ = null;
- goog.testing.TestCase.getActiveTestCase = function() {
- var gTestRunner = goog.global['G_testRunner'];
- if (gTestRunner && gTestRunner.testCase) {
- return gTestRunner.testCase;
- } else {
- return goog.testing.TestCase.activeTestCase_;
- }
- };
- goog.testing.TestCase.invalidateAssertionException = function(e) {
- var testCase = goog.testing.TestCase.getActiveTestCase();
- if (testCase) {
- testCase.invalidateAssertionException(e);
- } else {
- goog.global.console.error(
- 'Failed to remove expected exception: no test case is installed.');
- }
- };
- goog.testing.TestCase.prototype.setUpPage = function() {};
- goog.testing.TestCase.prototype.tearDownPage = function() {};
- goog.testing.TestCase.prototype.setUp = function() {};
- goog.testing.TestCase.prototype.tearDown = function() {};
- goog.testing.TestCase.prototype.getAutoDiscoveryPrefix = function() {
- return 'test';
- };
- goog.testing.TestCase.prototype.getBatchTime = function() {
- return this.batchTime_;
- };
- goog.testing.TestCase.prototype.setBatchTime = function(batchTime) {
- this.batchTime_ = batchTime;
- };
- goog.testing.TestCase.prototype.createTestFromAutoDiscoveredFunction = function(
- name, ref) {
- return new goog.testing.TestCase.Test(name, ref, goog.global);
- };
- goog.testing.TestCase.prototype.autoDiscoverLifecycle = function(opt_obj) {
- var obj = opt_obj || goog.global;
- if (obj['setUp']) {
- this.setUp = goog.bind(obj['setUp'], obj);
- }
- if (obj['tearDown']) {
- this.tearDown = goog.bind(obj['tearDown'], obj);
- }
- if (obj['setUpPage']) {
- this.setUpPage = goog.bind(obj['setUpPage'], obj);
- }
- if (obj['tearDownPage']) {
- this.tearDownPage = goog.bind(obj['tearDownPage'], obj);
- }
- if (obj['runTests']) {
- this.runTests = goog.bind(obj['runTests'], obj);
- }
- if (obj['shouldRunTests']) {
- this.shouldRunTests = goog.bind(obj['shouldRunTests'], obj);
- }
- };
- goog.testing.TestCase.prototype.setTestObj = function(obj) {
-
-
- goog.asserts.assert(
- this.tests_.length == 0, 'Test methods have already been configured.');
- var regex = new RegExp('^' + this.getAutoDiscoveryPrefix());
- var properties = goog.object.getAllPropertyNames(obj);
- for (var i = 0; i < properties.length; i++) {
- var name = properties[i];
- if (regex.test(name)) {
- var testMethod = obj[name];
- if (goog.isFunction(testMethod)) {
- this.addNewTest(name, testMethod, obj);
- }
- }
- }
- if (obj['getTestName']) {
- this.name_ = obj['getTestName']();
- }
- this.autoDiscoverLifecycle(obj);
- };
- goog.testing.TestCase.prototype.autoDiscoverTests = function() {
- var prefix = this.getAutoDiscoveryPrefix();
- var testSources = this.getGlobals(prefix);
- var foundTests = [];
- for (var i = 0; i < testSources.length; i++) {
- var testSource = testSources[i];
- for (var name in testSource) {
- if ((new RegExp('^' + prefix)).test(name)) {
- var ref;
- try {
- ref = testSource[name];
- } catch (ex) {
-
-
-
-
- ref = undefined;
- }
- if (goog.isFunction(ref)) {
- foundTests.push(this.createTestFromAutoDiscoveredFunction(name, ref));
- }
- }
- }
- }
- for (var i = 0; i < foundTests.length; i++) {
- this.add(foundTests[i]);
- }
- this.orderTests_();
- this.log(this.getCount() + ' tests auto-discovered');
-
-
-
- this.autoDiscoverLifecycle();
- };
- goog.testing.TestCase.prototype.maybeFailTestEarly = function(testCase) {
- if (this.exceptionBeforeTest) {
-
- testCase.name = 'setUpPage for ' + testCase.name;
- this.doError(testCase, this.exceptionBeforeTest);
- return true;
- }
- return false;
- };
- goog.testing.TestCase.prototype.cycleTests = function() {
- this.saveMessage('Start');
- this.batchTime_ = this.now();
- if (this.running) {
- this.runNextTestCallback_ = goog.nullFunction;
-
-
- goog.testing.Continuation_.run(this.runNextTest_());
- }
- };
- goog.testing.TestCase.prototype.countNumFilesLoaded_ = function() {
- var scripts = goog.dom.getElementsByTagName(goog.dom.TagName.SCRIPT);
- var count = 0;
- for (var i = 0, n = scripts.length; i < n; i++) {
- if (scripts[i].src) {
- count++;
- }
- }
- return count;
- };
- goog.testing.TestCase.prototype.timeout = function(fn, time) {
-
-
-
- var protectedSetTimeout = goog.testing.TestCase.protectedSetTimeout_;
- return protectedSetTimeout(fn, time);
- };
- goog.testing.TestCase.prototype.clearTimeout = function(id) {
-
- var protectedClearTimeout = goog.testing.TestCase.protectedClearTimeout_;
- protectedClearTimeout(id);
- };
- goog.testing.TestCase.prototype.now = function() {
- return goog.testing.TestCase.now();
- };
- goog.testing.TestCase.now = function() {
-
- if (goog.testing.TestCase.protectedPerformance_) {
- return goog.testing.TestCase.protectedPerformance_.now();
- }
-
-
- var protectedDate = goog.testing.TestCase.protectedDate_;
- return new protectedDate().getTime();
- };
- goog.testing.TestCase.prototype.getTimeStamp_ = function() {
-
- var protectedDate = goog.testing.TestCase.protectedDate_;
- var d = new protectedDate();
-
- var millis = '00' + d.getMilliseconds();
- millis = millis.substr(millis.length - 3);
- return this.pad_(d.getHours()) + ':' + this.pad_(d.getMinutes()) + ':' +
- this.pad_(d.getSeconds()) + '.' + millis;
- };
- goog.testing.TestCase.prototype.pad_ = function(number) {
- return number < 10 ? '0' + number : String(number);
- };
- goog.testing.TestCase.prototype.trimPath_ = function(path) {
- return path.substring(path.indexOf('google3') + 8);
- };
- goog.testing.TestCase.prototype.doSuccess = function(test) {
- this.result_.successCount++;
-
-
- if (!(test.name in this.result_.resultsByName)) {
- this.result_.resultsByName[test.name] = [];
- }
- var message = test.name + ' : PASSED';
- this.saveMessage(message);
- this.log(message);
- if (this.testDone_) {
- this.doTestDone_(test, []);
- }
- };
- goog.testing.TestCase.prototype.recordError_ = function(testName, opt_e) {
- var message = testName + ' : FAILED';
- this.log(message);
- this.saveMessage(message);
- var err = this.logError(testName, opt_e);
- this.result_.errors.push(err);
- if (testName in this.result_.resultsByName) {
- this.result_.resultsByName[testName].push(err);
- } else {
- this.result_.resultsByName[testName] = [err];
- }
- };
- goog.testing.TestCase.prototype.doError = function(test, opt_e) {
- if (!test || !test.name) {
- console.error('no name!' + opt_e);
- }
- this.recordError_(test.name, opt_e);
- if (this.testDone_) {
- var results = this.result_.resultsByName[test.name];
- var errMsgs = [];
- for (var i = 0; i < results.length; i++) {
- errMsgs.push(results[i].toString());
- }
- this.doTestDone_(test, errMsgs);
- }
- };
- goog.testing.TestCase.prototype.raiseAssertionException = function(e) {
- if (this.failOnUnreportedAsserts) {
- this.thrownAssertionExceptions_.push(e);
- }
- throw e;
- };
- goog.testing.TestCase.prototype.invalidateAssertionException = function(e) {
- if (this.failOnUnreportedAsserts) {
- goog.array.remove(this.thrownAssertionExceptions_, e);
- }
- };
- goog.testing.TestCase.prototype.logError = function(name, opt_e) {
- var errMsg = null;
- var stack = null;
- if (opt_e) {
- this.log(opt_e);
- if (goog.isString(opt_e)) {
- errMsg = opt_e;
- } else {
- errMsg = opt_e.message || opt_e.description || opt_e.toString();
- stack = opt_e.stack ? opt_e.stack : opt_e['stackTrace'];
- }
- } else {
- errMsg = 'An unknown error occurred';
- }
- if (stack) {
-
- stack = stack.replace(
- /\n.*goog\.testing\.TestCase\.(prototype\.)?invokeTestFunction[^\0]*/m,
- '');
- }
- var err = new goog.testing.TestCase.Error(name, errMsg, stack);
-
- if (!opt_e || !opt_e['isJsUnitException'] ||
- !opt_e['loggedJsUnitException']) {
- this.saveMessage(err.toString());
- }
- if (opt_e && opt_e['isJsUnitException']) {
- opt_e['loggedJsUnitException'] = true;
- }
- return err;
- };
- goog.testing.TestCase.Test = function(name, ref, opt_scope) {
-
- this.name = name;
-
- this.ref = ref;
-
- this.scope = opt_scope || null;
-
- this.startTime_;
-
- this.stoppedTime_;
- };
- goog.testing.TestCase.Test.prototype.execute = function() {
- this.ref.call(this.scope);
- };
- goog.testing.TestCase.Test.prototype.started = function() {
- this.startTime_ = goog.testing.TestCase.now();
- };
- goog.testing.TestCase.Test.prototype.stopped = function() {
- this.stoppedTime_ = goog.testing.TestCase.now();
- };
- goog.testing.TestCase.Test.prototype.getElapsedTime = function() {
- return this.stoppedTime_ - this.startTime_;
- };
- goog.testing.TestCase.Result = function(testCase) {
-
- this.testCase_ = testCase;
-
- this.totalCount = 0;
-
- this.runCount = 0;
-
- this.successCount = 0;
-
- this.runTime = 0;
-
- this.numFilesLoaded = 0;
-
- this.testSuppressed = false;
-
- this.resultsByName = {};
-
- this.errors = [];
-
- this.messages = [];
-
- this.complete = false;
- };
- goog.testing.TestCase.Result.prototype.isSuccess = function() {
- return this.complete && this.errors.length == 0;
- };
- goog.testing.TestCase.Result.prototype.getSummary = function() {
- var summary = this.runCount + ' of ' + this.totalCount + ' tests run in ' +
- this.runTime + 'ms.\n';
- if (this.testSuppressed) {
- summary += 'Tests not run because shouldRunTests() returned false.';
- } else {
- var failures = this.totalCount - this.successCount;
- var suppressionMessage = '';
- var countOfRunTests = this.testCase_.getActuallyRunCount();
- if (countOfRunTests) {
- failures = countOfRunTests - this.successCount;
- suppressionMessage = ', ' + (this.totalCount - countOfRunTests) +
- ' suppressed by querystring';
- }
- summary += this.successCount + ' passed, ' + failures + ' failed' +
- suppressionMessage + '.\n' + Math.round(this.runTime / this.runCount) +
- ' ms/test. ' + this.numFilesLoaded + ' files loaded.';
- }
- return summary;
- };
- goog.testing.TestCase.prototype.setTestDoneCallback = function(testDone) {
- this.testDone_ = testDone;
- };
- goog.testing.TestCase.prototype.doTestDone_ = function(test, errMsgs) {
- test.stopped();
- this.testDone_(test, errMsgs);
- };
- goog.testing.TestCase.initializeTestCase = function(testCase, opt_testDone) {
- if (opt_testDone) {
- testCase.setTestDoneCallback(opt_testDone);
- }
- testCase.autoDiscoverTests();
- if (goog.global.location) {
- var search = goog.global.location.search;
- testCase.setOrder(
- goog.testing.TestCase.parseOrder_(search) ||
- goog.testing.TestCase.Order.SORTED);
- testCase.setTestsToRun(goog.testing.TestCase.parseRunTests_(search));
- }
- goog.testing.TestCase.activeTestCase_ = testCase;
- };
- goog.testing.TestCase.initializeTestRunner = function(testCase, opt_testDone) {
- goog.testing.TestCase.initializeTestCase(testCase, opt_testDone);
- var gTestRunner = goog.global['G_testRunner'];
- if (gTestRunner) {
- gTestRunner['initialize'](testCase);
- } else {
- throw Error(
- 'G_testRunner is undefined. Please ensure goog.testing.jsunit' +
- ' is included.');
- }
- };
- goog.testing.TestCase.parseOrder_ = function(search) {
- var order = null;
- var orderMatch = search.match(/(?:\?|&)order=(natural|random|sorted)/i);
- if (orderMatch) {
- order = (
- orderMatch[1].toLowerCase());
- }
- return order;
- };
- goog.testing.TestCase.parseRunTests_ = function(search) {
- var testsToRun = null;
- var runTestsMatch = search.match(/(?:\?|&)runTests=([^?&]+)/i);
- if (runTestsMatch) {
- testsToRun = {};
- var arr = runTestsMatch[1].split(',');
- for (var i = 0, len = arr.length; i < len; i++) {
- testsToRun[arr[i]] = true;
- }
- }
- return testsToRun;
- };
- goog.testing.TestCase.prototype.rejectIfPromiseTimesOut_ = function(
- promise, timeoutInMs, errorMsg) {
- var self = this;
- var start = this.now();
- return new goog.Promise(function(resolve, reject) {
- var timeoutId = self.timeout(function() {
- var elapsed = self.now() - start;
- reject(new Error(errorMsg + '\nElapsed time: ' + elapsed + 'ms.'));
- }, timeoutInMs);
- promise.then(resolve, reject);
- var clearTimeout = goog.bind(self.clearTimeout, self, timeoutId);
- promise.then(clearTimeout, clearTimeout);
- });
- };
- goog.testing.TestCase.Error = function(source, message, opt_stack) {
-
- this.source = source;
-
- this.message = message;
-
- this.stack = null;
- if (opt_stack) {
- this.stack = opt_stack;
- } else {
-
- if (Error.captureStackTrace) {
-
- Error.captureStackTrace(this, goog.testing.TestCase.Error);
- } else {
- var stack = new Error().stack;
- if (stack) {
- this.stack = stack;
- }
- }
- }
- };
- goog.testing.TestCase.Error.prototype.toString = function() {
- return 'ERROR in ' + this.source + '\n' + this.message +
- (this.stack ? '\n' + this.stack : '');
- };
- goog.testing.TestCase.Error.prototype.toObject_ = function() {
- return {
- 'source': this.source,
- 'message': this.message,
- 'stacktrace': this.stack || ''
- };
- };
- goog.testing.Continuation_ = function(fn) {
-
- this.fn_ = fn;
- };
- goog.testing.Continuation_.run = function(continuation) {
- var fn = continuation && continuation.fn_;
- while (fn) {
- continuation = fn();
- fn = continuation && continuation.fn_;
- }
- };
|