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.

299 lines
10 KiB

  1. (function() {
  2. var createCustomMatcher, createMatcher, jasmineMessageGenerator, jasmineName, sinonMatchers, sinonName, sinonToJasmineMap, spies,
  3. __slice = [].slice;
  4. sinonMatchers = {};
  5. sinonToJasmineMap = {
  6. 'called': 'toHaveBeenCalled',
  7. 'calledOnce': 'toHaveBeenCalledOnce',
  8. 'calledTwice': 'toHaveBeenCalledTwice',
  9. 'calledThrice': 'toHaveBeenCalledThrice',
  10. 'calledBefore': 'toHaveBeenCalledBefore',
  11. 'calledAfter': 'toHaveBeenCalledAfter',
  12. 'calledOn': 'toHaveBeenCalledOn',
  13. 'alwaysCalledOn': 'toHaveBeenAlwaysCalledOn',
  14. 'calledWith': 'toHaveBeenCalledWith',
  15. 'alwaysCalledWith': 'toHaveBeenAlwaysCalledWith',
  16. 'calledWithExactly': 'toHaveBeenCalledWithExactly',
  17. 'alwaysCalledWithExactly': 'toHaveBeenAlwaysCalledWithExactly',
  18. 'calledWithMatch': 'toHaveBeenCalledWithMatch',
  19. 'alwaysCalledWithMatch': 'toHaveBeenAlwaysCalledWithMatch',
  20. 'returned': 'toHaveReturned',
  21. 'alwaysReturned': 'toHaveAlwaysReturned',
  22. 'threw': 'toHaveThrown',
  23. 'alwaysThrew': 'toHaveAlwaysThrown'
  24. };
  25. jasmineMessageGenerator = {
  26. 'toHaveBeenCalled': function(passed, spy, other_args) {
  27. var message;
  28. message = "Expected spy '" + spy + "' ";
  29. if (passed) {
  30. message += "not ";
  31. }
  32. message += "toHaveBeenCalled";
  33. return message.trim();
  34. },
  35. 'toHaveBeenCalledOnce': function(passed, spy, other_args) {
  36. var message;
  37. message = "Expected spy '" + spy + "' ";
  38. if (passed) {
  39. message += "not ";
  40. }
  41. message += "toHaveBeenCalledOnce";
  42. return message.trim();
  43. },
  44. 'toHaveBeenCalledTwice': function(passed, spy, other_args) {
  45. var message;
  46. message = "Expected spy '" + spy + "' ";
  47. if (passed) {
  48. message += "not ";
  49. }
  50. message += "toHaveBeenCalledTwice";
  51. return message.trim();
  52. },
  53. 'toHaveBeenCalledThrice': function(passed, spy, other_args) {
  54. var message;
  55. message = "Expected spy '" + spy + "' ";
  56. if (passed) {
  57. message += "not ";
  58. }
  59. message += "toHaveBeenCalledThrice";
  60. return message.trim();
  61. },
  62. 'toHaveBeenCalledBefore': function(passed, spy, other_args) {
  63. var message;
  64. message = "Expected spy '" + spy + "' ";
  65. if (passed) {
  66. message += "not ";
  67. }
  68. message += "toHaveBeenCalledBefore ";
  69. if ((other_args != null ? other_args.length : void 0) > 0) {
  70. message += jasmine.pp(other_args) + (" but was CalledBefore " + (jasmine.pp(spy.lastCall.args)));
  71. }
  72. return message.trim();
  73. },
  74. 'toHaveBeenCalledAfter': function(passed, spy, other_args) {
  75. var message;
  76. message = "Expected spy '" + spy + "' ";
  77. if (passed) {
  78. message += "not ";
  79. }
  80. message += "toHaveBeenCalledAfter ";
  81. if ((other_args != null ? other_args.length : void 0) > 0) {
  82. message += jasmine.pp(other_args) + (" but was CalledAfter " + (jasmine.pp(spy.lastCall.args)));
  83. }
  84. return message.trim();
  85. },
  86. 'toHaveBeenCalledOn': function(passed, spy, other_args) {
  87. var message;
  88. message = "Expected spy '" + spy + "' ";
  89. if (passed) {
  90. message += "not ";
  91. }
  92. message += "toHaveBeenCalledOn ";
  93. if ((other_args != null ? other_args.length : void 0) > 0) {
  94. message += jasmine.pp(other_args) + (" but was CalledOn " + (jasmine.pp(spy.lastCall.args)));
  95. }
  96. return message.trim();
  97. },
  98. 'toHaveBeenAlwaysCalledOn': function(passed, spy, other_args) {
  99. var message;
  100. message = "Expected spy '" + spy + "' ";
  101. if (passed) {
  102. message += "not ";
  103. }
  104. message += "toHaveBeenAlwaysCalledOn ";
  105. if ((other_args != null ? other_args.length : void 0) > 0) {
  106. message += jasmine.pp(other_args) + (" but was AlwaysCalledOn " + (jasmine.pp(spy.lastCall.args)));
  107. }
  108. return message.trim();
  109. },
  110. 'toHaveBeenCalledWith': function(passed, spy, other_args) {
  111. var message;
  112. message = "Expected spy '" + spy + "' ";
  113. if (passed) {
  114. message += "not ";
  115. }
  116. message += "toHaveBeenCalledWith ";
  117. if ((other_args != null ? other_args.length : void 0) > 0) {
  118. message += jasmine.pp(other_args) + (" but was CalledWith " + (jasmine.pp(spy.lastCall.args)));
  119. }
  120. return message.trim();
  121. },
  122. 'toHaveBeenAlwaysCalledWith': function(passed, spy, other_args) {
  123. var message;
  124. message = "Expected spy '" + spy + "' ";
  125. if (passed) {
  126. message += "not ";
  127. }
  128. message += "toHaveBeenAlwaysCalledWith ";
  129. if ((other_args != null ? other_args.length : void 0) > 0) {
  130. message += jasmine.pp(other_args) + (" but was AlwaysCalledWith " + (jasmine.pp(spy.lastCall.args)));
  131. }
  132. return message.trim();
  133. },
  134. 'toHaveBeenCalledWithExactly': function(passed, spy, other_args) {
  135. var message;
  136. message = "Expected spy '" + spy + "' ";
  137. if (passed) {
  138. message += "not ";
  139. }
  140. message += "toHaveBeenCalledWithExactly ";
  141. if ((other_args != null ? other_args.length : void 0) > 0) {
  142. message += jasmine.pp(other_args) + (" but was CalledWithExactly " + (jasmine.pp(spy.lastCall.args)));
  143. }
  144. return message.trim();
  145. },
  146. 'toHaveBeenAlwaysCalledWithExactly': function(passed, spy, other_args) {
  147. var message;
  148. message = "Expected spy '" + spy + "' ";
  149. if (passed) {
  150. message += "not ";
  151. }
  152. message += "toHaveBeenAlwaysCalledWithExactly ";
  153. if ((other_args != null ? other_args.length : void 0) > 0) {
  154. message += jasmine.pp(other_args) + (" but was AlwaysCalledWithExactly " + (jasmine.pp(spy.lastCall.args)));
  155. }
  156. return message.trim();
  157. },
  158. 'toHaveBeenCalledWithMatch': function(passed, spy, other_args) {
  159. var message;
  160. message = "Expected spy '" + spy + "' ";
  161. if (passed) {
  162. message += "not ";
  163. }
  164. message += "toHaveBeenCalledWithMatch ";
  165. if ((other_args != null ? other_args.length : void 0) > 0) {
  166. message += jasmine.pp(other_args) + (" but was CalledWithMatch " + (jasmine.pp(spy.lastCall.args)));
  167. }
  168. return message.trim();
  169. },
  170. 'toHaveBeenAlwaysCalledWithMatch': function(passed, spy, other_args) {
  171. var message;
  172. message = "Expected spy '" + spy + "' ";
  173. if (passed) {
  174. message += "not ";
  175. }
  176. message += "toHaveBeenAlwaysCalledWithMatch ";
  177. if ((other_args != null ? other_args.length : void 0) > 0) {
  178. message += jasmine.pp(other_args) + (" but was AlwaysCalledWithMatch " + (jasmine.pp(spy.lastCall.args)));
  179. }
  180. return message.trim();
  181. },
  182. 'toHaveReturned': function(passed, spy, other_args) {
  183. var message;
  184. message = "Expected spy '" + spy + "' ";
  185. if (passed) {
  186. message += "not ";
  187. }
  188. message += "toHaveReturned ";
  189. if ((other_args != null ? other_args.length : void 0) > 0) {
  190. message += jasmine.pp(other_args) + (" but returned " + (jasmine.pp(spy.lastCall.args)));
  191. }
  192. return message.trim();
  193. },
  194. 'toHaveAlwaysReturned': function(passed, spy, other_args) {
  195. var message;
  196. message = "Expected spy '" + spy + "' ";
  197. if (passed) {
  198. message += "not ";
  199. }
  200. message += "toHaveAlwaysReturned ";
  201. if ((other_args != null ? other_args.length : void 0) > 0) {
  202. message += jasmine.pp(other_args) + (" but returned " + (jasmine.pp(spy.lastCall.args)));
  203. }
  204. return message.trim();
  205. },
  206. 'toHaveThrown': function(passed, spy, other_args) {
  207. var message;
  208. message = "Expected spy '" + spy + "' ";
  209. if (passed) {
  210. message += "not ";
  211. }
  212. message += "toHaveThrown ";
  213. if ((other_args != null ? other_args.length : void 0) > 0) {
  214. message += jasmine.pp(other_args) + (" but threw " + (jasmine.pp(spy.lastCall.args)));
  215. }
  216. return message.trim();
  217. },
  218. 'toHaveAlwaysThrown': function(passed, spy, other_args) {
  219. var message;
  220. message = "Expected spy '" + spy + "' ";
  221. if (passed) {
  222. message += "not ";
  223. }
  224. message += "toHaveAlwaysThrown ";
  225. if ((other_args != null ? other_args.length : void 0) > 0) {
  226. message += jasmine.pp(other_args) + (" but threw " + (jasmine.pp(spy.lastCall.args)));
  227. }
  228. return message.trim();
  229. }
  230. };
  231. createCustomMatcher = function(arg, util, customEqualityTesters) {
  232. return sinon.match(function(val) {
  233. return util.equals(val, arg, customEqualityTesters);
  234. });
  235. };
  236. createMatcher = function(sinonName, jasmineName) {
  237. var original;
  238. original = jasmineRequire[jasmineName];
  239. return function(util, customEqualityTesters) {
  240. return {
  241. compare: function() {
  242. var arg, args, compare, i, message, pass, sinonProperty, spy, _i, _len;
  243. args = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
  244. spy = args[0];
  245. if (original && jasmine.isSpy(spy)) {
  246. compare = original(jasmine)(util, customEqualityTesters).compare;
  247. return compare.apply(null, args);
  248. }
  249. sinonProperty = spy[sinonName];
  250. for (i = _i = 0, _len = args.length; _i < _len; i = ++_i) {
  251. arg = args[i];
  252. if (arg != null) {
  253. if ((typeof arg.jasmineMatches === 'function') || (arg instanceof jasmine.ObjectContaining)) {
  254. args[i] = createCustomMatcher(arg, util, customEqualityTesters);
  255. }
  256. }
  257. }
  258. if (typeof sinonProperty === 'function') {
  259. pass = sinonProperty.apply(spy, args.slice(1));
  260. } else {
  261. pass = sinonProperty;
  262. }
  263. message = jasmineMessageGenerator[jasmineName](pass, spy, args.slice(1));
  264. return {
  265. pass: pass,
  266. message: message
  267. };
  268. }
  269. };
  270. };
  271. };
  272. for (sinonName in sinonToJasmineMap) {
  273. jasmineName = sinonToJasmineMap[sinonName];
  274. sinonMatchers[jasmineName] = createMatcher(sinonName, jasmineName);
  275. }
  276. jasmine.Expectation.addCoreMatchers(sinonMatchers);
  277. spies = [];
  278. this.sinonSpyOn = function(obj, method) {
  279. return spies.push(sinon.spy(obj, method));
  280. };
  281. afterEach(function() {
  282. var spy, _i, _len;
  283. for (_i = 0, _len = spies.length; _i < _len; _i++) {
  284. spy = spies[_i];
  285. spy.restore();
  286. }
  287. return spies = [];
  288. });
  289. }).call(this);