asserts_test.js 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273
  1. // Copyright 2007 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.assertsTest');
  15. goog.setTestOnly('goog.testing.assertsTest');
  16. goog.require('goog.array');
  17. goog.require('goog.dom');
  18. goog.require('goog.iter.Iterator');
  19. goog.require('goog.iter.StopIteration');
  20. goog.require('goog.labs.userAgent.browser');
  21. goog.require('goog.string');
  22. goog.require('goog.structs.Map');
  23. goog.require('goog.structs.Set');
  24. goog.require('goog.testing.TestCase');
  25. goog.require('goog.testing.asserts');
  26. goog.require('goog.testing.jsunit');
  27. goog.require('goog.userAgent');
  28. goog.require('goog.userAgent.product');
  29. function setUp() {
  30. // TODO(b/25875505): Fix unreported assertions (go/failonunreportedasserts).
  31. goog.testing.TestCase.getActiveTestCase().failOnUnreportedAsserts = false;
  32. }
  33. function testAssertTrue() {
  34. assertTrue(true);
  35. assertTrue('Good assertion', true);
  36. assertThrowsJsUnitException(function() {
  37. assertTrue(false);
  38. }, 'Call to assertTrue(boolean) with false');
  39. assertThrowsJsUnitException(function() {
  40. assertTrue('Should be true', false);
  41. }, 'Should be true\nCall to assertTrue(boolean) with false');
  42. assertThrowsJsUnitException(function() {
  43. assertTrue(null);
  44. }, 'Bad argument to assertTrue(boolean)');
  45. assertThrowsJsUnitException(function() {
  46. assertTrue(undefined);
  47. }, 'Bad argument to assertTrue(boolean)');
  48. }
  49. function testAssertFalse() {
  50. assertFalse(false);
  51. assertFalse('Good assertion', false);
  52. assertThrowsJsUnitException(function() {
  53. assertFalse(true);
  54. }, 'Call to assertFalse(boolean) with true');
  55. assertThrowsJsUnitException(function() {
  56. assertFalse('Should be false', true);
  57. }, 'Should be false\nCall to assertFalse(boolean) with true');
  58. assertThrowsJsUnitException(function() {
  59. assertFalse(null);
  60. }, 'Bad argument to assertFalse(boolean)');
  61. assertThrowsJsUnitException(function() {
  62. assertFalse(undefined);
  63. }, 'Bad argument to assertFalse(boolean)');
  64. }
  65. function testAssertEqualsWithString() {
  66. assertEquals('a', 'a');
  67. assertEquals('Good assertion', 'a', 'a');
  68. assertThrowsJsUnitException(function() {
  69. assertEquals('a', 'b');
  70. }, 'Expected <a> (String) but was <b> (String)');
  71. assertThrowsJsUnitException(function() {
  72. assertEquals('Bad assertion', 'a', 'b');
  73. }, 'Bad assertion\nExpected <a> (String) but was <b> (String)');
  74. }
  75. function testAssertEqualsWithInteger() {
  76. assertEquals(1, 1);
  77. assertEquals('Good assertion', 1, 1);
  78. assertThrowsJsUnitException(function() {
  79. assertEquals(1, 2);
  80. }, 'Expected <1> (Number) but was <2> (Number)');
  81. assertThrowsJsUnitException(function() {
  82. assertEquals('Bad assertion', 1, 2);
  83. }, 'Bad assertion\nExpected <1> (Number) but was <2> (Number)');
  84. }
  85. function testAssertNotEquals() {
  86. assertNotEquals('a', 'b');
  87. assertNotEquals('a', 'a', 'b');
  88. assertThrowsJsUnitException(function() {
  89. assertNotEquals('a', 'a');
  90. }, 'Expected not to be <a> (String)');
  91. assertThrowsJsUnitException(function() {
  92. assertNotEquals('a', 'a', 'a');
  93. }, 'a\nExpected not to be <a> (String)');
  94. }
  95. function testAssertNull() {
  96. assertNull(null);
  97. assertNull('Good assertion', null);
  98. assertThrowsJsUnitException(function() {
  99. assertNull(true);
  100. }, 'Expected <null> but was <true> (Boolean)');
  101. assertThrowsJsUnitException(function() {
  102. assertNull('Should be null', false);
  103. }, 'Should be null\nExpected <null> but was <false> (Boolean)');
  104. assertThrowsJsUnitException(function() {
  105. assertNull(undefined);
  106. }, 'Expected <null> but was <undefined>');
  107. assertThrowsJsUnitException(function() {
  108. assertNull(1);
  109. }, 'Expected <null> but was <1> (Number)');
  110. }
  111. function testAssertNotNull() {
  112. assertNotNull(true);
  113. assertNotNull('Good assertion', true);
  114. assertNotNull(false);
  115. assertNotNull(undefined);
  116. assertNotNull(1);
  117. assertNotNull('a');
  118. assertThrowsJsUnitException(function() {
  119. assertNotNull(null);
  120. }, 'Expected not to be <null>');
  121. assertThrowsJsUnitException(function() {
  122. assertNotNull('Should not be null', null);
  123. }, 'Should not be null\nExpected not to be <null>');
  124. }
  125. function testAssertUndefined() {
  126. assertUndefined(undefined);
  127. assertUndefined('Good assertion', undefined);
  128. assertThrowsJsUnitException(function() {
  129. assertUndefined(true);
  130. }, 'Expected <undefined> but was <true> (Boolean)');
  131. assertThrowsJsUnitException(function() {
  132. assertUndefined('Should be undefined', false);
  133. }, 'Should be undefined\nExpected <undefined> but was <false> (Boolean)');
  134. assertThrowsJsUnitException(function() {
  135. assertUndefined(null);
  136. }, 'Expected <undefined> but was <null>');
  137. assertThrowsJsUnitException(function() {
  138. assertUndefined(1);
  139. }, 'Expected <undefined> but was <1> (Number)');
  140. }
  141. function testAssertNotUndefined() {
  142. assertNotUndefined(true);
  143. assertNotUndefined('Good assertion', true);
  144. assertNotUndefined(false);
  145. assertNotUndefined(null);
  146. assertNotUndefined(1);
  147. assertNotUndefined('a');
  148. assertThrowsJsUnitException(function() {
  149. assertNotUndefined(undefined);
  150. }, 'Expected not to be <undefined>');
  151. assertThrowsJsUnitException(function() {
  152. assertNotUndefined('Should not be undefined', undefined);
  153. }, 'Should not be undefined\nExpected not to be <undefined>');
  154. }
  155. function testAssertNotNullNorUndefined() {
  156. assertNotNullNorUndefined(true);
  157. assertNotNullNorUndefined('Good assertion', true);
  158. assertNotNullNorUndefined(false);
  159. assertNotNullNorUndefined(1);
  160. assertNotNullNorUndefined(0);
  161. assertNotNullNorUndefined('a');
  162. assertThrowsJsUnitException(function() {
  163. assertNotNullNorUndefined(undefined);
  164. }, 'Expected not to be <undefined>');
  165. assertThrowsJsUnitException(function() {
  166. assertNotNullNorUndefined('Should not be undefined', undefined);
  167. }, 'Should not be undefined\nExpected not to be <undefined>');
  168. assertThrowsJsUnitException(function() {
  169. assertNotNullNorUndefined(null);
  170. }, 'Expected not to be <null>');
  171. assertThrowsJsUnitException(function() {
  172. assertNotNullNorUndefined('Should not be null', null);
  173. }, 'Should not be null\nExpected not to be <null>');
  174. }
  175. function testAssertNonEmptyString() {
  176. assertNonEmptyString('hello');
  177. assertNonEmptyString('Good assertion', 'hello');
  178. assertNonEmptyString('true');
  179. assertNonEmptyString('false');
  180. assertNonEmptyString('1');
  181. assertNonEmptyString('null');
  182. assertNonEmptyString('undefined');
  183. assertNonEmptyString('\n');
  184. assertNonEmptyString(' ');
  185. assertThrowsJsUnitException(function() {
  186. assertNonEmptyString('');
  187. }, 'Expected non-empty string but was <> (String)');
  188. assertThrowsJsUnitException(
  189. function() { assertNonEmptyString('Should be non-empty string', ''); },
  190. 'Should be non-empty string\n' +
  191. 'Expected non-empty string but was <> (String)');
  192. assertThrowsJsUnitException(function() {
  193. assertNonEmptyString(true);
  194. }, 'Expected non-empty string but was <true> (Boolean)');
  195. assertThrowsJsUnitException(function() {
  196. assertNonEmptyString(false);
  197. }, 'Expected non-empty string but was <false> (Boolean)');
  198. assertThrowsJsUnitException(function() {
  199. assertNonEmptyString(1);
  200. }, 'Expected non-empty string but was <1> (Number)');
  201. assertThrowsJsUnitException(function() {
  202. assertNonEmptyString(null);
  203. }, 'Expected non-empty string but was <null>');
  204. assertThrowsJsUnitException(function() {
  205. assertNonEmptyString(undefined);
  206. }, 'Expected non-empty string but was <undefined>');
  207. assertThrowsJsUnitException(function() {
  208. assertNonEmptyString(['hello']);
  209. }, 'Expected non-empty string but was <hello> (Array)');
  210. // Different browsers return different values/types in the failure message
  211. // so don't bother checking if the message is exactly as expected.
  212. assertThrowsJsUnitException(function() {
  213. assertNonEmptyString(goog.dom.createTextNode('hello'));
  214. });
  215. }
  216. function testAssertNaN() {
  217. assertNaN(NaN);
  218. assertNaN('Good assertion', NaN);
  219. assertThrowsJsUnitException(function() { assertNaN(1); }, 'Expected NaN');
  220. assertThrowsJsUnitException(function() {
  221. assertNaN('Should be NaN', 1);
  222. }, 'Should be NaN\nExpected NaN');
  223. assertThrowsJsUnitException(function() { assertNaN(true); }, 'Expected NaN');
  224. assertThrowsJsUnitException(function() { assertNaN(false); }, 'Expected NaN');
  225. assertThrowsJsUnitException(function() { assertNaN(null); }, 'Expected NaN');
  226. assertThrowsJsUnitException(function() { assertNaN(''); }, 'Expected NaN');
  227. // TODO(user): These assertions fail. We should decide on the
  228. // semantics of assertNaN
  229. // assertThrowsJsUnitException(function() { assertNaN(undefined); },
  230. // 'Expected NaN');
  231. // assertThrowsJsUnitException(function() { assertNaN('a'); },
  232. // 'Expected NaN');
  233. }
  234. function testAssertNotNaN() {
  235. assertNotNaN(1);
  236. assertNotNaN('Good assertion', 1);
  237. assertNotNaN(true);
  238. assertNotNaN(false);
  239. assertNotNaN('');
  240. assertNotNaN(null);
  241. // TODO(user): These assertions fail. We should decide on the
  242. // semantics of assertNotNaN
  243. // assertNotNaN(undefined);
  244. // assertNotNaN('a');
  245. assertThrowsJsUnitException(function() {
  246. assertNotNaN(Number.NaN);
  247. }, 'Expected not NaN');
  248. assertThrowsJsUnitException(function() {
  249. assertNotNaN('Should not be NaN', Number.NaN);
  250. }, 'Should not be NaN\nExpected not NaN');
  251. }
  252. function testAssertObjectEquals() {
  253. var obj1 = [{'a': 'hello', 'b': 'world'}];
  254. var obj2 = [{'a': 'hello', 'c': 'dear', 'b': 'world'}];
  255. // Check with obj1 and obj2 as first and second arguments respectively.
  256. assertThrows('Objects should not be equal', function() {
  257. assertObjectEquals(obj1, obj2);
  258. });
  259. // Check with obj1 and obj2 as second and first arguments respectively.
  260. assertThrows('Objects should not be equal', function() {
  261. assertObjectEquals(obj2, obj1);
  262. });
  263. // Test if equal objects are considered equal.
  264. var obj3 = [{'b': 'world', 'a': 'hello'}];
  265. assertObjectEquals(obj1, obj3);
  266. assertObjectEquals(obj3, obj1);
  267. // Test with a case where one of the members has an undefined value.
  268. var obj4 = [{'a': 'hello', 'b': undefined}];
  269. var obj5 = [{'a': 'hello'}];
  270. // Check with obj4 and obj5 as first and second arguments respectively.
  271. assertThrows('Objects should not be equal', function() {
  272. assertObjectEquals(obj4, obj5);
  273. });
  274. // Check with obj5 and obj4 as first and second arguments respectively.
  275. assertThrows('Objects should not be equal', function() {
  276. assertObjectEquals(obj5, obj4);
  277. });
  278. }
  279. function testAssertObjectNotEquals() {
  280. var obj1 = [{'a': 'hello', 'b': 'world'}];
  281. var obj2 = [{'a': 'hello', 'c': 'dear', 'b': 'world'}];
  282. // Check with obj1 and obj2 as first and second arguments respectively.
  283. assertObjectNotEquals(obj1, obj2);
  284. // Check with obj1 and obj2 as second and first arguments respectively.
  285. assertObjectNotEquals(obj2, obj1);
  286. // Test if equal objects are considered equal.
  287. var obj3 = [{'b': 'world', 'a': 'hello'}];
  288. var error = assertThrows('Objects should be equal', function() {
  289. assertObjectNotEquals(obj1, obj3);
  290. });
  291. assertContains('Objects should not be equal', error.message);
  292. error = assertThrows('Objects should be equal', function() {
  293. assertObjectNotEquals(obj3, obj1);
  294. });
  295. assertContains('Objects should not be equal', error.message);
  296. // Test with a case where one of the members has an undefined value.
  297. var obj4 = [{'a': 'hello', 'b': undefined}];
  298. var obj5 = [{'a': 'hello'}];
  299. // Check with obj4 and obj5 as first and second arguments respectively.
  300. assertObjectNotEquals(obj4, obj5);
  301. // Check with obj5 and obj4 as first and second arguments respectively.
  302. assertObjectNotEquals(obj5, obj4);
  303. assertObjectNotEquals(new Map([['a', '1']]), new Map([['b', '1']]));
  304. assertObjectNotEquals(new Set(['a', 'b']), new Set(['a']));
  305. }
  306. function testAssertObjectEquals2() {
  307. // NOTE: (0 in [undefined]) is true on FF but false on IE.
  308. // (0 in {'0': undefined}) is true on both.
  309. // grrr.
  310. assertObjectEquals('arrays should be equal', [undefined], [undefined]);
  311. assertThrows('arrays should not be equal', function() {
  312. assertObjectEquals([undefined, undefined], [undefined]);
  313. });
  314. assertThrows('arrays should not be equal', function() {
  315. assertObjectEquals([undefined], [undefined, undefined]);
  316. });
  317. }
  318. function testAssertObjectEquals3() {
  319. // Check that objects that contain identical Map objects compare
  320. // as equals. We can't do a negative test because on browsers that
  321. // implement __iterator__ we can't check the values of the iterated
  322. // properties.
  323. var obj1 = [
  324. {'a': 'hi', 'b': new goog.structs.Map('hola', 'amigo', 'como', 'estas?')},
  325. 14, 'yes', true
  326. ];
  327. var obj2 = [
  328. {'a': 'hi', 'b': new goog.structs.Map('hola', 'amigo', 'como', 'estas?')},
  329. 14, 'yes', true
  330. ];
  331. assertObjectEquals('Objects should be equal', obj1, obj2);
  332. var obj3 = {'a': [1, 2]};
  333. var obj4 = {'a': [1, 2, 3]};
  334. assertThrows('inner arrays should not be equal', function() {
  335. assertObjectEquals(obj3, obj4);
  336. });
  337. assertThrows('inner arrays should not be equal', function() {
  338. assertObjectEquals(obj4, obj3);
  339. });
  340. }
  341. function testAssertObjectEqualsSet() {
  342. // verify that Sets compare equal, when run in an environment that
  343. // supports iterators
  344. var set1 = new goog.structs.Set();
  345. var set2 = new goog.structs.Set();
  346. set1.add('a');
  347. set1.add('b');
  348. set1.add(13);
  349. set2.add('a');
  350. set2.add('b');
  351. set2.add(13);
  352. assertObjectEquals('sets should be equal', set1, set2);
  353. set2.add('hey');
  354. assertThrows('sets should not be equal', function() {
  355. assertObjectEquals(set1, set2);
  356. });
  357. }
  358. function testAssertObjectEqualsIterNoEquals() {
  359. // an object with an iterator but no equals() and no map_ cannot
  360. // be compared
  361. function Thing() { this.what = []; }
  362. Thing.prototype.add = function(n, v) { this.what.push(n + '@' + v); };
  363. Thing.prototype.get = function(n) {
  364. var m = new RegExp('^' + n + '@(.*)$', '');
  365. for (var i = 0; i < this.what.length; ++i) {
  366. var match = this.what[i].match(m);
  367. if (match) {
  368. return match[1];
  369. }
  370. }
  371. return null;
  372. };
  373. Thing.prototype.__iterator__ = function() {
  374. var iter = new goog.iter.Iterator;
  375. iter.index = 0;
  376. iter.thing = this;
  377. iter.next = function() {
  378. if (this.index < this.thing.what.length) {
  379. return this.thing.what[this.index++].split('@')[0];
  380. } else {
  381. throw goog.iter.StopIteration;
  382. }
  383. };
  384. return iter;
  385. };
  386. var thing1 = new Thing();
  387. thing1.name = 'thing1';
  388. var thing2 = new Thing();
  389. thing2.name = 'thing2';
  390. thing1.add('red', 'fish');
  391. thing1.add('blue', 'fish');
  392. thing2.add('red', 'fish');
  393. thing2.add('blue', 'fish');
  394. assertThrows('things should not be equal', function() {
  395. assertObjectEquals(thing1, thing2);
  396. });
  397. }
  398. function testAssertObjectEqualsWithDates() {
  399. var date = new Date(2010, 0, 1);
  400. var dateWithMilliseconds = new Date(2010, 0, 1, 0, 0, 0, 1);
  401. assertObjectEquals(new Date(2010, 0, 1), date);
  402. assertThrows(goog.partial(assertObjectEquals, date, dateWithMilliseconds));
  403. }
  404. function testAssertObjectEqualsSparseArrays() {
  405. var arr1 = [, 2, , 4];
  406. var arr2 = [1, 2, 3, 4, 5];
  407. assertThrows('Sparse arrays should not be equal', function() {
  408. assertObjectEquals(arr1, arr2);
  409. });
  410. assertThrows('Sparse arrays should not be equal', function() {
  411. assertObjectEquals(arr2, arr1);
  412. });
  413. }
  414. function testAssertObjectEqualsSparseArrays2() {
  415. // On IE6-8, the expression "1 in [4,undefined]" evaluates to false,
  416. // but true on other browsers. FML. This test verifies a regression
  417. // where IE reported that arr4 was not equal to arr1 or arr2.
  418. var arr1 = [1, , 3];
  419. var arr2 = [1, undefined, 3];
  420. var arr3 = goog.array.clone(arr1);
  421. var arr4 = [];
  422. arr4.push(1, undefined, 3);
  423. // Assert that all those arrays are equivalent pairwise.
  424. var arrays = [arr1, arr2, arr3, arr4];
  425. for (var i = 0; i < arrays.length; i++) {
  426. for (var j = 0; j < arrays.length; j++) {
  427. assertArrayEquals(arrays[i], arrays[j]);
  428. }
  429. }
  430. }
  431. function testAssertObjectEqualsArraysWithExtraProps() {
  432. var arr1 = [1];
  433. var arr2 = [1];
  434. arr2.foo = 3;
  435. assertThrows('Aarrays should not be equal', function() {
  436. assertObjectEquals(arr1, arr2);
  437. });
  438. assertThrows('Arrays should not be equal', function() {
  439. assertObjectEquals(arr2, arr1);
  440. });
  441. }
  442. function testAssertSameElementsOnArray() {
  443. assertSameElements([1, 2], [2, 1]);
  444. assertSameElements('Good assertion', [1, 2], [2, 1]);
  445. assertSameElements('Good assertion with duplicates', [1, 1, 2], [2, 1, 1]);
  446. assertThrowsJsUnitException(function() {
  447. assertSameElements([1, 2], [1]);
  448. }, 'Expected 2 elements: [1,2], got 1 elements: [1]');
  449. assertThrowsJsUnitException(function() {
  450. assertSameElements('Should match', [1, 2], [1]);
  451. }, 'Should match\nExpected 2 elements: [1,2], got 1 elements: [1]');
  452. assertThrowsJsUnitException(function() {
  453. assertSameElements([1, 2], [1, 3]);
  454. }, 'Expected [1,2], got [1,3]');
  455. assertThrowsJsUnitException(function() {
  456. assertSameElements('Should match', [1, 2], [1, 3]);
  457. }, 'Should match\nExpected [1,2], got [1,3]');
  458. assertThrowsJsUnitException(function() {
  459. assertSameElements([1, 1, 2], [2, 2, 1]);
  460. }, 'Expected [1,1,2], got [2,2,1]');
  461. }
  462. function testAssertSameElementsOnArrayLike() {
  463. assertSameElements({0: 0, 1: 1, length: 2}, {length: 2, 1: 1, 0: 0});
  464. assertThrowsJsUnitException(function() {
  465. assertSameElements({0: 0, 1: 1, length: 2}, {0: 0, length: 1});
  466. }, 'Expected 2 elements: [0,1], got 1 elements: [0]');
  467. }
  468. function testAssertSameElementsWithBadArguments() {
  469. assertThrowsJsUnitException(
  470. function() { assertSameElements([], new goog.structs.Set()); },
  471. 'Bad arguments to assertSameElements(opt_message, expected: ' +
  472. 'ArrayLike, actual: ArrayLike)\n' +
  473. 'Call to assertTrue(boolean) with false');
  474. }
  475. var implicitlyTrue = [true, 1, -1, ' ', 'string', Infinity, new Object()];
  476. var implicitlyFalse = [false, 0, '', null, undefined, NaN];
  477. function testAssertEvaluatesToTrue() {
  478. assertEvaluatesToTrue(true);
  479. assertEvaluatesToTrue('', true);
  480. assertEvaluatesToTrue('Good assertion', true);
  481. assertThrowsJsUnitException(function() {
  482. assertEvaluatesToTrue(false);
  483. }, 'Expected to evaluate to true');
  484. assertThrowsJsUnitException(function() {
  485. assertEvaluatesToTrue('Should be true', false);
  486. }, 'Should be true\nExpected to evaluate to true');
  487. for (var i = 0; i < implicitlyTrue.length; i++) {
  488. assertEvaluatesToTrue(
  489. String('Test ' + implicitlyTrue[i] + ' [' + i + ']'),
  490. implicitlyTrue[i]);
  491. }
  492. for (var i = 0; i < implicitlyFalse.length; i++) {
  493. assertThrowsJsUnitException(function() {
  494. assertEvaluatesToTrue(implicitlyFalse[i]);
  495. }, 'Expected to evaluate to true');
  496. }
  497. }
  498. function testAssertEvaluatesToFalse() {
  499. assertEvaluatesToFalse(false);
  500. assertEvaluatesToFalse('Good assertion', false);
  501. assertThrowsJsUnitException(function() {
  502. assertEvaluatesToFalse(true);
  503. }, 'Expected to evaluate to false');
  504. assertThrowsJsUnitException(function() {
  505. assertEvaluatesToFalse('Should be false', true);
  506. }, 'Should be false\nExpected to evaluate to false');
  507. for (var i = 0; i < implicitlyFalse.length; i++) {
  508. assertEvaluatesToFalse(
  509. String('Test ' + implicitlyFalse[i] + ' [' + i + ']'),
  510. implicitlyFalse[i]);
  511. }
  512. for (var i = 0; i < implicitlyTrue.length; i++) {
  513. assertThrowsJsUnitException(function() {
  514. assertEvaluatesToFalse(implicitlyTrue[i]);
  515. }, 'Expected to evaluate to false');
  516. }
  517. }
  518. function testAssertHTMLEquals() {
  519. // TODO
  520. }
  521. function testAssertHashEquals() {
  522. assertHashEquals({a: 1, b: 2}, {b: 2, a: 1});
  523. assertHashEquals('Good assertion', {a: 1, b: 2}, {b: 2, a: 1});
  524. assertHashEquals({a: undefined}, {a: undefined});
  525. // Missing key.
  526. assertThrowsJsUnitException(function() {
  527. assertHashEquals({a: 1, b: 2}, {a: 1});
  528. }, 'Expected hash had key b that was not found');
  529. assertThrowsJsUnitException(function() {
  530. assertHashEquals('Should match', {a: 1, b: 2}, {a: 1});
  531. }, 'Should match\nExpected hash had key b that was not found');
  532. assertThrowsJsUnitException(function() {
  533. assertHashEquals({a: undefined}, {});
  534. }, 'Expected hash had key a that was not found');
  535. // Not equal key.
  536. assertThrowsJsUnitException(function() {
  537. assertHashEquals({a: 1}, {a: 5});
  538. }, 'Value for key a mismatch - expected = 1, actual = 5');
  539. assertThrowsJsUnitException(function() {
  540. assertHashEquals('Should match', {a: 1}, {a: 5});
  541. }, 'Should match\nValue for key a mismatch - expected = 1, actual = 5');
  542. assertThrowsJsUnitException(function() {
  543. assertHashEquals({a: undefined}, {a: 1})
  544. }, 'Value for key a mismatch - expected = undefined, actual = 1');
  545. // Extra key.
  546. assertThrowsJsUnitException(function() {
  547. assertHashEquals({a: 1}, {a: 1, b: 1});
  548. }, 'Actual hash had key b that was not expected');
  549. assertThrowsJsUnitException(function() {
  550. assertHashEquals('Should match', {a: 1}, {a: 1, b: 1});
  551. }, 'Should match\nActual hash had key b that was not expected');
  552. }
  553. function testAssertRoughlyEquals() {
  554. assertRoughlyEquals(1, 1, 0);
  555. assertRoughlyEquals('Good assertion', 1, 1, 0);
  556. assertRoughlyEquals(1, 1.1, 0.11);
  557. assertRoughlyEquals(1.1, 1, 0.11);
  558. assertThrowsJsUnitException(function() {
  559. assertRoughlyEquals(1, 1.1, 0.05);
  560. }, 'Expected 1, but got 1.1 which was more than 0.05 away');
  561. assertThrowsJsUnitException(function() {
  562. assertRoughlyEquals('Close enough', 1, 1.1, 0.05);
  563. }, 'Close enough\nExpected 1, but got 1.1 which was more than 0.05 away');
  564. }
  565. function testAssertContains() {
  566. var a = [1, 2, 3];
  567. assertContains(1, [1, 2, 3]);
  568. assertContains('Should contain', 1, [1, 2, 3]);
  569. assertThrowsJsUnitException(function() {
  570. assertContains(4, [1, 2, 3]);
  571. }, 'Expected \'1,2,3\' to contain \'4\'');
  572. assertThrowsJsUnitException(function() {
  573. assertContains('Should contain', 4, [1, 2, 3]);
  574. }, 'Should contain\nExpected \'1,2,3\' to contain \'4\'');
  575. // assertContains uses ===.
  576. var o = new Object();
  577. assertContains(o, [o, 2, 3]);
  578. assertThrowsJsUnitException(function() {
  579. assertContains(o, [1, 2, 3]);
  580. }, 'Expected \'1,2,3\' to contain \'[object Object]\'');
  581. }
  582. function testAssertNotContains() {
  583. var a = [1, 2, 3];
  584. assertNotContains(4, [1, 2, 3]);
  585. assertNotContains('Should not contain', 4, [1, 2, 3]);
  586. assertThrowsJsUnitException(function() {
  587. assertNotContains(1, [1, 2, 3]);
  588. }, 'Expected \'1,2,3\' not to contain \'1\'');
  589. assertThrowsJsUnitException(function() {
  590. assertNotContains('Should not contain', 1, [1, 2, 3]);
  591. }, "Should not contain\nExpected '1,2,3' not to contain '1'");
  592. // assertNotContains uses ===.
  593. var o = new Object();
  594. assertNotContains({}, [o, 2, 3]);
  595. assertThrowsJsUnitException(function() {
  596. assertNotContains(o, [o, 2, 3]);
  597. }, 'Expected \'[object Object],2,3\' not to contain \'[object Object]\'');
  598. }
  599. function testAssertRegExp() {
  600. var a = 'I like turtles';
  601. assertRegExp(/turtles$/, a);
  602. assertRegExp('turtles$', a);
  603. assertRegExp('Expected subject to be about turtles', /turtles$/, a);
  604. assertRegExp('Expected subject to be about turtles', 'turtles$', a);
  605. var b = 'Hello';
  606. assertThrowsJsUnitException(function() {
  607. assertRegExp(/turtles$/, b);
  608. }, 'Expected \'Hello\' to match RegExp /turtles$/');
  609. assertThrowsJsUnitException(function() {
  610. assertRegExp('turtles$', b);
  611. }, 'Expected \'Hello\' to match RegExp /turtles$/');
  612. }
  613. function testAssertThrows() {
  614. var failed = false;
  615. try {
  616. assertThrows('assertThrows should not pass with null param', null);
  617. failed = true;
  618. } catch (e) {
  619. }
  620. assertFalse('Fail should not get set to true', failed);
  621. try {
  622. assertThrows(
  623. 'assertThrows should not pass with undefined param', undefined);
  624. failed = true;
  625. } catch (e) {
  626. }
  627. assertFalse('Fail should not get set to true', failed);
  628. try {
  629. assertThrows('assertThrows should not pass with number param', 1);
  630. failed = true;
  631. } catch (e) {
  632. }
  633. assertFalse('Fail should not get set to true', failed);
  634. try {
  635. assertThrows('assertThrows should not pass with string param', 'string');
  636. failed = true;
  637. } catch (e) {
  638. }
  639. assertFalse('Fail should not get set to true', failed);
  640. try {
  641. assertThrows('assertThrows should not pass with object param', {});
  642. failed = true;
  643. } catch (e) {
  644. }
  645. assertFalse('Fail should not get set to true', failed);
  646. try {
  647. var error = assertThrows(
  648. 'valid function throws Error', function() { throw new Error('test'); });
  649. } catch (e) {
  650. fail('assertThrows incorrectly doesn\'t detect a thrown exception');
  651. }
  652. assertEquals('error message', 'test', error.message);
  653. try {
  654. var stringError = assertThrows(
  655. 'valid function throws string error',
  656. function() { throw 'string error test'; });
  657. } catch (e) {
  658. fail('assertThrows doesn\'t detect a thrown string exception');
  659. }
  660. assertEquals('string error', 'string error test', stringError);
  661. }
  662. function testAssertThrowsThrowsIfJsUnitException() {
  663. // TODO(b/25875505): We currently need this true for this part of the test.
  664. goog.testing.TestCase.getActiveTestCase().failOnUnreportedAsserts = true;
  665. // Asserts that assertThrows will throw a JsUnitException if the method
  666. // passed to assertThrows throws a JsUnitException of its own. assertThrows
  667. // should not be used for catching JsUnitExceptions with
  668. // "failOnUnreportedAsserts" enabled.
  669. var e = assertThrowsJsUnitException(function() {
  670. assertThrows(function() {
  671. // We need to invalidate this exception so it's not flagged as a
  672. // legitimate failure by the test framework. The only way to get at the
  673. // exception thrown by assertTrue is to catch it so we can invalidate it.
  674. // We then need to rethrow it so the surrounding assertThrows behaves as
  675. // expected.
  676. try {
  677. assertTrue(false);
  678. } catch (ex) {
  679. goog.testing.TestCase.getActiveTestCase().invalidateAssertionException(
  680. ex);
  681. throw ex;
  682. }
  683. });
  684. });
  685. assertContains(
  686. 'Function passed to assertThrows caught a JsUnitException', e.message);
  687. // TODO(b/25875505): Set back to false so the rest of the tests pass.
  688. goog.testing.TestCase.getActiveTestCase().failOnUnreportedAsserts = false;
  689. }
  690. function testAssertThrowsJsUnitException() {
  691. var error = assertThrowsJsUnitException(function() { assertTrue(false); });
  692. assertEquals('Call to assertTrue(boolean) with false', error.message);
  693. error = assertThrowsJsUnitException(function() {
  694. assertThrowsJsUnitException(function() { throw new Error('fail'); });
  695. });
  696. assertEquals('Call to fail()\nExpected a JsUnitException', error.message);
  697. error = assertThrowsJsUnitException(function() {
  698. assertThrowsJsUnitException(goog.nullFunction);
  699. });
  700. assertEquals('Expected a failure', error.message);
  701. }
  702. function testAssertNotThrows() {
  703. if (goog.userAgent.product.SAFARI) {
  704. // TODO(b/20733468): Disabled so we can get the rest of the Closure test
  705. // suite running in a continuous build. Will investigate later.
  706. return;
  707. }
  708. var failed = false;
  709. try {
  710. assertNotThrows('assertNotThrows should not pass with null param', null);
  711. failed = true;
  712. } catch (e) {
  713. }
  714. assertFalse('Fail should not get set to true', failed);
  715. try {
  716. assertNotThrows(
  717. 'assertNotThrows should not pass with undefined param', undefined);
  718. failed = true;
  719. } catch (e) {
  720. }
  721. assertFalse('Fail should not get set to true', failed);
  722. try {
  723. assertNotThrows('assertNotThrows should not pass with number param', 1);
  724. failed = true;
  725. } catch (e) {
  726. }
  727. assertFalse('Fail should not get set to true', failed);
  728. try {
  729. assertNotThrows(
  730. 'assertNotThrows should not pass with string param', 'string');
  731. failed = true;
  732. } catch (e) {
  733. }
  734. assertFalse('Fail should not get set to true', failed);
  735. try {
  736. assertNotThrows('assertNotThrows should not pass with object param', {});
  737. failed = true;
  738. } catch (e) {
  739. }
  740. assertFalse('Fail should not get set to true', failed);
  741. var result;
  742. try {
  743. result =
  744. assertNotThrows('valid function', function() { return 'some value'; });
  745. } catch (e) {
  746. // Shouldn't be here: throw exception.
  747. fail('assertNotThrows returned failure on a valid function');
  748. }
  749. assertEquals(
  750. 'assertNotThrows should return the result of the function.', 'some value',
  751. result);
  752. var errorDescription = 'a test error exception';
  753. try {
  754. assertNotThrows('non valid error throwing function', function() {
  755. throw new Error(errorDescription);
  756. });
  757. failed = true;
  758. } catch (e) {
  759. // Some browsers don't have a stack trace so expect to at least have the
  760. // error description. For Gecko and IE7 not even that is included.
  761. if (!goog.userAgent.GECKO &&
  762. (!goog.userAgent.IE || goog.userAgent.isVersionOrHigher('8'))) {
  763. assertContains(errorDescription, e.message);
  764. }
  765. }
  766. assertFalse('assertNotThrows did not fail on a thrown exception', failed);
  767. }
  768. function testAssertArrayEquals() {
  769. var a1 = [0, 1, 2];
  770. var a2 = [0, 1, 2];
  771. assertArrayEquals('Arrays should be equal', a1, a2);
  772. assertThrows('Should have thrown because args are not arrays', function() {
  773. assertArrayEquals(true, true);
  774. });
  775. a1 = [0, undefined, 2];
  776. a2 = [0, , 2];
  777. // The following test fails unexpectedly. The bug is tracked at
  778. // http://code.google.com/p/closure-library/issues/detail?id=419
  779. // assertThrows(
  780. // 'assertArrayEquals distinguishes undefined items from sparse arrays',
  781. // function() {
  782. // assertArrayEquals(a1, a2);
  783. // });
  784. // For the record. This behavior will probably change in the future.
  785. assertArrayEquals(
  786. 'Bug: sparse arrays and undefined items are not distinguished',
  787. [0, undefined, 2], [0, , 2]);
  788. assertThrows('The array elements should be compared with ===', function() {
  789. assertArrayEquals([0], ['0']);
  790. });
  791. assertThrows('Arrays with different length should be different', function() {
  792. assertArrayEquals([0, undefined], [0]);
  793. });
  794. a1 = [0];
  795. a2 = [0];
  796. a2[-1] = -1;
  797. assertArrayEquals('Negative indexes are ignored', a1, a2);
  798. a1 = [0];
  799. a2 = [0];
  800. a2['extra'] = 1;
  801. assertArrayEquals(
  802. 'Extra properties are ignored. Use assertObjectEquals to compare them.',
  803. a1, a2);
  804. assertArrayEquals(
  805. 'An example where assertObjectEquals would fail in IE.', ['x'],
  806. 'x'.match(/x/g));
  807. }
  808. function testAssertObjectsEqualsDifferentArrays() {
  809. assertThrows('Should have thrown because args are different', function() {
  810. var a1 = ['className1'];
  811. var a2 = ['className2'];
  812. assertObjectEquals(a1, a2);
  813. });
  814. }
  815. function testAssertObjectsEqualsNegativeArrayIndexes() {
  816. var a1 = [0];
  817. var a2 = [0];
  818. a2[-1] = -1;
  819. // The following test fails unexpectedly. The bug is tracked at
  820. // http://code.google.com/p/closure-library/issues/detail?id=418
  821. // assertThrows('assertObjectEquals compares negative indexes', function() {
  822. // assertObjectEquals(a1, a2);
  823. // });
  824. }
  825. function testAssertObjectsEqualsDifferentTypeSameToString() {
  826. assertThrows('Should have thrown because args are different', function() {
  827. var a1 = 'className1';
  828. var a2 = ['className1'];
  829. assertObjectEquals(a1, a2);
  830. });
  831. assertThrows('Should have thrown because args are different', function() {
  832. var a1 = ['className1'];
  833. var a2 = {'0': 'className1'};
  834. assertObjectEquals(a1, a2);
  835. });
  836. assertThrows('Should have thrown because args are different', function() {
  837. var a1 = ['className1'];
  838. var a2 = [['className1']];
  839. assertObjectEquals(a1, a2);
  840. });
  841. }
  842. function testAssertObjectsRoughlyEquals() {
  843. assertObjectRoughlyEquals({'a': 1}, {'a': 1.2}, 0.3);
  844. assertThrowsJsUnitException(
  845. function() { assertObjectRoughlyEquals({'a': 1}, {'a': 1.2}, 0.1); },
  846. 'Expected <[object Object]> (Object) but was <[object Object]> ' +
  847. '(Object)\n a: Expected <1> (Number) but was <1.2> (Number) which ' +
  848. 'was more than 0.1 away');
  849. }
  850. function testAssertObjectRoughlyEqualsWithStrings() {
  851. // Check that objects with string properties are compared properly.
  852. var obj1 = {'description': [{'colName': 'x1'}]};
  853. var obj2 = {'description': [{'colName': 'x2'}]};
  854. assertThrowsJsUnitException(
  855. function() {
  856. assertObjectRoughlyEquals(obj1, obj2, 0.00001);
  857. },
  858. 'Expected <[object Object]> (Object)' +
  859. ' but was <[object Object]> (Object)' +
  860. '\n description[0].colName: Expected String "x1" but got "x2"');
  861. }
  862. function testFindDifferences_equal() {
  863. assertNull(goog.testing.asserts.findDifferences(true, true));
  864. assertNull(goog.testing.asserts.findDifferences(null, null));
  865. assertNull(goog.testing.asserts.findDifferences(undefined, undefined));
  866. assertNull(goog.testing.asserts.findDifferences(1, 1));
  867. assertNull(goog.testing.asserts.findDifferences([1, 'a'], [1, 'a']));
  868. assertNull(
  869. goog.testing.asserts.findDifferences([[1, 2], [3, 4]], [[1, 2], [3, 4]]));
  870. assertNull(
  871. goog.testing.asserts.findDifferences([{a: 1, b: 2}], [{b: 2, a: 1}]));
  872. assertNull(goog.testing.asserts.findDifferences(null, null));
  873. assertNull(goog.testing.asserts.findDifferences(undefined, undefined));
  874. assertNull(goog.testing.asserts.findDifferences(
  875. new Map([['a', 1], ['b', 2]]), new Map([['b', 2], ['a', 1]])));
  876. assertNull(goog.testing.asserts.findDifferences(
  877. new Set(['a', 'b']), new Set(['b', 'a'])));
  878. }
  879. function testFindDifferences_unequal() {
  880. assertNotNull(goog.testing.asserts.findDifferences(true, false));
  881. assertNotNull(
  882. goog.testing.asserts.findDifferences([{a: 1, b: 2}], [{a: 2, b: 1}]));
  883. assertNotNull(
  884. goog.testing.asserts.findDifferences([{a: 1}], [{a: 1, b: [2]}]));
  885. assertNotNull(
  886. goog.testing.asserts.findDifferences([{a: 1, b: [2]}], [{a: 1}]));
  887. assertNotNull(
  888. 'Second map is missing key "a"; first map is missing key "b"',
  889. goog.testing.asserts.findDifferences(
  890. new Map([['a', 1]]), new Map([['b', 2]])));
  891. assertNotNull(
  892. 'Value for key "a" differs by value',
  893. goog.testing.asserts.findDifferences(
  894. new Map([['a', '1']]), new Map([['a', '2']])));
  895. assertNotNull(
  896. 'Value for key "a" differs by type',
  897. goog.testing.asserts.findDifferences(
  898. new Map([['a', '1']]), new Map([['a', 1]])));
  899. assertNotNull(
  900. 'Second set is missing key "a"',
  901. goog.testing.asserts.findDifferences(
  902. new Set(['a', 'b']), new Set(['b'])));
  903. assertNotNull(
  904. 'First set is missing key "b"',
  905. goog.testing.asserts.findDifferences(
  906. new Set(['a']), new Set(['a', 'b'])));
  907. assertNotNull(
  908. 'Values have different types"',
  909. goog.testing.asserts.findDifferences(new Set(['1']), new Set([1])));
  910. }
  911. function testFindDifferences_objectsAndNull() {
  912. assertNotNull(goog.testing.asserts.findDifferences({a: 1}, null));
  913. assertNotNull(goog.testing.asserts.findDifferences(null, {a: 1}));
  914. assertNotNull(goog.testing.asserts.findDifferences(null, []));
  915. assertNotNull(goog.testing.asserts.findDifferences([], null));
  916. assertNotNull(goog.testing.asserts.findDifferences([], undefined));
  917. }
  918. function testFindDifferences_basicCycle() {
  919. var a = {};
  920. var b = {};
  921. a.self = a;
  922. b.self = b;
  923. assertNull(goog.testing.asserts.findDifferences(a, b));
  924. a.unique = 1;
  925. assertNotNull(goog.testing.asserts.findDifferences(a, b));
  926. }
  927. function testFindDifferences_crossedCycle() {
  928. var a = {};
  929. var b = {};
  930. a.self = b;
  931. b.self = a;
  932. assertNull(goog.testing.asserts.findDifferences(a, b));
  933. a.unique = 1;
  934. assertNotNull(goog.testing.asserts.findDifferences(a, b));
  935. }
  936. function testFindDifferences_asymmetricCycle() {
  937. var a = {};
  938. var b = {};
  939. var c = {};
  940. var d = {};
  941. var e = {};
  942. a.self = b;
  943. b.self = a;
  944. c.self = d;
  945. d.self = e;
  946. e.self = c;
  947. assertNotNull(goog.testing.asserts.findDifferences(a, c));
  948. }
  949. function testFindDifferences_basicCycleArray() {
  950. var a = [];
  951. var b = [];
  952. a[0] = a;
  953. b[0] = b;
  954. assertNull(goog.testing.asserts.findDifferences(a, b));
  955. a[1] = 1;
  956. assertNotNull(goog.testing.asserts.findDifferences(a, b));
  957. }
  958. function testFindDifferences_crossedCycleArray() {
  959. var a = [];
  960. var b = [];
  961. a[0] = b;
  962. b[0] = a;
  963. assertNull(goog.testing.asserts.findDifferences(a, b));
  964. a[1] = 1;
  965. assertNotNull(goog.testing.asserts.findDifferences(a, b));
  966. }
  967. function testFindDifferences_asymmetricCycleArray() {
  968. var a = [];
  969. var b = [];
  970. var c = [];
  971. var d = [];
  972. var e = [];
  973. a[0] = b;
  974. b[0] = a;
  975. c[0] = d;
  976. d[0] = e;
  977. e[0] = c;
  978. assertNotNull(goog.testing.asserts.findDifferences(a, c));
  979. }
  980. function testFindDifferences_multiCycles() {
  981. var a = {};
  982. a.cycle1 = a;
  983. a.test = {cycle2: a};
  984. var b = {};
  985. b.cycle1 = b;
  986. b.test = {cycle2: b};
  987. assertNull(goog.testing.asserts.findDifferences(a, b));
  988. }
  989. function testFindDifferences_binaryTree() {
  990. function createBinTree(depth, root) {
  991. if (depth == 0) {
  992. return {root: root};
  993. } else {
  994. var node = {};
  995. node.left = createBinTree(depth - 1, root || node);
  996. node.right = createBinTree(depth - 1, root || node);
  997. return node;
  998. }
  999. }
  1000. // TODO(gboyer,user): This test does not terminate with the current
  1001. // algorithm. Can be enabled when (if) the algorithm is improved.
  1002. // assertNull(goog.testing.asserts.findDifferences(
  1003. // createBinTree(5, null), createBinTree(5, null)));
  1004. assertNotNull(
  1005. goog.testing.asserts.findDifferences(
  1006. createBinTree(4, null), createBinTree(5, null)));
  1007. }
  1008. function testStringForWindowIE() {
  1009. if (goog.userAgent.IE && !goog.userAgent.isVersionOrHigher('8')) {
  1010. // NOTE(user): This test sees of we are being affected by a JScript bug
  1011. // in try/finally handling. This bug only affects the lowest try/finally
  1012. // block in the stack. Calling this function via VBScript allows
  1013. // us to run the test synchronously in an empty JS stack.
  1014. window.execScript('stringForWindowIEHelper()', 'vbscript');
  1015. assertEquals('<[object]> (Object)', window.stringForWindowIEResult);
  1016. }
  1017. }
  1018. function testStringSamePrefix() {
  1019. assertThrowsJsUnitException(
  1020. function() { assertEquals('abcdefghi', 'abcdefghx'); },
  1021. 'Expected <abcdefghi> (String) but was <abcdefghx> (String)\n' +
  1022. 'Difference was at position 8. Expected [...ghi] vs. actual [...ghx]');
  1023. }
  1024. function testStringSameSuffix() {
  1025. assertThrowsJsUnitException(
  1026. function() { assertEquals('xbcdefghi', 'abcdefghi'); },
  1027. 'Expected <xbcdefghi> (String) but was <abcdefghi> (String)\n' +
  1028. 'Difference was at position 0. Expected [xbc...] vs. actual [abc...]');
  1029. }
  1030. function testStringLongComparedValues() {
  1031. assertThrowsJsUnitException(
  1032. function() {
  1033. assertEquals(
  1034. 'abcdefghijkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkklmnopqrstuvwxyz',
  1035. 'abcdefghijkkkkkkkkkkkkkkkkkkkkkkkkkkkkkklmnopqrstuvwxyz');
  1036. },
  1037. 'Expected\n' +
  1038. '<abcdefghijkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkklmnopqrstuvwxyz> (String)\n' +
  1039. 'but was\n' +
  1040. '<abcdefghijkkkkkkkkkkkkkkkkkkkkkkkkkkkkkklmnopqrstuvwxyz> (String)\n' +
  1041. 'Difference was at position 40. Expected [...kkklmnopqrstuvwxyz] vs. actual [...kklmnopqrstuvwxyz]');
  1042. }
  1043. function testStringLongDiff() {
  1044. assertThrowsJsUnitException(
  1045. function() {
  1046. assertEquals(
  1047. 'abcdefghijkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkklmnopqrstuvwxyz',
  1048. 'abc...xyz');
  1049. },
  1050. 'Expected\n' +
  1051. '<abcdefghijkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkklmnopqrstuvwxyz> (String)\n' +
  1052. 'but was\n' +
  1053. '<abc...xyz> (String)\n' +
  1054. 'Difference was at position 3. Expected\n' +
  1055. '[...bcdefghijkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkklmnopqrstuvwxy...]\n' +
  1056. 'vs. actual\n' +
  1057. '[...bc...xy...]');
  1058. }
  1059. function testStringDissimilarShort() {
  1060. assertThrowsJsUnitException(function() {
  1061. assertEquals('x', 'y');
  1062. }, 'Expected <x> (String) but was <y> (String)');
  1063. }
  1064. function testStringDissimilarLong() {
  1065. assertThrowsJsUnitException(function() {
  1066. assertEquals('xxxxxxxxxx', 'yyyyyyyyyy');
  1067. }, 'Expected <xxxxxxxxxx> (String) but was <yyyyyyyyyy> (String)');
  1068. }
  1069. function testAssertElementsEquals() {
  1070. assertElementsEquals([1, 2], [1, 2]);
  1071. assertElementsEquals([1, 2], {0: 1, 1: 2, length: 2});
  1072. assertElementsEquals('Good assertion', [1, 2], [1, 2]);
  1073. assertThrowsJsUnitException(
  1074. function() {
  1075. assertElementsEquals('Message', [1, 2], [1]);
  1076. },
  1077. 'length mismatch: Message\n' +
  1078. 'Expected <2> (Number) but was <1> (Number)');
  1079. }
  1080. function testStackTrace() {
  1081. try {
  1082. assertTrue(false);
  1083. } catch (e) {
  1084. if (Error.captureStackTrace) {
  1085. assertNotUndefined(e.stack);
  1086. }
  1087. if (e.stack) {
  1088. var stack = e.stack;
  1089. var stackTraceContainsTestName =
  1090. goog.string.contains(stack, 'testStackTrace');
  1091. if (!stackTraceContainsTestName &&
  1092. goog.labs.userAgent.browser.isChrome()) {
  1093. // Occasionally Chrome does not give us a full stack trace, making for
  1094. // a flaky test. If we don't have the full stack trace, at least
  1095. // check that we got the error string.
  1096. // Filed a bug on Chromium here:
  1097. // https://code.google.com/p/chromium/issues/detail?id=403029
  1098. var expected = 'Call to assertTrue(boolean) with false';
  1099. assertContains(expected, stack);
  1100. return;
  1101. }
  1102. assertTrue(
  1103. 'Expected the stack trace to contain string "testStackTrace"',
  1104. stackTraceContainsTestName);
  1105. }
  1106. }
  1107. }
  1108. function stringForWindowIEHelper() {
  1109. window.stringForWindowIEResult = _displayStringForValue(window);
  1110. }
  1111. function testDisplayStringForValue() {
  1112. assertEquals('<hello> (String)', _displayStringForValue('hello'));
  1113. assertEquals('<1> (Number)', _displayStringForValue(1));
  1114. assertEquals('<null>', _displayStringForValue(null));
  1115. assertEquals('<undefined>', _displayStringForValue(undefined));
  1116. assertEquals(
  1117. '<hello,,,,1> (Array)',
  1118. _displayStringForValue(['hello', /* array hole */, undefined, null, 1]));
  1119. }
  1120. function testDisplayStringForValue_exception() {
  1121. assertEquals(
  1122. '<toString failed: foo message> (Object)',
  1123. _displayStringForValue(
  1124. {toString: function() { throw Error('foo message'); }}));
  1125. }
  1126. function testDisplayStringForValue_cycle() {
  1127. var cycle = ['cycle'];
  1128. cycle.push(cycle);
  1129. assertTrue(
  1130. 'Computing string should terminate and result in a reasonable length',
  1131. _displayStringForValue(cycle).length < 1000);
  1132. }