upgrade to latest jasmine adapter

This commit is contained in:
Misko Hevery 2011-01-06 12:25:18 -08:00
parent da1d50fbe9
commit 247c99a8a4

View file

@ -1,111 +1,165 @@
/** /**
* @fileoverview Jasmine JsTestDriver Adapter. * @fileoverview Jasmine JsTestDriver Adapter.
* @author ibolmo@gmail.com (Olmo Maldonado)
* @author misko@hevery.com (Misko Hevery) * @author misko@hevery.com (Misko Hevery)
*/ */
(function(window) {
var rootDescribes = new Describes(window);
var describePath = [];
rootDescribes.collectMode();
var jasmineTest = TestCase('Jasmine Adapter Tests');
var jasminePlugin = {
name:'jasmine',
runTestConfiguration: function(testRunConfiguration, onTestDone, onTestRunConfigurationComplete){
if (testRunConfiguration.testCaseInfo_.template_ !== jasmineTest) return;
var jasmineEnv = jasmine.currentEnv_ = new jasmine.Env();
rootDescribes.playback();
var specLog = jstestdriver.console.log_ = [];
var start;
jasmineEnv.specFilter = function(spec) {
return rootDescribes.isExclusive(spec);
};
jasmineEnv.reporter = {
log: function(str){
specLog.push(str);
},
(function() { reportRunnerStarting: function(runner) { },
function bind(_this, _function){ reportSpecStarting: function(spec) {
return function(){ specLog = jstestdriver.console.log_ = [];
return _function.call(_this); start = new Date().getTime();
}; },
}
var currentFrame = frame(null, null); reportSpecResults: function(spec) {
var suite = spec.suite;
var results = spec.results();
if (results.skipped) return;
var end = new Date().getTime();
var messages = [];
var resultItems = results.getItems();
var state = 'passed';
for ( var i = 0; i < resultItems.length; i++) {
if (!resultItems[i].passed()) {
state = resultItems[i].message.match(/AssertionError:/) ? 'error' : 'failed';
messages.push(resultItems[i].toString());
messages.push(resultItems[i].trace.stack);
}
}
onTestDone(
new jstestdriver.TestResult(
suite.getFullName(),
spec.description,
state,
messages.join('\n'),
specLog.join('\n'),
end - start));
},
function frame(parent, name){ reportSuiteResults: function(suite) {},
var caseName = (parent && parent.caseName ? parent.caseName + " " : '') + (name ? name : '');
var frame = { reportRunnerResults: function(runner) {
name: name, onTestRunConfigurationComplete();
caseName: caseName, }
parent: parent, };
testCase: TestCase(caseName), jasmineEnv.execute();
before: [], return true;
after: [],
runBefore: function(){
if (parent) parent.runBefore.apply(this);
for ( var i = 0; i < frame.before.length; i++) {
frame.before[i].apply(this);
}
}, },
runAfter: function(){ onTestsFinish: function(){
for ( var i = 0; i < frame.after.length; i++) { jasmine.currentEnv_ = null;
frame.after[i].apply(this); rootDescribes.collectMode();
}
if (parent) parent.runAfter.apply(this);
} }
};
return frame;
}; };
jstestdriver.pluginRegistrar.register(jasminePlugin);
jasmine.Env.prototype.describe = (function(describe){
return function(description){ function noop(){}
currentFrame = frame(currentFrame, description); function Describes(window){
var val = describe.apply(this, arguments); var describes = {};
currentFrame = currentFrame.parent; var beforeEachs = {};
return val; var afterEachs = {};
}; var exclusive;
var collectMode = true;
})(jasmine.Env.prototype.describe); intercept('describe', describes);
intercept('xdescribe', describes);
var id = 0; intercept('beforeEach', beforeEachs);
intercept('afterEach', afterEachs);
jasmine.Env.prototype.it = (function(it){
return function(desc, itFn){ function intercept(functionName, collection){
var self = this; window[functionName] = function(desc, fn){
var spec = it.apply(this, arguments); if (collectMode) {
var currentSpec = this.currentSpec; collection[desc] = function(){
if (!currentSpec.$id) { jasmine.getEnv()[functionName](desc, fn);
currentSpec.$id = id++; };
} } else {
var frame = this.jstdFrame = currentFrame; jasmine.getEnv()[functionName](desc, fn);
var name = 'test that it ' + desc;
if (this.jstdFrame.testCase.prototype[name])
throw "Spec with name '" + desc + "' already exists.";
this.jstdFrame.testCase.prototype[name] = function(){
jasmine.getEnv().currentSpec = currentSpec;
frame.runBefore.apply(currentSpec);
try {
itFn.apply(currentSpec);
} finally {
frame.runAfter.apply(currentSpec);
} }
}; };
return spec; }
window.ddescribe = function(name, fn){
exclusive = true;
console.log('ddescribe', name);
window.describe(name, function(){
var oldIt = window.it;
window.it = window.iit;
try {
fn.call(this);
} finally {
window.it = oldIt;
};
});
}; };
window.iit = function(name, fn){
})(jasmine.Env.prototype.it); exclusive = fn.exclusive = true;
console.log(fn);
jasmine.getEnv().it(name, fn);
jasmine.Env.prototype.beforeEach = (function(beforeEach){
return function(beforeEachFunction) {
beforeEach.apply(this, arguments);
currentFrame.before.push(beforeEachFunction);
}; };
})(jasmine.Env.prototype.beforeEach);
this.collectMode = function() {
collectMode = true;
jasmine.Env.prototype.afterEach = (function(afterEach){ exclusive = false;
return function(afterEachFunction) {
afterEach.apply(this, arguments);
currentFrame.after.push(afterEachFunction);
}; };
this.playback = function(){
})(jasmine.Env.prototype.afterEach); collectMode = false;
playback(beforeEachs);
playback(afterEachs);
jasmine.NestedResults.prototype.addResult = (function(addResult){ playback(describes);
return function(result) {
addResult.call(this, result); function playback(set) {
if (result.type != 'MessageResult' && !result.passed()) fail(result.message); for ( var name in set) {
set[name]();
}
}
}; };
this.isExclusive = function(spec) {
if (exclusive) {
var blocks = spec.queue.blocks;
for ( var i = 0; i < blocks.length; i++) {
if (blocks[i].func.exclusive) {
return true;
}
}
return false;
}
return true;
};
}
})(window);
})(jasmine.NestedResults.prototype.addResult); // Patch Jasmine for proper stack traces
jasmine.Spec.prototype.fail = function (e) {
var expectationResult = new jasmine.ExpectationResult({
passed: false,
message: e ? jasmine.util.formatException(e) : 'Exception'
});
// PATCH
if (e) {
expectationResult.trace = e;
}
this.results_.addResult(expectationResult);
};
// Reset environment with overriden methods.
jasmine.currentEnv_ = null;
jasmine.getEnv();
})();