forked from jvennix-r7/jasmine-set
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjasmine-set.js
130 lines (117 loc) · 4.07 KB
/
jasmine-set.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/*
jasmine-set - 0.1.11
A plugin for the Jasmine behavior-driven Javascript testing framework that
adds a `set` global function. It is inspired by rspec's very nice `let` syntax.
Works in both node.js and a browser environment.
Requires jasmine.js, and underscore.js.
@author Joe Vennix
@copyright Rapid7 2015
@see https://github.com/joevennix/jasmine-set
Released under the MIT License.
*/
(function() {
var _, context, install, jasmine;
install = function(_, jasmine) {
var autoincrement, context, globalPatches, namespaceStack, suites;
suites = {};
namespaceStack = {};
context = this;
autoincrement = 0;
globalPatches = {
set: function(name, opts, fn) {
var ret;
beforeEach(function() {
var def, definitions, ref, ref1, suite;
suite = jasmine != null ? (ref = jasmine.getEnv()) != null ? (ref1 = ref.currentSpec) != null ? ref1.suite : void 0 : void 0 : void 0;
definitions = [];
while (suite != null) {
def = _.find(suites[suite.id], function(obj) {
return obj.name === name;
});
if (def != null) {
definitions.unshift(def);
}
suite = suite.parentSuite;
}
namespaceStack[name] || (namespaceStack[name] = []);
return _.each(definitions, function(def) {
if (!_.contains(namespaceStack[name], def)) {
namespaceStack[name].push(def);
}
return def.fn();
});
});
afterEach(function() {
var ref, ref1;
delete context[name];
if ((ref = namespaceStack[name]) != null) {
ref.pop();
}
return (ref1 = _.last(namespaceStack[name])) != null ? typeof ref1.fn === "function" ? ref1.fn() : void 0 : void 0;
});
if (_.isFunction(opts)) {
fn = opts;
opts = null;
}
opts || (opts = {});
if (opts.now == null) {
opts.now = false;
}
ret = function(fn) {
var doit, id, oncePerSuiteWrapper, ref, ref1, setter;
setter = function(x) {
oncePerSuiteWrapper();
delete context[name];
return context[name] = x;
};
oncePerSuiteWrapper = null;
doit = function() {
var cachedId, cachedResult;
if (opts.now) {
return context[name] = fn();
} else {
cachedId = null;
cachedResult = null;
oncePerSuiteWrapper = function() {
var id, ref, ref1, ref2;
id = (ref = jasmine != null ? (ref1 = jasmine.getEnv()) != null ? (ref2 = ref1.currentSpec) != null ? ref2.id : void 0 : void 0 : void 0) != null ? ref : autoincrement++;
if (id !== cachedId) {
cachedResult = fn();
cachedId = id;
}
return cachedResult;
};
return Object.defineProperty(context, name, {
get: oncePerSuiteWrapper,
set: setter,
configurable: true
});
}
};
id = jasmine != null ? (ref = jasmine.getEnv()) != null ? (ref1 = ref.currentSuite) != null ? ref1.id : void 0 : void 0 : void 0;
suites[id] || (suites[id] = []);
return suites[id].push({
fn: doit,
name: name
});
};
if (fn) {
return ret(fn);
} else {
return ret;
}
}
};
return _.extend(this, globalPatches);
};
context = (typeof window === "object" && window) || (typeof global === "object" && global) || this;
jasmine = context.jasmine || require("jasmine");
_ = context._ || require("underscore");
if (jasmine == null) {
console.error("jasmine-set: Jasmine must be required first. Aborting.");
} else if (_ == null) {
console.error("jasmine-set: underscore.js must be required first. Aborting.");
} else {
install.call(context, _, jasmine);
}
}).call(this);