asserts_test.js 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. // Copyright 2008 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.assertsTest');
  15. goog.setTestOnly('goog.assertsTest');
  16. goog.require('goog.asserts');
  17. goog.require('goog.asserts.AssertionError');
  18. goog.require('goog.dom');
  19. goog.require('goog.dom.TagName');
  20. goog.require('goog.string');
  21. goog.require('goog.testing.jsunit');
  22. goog.require('goog.userAgent');
  23. /**
  24. * Test that the function throws an error with the given message.
  25. * @param {function()} failFunc
  26. * @param {string} expectedMsg
  27. */
  28. function doTestMessage(failFunc, expectedMsg) {
  29. var error = assertThrows('failFunc should throw.', failFunc);
  30. // Test error message.
  31. assertEquals(expectedMsg, error.message);
  32. }
  33. function testAssert() {
  34. // None of them may throw exception
  35. goog.asserts.assert(true);
  36. goog.asserts.assert(1);
  37. goog.asserts.assert([]);
  38. goog.asserts.assert({});
  39. assertThrows('assert(false)', goog.partial(goog.asserts.assert, false));
  40. assertThrows('assert(0)', goog.partial(goog.asserts.assert, 0));
  41. assertThrows('assert(null)', goog.partial(goog.asserts.assert, null));
  42. assertThrows(
  43. 'assert(undefined)', goog.partial(goog.asserts.assert, undefined));
  44. // Test error messages.
  45. doTestMessage(goog.partial(goog.asserts.assert, false), 'Assertion failed');
  46. doTestMessage(
  47. goog.partial(goog.asserts.assert, false, 'ouch %s', 1),
  48. 'Assertion failed: ouch 1');
  49. }
  50. function testFail() {
  51. assertThrows('fail()', goog.asserts.fail);
  52. // Test error messages.
  53. doTestMessage(goog.partial(goog.asserts.fail, false), 'Failure');
  54. doTestMessage(
  55. goog.partial(goog.asserts.fail, 'ouch %s', 1), 'Failure: ouch 1');
  56. }
  57. function testNumber() {
  58. goog.asserts.assertNumber(1);
  59. assertThrows(
  60. 'assertNumber(null)', goog.partial(goog.asserts.assertNumber, null));
  61. // Test error messages.
  62. doTestMessage(
  63. goog.partial(goog.asserts.assertNumber, null),
  64. 'Assertion failed: Expected number but got null: null.');
  65. doTestMessage(
  66. goog.partial(goog.asserts.assertNumber, '1234'),
  67. 'Assertion failed: Expected number but got string: 1234.');
  68. doTestMessage(
  69. goog.partial(goog.asserts.assertNumber, null, 'ouch %s', 1),
  70. 'Assertion failed: ouch 1');
  71. }
  72. function testString() {
  73. assertEquals('1', goog.asserts.assertString('1'));
  74. assertThrows(
  75. 'assertString(null)', goog.partial(goog.asserts.assertString, null));
  76. // Test error messages.
  77. doTestMessage(
  78. goog.partial(goog.asserts.assertString, null),
  79. 'Assertion failed: Expected string but got null: null.');
  80. doTestMessage(
  81. goog.partial(goog.asserts.assertString, 1234),
  82. 'Assertion failed: Expected string but got number: 1234.');
  83. doTestMessage(
  84. goog.partial(goog.asserts.assertString, null, 'ouch %s', 1),
  85. 'Assertion failed: ouch 1');
  86. }
  87. function testFunction() {
  88. function f(){};
  89. assertEquals(f, goog.asserts.assertFunction(f));
  90. assertThrows(
  91. 'assertFunction(null)', goog.partial(goog.asserts.assertFunction, null));
  92. // Test error messages.
  93. doTestMessage(
  94. goog.partial(goog.asserts.assertFunction, null),
  95. 'Assertion failed: Expected function but got null: null.');
  96. doTestMessage(
  97. goog.partial(goog.asserts.assertFunction, 1234),
  98. 'Assertion failed: Expected function but got number: 1234.');
  99. doTestMessage(
  100. goog.partial(goog.asserts.assertFunction, null, 'ouch %s', 1),
  101. 'Assertion failed: ouch 1');
  102. }
  103. function testObject() {
  104. var o = {};
  105. assertEquals(o, goog.asserts.assertObject(o));
  106. assertThrows(
  107. 'assertObject(null)', goog.partial(goog.asserts.assertObject, null));
  108. // Test error messages.
  109. doTestMessage(
  110. goog.partial(goog.asserts.assertObject, null),
  111. 'Assertion failed: Expected object but got null: null.');
  112. doTestMessage(
  113. goog.partial(goog.asserts.assertObject, 1234),
  114. 'Assertion failed: Expected object but got number: 1234.');
  115. doTestMessage(
  116. goog.partial(goog.asserts.assertObject, null, 'ouch %s', 1),
  117. 'Assertion failed: ouch 1');
  118. }
  119. function testArray() {
  120. var a = [];
  121. assertEquals(a, goog.asserts.assertArray(a));
  122. assertThrows('assertArray({})', goog.partial(goog.asserts.assertArray, {}));
  123. // Test error messages.
  124. doTestMessage(
  125. goog.partial(goog.asserts.assertArray, null),
  126. 'Assertion failed: Expected array but got null: null.');
  127. doTestMessage(
  128. goog.partial(goog.asserts.assertArray, 1234),
  129. 'Assertion failed: Expected array but got number: 1234.');
  130. doTestMessage(
  131. goog.partial(goog.asserts.assertArray, null, 'ouch %s', 1),
  132. 'Assertion failed: ouch 1');
  133. }
  134. function testBoolean() {
  135. assertEquals(true, goog.asserts.assertBoolean(true));
  136. assertEquals(false, goog.asserts.assertBoolean(false));
  137. assertThrows(goog.partial(goog.asserts.assertBoolean, null));
  138. assertThrows(goog.partial(goog.asserts.assertBoolean, 'foo'));
  139. // Test error messages.
  140. doTestMessage(
  141. goog.partial(goog.asserts.assertBoolean, null),
  142. 'Assertion failed: Expected boolean but got null: null.');
  143. doTestMessage(
  144. goog.partial(goog.asserts.assertBoolean, 1234),
  145. 'Assertion failed: Expected boolean but got number: 1234.');
  146. doTestMessage(
  147. goog.partial(goog.asserts.assertBoolean, null, 'ouch %s', 1),
  148. 'Assertion failed: ouch 1');
  149. }
  150. function testElement() {
  151. assertThrows(goog.partial(goog.asserts.assertElement, null));
  152. assertThrows(goog.partial(goog.asserts.assertElement, 'foo'));
  153. assertThrows(
  154. goog.partial(goog.asserts.assertElement, goog.dom.createTextNode('foo')));
  155. var elem = goog.dom.createElement(goog.dom.TagName.DIV);
  156. assertEquals(elem, goog.asserts.assertElement(elem));
  157. }
  158. function testInstanceof() {
  159. /** @constructor */
  160. var F = function() {};
  161. goog.asserts.assertInstanceof(new F(), F);
  162. var error = assertThrows(
  163. 'assertInstanceof({}, F)',
  164. goog.partial(goog.asserts.assertInstanceof, {}, F));
  165. // IE lacks support for function.name and will fallback to toString().
  166. var object = /object/.test(error.message) ? '[object Object]' : 'Object';
  167. var name = /F/.test(error.message) ? 'F' : 'unknown type name';
  168. // Test error messages.
  169. doTestMessage(
  170. goog.partial(goog.asserts.assertInstanceof, {}, F),
  171. 'Assertion failed: Expected instanceof ' + name + ' but got ' + object +
  172. '.');
  173. doTestMessage(
  174. goog.partial(goog.asserts.assertInstanceof, {}, F, 'a %s', 1),
  175. 'Assertion failed: a 1');
  176. doTestMessage(
  177. goog.partial(goog.asserts.assertInstanceof, null, F),
  178. 'Assertion failed: Expected instanceof ' + name + ' but got null.');
  179. doTestMessage(
  180. goog.partial(goog.asserts.assertInstanceof, 5, F), 'Assertion failed: ' +
  181. 'Expected instanceof ' + name + ' but got number.');
  182. // Test a constructor a with a name (IE does not support function.name).
  183. if (!goog.userAgent.IE) {
  184. F = function foo() {};
  185. doTestMessage(
  186. goog.partial(goog.asserts.assertInstanceof, {}, F),
  187. 'Assertion failed: Expected instanceof foo but got ' + object + '.');
  188. }
  189. // Test a constructor with a displayName.
  190. F.displayName = 'bar';
  191. doTestMessage(
  192. goog.partial(goog.asserts.assertInstanceof, {}, F),
  193. 'Assertion failed: Expected instanceof bar but got ' + object + '.');
  194. }
  195. function testObjectPrototypeIsIntact() {
  196. goog.asserts.assertObjectPrototypeIsIntact();
  197. var originalToString = Object.prototype.toString;
  198. Object.prototype.toString = goog.nullFunction;
  199. try {
  200. goog.asserts.assertObjectPrototypeIsIntact();
  201. Object.prototype.foo = 1;
  202. doTestMessage(
  203. goog.asserts.assertObjectPrototypeIsIntact,
  204. 'Failure: foo should not be enumerable in Object.prototype.');
  205. } finally {
  206. Object.prototype.toString = originalToString;
  207. delete Object.prototype.foo;
  208. }
  209. }
  210. function testAssertionError() {
  211. var error = new goog.asserts.AssertionError('foo %s %s', [1, 'two']);
  212. assertEquals('Wrong message', 'foo 1 two', error.message);
  213. assertEquals('Wrong messagePattern', 'foo %s %s', error.messagePattern);
  214. }
  215. function testFailWithCustomErrorHandler() {
  216. try {
  217. var handledException;
  218. goog.asserts.setErrorHandler(function(e) { handledException = e; });
  219. var expectedMessage = 'Failure: Gevalt!';
  220. goog.asserts.fail('Gevalt!');
  221. assertTrue('handledException is null.', handledException != null);
  222. assertTrue(
  223. 'Message check failed. Expected: ' + expectedMessage + ' Actual: ' +
  224. handledException.message,
  225. goog.string.startsWith(expectedMessage, handledException.message));
  226. } finally {
  227. goog.asserts.setErrorHandler(goog.asserts.DEFAULT_ERROR_HANDLER);
  228. }
  229. }
  230. function testAssertWithCustomErrorHandler() {
  231. try {
  232. var handledException;
  233. goog.asserts.setErrorHandler(function(e) { handledException = e; });
  234. var expectedMessage = 'Assertion failed: Gevalt!';
  235. goog.asserts.assert(false, 'Gevalt!');
  236. assertTrue('handledException is null.', handledException != null);
  237. assertTrue(
  238. 'Message check failed. Expected: ' + expectedMessage + ' Actual: ' +
  239. handledException.message,
  240. goog.string.startsWith(expectedMessage, handledException.message));
  241. } finally {
  242. goog.asserts.setErrorHandler(goog.asserts.DEFAULT_ERROR_HANDLER);
  243. }
  244. }