|
| 1 | +/** |
| 2 | + * @fileoverview Jasmine JsTestDriver Adapter. |
| 3 | + * @author misko@hevery.com (Misko Hevery) |
| 4 | + * @author olmo.maldonado@gmail.com (Olmo Maldonado) |
| 5 | + */ |
| 6 | +(function(){ |
| 7 | + |
| 8 | + |
| 9 | +var Env = function(onTestDone, onComplete){ |
| 10 | + jasmine.Env.call(this); |
| 11 | + |
| 12 | + this.specFilter = function(spec){ |
| 13 | + if (!this.exclusive) return true; |
| 14 | + var blocks = spec.queue.blocks, l = blocks.length; |
| 15 | + for (var i = 0; i < l; i++) if (blocks[i].func.exclusive >= this.exclusive) return true; |
| 16 | + return false; |
| 17 | + }; |
| 18 | + |
| 19 | + this.reporter = new Reporter(onTestDone, onComplete); |
| 20 | +}; |
| 21 | +jasmine.util.inherit(Env, jasmine.Env); |
| 22 | + |
| 23 | +// Here we store: |
| 24 | +// 0: everyone runs |
| 25 | +// 1: run everything under ddescribe |
| 26 | +// 2: run only iits (ignore ddescribe) |
| 27 | +Env.prototype.exclusive = 0; |
| 28 | + |
| 29 | + |
| 30 | +Env.prototype.execute = function(){ |
| 31 | + collectMode = false; |
| 32 | + playback(); |
| 33 | + jasmine.Env.prototype.execute.call(this); |
| 34 | +}; |
| 35 | + |
| 36 | + |
| 37 | +var Reporter = function(onTestDone, onComplete){ |
| 38 | + this.onTestDone = onTestDone; |
| 39 | + this.onComplete = onComplete; |
| 40 | + this.reset(); |
| 41 | +}; |
| 42 | +jasmine.util.inherit(Reporter, jasmine.Reporter); |
| 43 | + |
| 44 | + |
| 45 | +Reporter.formatStack = function(stack) { |
| 46 | + var line, lines = (stack || '').split(/\r?\n/), l = lines.length, frames = []; |
| 47 | + for (var i = 0; i < l; i++){ |
| 48 | + line = lines[i]; |
| 49 | + if (line.match(/\/jasmine[\.-]/)) continue; |
| 50 | + frames.push(line.replace(/https?:\/\/\w+(:\d+)?\/test\//, '').replace(/^\s*/, ' ')); |
| 51 | + } |
| 52 | + return frames.join('\n'); |
| 53 | +}; |
| 54 | + |
| 55 | + |
| 56 | +Reporter.prototype.reset = function(){ |
| 57 | + this.specLog = jstestdriver.console.log_ = []; |
| 58 | +}; |
| 59 | + |
| 60 | + |
| 61 | +Reporter.prototype.log = function(str){ |
| 62 | + this.specLog.push(str); |
| 63 | +}; |
| 64 | + |
| 65 | + |
| 66 | +Reporter.prototype.reportSpecStarting = function(){ |
| 67 | + this.reset(); |
| 68 | + this.start = +new Date(); |
| 69 | +}; |
| 70 | + |
| 71 | + |
| 72 | +Reporter.prototype.reportSpecResults = function(spec){ |
| 73 | + var elapsed = +new Date() - this.start, results = spec.results(); |
| 74 | + |
| 75 | + if (results.skipped) return; |
| 76 | + |
| 77 | + var item, state = 'passed', items = results.getItems(), l = items.length, messages = []; |
| 78 | + for (var i = 0; i < l; i++){ |
| 79 | + item = items[i]; |
| 80 | + if (item.passed()) continue; |
| 81 | + state = (item.message.indexOf('AssertionError:') != -1) ? 'error' : 'failed'; |
| 82 | + messages.push({ |
| 83 | + message: item + '', |
| 84 | + name: item.trace.name, |
| 85 | + stack: Reporter.formatStack(item.trace.stack) |
| 86 | + }); |
| 87 | + } |
| 88 | + |
| 89 | + this.onTestDone(new jstestdriver.TestResult( |
| 90 | + spec.suite.getFullName(), |
| 91 | + spec.description, |
| 92 | + state, |
| 93 | + jstestdriver.angular.toJson(messages), |
| 94 | + this.specLog.join('\n'), |
| 95 | + elapsed |
| 96 | + )); |
| 97 | +}; |
| 98 | + |
| 99 | + |
| 100 | +Reporter.prototype.reportRunnerResults = function(){ |
| 101 | + this.onComplete(); |
| 102 | +}; |
| 103 | + |
| 104 | + |
| 105 | +var collectMode = true, intercepted = {}; |
| 106 | + |
| 107 | +describe = intercept('describe'); |
| 108 | +beforeEach = intercept('beforeEach'); |
| 109 | +afterEach = intercept('afterEach'); |
| 110 | + |
| 111 | +var JASMINE_TYPE = 'jasmine test case'; |
| 112 | +TestCase('Jasmine Adapter Tests', null, JASMINE_TYPE); |
| 113 | + |
| 114 | +jstestdriver.pluginRegistrar.register({ |
| 115 | + |
| 116 | + name: 'jasmine', |
| 117 | + |
| 118 | + getTestRunsConfigurationFor: function(testCaseInfos, expressions, testRunsConfiguration) { |
| 119 | + for (var i = 0; i < testCaseInfos.length; i++) { |
| 120 | + if (testCaseInfos[i].getType() == JASMINE_TYPE) { |
| 121 | + testRunsConfiguration.push(new jstestdriver.TestRunConfiguration(testCaseInfos[i], [])); |
| 122 | + } |
| 123 | + } |
| 124 | + return false; // allow other TestCases to be collected. |
| 125 | + }, |
| 126 | + |
| 127 | + runTestConfiguration: function(config, onTestDone, onComplete){ |
| 128 | + if (config.getTestCaseInfo().getType() != JASMINE_TYPE) return false; |
| 129 | + (jasmine.currentEnv_ = new Env(onTestDone, onComplete)).execute(); |
| 130 | + return true; |
| 131 | + }, |
| 132 | + |
| 133 | + onTestsFinish: function(){ |
| 134 | + jasmine.currentEnv_ = null; |
| 135 | + collectMode = true; |
| 136 | + } |
| 137 | + |
| 138 | +}); |
| 139 | + |
| 140 | +function intercept(method){ |
| 141 | + var bucket = intercepted[method] = [], method = window[method]; |
| 142 | + return function(desc, fn){ |
| 143 | + if (collectMode) bucket.push(function(){ method(desc, fn); }); |
| 144 | + else method(desc, fn); |
| 145 | + }; |
| 146 | +} |
| 147 | + |
| 148 | +function playback(){ |
| 149 | + for (var method in intercepted){ |
| 150 | + var bucket = intercepted[method]; |
| 151 | + for (var i = 0, l = bucket.length; i < l; i++) bucket[i](); |
| 152 | + } |
| 153 | +} |
| 154 | + |
| 155 | +})(); |
| 156 | + |
| 157 | +var ddescribe = function(name, fn){ |
| 158 | + var env = jasmine.getEnv(); |
| 159 | + if (!env.exclusive) env.exclusive = 1; // run ddescribe only |
| 160 | + describe(name, function(){ |
| 161 | + var oldIt = it; |
| 162 | + it = function(name, fn){ |
| 163 | + fn.exclusive = 1; // run anything under ddescribe |
| 164 | + env.it(name, fn); |
| 165 | + }; |
| 166 | + |
| 167 | + try { |
| 168 | + fn.call(this); |
| 169 | + } finally { |
| 170 | + it = oldIt; |
| 171 | + }; |
| 172 | + }); |
| 173 | +}; |
| 174 | + |
| 175 | +var iit = function(name, fn){ |
| 176 | + var env = jasmine.getEnv(); |
| 177 | + env.exclusive = fn.exclusive = 2; // run only iits |
| 178 | + env.it(name, fn); |
| 179 | +}; |
| 180 | + |
| 181 | +// Patch Jasmine for proper stack traces |
| 182 | +jasmine.Spec.prototype.fail = function (e) { |
| 183 | + var result = new jasmine.ExpectationResult({ |
| 184 | + passed: false, |
| 185 | + message: e ? jasmine.util.formatException(e) : 'Exception' |
| 186 | + }); |
| 187 | + if(e) result.trace = e; |
| 188 | + this.results_.addResult(result); |
| 189 | +}; |
0 commit comments