4 changed files with 1909 additions and 1011 deletions
Unified View
Diff Options
-
69package.json
-
713test/helpers/jasmine-jquery.js
-
315test/helpers/jasmine-sinon.js
-
1823test/helpers/sinon.js
@ -1,43 +1,300 @@ |
|||||
/* global jasmine */ |
|
||||
|
(function() { |
||||
|
var createCustomMatcher, createMatcher, jasmineMessageGenerator, jasmineName, sinonMatchers, sinonName, sinonToJasmineMap, spies, |
||||
|
__slice = [].slice; |
||||
|
|
||||
'use strict'; |
|
||||
|
sinonMatchers = {}; |
||||
|
|
||||
(function(jasmine, beforeEach) { |
|
||||
|
sinonToJasmineMap = { |
||||
|
'called': 'toHaveBeenCalled', |
||||
|
'calledOnce': 'toHaveBeenCalledOnce', |
||||
|
'calledTwice': 'toHaveBeenCalledTwice', |
||||
|
'calledThrice': 'toHaveBeenCalledThrice', |
||||
|
'calledBefore': 'toHaveBeenCalledBefore', |
||||
|
'calledAfter': 'toHaveBeenCalledAfter', |
||||
|
'calledOn': 'toHaveBeenCalledOn', |
||||
|
'alwaysCalledOn': 'toHaveBeenAlwaysCalledOn', |
||||
|
'calledWith': 'toHaveBeenCalledWith', |
||||
|
'alwaysCalledWith': 'toHaveBeenAlwaysCalledWith', |
||||
|
'calledWithExactly': 'toHaveBeenCalledWithExactly', |
||||
|
'alwaysCalledWithExactly': 'toHaveBeenAlwaysCalledWithExactly', |
||||
|
'calledWithMatch': 'toHaveBeenCalledWithMatch', |
||||
|
'alwaysCalledWithMatch': 'toHaveBeenAlwaysCalledWithMatch', |
||||
|
'returned': 'toHaveReturned', |
||||
|
'alwaysReturned': 'toHaveAlwaysReturned', |
||||
|
'threw': 'toHaveThrown', |
||||
|
'alwaysThrew': 'toHaveAlwaysThrown' |
||||
|
}; |
||||
|
|
||||
var spyMatchers = 'called calledOnce calledTwice calledThrice calledBefore calledAfter calledOn alwaysCalledOn calledWith alwaysCalledWith calledWithExactly alwaysCalledWithExactly calledWithMatch alwaysCalledWithMatch'.split(' '), |
|
||||
i = spyMatchers.length, |
|
||||
spyMatcherHash = {}, |
|
||||
unusualMatchers = { |
|
||||
"returned": "toHaveReturned", |
|
||||
"alwaysReturned": "toHaveAlwaysReturned", |
|
||||
"threw": "toHaveThrown", |
|
||||
"alwaysThrew": "toHaveAlwaysThrown" |
|
||||
|
jasmineMessageGenerator = { |
||||
|
'toHaveBeenCalled': function(passed, spy, other_args) { |
||||
|
var message; |
||||
|
message = "Expected spy '" + spy + "' "; |
||||
|
if (passed) { |
||||
|
message += "not "; |
||||
|
} |
||||
|
message += "toHaveBeenCalled"; |
||||
|
return message.trim(); |
||||
}, |
}, |
||||
|
'toHaveBeenCalledOnce': function(passed, spy, other_args) { |
||||
|
var message; |
||||
|
message = "Expected spy '" + spy + "' "; |
||||
|
if (passed) { |
||||
|
message += "not "; |
||||
|
} |
||||
|
message += "toHaveBeenCalledOnce"; |
||||
|
return message.trim(); |
||||
|
}, |
||||
|
'toHaveBeenCalledTwice': function(passed, spy, other_args) { |
||||
|
var message; |
||||
|
message = "Expected spy '" + spy + "' "; |
||||
|
if (passed) { |
||||
|
message += "not "; |
||||
|
} |
||||
|
message += "toHaveBeenCalledTwice"; |
||||
|
return message.trim(); |
||||
|
}, |
||||
|
'toHaveBeenCalledThrice': function(passed, spy, other_args) { |
||||
|
var message; |
||||
|
message = "Expected spy '" + spy + "' "; |
||||
|
if (passed) { |
||||
|
message += "not "; |
||||
|
} |
||||
|
message += "toHaveBeenCalledThrice"; |
||||
|
return message.trim(); |
||||
|
}, |
||||
|
'toHaveBeenCalledBefore': function(passed, spy, other_args) { |
||||
|
var message; |
||||
|
message = "Expected spy '" + spy + "' "; |
||||
|
if (passed) { |
||||
|
message += "not "; |
||||
|
} |
||||
|
message += "toHaveBeenCalledBefore "; |
||||
|
if ((other_args != null ? other_args.length : void 0) > 0) { |
||||
|
message += jasmine.pp(other_args) + (" but was CalledBefore " + (jasmine.pp(spy.lastCall.args))); |
||||
|
} |
||||
|
return message.trim(); |
||||
|
}, |
||||
|
'toHaveBeenCalledAfter': function(passed, spy, other_args) { |
||||
|
var message; |
||||
|
message = "Expected spy '" + spy + "' "; |
||||
|
if (passed) { |
||||
|
message += "not "; |
||||
|
} |
||||
|
message += "toHaveBeenCalledAfter "; |
||||
|
if ((other_args != null ? other_args.length : void 0) > 0) { |
||||
|
message += jasmine.pp(other_args) + (" but was CalledAfter " + (jasmine.pp(spy.lastCall.args))); |
||||
|
} |
||||
|
return message.trim(); |
||||
|
}, |
||||
|
'toHaveBeenCalledOn': function(passed, spy, other_args) { |
||||
|
var message; |
||||
|
message = "Expected spy '" + spy + "' "; |
||||
|
if (passed) { |
||||
|
message += "not "; |
||||
|
} |
||||
|
message += "toHaveBeenCalledOn "; |
||||
|
if ((other_args != null ? other_args.length : void 0) > 0) { |
||||
|
message += jasmine.pp(other_args) + (" but was CalledOn " + (jasmine.pp(spy.lastCall.args))); |
||||
|
} |
||||
|
return message.trim(); |
||||
|
}, |
||||
|
'toHaveBeenAlwaysCalledOn': function(passed, spy, other_args) { |
||||
|
var message; |
||||
|
message = "Expected spy '" + spy + "' "; |
||||
|
if (passed) { |
||||
|
message += "not "; |
||||
|
} |
||||
|
message += "toHaveBeenAlwaysCalledOn "; |
||||
|
if ((other_args != null ? other_args.length : void 0) > 0) { |
||||
|
message += jasmine.pp(other_args) + (" but was AlwaysCalledOn " + (jasmine.pp(spy.lastCall.args))); |
||||
|
} |
||||
|
return message.trim(); |
||||
|
}, |
||||
|
'toHaveBeenCalledWith': function(passed, spy, other_args) { |
||||
|
var message; |
||||
|
message = "Expected spy '" + spy + "' "; |
||||
|
if (passed) { |
||||
|
message += "not "; |
||||
|
} |
||||
|
message += "toHaveBeenCalledWith "; |
||||
|
if ((other_args != null ? other_args.length : void 0) > 0) { |
||||
|
message += jasmine.pp(other_args) + (" but was CalledWith " + (jasmine.pp(spy.lastCall.args))); |
||||
|
} |
||||
|
return message.trim(); |
||||
|
}, |
||||
|
'toHaveBeenAlwaysCalledWith': function(passed, spy, other_args) { |
||||
|
var message; |
||||
|
message = "Expected spy '" + spy + "' "; |
||||
|
if (passed) { |
||||
|
message += "not "; |
||||
|
} |
||||
|
message += "toHaveBeenAlwaysCalledWith "; |
||||
|
if ((other_args != null ? other_args.length : void 0) > 0) { |
||||
|
message += jasmine.pp(other_args) + (" but was AlwaysCalledWith " + (jasmine.pp(spy.lastCall.args))); |
||||
|
} |
||||
|
return message.trim(); |
||||
|
}, |
||||
|
'toHaveBeenCalledWithExactly': function(passed, spy, other_args) { |
||||
|
var message; |
||||
|
message = "Expected spy '" + spy + "' "; |
||||
|
if (passed) { |
||||
|
message += "not "; |
||||
|
} |
||||
|
message += "toHaveBeenCalledWithExactly "; |
||||
|
if ((other_args != null ? other_args.length : void 0) > 0) { |
||||
|
message += jasmine.pp(other_args) + (" but was CalledWithExactly " + (jasmine.pp(spy.lastCall.args))); |
||||
|
} |
||||
|
return message.trim(); |
||||
|
}, |
||||
|
'toHaveBeenAlwaysCalledWithExactly': function(passed, spy, other_args) { |
||||
|
var message; |
||||
|
message = "Expected spy '" + spy + "' "; |
||||
|
if (passed) { |
||||
|
message += "not "; |
||||
|
} |
||||
|
message += "toHaveBeenAlwaysCalledWithExactly "; |
||||
|
if ((other_args != null ? other_args.length : void 0) > 0) { |
||||
|
message += jasmine.pp(other_args) + (" but was AlwaysCalledWithExactly " + (jasmine.pp(spy.lastCall.args))); |
||||
|
} |
||||
|
return message.trim(); |
||||
|
}, |
||||
|
'toHaveBeenCalledWithMatch': function(passed, spy, other_args) { |
||||
|
var message; |
||||
|
message = "Expected spy '" + spy + "' "; |
||||
|
if (passed) { |
||||
|
message += "not "; |
||||
|
} |
||||
|
message += "toHaveBeenCalledWithMatch "; |
||||
|
if ((other_args != null ? other_args.length : void 0) > 0) { |
||||
|
message += jasmine.pp(other_args) + (" but was CalledWithMatch " + (jasmine.pp(spy.lastCall.args))); |
||||
|
} |
||||
|
return message.trim(); |
||||
|
}, |
||||
|
'toHaveBeenAlwaysCalledWithMatch': function(passed, spy, other_args) { |
||||
|
var message; |
||||
|
message = "Expected spy '" + spy + "' "; |
||||
|
if (passed) { |
||||
|
message += "not "; |
||||
|
} |
||||
|
message += "toHaveBeenAlwaysCalledWithMatch "; |
||||
|
if ((other_args != null ? other_args.length : void 0) > 0) { |
||||
|
message += jasmine.pp(other_args) + (" but was AlwaysCalledWithMatch " + (jasmine.pp(spy.lastCall.args))); |
||||
|
} |
||||
|
return message.trim(); |
||||
|
}, |
||||
|
'toHaveReturned': function(passed, spy, other_args) { |
||||
|
var message; |
||||
|
message = "Expected spy '" + spy + "' "; |
||||
|
if (passed) { |
||||
|
message += "not "; |
||||
|
} |
||||
|
message += "toHaveReturned "; |
||||
|
if ((other_args != null ? other_args.length : void 0) > 0) { |
||||
|
message += jasmine.pp(other_args) + (" but returned " + (jasmine.pp(spy.lastCall.args))); |
||||
|
} |
||||
|
return message.trim(); |
||||
|
}, |
||||
|
'toHaveAlwaysReturned': function(passed, spy, other_args) { |
||||
|
var message; |
||||
|
message = "Expected spy '" + spy + "' "; |
||||
|
if (passed) { |
||||
|
message += "not "; |
||||
|
} |
||||
|
message += "toHaveAlwaysReturned "; |
||||
|
if ((other_args != null ? other_args.length : void 0) > 0) { |
||||
|
message += jasmine.pp(other_args) + (" but returned " + (jasmine.pp(spy.lastCall.args))); |
||||
|
} |
||||
|
return message.trim(); |
||||
|
}, |
||||
|
'toHaveThrown': function(passed, spy, other_args) { |
||||
|
var message; |
||||
|
message = "Expected spy '" + spy + "' "; |
||||
|
if (passed) { |
||||
|
message += "not "; |
||||
|
} |
||||
|
message += "toHaveThrown "; |
||||
|
if ((other_args != null ? other_args.length : void 0) > 0) { |
||||
|
message += jasmine.pp(other_args) + (" but threw " + (jasmine.pp(spy.lastCall.args))); |
||||
|
} |
||||
|
return message.trim(); |
||||
|
}, |
||||
|
'toHaveAlwaysThrown': function(passed, spy, other_args) { |
||||
|
var message; |
||||
|
message = "Expected spy '" + spy + "' "; |
||||
|
if (passed) { |
||||
|
message += "not "; |
||||
|
} |
||||
|
message += "toHaveAlwaysThrown "; |
||||
|
if ((other_args != null ? other_args.length : void 0) > 0) { |
||||
|
message += jasmine.pp(other_args) + (" but threw " + (jasmine.pp(spy.lastCall.args))); |
||||
|
} |
||||
|
return message.trim(); |
||||
|
} |
||||
|
}; |
||||
|
|
||||
getMatcherFunction = function(sinonName, matcherName) { |
|
||||
var original = jasmine.Matchers.prototype[matcherName]; |
|
||||
return function () { |
|
||||
if (jasmine.isSpy(this.actual) && original) { |
|
||||
return original.apply(this, arguments); |
|
||||
|
createCustomMatcher = function(arg, util, customEqualityTesters) { |
||||
|
return sinon.match(function(val) { |
||||
|
return util.equals(val, arg, customEqualityTesters); |
||||
|
}); |
||||
|
}; |
||||
|
|
||||
|
createMatcher = function(sinonName, jasmineName) { |
||||
|
var original; |
||||
|
original = jasmineRequire[jasmineName]; |
||||
|
return function(util, customEqualityTesters) { |
||||
|
return { |
||||
|
compare: function() { |
||||
|
var arg, args, compare, i, message, pass, sinonProperty, spy, _i, _len; |
||||
|
args = 1 <= arguments.length ? __slice.call(arguments, 0) : []; |
||||
|
spy = args[0]; |
||||
|
if (original && jasmine.isSpy(spy)) { |
||||
|
compare = original(jasmine)(util, customEqualityTesters).compare; |
||||
|
return compare.apply(null, args); |
||||
|
} |
||||
|
sinonProperty = spy[sinonName]; |
||||
|
for (i = _i = 0, _len = args.length; _i < _len; i = ++_i) { |
||||
|
arg = args[i]; |
||||
|
if (arg != null) { |
||||
|
if ((typeof arg.jasmineMatches === 'function') || (arg instanceof jasmine.ObjectContaining)) { |
||||
|
args[i] = createCustomMatcher(arg, util, customEqualityTesters); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
if (typeof sinonProperty === 'function') { |
||||
|
pass = sinonProperty.apply(spy, args.slice(1)); |
||||
|
} else { |
||||
|
pass = sinonProperty; |
||||
|
} |
||||
|
message = jasmineMessageGenerator[jasmineName](pass, spy, args.slice(1)); |
||||
|
return { |
||||
|
pass: pass, |
||||
|
message: message |
||||
|
}; |
||||
} |
} |
||||
var sinonProperty = this.actual[sinonName]; |
|
||||
return (typeof sinonProperty === 'function') ? sinonProperty.apply(this.actual, arguments) : sinonProperty; |
|
||||
}; |
}; |
||||
}; |
}; |
||||
|
}; |
||||
|
|
||||
while(i--) { |
|
||||
var sinonName = spyMatchers[i], |
|
||||
matcherName = "toHaveBeen" + sinonName.charAt(0).toUpperCase() + sinonName.slice(1); |
|
||||
|
|
||||
spyMatcherHash[matcherName] = getMatcherFunction(sinonName, matcherName); |
|
||||
|
for (sinonName in sinonToJasmineMap) { |
||||
|
jasmineName = sinonToJasmineMap[sinonName]; |
||||
|
sinonMatchers[jasmineName] = createMatcher(sinonName, jasmineName); |
||||
} |
} |
||||
|
|
||||
for (var j in unusualMatchers) { |
|
||||
spyMatcherHash[unusualMatchers[j]] = getMatcherFunction(j, unusualMatchers[j]); |
|
||||
} |
|
||||
|
jasmine.Expectation.addCoreMatchers(sinonMatchers); |
||||
|
|
||||
|
spies = []; |
||||
|
|
||||
|
this.sinonSpyOn = function(obj, method) { |
||||
|
return spies.push(sinon.spy(obj, method)); |
||||
|
}; |
||||
|
|
||||
beforeEach(function() { |
|
||||
this.addMatchers(spyMatcherHash); |
|
||||
|
afterEach(function() { |
||||
|
var spy, _i, _len; |
||||
|
for (_i = 0, _len = spies.length; _i < _len; _i++) { |
||||
|
spy = spies[_i]; |
||||
|
spy.restore(); |
||||
|
} |
||||
|
return spies = []; |
||||
}); |
}); |
||||
|
|
||||
})(jasmine, beforeEach); |
|
||||
|
}).call(this); |
1823
test/helpers/sinon.js
File diff suppressed because it is too large
View File
File diff suppressed because it is too large
View File
Write
Preview
Loading…
Cancel
Save