/
smoke.mock.js
132 lines (129 loc) · 3.79 KB
/
smoke.mock.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
Smoke = {
mocks: [],
passCount: 0,
failCount: 0,
print: function(v) {
// use the jquery print plugin if ti is available or fall back to toString();
return (jQuery && jQuery.print) ? $.print(v) : v.toString();
},
checkExpectations: function(){
for(var i=0; i<Smoke.mocks.length; i++) Smoke.mocks[i].checkExpectations();
},
reset: function(){
Smoke.mocks = [];
passCount = 0;
failCount = 0;
},
passed: function(mock){
Smoke.passCount += 0;
},
failed: function(mock, message){
Smoke.failCount += 1;
throw(message);
}
};
Smoke.Mock = function(originalObj) {
var obj = originalObj || {} ;
obj._expectations = {};
obj.stub = function(attr){
return new Smoke.Stub(this, attr);
},
obj.should_receive = function(attr){
var expectation = new Smoke.Mock.Expectation(this, attr);
if(this._expectations[attr]==undefined) this._expectations[attr] = [];
this._expectations[attr].push(expectation);
var previousFunction = this[attr];
var mock = this;
this[attr] = function() {
var result = expectation.run(arguments);
if(result!=undefined) return result;
return previousFunction!=undefined ? previousFunction.apply(mock,arguments) : undefined;
};
return expectation;
},
obj.checkExpectations = function(){
for(var e in this._expectations) {
var expectations = this._expectations[e]
for(var i in expectations) expectations[i].check();
};
},
Smoke.mocks.push(obj);
return obj;
};
Smoke.Mock.Expectation = function(mock, attr) {
this._mock = mock;
this._attr = attr;
this.callCount = 0;
this.returnValue = undefined;
this.callerArgs = [];
};
Smoke.Mock.Expectation.prototype = {
exactly: function(count,type){
// type isn't used for now, it's just syntax ;)
this.minCount = this.maxCount = undefined;
this.exactCount = this.parseCount(count);
return this;
},
at_most: function(count,type){
this.maxCount = this.parseCount(count);
return this;
},
at_least: function(count,type){
this.minCount = this.parseCount(count);
return this;
},
with_arguments: function(){
this.callerArgs = arguments;
return this
},
run: function(args){
if(this.compareArrays(args, this.callerArgs)) {
this.callCount+=1;
return this.returnValue;
};
},
and_return: function(v){
this.returnValue = v
},
check: function(){
if(this.exactCount!=undefined) this.checkExactCount();
if(this.minCount!=undefined) this.checkMinCount();
if(this.maxCount!=undefined) this.checkMaxCount();
},
checkExactCount: function(){
if(this.exactCount==this.callCount) Smoke.passed(this)//console.log('Mock passed!')
else Smoke.failed(this, 'expected '+this.methodSignature()+' to be called exactly '+this.exactCount+" times but it got called "+this.callCount+' times');
},
checkMinCount: function(){
if(this.minCount<=this.callCount) Smoke.passed(this);
else Smoke.failed(this, 'expected '+this.methodSignature()+' to be called at least '+this.minCount+" times but it only got called "+this.callCount+' times');
},
checkMaxCount: function(){
if(this.maxCount>=this.callCount) Smoke.passed(this);//console.log('Mock passed!')
else Smoke.failed(this, 'expected '+this.methodSignature()+' to be called at most '+this.maxCount+" times but it actually got called "+this.callCount+' times');
},
methodSignature: function(){
var a = '';
var args = this.callerArgs;
for(var i=0; i<args.length; i++) a += Smoke.print(args[i])+', ';
a =a.slice(0,-2);
return this._attr+'('+a+')'
},
parseCount: function(c){
switch(c){
case 'once' : c=1; break;
case 'twice' : c=2; break;
}
return c;
},
compareArrays: function(a,b) {
if (a.length != b.length) return false;
for (var i = 0; i < b.length; i++) {
if (a[i].compare) {
if (!a[i].compare(b[i])) return false;
}
if (a[i] !== b[i]) return false;
}
return true;
}
};