jasmine/spec/bootstrap.js

146 lines
3.9 KiB
JavaScript
Raw Normal View History

2009-03-01 13:34:00 +00:00
var createElement = function(tag, attrs) {
var element = document.createElement(tag);
for (var attr in attrs) {
element[attr] = attrs[attr];
}
return element;
};
// Bootstrap Test Reporter function
2009-03-01 13:34:00 +00:00
var Reporter = function () {
this.total = 0;
this.passes = 0;
this.fails = 0;
this.start = new Date();
};
2009-03-01 13:34:00 +00:00
Reporter.prototype.toJSON = function(object) {
return JSON.stringify(object);
};
2009-03-01 13:34:00 +00:00
Reporter.prototype.test = function (result, message) {
this.total++;
if (result) {
2009-03-01 13:34:00 +00:00
this.passes++;
iconElement = document.getElementById('icons');
iconElement.appendChild(createElement('img', {src: '../images/go-16.png'}));
}
else {
this.fails++;
var fails_report = document.getElementById('fails');
fails_report.style.display = "";
var iconElement = document.getElementById('icons');
iconElement.appendChild(createElement('img', {src: '../images/fail-16.png'}));
var failMessages = document.getElementById('fail_messages');
var newFail = createElement('p', {'class': 'fail'});
newFail.innerHTML = message;
failMessages.appendChild(newFail);
}
};
2009-03-01 13:34:00 +00:00
Reporter.prototype.summary = function () {
var el = createElement('p', {'class': ((this.fails > 0) ? 'failInSummary' : '') });
2009-03-01 13:34:00 +00:00
el.innerHTML = this.total + ' expectations, ' + this.passes + ' passing, ' + this.fails + ' failed in ' + (new Date().getTime() - this.start.getTime()) + "ms.";
2008-12-01 20:26:12 +00:00
2009-03-01 13:34:00 +00:00
var summaryElement = document.getElementById('results_summary');
summaryElement.appendChild(el);
summaryElement.style.display = "";
};
var reporter = new Reporter();
function runSuite(filename) {
2009-06-16 14:19:15 +00:00
console.log(filename);
var suite = jasmine.include(filename);
2009-03-01 13:34:00 +00:00
suite.execute();
emitSuiteResults(filename, suite);
}
2009-03-01 13:34:00 +00:00
function emitSpecResults(testName, spec) {
var results = spec.results.getItems();
reporter.test(results.length > 0, testName + ": should have results, got " + results.length);
2009-03-01 13:34:00 +00:00
for (var i = 0; i < results.length; i++) {
reporter.test(results[i].passed === true, testName + ':' + spec.getFullName() + ": expectation number " + i + " failed: " + results[i].message);
}
}
2009-03-01 13:34:00 +00:00
function emitSuiteResults(testName, suite) {
for (var j = 0; j < suite.specs.length; j++) {
var specOrSuite = suite.specs[j];
2009-03-01 13:34:00 +00:00
if (specOrSuite instanceof jasmine.Suite) {
emitSuiteResults(testName, specOrSuite);
} else {
emitSpecResults(testName, specOrSuite);
2009-03-01 13:34:00 +00:00
}
}
}
2009-03-01 13:34:00 +00:00
var testExplodes = function () {
var suite = describe('exploding', function () {
it('should throw an exception when this.explodes is called inside a spec', function() {
var exceptionMessage = false;
2009-03-01 13:34:00 +00:00
try {
this.explodes();
}
catch (e) {
exceptionMessage = e;
}
expect(exceptionMessage).toEqual('explodes function should not have been called');
});
});
suite.execute();
emitSuiteResults('testExplodes', suite);
2009-03-01 13:34:00 +00:00
};
2008-12-02 01:57:21 +00:00
function newJasmineEnv() {
return new jasmine.Env();
}
var testRunner = function() {
2009-03-01 13:34:00 +00:00
};
var testRunnerFinishCallback = function () {
var env = newJasmineEnv();
var foo = 0;
env.currentRunner.finish();
reporter.test((env.currentRunner.finished === true),
"Runner finished flag was not set.");
env.currentRunner.finishCallback = function () {
foo++;
2009-03-01 13:34:00 +00:00
};
env.currentRunner.finish();
reporter.test((env.currentRunner.finished === true),
"Runner finished flag was not set.");
reporter.test((foo === 1),
"Runner finish callback was not called");
2009-03-01 13:34:00 +00:00
};
var runTests = function () {
2009-03-01 13:34:00 +00:00
document.getElementById('spinner').style.display = "";
2009-06-16 14:19:15 +00:00
runSuite('suites/PrettyPrintTest.js');
runSuite('suites/MatchersTest.js');
runSuite('suites/SpecRunningTest.js');
runSuite('suites/NestedResultsTest.js');
runSuite('suites/ReporterTest.js');
runSuite('suites/RunnerTest.js');
runSuite('suites/SpyTest.js');
runSuite('suites/ExceptionsTest.js');
2009-03-01 13:34:00 +00:00
reporter.summary();
document.getElementById('spinner').style.display = "none";
2009-06-15 06:34:29 +00:00
};