You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

57 lines
2.0 KiB

  1. /* global jasmine */
  2. 'use strict';
  3. (function(jasmine, beforeEach) {
  4. var sinon = (typeof require === 'function' && typeof module === 'object') ? require('sinon') : window.sinon,
  5. spyMatchers = 'called calledOnce calledTwice calledThrice calledBefore calledAfter calledOn alwaysCalledOn calledWith alwaysCalledWith calledWithExactly alwaysCalledWithExactly calledWithMatch alwaysCalledWithMatch'.split(' '),
  6. i = spyMatchers.length,
  7. spyMatcherHash = {},
  8. unusualMatchers = {
  9. "returned": "toHaveReturned",
  10. "alwaysReturned": "toHaveAlwaysReturned",
  11. "threw": "toHaveThrown",
  12. "alwaysThrew": "toHaveAlwaysThrown"
  13. },
  14. createCustomMatcher = function(arg) {
  15. return sinon.match(function (val) {
  16. return jasmine.getEnv().equals_(val, arg);
  17. });
  18. },
  19. getMatcherFunction = function(sinonName, matcherName) {
  20. var original = jasmine.Matchers.prototype[matcherName];
  21. return function () {
  22. if (jasmine.isSpy(this.actual) && original) {
  23. return original.apply(this, arguments);
  24. }
  25. var sinonProperty = this.actual[sinonName];
  26. var args = Array.prototype.slice.call(arguments);
  27. for (var i = 0; i < args.length; i++) {
  28. if (args[i] && (typeof args[i].jasmineMatches === 'function' || args[i] instanceof jasmine.Matchers.ObjectContaining)) {
  29. args[i] = createCustomMatcher(args[i]);
  30. }
  31. }
  32. return (typeof sinonProperty === 'function') ? sinonProperty.apply(this.actual, args) : sinonProperty;
  33. };
  34. };
  35. while(i--) {
  36. var sinonName = spyMatchers[i],
  37. matcherName = "toHaveBeen" + sinonName.charAt(0).toUpperCase() + sinonName.slice(1);
  38. spyMatcherHash[matcherName] = getMatcherFunction(sinonName, matcherName);
  39. }
  40. for (var j in unusualMatchers) {
  41. spyMatcherHash[unusualMatchers[j]] = getMatcherFunction(j, unusualMatchers[j]);
  42. }
  43. beforeEach(function() {
  44. this.addMatchers(spyMatcherHash);
  45. });
  46. })(jasmine, beforeEach);