recordfunction_test.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. // Copyright 2010 The Closure Library Authors. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS-IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. goog.provide('goog.testing.recordFunctionTest');
  15. goog.setTestOnly('goog.testing.recordFunctionTest');
  16. goog.require('goog.functions');
  17. goog.require('goog.testing.PropertyReplacer');
  18. goog.require('goog.testing.TestCase');
  19. goog.require('goog.testing.jsunit');
  20. goog.require('goog.testing.recordConstructor');
  21. goog.require('goog.testing.recordFunction');
  22. var stubs = new goog.testing.PropertyReplacer();
  23. function setUp() {
  24. // TODO(b/25875505): Fix unreported assertions (go/failonunreportedasserts).
  25. goog.testing.TestCase.getActiveTestCase().failOnUnreportedAsserts = false;
  26. }
  27. function tearDown() {
  28. stubs.reset();
  29. }
  30. function testNoCalls() {
  31. var f = goog.testing.recordFunction(goog.functions.identity);
  32. assertEquals('call count', 0, f.getCallCount());
  33. assertNull('last call', f.getLastCall());
  34. assertArrayEquals('all calls', [], f.getCalls());
  35. }
  36. function testWithoutArguments() {
  37. var f = goog.testing.recordFunction();
  38. assertUndefined('f(1)', f(1));
  39. assertEquals('call count', 1, f.getCallCount());
  40. var lastCall = f.getLastCall();
  41. assertEquals('original function', goog.nullFunction, lastCall.getFunction());
  42. assertEquals('this context', window, lastCall.getThis());
  43. assertArrayEquals('arguments', [1], lastCall.getArguments());
  44. assertEquals('arguments[0]', 1, lastCall.getArgument(0));
  45. assertUndefined('arguments[1]', lastCall.getArgument(1));
  46. assertUndefined('return value', lastCall.getReturnValue());
  47. }
  48. function testWithIdentityFunction() {
  49. var f = goog.testing.recordFunction(goog.functions.identity);
  50. var dummyThis = {};
  51. assertEquals('f(1)', 1, f(1));
  52. assertEquals('f.call(dummyThis, 2)', 2, f.call(dummyThis, 2));
  53. var calls = f.getCalls();
  54. var firstCall = calls[0];
  55. var lastCall = f.getLastCall();
  56. assertEquals('call count', 2, f.getCallCount());
  57. assertEquals('last call', calls[1], lastCall);
  58. assertEquals(
  59. 'original function', goog.functions.identity, lastCall.getFunction());
  60. assertEquals('this context of first call', window, firstCall.getThis());
  61. assertEquals('this context of last call', dummyThis, lastCall.getThis());
  62. assertArrayEquals(
  63. 'arguments of the first call', [1], firstCall.getArguments());
  64. assertArrayEquals('arguments of the last call', [2], lastCall.getArguments());
  65. assertEquals('return value of the first call', 1, firstCall.getReturnValue());
  66. assertEquals('return value of the last call', 2, lastCall.getReturnValue());
  67. assertNull('error thrown by the first call', firstCall.getError());
  68. assertNull('error thrown by the last call', lastCall.getError());
  69. }
  70. function testWithErrorFunction() {
  71. var f = goog.testing.recordFunction(goog.functions.error('error'));
  72. var error = assertThrows('f(1) should throw an error', function() { f(1); });
  73. assertEquals('error message', 'error', error.message);
  74. assertEquals('call count', 1, f.getCallCount());
  75. var lastCall = f.getLastCall();
  76. assertEquals('this context', window, lastCall.getThis());
  77. assertArrayEquals('arguments', [1], lastCall.getArguments());
  78. assertUndefined('return value', lastCall.getReturnValue());
  79. assertEquals('recorded error message', 'error', lastCall.getError().message);
  80. }
  81. function testWithClass() {
  82. var ns = {};
  83. /** @constructor */
  84. ns.TestClass = function(num) { this.setX(ns.TestClass.identity(1) + num); };
  85. ns.TestClass.prototype.setX = function(x) { this.x = x; };
  86. ns.TestClass.identity = function(x) { return x; };
  87. var originalNsTestClass = ns.TestClass;
  88. stubs.set(ns, 'TestClass', goog.testing.recordConstructor(ns.TestClass));
  89. stubs.set(
  90. ns.TestClass.prototype, 'setX',
  91. goog.testing.recordFunction(ns.TestClass.prototype.setX));
  92. stubs.set(
  93. ns.TestClass, 'identity',
  94. goog.testing.recordFunction(ns.TestClass.identity));
  95. var obj = new ns.TestClass(2);
  96. assertEquals('constructor is called once', 1, ns.TestClass.getCallCount());
  97. var lastConstructorCall = ns.TestClass.getLastCall();
  98. assertArrayEquals(
  99. '... with argument 2', [2], lastConstructorCall.getArguments());
  100. assertEquals('the created object', obj, lastConstructorCall.getThis());
  101. assertEquals(
  102. 'type of the created object', originalNsTestClass, obj.constructor);
  103. assertEquals('setX is called once', 1, obj.setX.getCallCount());
  104. assertArrayEquals(
  105. '... with argument 3', [3], obj.setX.getLastCall().getArguments());
  106. assertEquals('The x field is properly set', 3, obj.x);
  107. assertEquals(
  108. 'identity is called once', 1, ns.TestClass.identity.getCallCount());
  109. assertArrayEquals(
  110. '... with argument 1', [1],
  111. ns.TestClass.identity.getLastCall().getArguments());
  112. }
  113. function testPopLastCall() {
  114. var f = goog.testing.recordFunction();
  115. f(0);
  116. f(1);
  117. var firstCall = f.getCalls()[0];
  118. var lastCall = f.getCalls()[1];
  119. assertEquals('return value of popLastCall', lastCall, f.popLastCall());
  120. assertArrayEquals('1 call remains', [firstCall], f.getCalls());
  121. assertEquals('next return value of popLastCall', firstCall, f.popLastCall());
  122. assertArrayEquals('0 calls remain', [], f.getCalls());
  123. assertNull('no more calls to pop', f.popLastCall());
  124. }
  125. function testReset() {
  126. var f = goog.testing.recordFunction();
  127. f(0);
  128. f(1);
  129. assertEquals('Should be two calls.', 2, f.getCallCount());
  130. f.reset();
  131. assertEquals('Call count should reset.', 0, f.getCallCount());
  132. }
  133. function testAssertCallCount() {
  134. var f = goog.testing.recordFunction(goog.functions.identity);
  135. f.assertCallCount(0);
  136. f.assertCallCount('Unexpected failure.', 0);
  137. f('Poodles');
  138. f.assertCallCount(1);
  139. f.assertCallCount('Unexpected failure.', 1);
  140. f('Hopscotch');
  141. f.assertCallCount(2);
  142. f.reset();
  143. f.assertCallCount(0);
  144. f('Bedazzler');
  145. f.assertCallCount(1);
  146. var error = assertThrows(function() { f.assertCallCount(11); });
  147. assertEquals(error.comment, 'Expected 11 call(s), but was 1.');
  148. var comment = 'This application has requested the Runtime to terminate it ' +
  149. 'in an unusual way.';
  150. var error2 = assertThrows(function() { f.assertCallCount(comment, 12); });
  151. assertEquals(error2.comment, 'Expected 12 call(s), but was 1. ' + comment);
  152. }