jsunit.js 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711
  1. /* appjet:version 0.1 */
  2. /* appjet:library */
  3. function assert() {
  4. var booleanValue = _validateArguments(1, arguments)[0];
  5. if (typeof(booleanValue) != 'boolean')
  6. error('Bad argument to assert(boolean)');
  7. _assert(commentArg(1, arguments), booleanValue === true, 'Call to assert(boolean) with false');
  8. }
  9. function assertTrue() {
  10. var booleanValue = _validateArguments(1, arguments)[0];
  11. if (typeof(booleanValue) != 'boolean')
  12. error('Bad argument to assertTrue(boolean)');
  13. _assert(commentArg(1, arguments), booleanValue === true, 'Call to assertTrue(boolean) with false');
  14. }
  15. function assertFalse() {
  16. var booleanValue = _validateArguments(1, arguments)[0];
  17. if (typeof(booleanValue) != 'boolean')
  18. error('Bad argument to assertFalse(boolean)');
  19. _assert(commentArg(1, arguments), booleanValue === false, 'Call to assertFalse(boolean) with true');
  20. }
  21. function assertEquals() {
  22. var args = _validateArguments(2, arguments),
  23. exp = args[0], act = args[1];
  24. _assert(commentArg(2, arguments), exp === act, 'Expected ' + _displayStringForValue(exp) + ' but was ' + _displayStringForValue(act));
  25. }
  26. function assertNotEquals() {
  27. var args = _validateArguments(2, arguments),
  28. exp = args[0], act = args[1];
  29. _assert(commentArg(2, arguments), exp !== act, 'Expected not to be ' + _displayStringForValue(act));
  30. }
  31. function assertNull() {
  32. var aVar = _validateArguments(1, arguments)[0];
  33. _assert(commentArg(1, arguments), aVar === null, 'Expected ' + _displayStringForValue(null) + ' but was ' + _displayStringForValue(aVar));
  34. }
  35. function assertNotNull() {
  36. var aVar = _validateArguments(1, arguments)[0];
  37. _assert(commentArg(1, arguments), aVar !== null, 'Expected not to be ' + _displayStringForValue(null));
  38. }
  39. function assertUndefined() {
  40. var aVar = _validateArguments(1, arguments)[0];
  41. _assert(commentArg(1, arguments), aVar === undefined, 'Expected ' + _displayStringForValue(undefined) + ' but was ' + _displayStringForValue(aVar));
  42. }
  43. function assertNotUndefined() {
  44. var aVar = _validateArguments(1, arguments)[0];
  45. _assert(commentArg(1, arguments), aVar !== undefined, 'Expected not to be ' + _displayStringForValue(undefined));
  46. }
  47. function assertNaN() {
  48. var aVar = _validateArguments(1, arguments)[0];
  49. _assert(commentArg(1, arguments), isNaN(aVar), 'Expected NaN');
  50. }
  51. function assertNotNaN() {
  52. var aVar = _validateArguments(1, arguments)[0];
  53. _assert(commentArg(1, arguments), !isNaN(aVar), 'Expected not NaN');
  54. }
  55. function assertHTMLEquals() {
  56. var args = _validateArguments(2, arguments),
  57. exp = args[0], act = args[1];
  58. expStd = _standardizeHTML(exp),
  59. actStd = _standardizeHTML(act);
  60. assertEquals(commentArg(2, arguments)||"", expStd, actStd);
  61. }
  62. var _helper = null;//import({}, "lib-simple-htmlparser", "lib-xmldom");
  63. function _standardizeHTML(html) {
  64. result = _helper.HTMLtoXML("<scrap>"+html+"</scrap>");
  65. xmldoc = new _helper.XMLDoc(result);
  66. result = xmldoc. getUnderlyingXMLText();
  67. return result;
  68. }
  69. function assertObjectEquals() {
  70. var args = _validateArguments(2, arguments),
  71. exp = args[0], act = args[1];
  72. if (exp === act) return;
  73. var comment = commentArg(2,arguments);
  74. var msg = comment ? comment : '';
  75. var type = jsuTrueTypeOf(exp);
  76. var isEqual = type == jsuTrueTypeOf(act);
  77. if (isEqual) {
  78. switch (type) {
  79. case 'String':
  80. case 'Number':
  81. isEqual = exp == act;
  82. break;
  83. case 'Boolean':
  84. case 'Date':
  85. isEqual = exp === act;
  86. break;
  87. case 'RegExp':
  88. case 'Function':
  89. isEqual = exp.toString() === act.toString();
  90. break;
  91. default: //Object | Array
  92. isEqual = exp.length === act.length;
  93. if (isEqual)
  94. if (comment == null)
  95. _assertRecursiveEquals(exp, act, assertObjectEquals);
  96. else
  97. _assertRecursiveEquals("Nested: "+comment, exp, act, assertObjectEquals);
  98. }
  99. }
  100. _assert(msg, isEqual, 'Expected ' + _displayStringForValue(exp) + ' but was ' + _displayStringForValue(act));
  101. }
  102. assertArrayEquals = assertObjectEquals;
  103. function assertHashEquals() {
  104. var args = _validateArguments(2, arguments),
  105. exp = args[0], act = args[1];
  106. var comment = commentArg(2, arguments);
  107. if (comment == null)
  108. _assertRecursiveEquals(exp, act, assertEquals);
  109. else
  110. _assertRecursiveEquals(comment, exp, act, assertEquals);
  111. }
  112. function _assertRecursiveEquals() {
  113. var args = _validateArguments(3, arguments),
  114. exp = args[0], act = args[1], compare = args[2];
  115. var comment = commentArg(3, arguments);
  116. var comment = comment ? comment + "\n" : "";
  117. for (var key in exp) {
  118. assertTrue(comment + "Expected had key " + key + " that was not found", key in act);
  119. compare(
  120. comment + "Value for key " + key + " mismatch - expected = " + exp[key] + ", actual = " + act[key],
  121. exp[key], act[key]
  122. );
  123. }
  124. for (var key in act) {
  125. assertTrue(comment + "Actual had key " + key + " that was not expected", key in exp);
  126. }
  127. }
  128. function assertRoughlyEquals() {
  129. var args = _validateArguments(3, arguments),
  130. exp = args[0], act = args[1], tolerance = args[2];
  131. assertTrue(
  132. "Expected " + exp + ", but got " + act + " which was more than " + tolerance + " away",
  133. Math.abs(exp - act) < tolerance
  134. );
  135. }
  136. function assertContains() {
  137. var args = _validateArguments(2, arguments),
  138. contained = args[0], container = args[1];
  139. assertTrue(
  140. "Expected '" + container + "' to contain '" + contained + "'",
  141. container.indexOf(contained) != -1
  142. );
  143. }
  144. function assertEvaluatesToTrue() {
  145. var value = _validateArguments(1, arguments)[0];
  146. if (!value)
  147. fail(commentArg(1, arguments));
  148. }
  149. function assertEvaluatesToFalse() {
  150. var value = _validateArguments(1, arguments)[0];
  151. if (value)
  152. fail(commentArg(1, arguments));
  153. }
  154. function _assert(comment, booleanValue, failureMessage) {
  155. if (!booleanValue)
  156. throw new JsUnitException(comment, failureMessage);
  157. }
  158. /**@ignore*/
  159. function argumentsIncludeComments(expectedNumberOfNonCommentArgs, args) {
  160. return args.length == expectedNumberOfNonCommentArgs + 1;
  161. }
  162. /**@ignore*/
  163. function commentArg(expectedNumberOfNonCommentArgs, args) {
  164. if (argumentsIncludeComments(expectedNumberOfNonCommentArgs, args))
  165. return args[0];
  166. return null;
  167. }
  168. /**@ignore*/
  169. /*function nonCommentArg(desiredNonCommentArgIndex, expectedNumberOfNonCommentArgs, args) {
  170. return argumentsIncludeComments(expectedNumberOfNonCommentArgs, args) ?
  171. args[desiredNonCommentArgIndex] :
  172. args[desiredNonCommentArgIndex - 1];
  173. }*/
  174. function _validateArguments(expectedNumberOfNonCommentArgs, args) {
  175. if (!( args.length == expectedNumberOfNonCommentArgs ||
  176. (args.length == expectedNumberOfNonCommentArgs + 1 && typeof(args[0]) == 'string') ))
  177. error('Incorrect arguments passed to assert function');
  178. return Array.prototype.slice.call(args, -expectedNumberOfNonCommentArgs);
  179. }
  180. /**
  181. * A more functional typeof
  182. * @param Object o
  183. * @return String
  184. */
  185. function jsuTrueTypeOf(something) {
  186. var result = typeof something;
  187. try {
  188. switch (result) {
  189. case 'string':
  190. case 'boolean':
  191. case 'number':
  192. break;
  193. case 'object':
  194. case 'function':
  195. switch (something.constructor)
  196. {
  197. case String:
  198. result = 'String';
  199. break;
  200. case Boolean:
  201. result = 'Boolean';
  202. break;
  203. case Number:
  204. result = 'Number';
  205. break;
  206. case Array:
  207. result = 'Array';
  208. break;
  209. case RegExp:
  210. result = 'RegExp';
  211. break;
  212. case Function:
  213. result = 'Function';
  214. break;
  215. default:
  216. var m = something.constructor.toString().match(/function\s*([^( ]+)\(/);
  217. if (m)
  218. result = m[1];
  219. else
  220. break;
  221. }
  222. break;
  223. }
  224. }
  225. finally {
  226. result = result.substr(0, 1).toUpperCase() + result.substr(1);
  227. return result;
  228. }
  229. }
  230. function _displayStringForValue(aVar) {
  231. var result = '<' + aVar + '>';
  232. if (aVar != null) {
  233. result += ' (' + jsuTrueTypeOf(aVar) + ')';
  234. }
  235. return result;
  236. }
  237. /**Call from within test to end it with failure*/
  238. function fail(failureMessage) {
  239. throw new JsUnitException("Call to fail()", failureMessage);
  240. }
  241. /**Call from within test to end it with error*/
  242. function error(errorMessage) {
  243. var errorObject = new Object();
  244. errorObject.description = errorMessage;
  245. throw errorObject;
  246. }
  247. /**This exception is used by jsunit for failures.*/
  248. function JsUnitException(comment, message) {
  249. this.isTestException = true;
  250. this.comment = comment;
  251. this.testMessage = message;
  252. }
  253. var counter = 0;
  254. /* appjet:server */
  255. import("lib-support/jsunit");
  256. function assertJsUnitException(comment, allegedJsUnitException) {
  257. assertNotNull(comment, allegedJsUnitException);
  258. assert(comment, allegedJsUnitException.isTestException);
  259. assertNotUndefined(comment, allegedJsUnitException.comment);
  260. }
  261. function assertNonJsUnitException(comment, allegedNonJsUnitException) {
  262. assertNotNull(comment, allegedNonJsUnitException);
  263. assertUndefined(comment, allegedNonJsUnitException.isTestUnitException);
  264. assertNotUndefined(comment, allegedNonJsUnitException.description);
  265. }
  266. page.testSuite = [
  267. function assertWorks() {
  268. assert("true should be true", true);
  269. assert(true);
  270. },
  271. function assertTrueWorks() {
  272. assertTrue("true should be true", true);
  273. assertTrue(true);
  274. },
  275. function assertFalseWorks() {
  276. assertFalse("false should be false", false);
  277. assertFalse(false);
  278. },
  279. function assertEqualsWorks() {
  280. assertEquals("1 should equal 1", 1, 1);
  281. assertEquals(1, 1);
  282. },
  283. function assertNotEqualsWorks() {
  284. assertNotEquals("1 should not equal 2", 1, 2);
  285. assertNotEquals(1, 2);
  286. },
  287. function assertNullWorks() {
  288. assertNull("null should be null", null);
  289. assertNull(null);
  290. },
  291. function assertNotNullWorks() {
  292. assertNotNull("1 should not be null", 1);
  293. assertNotNull(1);
  294. },
  295. function assertUndefinedWorks() {
  296. var myVar;
  297. assertUndefined("A declared but unassigned variable should have the undefined value", myVar);
  298. assertUndefined(myVar);
  299. },
  300. function assertNotUndefinedWorks() {
  301. assertNotUndefined("1 should not be undefined", 1);
  302. assertNotUndefined(1);
  303. },
  304. function assertNaNWorks() {
  305. assertNaN("a string should not be a number", "string");
  306. assertNaN("string");
  307. },
  308. function assertNotNaNWorks() {
  309. assertNotNaN("1 should not be not a number", 1);
  310. assertNotNaN(1);
  311. },
  312. function failWorks() {
  313. var excep = null;
  314. try {
  315. fail("Failure message");
  316. } catch (e) {
  317. excep = e;
  318. }
  319. assertJsUnitException("fail(string) should throw a JsUnitException", excep);
  320. },
  321. function tooFewArguments() {
  322. var excep = null;
  323. try {
  324. assert();
  325. } catch (e1) {
  326. excep = e1;
  327. }
  328. assertNonJsUnitException("Calling an assertion function with too few arguments should throw an exception", excep);
  329. },
  330. function tooManyArguments() {
  331. var excep = null;
  332. try {
  333. assertEquals("A comment", true, true, true);
  334. } catch (e2) {
  335. excep = e2;
  336. }
  337. assertNonJsUnitException("Calling an assertion function with too many arguments should throw an exception", excep);
  338. },
  339. function invalidCommentArgumentType() {
  340. var excep = null;
  341. try {
  342. assertNull(1, true);
  343. } catch (e3) {
  344. excep = e3;
  345. }
  346. assertNonJsUnitException("Calling an assertion function with a non-string comment should throw an exception", excep);
  347. },
  348. function invalidArgumentType() {
  349. var excep = null;
  350. try {
  351. assert("string");
  352. } catch (e) {
  353. excep = e;
  354. }
  355. assertNonJsUnitException("Calling an assertion function with an invalid argument should throw an exception", excep);
  356. },
  357. function assertArrayEqualsWorks() {
  358. var array1 = ["foo", "bar", "foobar"];
  359. var array2 = ["foo", "bar", "foobar"];
  360. var array3 = ["foo", "bar"];
  361. var array4 = ["bar", "foo", "foobar"];
  362. assertArrayEquals([undefined],[undefined]);
  363. assertArrayEquals(array1, array1);
  364. assertArrayEquals(array1, array2);
  365. try {
  366. assertArrayEquals(array1, undefined);
  367. fail("Should not be equal to undefined");
  368. } catch (e) {
  369. assertJsUnitException("Should be a JsUnitException", e);
  370. if (e.comment == "Call to fail()")
  371. fail(e.comment + e.testMessage); //tried fail is also caught
  372. }
  373. try {
  374. assertArrayEquals(array1, array3);
  375. fail("Should not be equal");
  376. } catch (e) {
  377. assertJsUnitException("Should be a JsUnitException", e);
  378. if (e.comment == "Call to fail()")
  379. fail(e.comment + e.testMessage); //tried fail is also caught
  380. }
  381. try {
  382. assertArrayEquals(array1, array4);
  383. fail("Should not be equal");
  384. } catch (e) {
  385. assertJsUnitException("Should be a JsUnitException", e);
  386. if (e.comment == "Call to fail()")
  387. fail(e.comment + e.testMessage); //tried fail is also caught
  388. }
  389. var array5 = ['foo', 'bar', ['nested', 'bar'], 'foobar'];
  390. var array6 = ['foo', 'bar', ['nested', 'bar'], 'foobar'];
  391. var array7 = ['foo', 'bar', ['nested', 'foo'], 'foobar'];
  392. assertArrayEquals('Equal nested arrays', array5, array6);
  393. try {
  394. assertArrayEquals(array5, array7);
  395. var failure = 'Differing nested arrays found to be equal';
  396. fail(failure);
  397. } catch (e) {
  398. assertJsUnitException("Should be a JsUnitException", e);
  399. if (e.testMessage == failure)
  400. fail(e.testMessage);
  401. }
  402. },
  403. function assertObjectEqualsWorksOnObjects() {
  404. var failure;
  405. assertObjectEquals(undefined, undefined);
  406. var o1 = {foo:'bar'};
  407. var o2 = {foo:'bar'};
  408. assertObjectEquals('Single object', o1, o1);
  409. assertObjectEquals('Same objects', o1, o2);
  410. var o3 = {foo:'foo'};
  411. var o4 = {foo:'foo',
  412. bar: function () {
  413. this.foo = 'bar';
  414. delete this.bar;
  415. }};
  416. var o5 = {foo:'foo',
  417. bar: function () {
  418. this.foo = 'foo';
  419. delete this.bar;
  420. }};
  421. try {
  422. assertObjectEquals(o1, o3);
  423. fail(failure = 'Simple differing objects found to be the same');
  424. } catch (e) {
  425. assertJsUnitException("Should be a JsUnitException", e);
  426. if (e.testMessage == failure)
  427. fail(e.testtMessage);
  428. }
  429. try {
  430. assertObjectEquals(o3, o4);
  431. fail(failure = 'Object with additional fields found to be the same with non-grown one.');
  432. } catch (e) {
  433. assertJsUnitException("Should be a JsUnitException", e);
  434. if (e.testMessage == failure)
  435. fail(e.testMessage);
  436. }
  437. try {
  438. assertObjectEquals(o4, o5);
  439. fail(failure = 'Objects with different methods found to be the same');
  440. } catch (e) {
  441. assertJsUnitException("Should be a JsUnitException", e);
  442. if (e.testMessage == failure)
  443. fail(e.testMessage);
  444. }
  445. o4.bar();
  446. assertObjectEquals('Different objects, made to be the same', o1, o4);
  447. try {
  448. assertObjectEquals({ts:new Date()}, {ts:new Date()});
  449. fail(failure = 'Objects with different Date attributes found to be the same');
  450. } catch (e) {
  451. assertJsUnitException("Should be a JsUnitException", e);
  452. if (e.testMessage == failure)
  453. fail(e.testMessage);
  454. }
  455. try {
  456. assertObjectEquals(new Date(), new Date());
  457. fail(failure = 'Different Date objects found to be the same');
  458. } catch (e) {
  459. assertJsUnitException("Should be a JsUnitException", e);
  460. if (e.testMessage == failure)
  461. fail(e.testMessage);
  462. }
  463. assertObjectEquals(/a/, new RegExp('a'));
  464. assertObjectEquals(/a/i, new RegExp('a', 'i'));
  465. assertObjectEquals(function(a,b){return a+b;}, function(a,b){return a+b;});
  466. //XXX challenge for the future
  467. //assertObjectEquals(function(a,b){return a+b}, new Function("a","b","return a+b"));
  468. try {
  469. assertObjectEquals(/a/i, new RegExp('a', 'g'));
  470. fail(failure = 'RegExp with different flags found to be the same');
  471. } catch (e) {
  472. assertJsUnitException("Should be a JsUnitException", e);
  473. if (e.testMessage == failure)
  474. fail(e.testMessage);
  475. }
  476. try {
  477. assertObjectEquals(/a/, new RegExp('b'));
  478. fail(failure = 'RegExp with different patterns found to be the same');
  479. } catch (e) {
  480. assertJsUnitException("Should be a JsUnitException", e);
  481. if (e.testMessage == failure)
  482. fail(e.testMessage);
  483. }
  484. },
  485. function assertObjectEqualsWorksOnStrings() {
  486. var s1 = 'string1';
  487. var s2 = 'string1';
  488. var newS1 = new String('string1');
  489. assertObjectEquals('Same Strings', s1, s2);
  490. assertObjectEquals('Same Strings 1 with new', s1, newS1);
  491. },
  492. function assertObjectEqualsWorksOnNumbers() {
  493. var failure;
  494. var n1 = 1;
  495. var n2 = 1;
  496. var newN1 = new Number(1);
  497. assertObjectEquals('Same Numbers', n1, n2);
  498. assertObjectEquals('Same Numbers 1 with new', n1, newN1);
  499. var n3 = 2;
  500. var newN3 = new Number(2);
  501. try {
  502. assertObjectEquals(n1, n3);
  503. fail(failure = 'Different Numbers');
  504. } catch (e) {
  505. assertJsUnitException("Should be a JsUnitException", e);
  506. if (e.testMessage == failure)
  507. fail(e.testMessage);
  508. }
  509. try {
  510. assertObjectEquals(newN1, newN3);
  511. fail(failure = 'Different New Numbers');
  512. } catch (e) {
  513. assertJsUnitException("Should be a JsUnitException", e);
  514. if (e.testMessage == failure)
  515. fail(e.testMessage);
  516. }
  517. },
  518. function assertEvaluatesToTrueWorks() {
  519. assertEvaluatesToTrue("foo");
  520. assertEvaluatesToTrue(true);
  521. assertEvaluatesToTrue(1);
  522. try {
  523. assertEvaluatesToTrue(null);
  524. fail("Should have thrown a JsUnitException");
  525. } catch (e) {
  526. assertJsUnitException("Should be a JsUnitException", e);
  527. }
  528. },
  529. function assertEvaluatesToFalseWorks() {
  530. assertEvaluatesToFalse("");
  531. assertEvaluatesToFalse(null);
  532. assertEvaluatesToFalse(false);
  533. assertEvaluatesToFalse(0);
  534. try {
  535. assertEvaluatesToFalse("foo");
  536. fail("Should have thrown a JsUnitException");
  537. } catch (e) {
  538. assertJsUnitException("Should be a JsUnitException", e);
  539. }
  540. },
  541. function assertHtmlEqualsWorks() {
  542. assertHTMLEquals("<div id=mydiv>foobar</div>", "<div id='mydiv'>foobar</div>");
  543. assertHTMLEquals("<p/>", "<p></p>");
  544. assertHTMLEquals("foo bar", "foo bar");
  545. assertHTMLEquals("a comment", "<p id='foo'>foo bar</p>", "<p id=foo>foo bar</p>");
  546. },
  547. function assertHashEqualsWorks() {
  548. var hash1 = new Array();
  549. hash1["key1"] = "value1";
  550. hash1["key2"] = "value2";
  551. var hash2 = new Array();
  552. try {
  553. assertHashEquals(hash1, hash2);
  554. fail();
  555. } catch (e) {
  556. assertJsUnitException("hash2 is empty", e);
  557. }
  558. hash2["key1"] = "value1";
  559. try {
  560. assertHashEquals(hash1, hash2);
  561. fail();
  562. } catch (e) {
  563. assertJsUnitException("hash2 is a of a different size", e);
  564. }
  565. hash2["key2"] = "foo";
  566. try {
  567. assertHashEquals(hash1, hash2);
  568. fail();
  569. } catch (e) {
  570. assertJsUnitException("hash2 has different values", e);
  571. }
  572. hash2["key2"] = "value2";
  573. assertHashEquals(hash1, hash2);
  574. },
  575. function assertRoughlyEqualsWorks() {
  576. assertRoughlyEquals(1, 1.1, 0.5);
  577. assertRoughlyEquals(1, 5, 6);
  578. assertRoughlyEquals(-4, -5, 2);
  579. assertRoughlyEquals(-0.5, 0.1, 0.7);
  580. try {
  581. assertRoughlyEquals(1, 2, 0.5);
  582. } catch (e) {
  583. assertJsUnitException("1 and 2 are more than 0.5 apart", e);
  584. }
  585. },
  586. function assertContainsWorks() {
  587. assertContains("foo", "foobar");
  588. assertContains("ooba", "foobar");
  589. assertContains("bar", "foobar");
  590. },
  591. ];
  592. /**@ignore*/
  593. function FooBarThingy() {
  594. this.foo = 'bar';
  595. }
  596. /**@ignore*/
  597. FooBarThingy.prototype.bar = function() {
  598. return this.foo;
  599. };
  600. page.testSuite.push(function testTrueTypeOf() {
  601. assertEquals('Boolean', jsuTrueTypeOf(true));
  602. assertEquals('Using new', 'Boolean', jsuTrueTypeOf(new Boolean('1')));
  603. assertEquals('Number', jsuTrueTypeOf(1));
  604. var GI = new Number(1);
  605. assertEquals('Using new', 'Number', jsuTrueTypeOf(GI));
  606. assertEquals('Number', jsuTrueTypeOf(1.5));
  607. assertEquals('String', jsuTrueTypeOf('foo'));
  608. assertEquals('Using new', 'String', jsuTrueTypeOf(new String('foo')));
  609. assertEquals('Using new', 'Function', jsuTrueTypeOf(new Function()));
  610. assertEquals('Function', jsuTrueTypeOf(function foo() {}));
  611. assertEquals('Function', jsuTrueTypeOf(testTrueTypeOf));
  612. assertEquals('RegExp', jsuTrueTypeOf(/foo/));
  613. assertEquals('Using new', 'RegExp', jsuTrueTypeOf(new RegExp('foo')));
  614. var o = {foo: 'bar'};
  615. assertEquals('Object', jsuTrueTypeOf(o));
  616. var o = new FooBarThingy();
  617. assertEquals('FooBarThingy', jsuTrueTypeOf(o));
  618. assertEquals('String', jsuTrueTypeOf(o.foo));
  619. assertEquals('String', jsuTrueTypeOf(o.bar()));
  620. assertEquals('Function', jsuTrueTypeOf(o.bar));
  621. assertEquals('Object without constructor', 'Object', jsuTrueTypeOf(appjet));
  622. });
  623. import("lib-support/runner");
  624. runTestsByDefault();