|
1 | 1 | /** |
2 | 2 | * @fileoverview Jasmine JsTestDriver Adapter. |
3 | | - * @author [email protected] (Olmo Maldonado) |
4 | 3 | * @author [email protected] (Misko Hevery) |
5 | 4 | */ |
6 | | - |
7 | | -(function() { |
8 | | - |
9 | | - function bind(_this, _function){ |
10 | | - return function(){ |
11 | | - return _function.call(_this); |
12 | | - }; |
13 | | - } |
14 | | - |
15 | | - var currentFrame = frame(null, null); |
16 | | - |
17 | | - function frame(parent, name){ |
18 | | - var caseName = (parent && parent.caseName ? parent.caseName + " " : '') + (name ? name : ''); |
19 | | - var frame = { |
20 | | - name: name, |
21 | | - caseName: caseName, |
22 | | - parent: parent, |
23 | | - testCase: TestCase(caseName), |
24 | | - before: [], |
25 | | - after: [], |
26 | | - runBefore: function(){ |
27 | | - if (parent) parent.runBefore.apply(this); |
28 | | - for ( var i = 0; i < frame.before.length; i++) { |
29 | | - frame.before[i].apply(this); |
30 | | - } |
| 5 | +(function(window) { |
| 6 | + var rootDescribes = new Describes(window); |
| 7 | + var describePath = []; |
| 8 | + rootDescribes.collectMode(); |
| 9 | + |
| 10 | + var jasmineTest = TestCase('Jasmine Adapter Tests'); |
| 11 | + |
| 12 | + var jasminePlugin = { |
| 13 | + name:'jasmine', |
| 14 | + runTestConfiguration: function(testRunConfiguration, onTestDone, onTestRunConfigurationComplete){ |
| 15 | + if (testRunConfiguration.testCaseInfo_.template_ !== jasmineTest) return; |
| 16 | + |
| 17 | + var jasmineEnv = jasmine.currentEnv_ = new jasmine.Env(); |
| 18 | + rootDescribes.playback(); |
| 19 | + var specLog = jstestdriver.console.log_ = []; |
| 20 | + var start; |
| 21 | + jasmineEnv.specFilter = function(spec) { |
| 22 | + return rootDescribes.isExclusive(spec); |
| 23 | + }; |
| 24 | + jasmineEnv.reporter = { |
| 25 | + log: function(str){ |
| 26 | + specLog.push(str); |
| 27 | + }, |
| 28 | + |
| 29 | + reportRunnerStarting: function(runner) { }, |
| 30 | + |
| 31 | + reportSpecStarting: function(spec) { |
| 32 | + specLog = jstestdriver.console.log_ = []; |
| 33 | + start = new Date().getTime(); |
| 34 | + }, |
| 35 | + |
| 36 | + reportSpecResults: function(spec) { |
| 37 | + var suite = spec.suite; |
| 38 | + var results = spec.results(); |
| 39 | + if (results.skipped) return; |
| 40 | + var end = new Date().getTime(); |
| 41 | + var messages = []; |
| 42 | + var resultItems = results.getItems(); |
| 43 | + var state = 'passed'; |
| 44 | + for ( var i = 0; i < resultItems.length; i++) { |
| 45 | + if (!resultItems[i].passed()) { |
| 46 | + state = resultItems[i].message.match(/AssertionError:/) ? 'error' : 'failed'; |
| 47 | + messages.push(resultItems[i].toString()); |
| 48 | + messages.push(formatStack(resultItems[i].trace.stack)); |
| 49 | + } |
| 50 | + } |
| 51 | + onTestDone( |
| 52 | + new jstestdriver.TestResult( |
| 53 | + suite.getFullName(), |
| 54 | + spec.description, |
| 55 | + state, |
| 56 | + messages.join('\n'), |
| 57 | + specLog.join('\n'), |
| 58 | + end - start)); |
| 59 | + }, |
| 60 | + |
| 61 | + reportSuiteResults: function(suite) {}, |
| 62 | + |
| 63 | + reportRunnerResults: function(runner) { |
| 64 | + onTestRunConfigurationComplete(); |
| 65 | + } |
| 66 | + }; |
| 67 | + jasmineEnv.execute(); |
| 68 | + return true; |
31 | 69 | }, |
32 | | - runAfter: function(){ |
33 | | - for ( var i = 0; i < frame.after.length; i++) { |
34 | | - frame.after[i].apply(this); |
35 | | - } |
36 | | - if (parent) parent.runAfter.apply(this); |
| 70 | + onTestsFinish: function(){ |
| 71 | + jasmine.currentEnv_ = null; |
| 72 | + rootDescribes.collectMode(); |
37 | 73 | } |
38 | | - }; |
39 | | - return frame; |
40 | 74 | }; |
41 | | - |
42 | | - jasmine.Env.prototype.describe = (function(describe){ |
43 | | - return function(description){ |
44 | | - currentFrame = frame(currentFrame, description); |
45 | | - var val = describe.apply(this, arguments); |
46 | | - currentFrame = currentFrame.parent; |
47 | | - return val; |
48 | | - }; |
49 | | - |
50 | | - })(jasmine.Env.prototype.describe); |
51 | | - |
52 | | - var id = 0; |
53 | | - |
54 | | - jasmine.Env.prototype.it = (function(it){ |
55 | | - return function(desc, itFn){ |
56 | | - var self = this; |
57 | | - var spec = it.apply(this, arguments); |
58 | | - var currentSpec = this.currentSpec; |
59 | | - if (!currentSpec.$id) { |
60 | | - currentSpec.$id = id++; |
| 75 | + jstestdriver.pluginRegistrar.register(jasminePlugin); |
| 76 | + |
| 77 | + function formatStack(stack) { |
| 78 | + var lines = (stack||'').split(/\r?\n/); |
| 79 | + var frames = []; |
| 80 | + for (i = 0; i < lines.length; i++) { |
| 81 | + if (!lines[i].match(/\/jasmine[\.-]/)) { |
| 82 | + frames.push(lines[i].replace(/https?:\/\/\w+(:\d+)?\/test\//, '').replace(/^\s*/, ' ')); |
61 | 83 | } |
62 | | - var frame = this.jstdFrame = currentFrame; |
63 | | - var name = 'test that it ' + desc; |
64 | | - if (this.jstdFrame.testCase.prototype[name]) |
65 | | - throw "Spec with name '" + desc + "' already exists."; |
66 | | - this.jstdFrame.testCase.prototype[name] = function(){ |
67 | | - jasmine.getEnv().currentSpec = currentSpec; |
68 | | - frame.runBefore.apply(currentSpec); |
69 | | - try { |
70 | | - itFn.apply(currentSpec); |
71 | | - } finally { |
72 | | - frame.runAfter.apply(currentSpec); |
| 84 | + } |
| 85 | + return frames.join('\n'); |
| 86 | + } |
| 87 | + |
| 88 | + function noop(){} |
| 89 | + function Describes(window){ |
| 90 | + var describes = {}; |
| 91 | + var beforeEachs = {}; |
| 92 | + var afterEachs = {}; |
| 93 | + var exclusive; |
| 94 | + var collectMode = true; |
| 95 | + intercept('describe', describes); |
| 96 | + intercept('xdescribe', describes); |
| 97 | + intercept('beforeEach', beforeEachs); |
| 98 | + intercept('afterEach', afterEachs); |
| 99 | + |
| 100 | + function intercept(functionName, collection){ |
| 101 | + window[functionName] = function(desc, fn){ |
| 102 | + if (collectMode) { |
| 103 | + collection[desc] = function(){ |
| 104 | + jasmine.getEnv()[functionName](desc, fn); |
| 105 | + }; |
| 106 | + } else { |
| 107 | + jasmine.getEnv()[functionName](desc, fn); |
73 | 108 | } |
74 | 109 | }; |
75 | | - return spec; |
| 110 | + } |
| 111 | + window.ddescribe = function(name, fn){ |
| 112 | + exclusive = true; |
| 113 | + console.log('ddescribe', name); |
| 114 | + window.describe(name, function(){ |
| 115 | + var oldIt = window.it; |
| 116 | + window.it = window.iit; |
| 117 | + try { |
| 118 | + fn.call(this); |
| 119 | + } finally { |
| 120 | + window.it = oldIt; |
| 121 | + }; |
| 122 | + }); |
76 | 123 | }; |
77 | | - |
78 | | - })(jasmine.Env.prototype.it); |
79 | | - |
80 | | - |
81 | | - jasmine.Env.prototype.beforeEach = (function(beforeEach){ |
82 | | - return function(beforeEachFunction) { |
83 | | - beforeEach.apply(this, arguments); |
84 | | - currentFrame.before.push(beforeEachFunction); |
| 124 | + window.iit = function(name, fn){ |
| 125 | + exclusive = fn.exclusive = true; |
| 126 | + console.log(fn); |
| 127 | + jasmine.getEnv().it(name, fn); |
85 | 128 | }; |
86 | | - |
87 | | - })(jasmine.Env.prototype.beforeEach); |
88 | | - |
89 | | - |
90 | | - jasmine.Env.prototype.afterEach = (function(afterEach){ |
91 | | - return function(afterEachFunction) { |
92 | | - afterEach.apply(this, arguments); |
93 | | - currentFrame.after.push(afterEachFunction); |
| 129 | + |
| 130 | + |
| 131 | + this.collectMode = function() { |
| 132 | + collectMode = true; |
| 133 | + exclusive = false; |
94 | 134 | }; |
95 | | - |
96 | | - })(jasmine.Env.prototype.afterEach); |
97 | | - |
98 | | - |
99 | | - jasmine.NestedResults.prototype.addResult = (function(addResult){ |
100 | | - return function(result) { |
101 | | - addResult.call(this, result); |
102 | | - if (result.type != 'MessageResult' && !result.passed()) fail(result.message); |
| 135 | + this.playback = function(){ |
| 136 | + collectMode = false; |
| 137 | + playback(beforeEachs); |
| 138 | + playback(afterEachs); |
| 139 | + playback(describes); |
| 140 | + |
| 141 | + function playback(set) { |
| 142 | + for ( var name in set) { |
| 143 | + set[name](); |
| 144 | + } |
| 145 | + } |
103 | 146 | }; |
| 147 | + |
| 148 | + this.isExclusive = function(spec) { |
| 149 | + if (exclusive) { |
| 150 | + var blocks = spec.queue.blocks; |
| 151 | + for ( var i = 0; i < blocks.length; i++) { |
| 152 | + if (blocks[i].func.exclusive) { |
| 153 | + return true; |
| 154 | + } |
| 155 | + } |
| 156 | + return false; |
| 157 | + } |
| 158 | + return true; |
| 159 | + }; |
| 160 | + } |
| 161 | + |
| 162 | +})(window); |
| 163 | + |
| 164 | +// Patch Jasmine for proper stack traces |
| 165 | +jasmine.Spec.prototype.fail = function (e) { |
| 166 | + var expectationResult = new jasmine.ExpectationResult({ |
| 167 | + passed: false, |
| 168 | + message: e ? jasmine.util.formatException(e) : 'Exception' |
| 169 | + }); |
| 170 | + // PATCH |
| 171 | + if (e) { |
| 172 | + expectationResult.trace = e; |
| 173 | + } |
| 174 | + this.results_.addResult(expectationResult); |
| 175 | +}; |
104 | 176 |
|
105 | | - })(jasmine.NestedResults.prototype.addResult); |
106 | | - |
107 | | - // Reset environment with overriden methods. |
108 | | - jasmine.currentEnv_ = null; |
109 | | - jasmine.getEnv(); |
110 | | - |
111 | | -})(); |
|
0 commit comments