/
chai-changes.js
178 lines (175 loc) · 7.44 KB
/
chai-changes.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
// Generated by CoffeeScript 1.3.3
(function() {
(function(chaiChanges) {
if (typeof require === "function" && typeof exports === "object" && typeof module === "object") {
return module.exports = chaiChanges;
} else if (typeof define === "function" && define.amd) {
return define(function() {
return chaiChanges;
});
} else {
return chai.use(chaiChanges);
}
})(function(chai, utils) {
var changeBy, changeByAssert, changeFrom, changeFromAssert, changeFromBeginAssert, changeTo, changeToAssert, changeToBeginAssert, flag, formatFunction, inspect, noChangeAssert;
inspect = utils.inspect;
flag = utils.flag;
/*
#
# Changes Matchers
#
*/
chai.Assertion.addMethod('when', function(val) {
var action, definedActions, _i, _j, _len, _len1, _results;
definedActions = flag(this, 'whenActions') || [];
for (_i = 0, _len = definedActions.length; _i < _len; _i++) {
action = definedActions[_i];
if (typeof action.before === "function") {
action.before(this);
}
}
val();
_results = [];
for (_j = 0, _len1 = definedActions.length; _j < _len1; _j++) {
action = definedActions[_j];
_results.push(typeof action.after === "function" ? action.after(this) : void 0);
}
return _results;
});
noChangeAssert = function(context) {
var endValue, negate, object, relevant, result, startValue;
relevant = flag(context, 'no-change');
if (!relevant) {
return;
}
negate = flag(context, 'negate');
flag(context, 'negate', this.negate);
object = flag(context, 'object');
startValue = flag(context, 'changeStart');
endValue = object();
result = !utils.eql(endValue, startValue);
context.assert(result, "expected `" + (formatFunction(object)) + "` to change, but it stayed " + (utils.inspect(startValue)), "expected `" + (formatFunction(object)) + "` not to change, but it changed from " + (utils.inspect(startValue)) + " to " + (utils.inspect(endValue)));
return flag(context, 'negate', negate);
};
changeByAssert = function(context) {
var actualDelta, endValue, negate, object, startValue;
negate = flag(context, 'negate');
flag(context, 'negate', this.negate);
object = flag(context, 'object');
startValue = flag(context, 'changeStart');
endValue = object();
actualDelta = endValue - startValue;
context.assert(this.expectedDelta === actualDelta, "expected `" + (formatFunction(object)) + "` to change by " + this.expectedDelta + ", but it changed by " + actualDelta, "expected `" + (formatFunction(object)) + "` not to change by " + this.expectedDelta + ", but it did");
return flag(context, 'negate', negate);
};
changeToBeginAssert = function(context) {
var negate, object, result, startValue;
negate = flag(context, 'negate');
flag(context, 'negate', this.negate);
object = flag(context, 'object');
startValue = object();
result = !utils.eql(startValue, this.expectedEndValue);
if (negate) {
result = !result;
}
context.assert(result, "expected `" + (formatFunction(object)) + "` to change to " + (utils.inspect(this.expectedEndValue)) + ", but it was already " + (utils.inspect(startValue)), "not supported");
return flag(context, 'negate', negate);
};
changeToAssert = function(context) {
var endValue, negate, object, result;
negate = flag(context, 'negate');
flag(context, 'negate', this.negate);
object = flag(context, 'object');
endValue = object();
result = utils.eql(endValue, this.expectedEndValue);
context.assert(result, "expected `" + (formatFunction(object)) + "` to change to " + (utils.inspect(this.expectedEndValue)) + ", but it changed to " + (utils.inspect(endValue)), "expected `" + (formatFunction(object)) + "` not to change to " + (utils.inspect(this.expectedEndValue)) + ", but it did");
return flag(context, 'negate', negate);
};
changeFromBeginAssert = function(context) {
var negate, object, result, startValue;
negate = flag(context, 'negate');
flag(context, 'negate', this.negate);
object = flag(context, 'object');
startValue = object();
result = utils.eql(startValue, this.expectedStartValue);
context.assert(result, "expected the change of `" + (formatFunction(object)) + "` to start from " + (utils.inspect(this.expectedStartValue)) + ", but it started from " + (utils.inspect(startValue)), "expected the change of `" + (formatFunction(object)) + "` not to start from " + (utils.inspect(this.expectedStartValue)) + ", but it did");
return flag(context, 'negate', negate);
};
changeFromAssert = function(context) {
var endValue, negate, object, result, startValue;
negate = flag(context, 'negate');
flag(context, 'negate', this.negate);
object = flag(context, 'object');
startValue = flag(context, 'changeStart');
endValue = object();
result = !utils.eql(startValue, endValue);
if (negate) {
result = !result;
}
context.assert(result, "expected `" + (formatFunction(object)) + "` to change from " + (utils.inspect(this.expectedStartValue)) + ", but it did not change", "not supported");
return flag(context, 'negate', negate);
};
chai.Assertion.addProperty('change', function() {
var definedActions;
flag(this, 'no-change', true);
definedActions = flag(this, 'whenActions') || [];
definedActions.push({
negate: flag(this, 'negate'),
before: function(context) {
var startValue;
startValue = flag(context, 'object')();
return flag(context, 'changeStart', startValue);
},
after: noChangeAssert
});
return flag(this, 'whenActions', definedActions);
});
formatFunction = function(func) {
return func.toString().replace(/^\s*function \(\) {\s*/, '').replace(/\s+}$/, '').replace(/\s*return\s*/, '');
};
changeBy = function(delta) {
var definedActions;
flag(this, 'no-change', false);
definedActions = flag(this, 'whenActions') || [];
definedActions.push({
negate: flag(this, 'negate'),
expectedDelta: delta,
after: changeByAssert
});
return flag(this, 'whenActions', definedActions);
};
chai.Assertion.addChainableMethod('by', changeBy, function() {
return this;
});
changeTo = function(endValue) {
var definedActions;
flag(this, 'no-change', false);
definedActions = flag(this, 'whenActions') || [];
definedActions.push({
negate: flag(this, 'negate'),
expectedEndValue: endValue,
before: changeToBeginAssert,
after: changeToAssert
});
return flag(this, 'whenActions', definedActions);
};
chai.Assertion.addChainableMethod('to', changeTo, function() {
return this;
});
changeFrom = function(startValue) {
var definedActions;
flag(this, 'no-change', false);
definedActions = flag(this, 'whenActions') || [];
definedActions.push({
negate: flag(this, 'negate'),
expectedStartValue: startValue,
before: changeFromBeginAssert,
after: changeFromAssert
});
return flag(this, 'whenActions', definedActions);
};
return chai.Assertion.addChainableMethod('from', changeFrom, function() {
return this;
});
});
}).call(this);