From 5ffc5f0ba7ae07fc165fecfc13cceaee1b28dc3e Mon Sep 17 00:00:00 2001 From: Olmo Maldonado Date: Fri, 15 Apr 2011 23:06:35 -0500 Subject: [PATCH] Refactored into a new Env class. Now runTestConfiguration just resets the jasmine.currentEnv_ variable with ours. --- src/JasmineAdapter.js | 194 ++++++++++++++++++++++-------------------- 1 file changed, 100 insertions(+), 94 deletions(-) diff --git a/src/JasmineAdapter.js b/src/JasmineAdapter.js index cfce5be..a0ef268 100644 --- a/src/JasmineAdapter.js +++ b/src/JasmineAdapter.js @@ -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_ = []; }; @@ -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) }); } @@ -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); +}; })();