-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqunit_spec.js
147 lines (132 loc) · 3.88 KB
/
qunit_spec.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
/*
* Copyright (c) 2009 sousk.net
* Dual licensed under the MIT and GPL licenses.
*/
if (!window.log) {
window.log = window.getLogger ? window.getLogger() : (function() {
if (typeof(window) != "undefined" && window.console
&& window.console.log) {
// Safari and FireBug 0.4
// Percent replacement is a workaround for cute Safari crashing bug
// window.console.log(msg.replace(/%/g, '\uFF05'));
return window.console.log;
}
else {
return function() {};
}
})();
}
function __test() {};
function up(n) {
return n ? _expect_countup_cn+=n : _expect_countup_cn=0;
};
var _expect_countup_cn = 0;
function not_empty(expr, msg) {
ok(function() {
if (! expr) {
return false;
}
// has length
else if (typeof expr['length'] == 'number') {
return expr.length > 0;
}
else if (typeof expr == 'number') {
return expr > 0;
}
else {
log("got undefined type: "+ typeof expr);
return false;
}
}(), msg);
};
var spec = function(object, config) {
//----------------------------------------------------------
// privaet members
//----------------------------------------------------------
var set_source_object = function(object) {
speced._original = object;
return this;
};
var source_object = function() {
return speced._original || {};
};
var method_taker_of = function(sig) {
return speced._takers[sig];
};
var mocked_response_of = function(sig) {
return speced._mocked_responses[sig];
};
var create = function(o) {
var F = function() {};
F.prototype = o;
return new F();
};
var verbose = (config && config.verbose) ? log : function() {};
//----------------------------------------------------------
// appending method, keep minimum not to override original one
//----------------------------------------------------------
var speced = create(object);
$.extend(speced, {
_caller: {},
_mocked_responses: {},
_interrupted_methods: [],
and_return: function(response) {
this.receiver._mocked_responses[this.sig] = response;
return this;
},
and_call: function(fn) {
this.receiver._caller[this.sig] = fn;
return this;
},
with_args: function() {
return this;
},
match_signiture: function() {
return true;
},
should_receive: function(method_name, opts) {
var self = this;
var sig = method_name;
var orig = self[method_name];
var sig_args = {};
if (opts && opts['with']) {
}
self._interrupted_methods.push(method_name); // record just as for information
verbose('should_receive: block execution of '+method_name);
// replace method
self[method_name] = function() {
if (self.match_signiture(method_name, arguments)) {
return self.stub(method_name, arguments);
}
else {
return orig.apply(source_object(), arguments);
}
};
return {
receiver: self,
sig: sig,
orig: orig,
with_args: self.with_args,
and_return: self.and_return,
and_call: self.and_call
};
},
stub: function(method_name, args) {
var response = self = this;
var sig = method_name;
// count up for expects(n)
ok(true, method_name+" called");
verbose(method_name+" interrupted by 'should_receive'. fn/args is:", source_object()[method_name], args);
// execute if taken orver
var fn = self._caller[sig];
if (fn) {
response = fn.apply(source_object(), args);
}
var mocked_response = self._mocked_responses[sig];
return typeof(mocked_response) == 'undefined' ? response : mocked_response;
// return speced_method(sig, orig, self[method_name]);
}
});
set_source_object(object);
return speced;
};