Skip to content

Commit

Permalink
Refactored into a new Env class. Now runTestConfiguration just resets…
Browse files Browse the repository at this point in the history
… the jasmine.currentEnv_ variable with ours.
  • Loading branch information
ibolmo committed Apr 16, 2011
1 parent c422e00 commit 5ffc5f0
Showing 1 changed file with 100 additions and 94 deletions.
194 changes: 100 additions & 94 deletions src/JasmineAdapter.js
Expand Up @@ -4,16 +4,50 @@
* @author olmo.maldonado@gmail.com (Olmo Maldonado)
*/
(function(){

var Env = function(onTestDone, onComplete){
jasmine.Env.call(this);

this.specFilter = function(spec){
if (!exclusive) return true;
var blocks = spec.queue.blocks, l = blocks.length;
for (var i = 0; i < l; i++) if (blocks[i].func.exclusive >= exclusive) return true;
return false;
};

this.reporter = new Reporter(onTestDone, onComplete);
};

jasmine.util.inherit(Env, jasmine.Env);

var Reporter = function(onTestDone, onTestRunConfigurationComplete){
Env.prototype.execute = function(){
collectMode = false;
playback(beforeEachs);
playback(afterEachs);
playback(describes);

jasmine.Env.prototype.execute.call(this);
};

var Reporter = function(onTestDone, onComplete){
this.onTestDone = onTestDone;
this.onTestRunConfigurationComplete = onTestRunConfigurationComplete;
this.onComplete = onComplete;
this.reset();
};

jasmine.util.inherit(Reporter, jasmine.Reporter);


Reporter.formatStack = function(stack) {
var line, lines = (stack || '').split(/\r?\n/), l = lines.length, frames = new Array(l);
for (var i = 0; i < l; i++){
line = lines[i];
if (line.match(/\/jasmine[\.-]/)) continue;
frames.push(line.replace(/https?:\/\/\w+(:\d+)?\/test\//, '').replace(/^\s*/, ' '));
}
return frames.join('\n');
};

Reporter.prototype.reset = function(){
this.specLog = jstestdriver.console.log_ = [];
};
Expand Down Expand Up @@ -43,7 +77,7 @@ Reporter.prototype.reportSpecResults = function(spec){
messages.push({
message: item + '',
name: item.trace.name,
stack: formatStack(item.trace.stack)
stack: Reporter.formatStack(item.trace.stack)
});
}

Expand All @@ -59,108 +93,80 @@ Reporter.prototype.reportSpecResults = function(spec){


Reporter.prototype.reportRunnerResults = function(){
this.onTestRunConfigurationComplete();
this.onComplete();
};

var describes = [], beforeEachs = [], afterEachs = [];

var describes = [], beforeEachs = [], afterEachs = [];

// Here we store:
// 0: everyone runs
// 1: run everything under ddescribe
// 2: run only iits (ignore ddescribe)
var exclusive = 0, collectMode = true;
// Here we store:
// 0: everyone runs
// 1: run everything under ddescribe
// 2: run only iits (ignore ddescribe)
var exclusive = 0, collectMode = true;

intercept('describe', describes);
intercept('beforeEach', beforeEachs);
intercept('afterEach', afterEachs);
intercept('describe', describes);
intercept('beforeEach', beforeEachs);
intercept('afterEach', afterEachs);

var template = TestCase('Jasmine Adapter Tests', null, 'jasmine test case');
var template = TestCase('Jasmine Adapter Tests', null, 'jasmine test case');

jstestdriver.pluginRegistrar.register({
jstestdriver.pluginRegistrar.register({

name: 'jasmine',

name: 'jasmine',

runTestConfiguration: function(config, onTestDone, onTestRunConfigurationComplete){
if (config.testCaseInfo_.template_ !== template) return;

var jasmineEnv = jasmine.currentEnv_ = new jasmine.Env();

collectMode = false;
playback(beforeEachs);
playback(afterEachs);
playback(describes);

jasmineEnv.specFilter = function(spec) {
if (!exclusive) return true;
var block, blocks = spec.queue.blocks, l = blocks.length;
for (var i = 0; i < l; i++) if (blocks[i].func.exclusive >= exclusive) return true;
return false;
};

jasmineEnv.reporter = new Reporter(onTestDone, onTestRunConfigurationComplete);

jasmineEnv.execute();
return true;
},

onTestsFinish: function(){
jasmine.currentEnv_ = null;
collectMode = true;
exclusive = 0; // run everything
}

});

function formatStack(stack) {
var line, lines = (stack || '').split(/\r?\n/), l = lines.length, frames = new Array(l);
for (var i = 0; i < l; i++){
line = lines[i];
if (line.match(/\/jasmine[\.-]/)) continue;
frames.push(line.replace(/https?:\/\/\w+(:\d+)?\/test\//, '').replace(/^\s*/, ' '));
runTestConfiguration: function(config, onTestDone, onComplete){
if (config.testCaseInfo_.template_ !== template) return;
(jasmine.currentEnv_ = new Env(onTestDone, onComplete)).execute();
return true;
},

onTestsFinish: function(){
jasmine.currentEnv_ = null;
collectMode = true;
exclusive = 0; // run everything
}

});

function playback(set) {
for (var i = 0, l = set.length; i < l; i++) set[i]();
}

function intercept(functionName, collection){
window[functionName] = function(desc, fn){
if (collectMode){
collection.push(function(){
jasmine.getEnv()[functionName](desc, fn);
});
} else {
jasmine.getEnv()[functionName](desc, fn);
}
return frames.join('\n');
}

function playback(set) {
for (var i = 0, l = set.length; i < l; i++) set[i]();
}
};
}

function intercept(functionName, collection){
window[functionName] = function(desc, fn){
if (collectMode){
collection.push(function(){
jasmine.getEnv()[functionName](desc, fn);
});
} else {
jasmine.getEnv()[functionName](desc, fn);
}
};
}
window.ddescribe = function(name, fn){
if (exclusive < 1) exclusive = 1; // run ddescribe only

window.ddescribe = function(name, fn){
if (exclusive < 1) exclusive = 1; // run ddescribe only
window.describe(name, function(){
var oldIt = window.it;
window.it = function(name, fn){
fn.exclusive = 1; // run anything under ddescribe
jasmine.getEnv().it(name, fn);
};

window.describe(name, function(){
var oldIt = window.it;
window.it = function(name, fn){
fn.exclusive = 1; // run anything under ddescribe
jasmine.getEnv().it(name, fn);
};

try {
fn.call(this);
} finally {
window.it = oldIt;
};

});
};

window.iit = function(name, fn){
exclusive = fn.exclusive = 2; // run only iits
jasmine.getEnv().it(name, fn);
};
try {
fn.call(this);
} finally {
window.it = oldIt;
};

});
};

window.iit = function(name, fn){
exclusive = fn.exclusive = 2; // run only iits
jasmine.getEnv().it(name, fn);
};

})();

Expand Down

0 comments on commit 5ffc5f0

Please sign in to comment.