-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathjasmine-let.js
95 lines (72 loc) · 1.95 KB
/
jasmine-let.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
/*jslint indent: 2 */
/*global module */
function jasmineLet(jasmine, namespace) {
"use strict";
var env, scopes, propertyNames;
env = jasmine.getEnv();
scopes = {};
propertyNames = [];
function declare(name, expr, options) {
var suite, scope, block;
if (options === null || typeof options !== "object") {
options = {};
}
if (typeof expr === "function") {
block = expr;
} else {
block = function () { return expr; };
}
suite = env.currentSuite;
scope = scopes[suite.id] || (scopes[suite.id] = {});
scope[name] = block;
if (options.evaluateBefore) {
suite.beforeEach(function () {
/*jslint expr:true */
namespace[name];
});
}
}
function makeGetter(name, values, fn) {
return function () {
if (values.hasOwnProperty(name)) {
return values[name];
}
return values[name] = fn();
};
}
function makeSetter(name, values) {
return function (val) { values[name] = val; };
}
function defineProperties() {
var spec, suite, declarations, values;
spec = env.currentSpec;
values = {};
function defineProperty(name) {
if (propertyNames.indexOf(name) >= 0) { return; }
propertyNames.push(name);
Object.defineProperty(namespace, name, {
enumerable: true,
configurable: true,
get: makeGetter(name, values, declarations[name]),
set: makeSetter(name, values)
});
}
for (suite = spec.suite; suite; suite = suite.parentSuite) {
declarations = scopes[suite.id];
if (!declarations) continue;
Object.keys(declarations).forEach(defineProperty);
}
}
function deleteProperties() {
var name;
while ((name = propertyNames.pop(name))) {
delete namespace[name];
}
}
env.beforeEach(defineProperties);
env.afterEach(deleteProperties);
return declare;
}
if (typeof module !== 'undefined') {
module.exports = jasmineLet;
}