PageRenderTime 51ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/root/static/lib/qunit/test/test.js

https://bitbucket.org/metabrainz/musicbrainz-server
JavaScript | 602 lines | 482 code | 106 blank | 14 comment | 26 complexity | 5f7fb5518e9765a217321d42cc31a019 MD5 | raw file
Possible License(s): GPL-2.0, AGPL-1.0, CC-BY-3.0, CC0-1.0
  1. test("module without setup/teardown (default)", function() {
  2. expect(1);
  3. ok(true);
  4. });
  5. test("expect in test", 3, function() {
  6. ok(true);
  7. ok(true);
  8. ok(true);
  9. });
  10. test("expect in test", 1, function() {
  11. ok(true);
  12. });
  13. test("expect query and multiple issue", function() {
  14. expect(2);
  15. ok(true);
  16. var expected = expect();
  17. equal(expected, 2);
  18. expect(expected + 1);
  19. ok(true);
  20. });
  21. QUnit.module("assertion helpers");
  22. QUnit.test( "QUnit.assert compatibility", function( assert ) {
  23. QUnit.expect(4);
  24. assert.ok( true, "Calling method on `assert` argument to test() callback" );
  25. // Should also work, although not documented
  26. QUnit.assert.ok( true, "Calling method on QUnit.assert object" );
  27. // Test compatibility aliases
  28. QUnit.ok( true, "Calling aliased method in QUnit root object" );
  29. ok( true, "Calling aliased function in global namespace" );
  30. });
  31. module("setup test", {
  32. setup: function() {
  33. ok(true);
  34. }
  35. });
  36. test("module with setup", function() {
  37. expect(2);
  38. ok(true);
  39. });
  40. test("module with setup, expect in test call", 2, function() {
  41. ok(true);
  42. });
  43. var state;
  44. module("setup/teardown test", {
  45. setup: function() {
  46. state = true;
  47. ok(true);
  48. x = 1;
  49. },
  50. teardown: function() {
  51. ok(true);
  52. // can introduce and delete globals in setup/teardown
  53. // without noglobals sounding the alarm
  54. delete x;
  55. }
  56. });
  57. test("module with setup/teardown", function() {
  58. expect(3);
  59. ok(true);
  60. });
  61. module("setup/teardown test 2");
  62. test("module without setup/teardown", function() {
  63. expect(1);
  64. ok(true);
  65. });
  66. var orgDate;
  67. module("Date test", {
  68. setup: function() {
  69. orgDate = Date;
  70. Date = function () {
  71. ok( false, 'QUnit should internally be independant from Date-related manipulation and testing' );
  72. return new orgDate();
  73. };
  74. },
  75. teardown: function() {
  76. Date = orgDate;
  77. }
  78. });
  79. test("sample test for Date test", function () {
  80. expect(1);
  81. ok(true);
  82. });
  83. if (typeof setTimeout !== 'undefined') {
  84. state = 'fail';
  85. module("teardown and stop", {
  86. teardown: function() {
  87. equal(state, "done", "Test teardown.");
  88. }
  89. });
  90. test("teardown must be called after test ended", function() {
  91. expect(1);
  92. stop();
  93. setTimeout(function() {
  94. state = "done";
  95. start();
  96. }, 13);
  97. });
  98. test("parameter passed to stop increments semaphore n times", function() {
  99. expect(1);
  100. stop(3);
  101. setTimeout(function() {
  102. state = "not enough starts";
  103. start(), start();
  104. }, 13);
  105. setTimeout(function() {
  106. state = "done";
  107. start();
  108. }, 15);
  109. });
  110. test("parameter passed to start decrements semaphore n times", function() {
  111. expect(1);
  112. stop(), stop(), stop();
  113. setTimeout(function() {
  114. state = "done";
  115. start(3);
  116. }, 18);
  117. });
  118. module("async setup test", {
  119. setup: function() {
  120. stop();
  121. setTimeout(function(){
  122. ok(true);
  123. start();
  124. }, 500);
  125. }
  126. });
  127. asyncTest("module with async setup", function() {
  128. expect(2);
  129. ok(true);
  130. start();
  131. });
  132. module("async teardown test", {
  133. teardown: function() {
  134. stop();
  135. setTimeout(function(){
  136. ok(true);
  137. start();
  138. }, 500);
  139. }
  140. });
  141. asyncTest("module with async teardown", function() {
  142. expect(2);
  143. ok(true);
  144. start();
  145. });
  146. module("asyncTest");
  147. asyncTest("asyncTest", function() {
  148. expect(2);
  149. ok(true);
  150. setTimeout(function() {
  151. state = "done";
  152. ok(true);
  153. start();
  154. }, 13);
  155. });
  156. asyncTest("asyncTest", 2, function() {
  157. ok(true);
  158. setTimeout(function() {
  159. state = "done";
  160. ok(true);
  161. start();
  162. }, 13);
  163. });
  164. test("sync", 2, function() {
  165. stop();
  166. setTimeout(function() {
  167. ok(true);
  168. start();
  169. }, 13);
  170. stop();
  171. setTimeout(function() {
  172. ok(true);
  173. start();
  174. }, 125);
  175. });
  176. test("test synchronous calls to stop", 2, function() {
  177. stop();
  178. setTimeout(function(){
  179. ok(true, 'first');
  180. start();
  181. stop();
  182. setTimeout(function(){
  183. ok(true, 'second');
  184. start();
  185. }, 150);
  186. }, 150);
  187. });
  188. }
  189. module("save scope", {
  190. setup: function() {
  191. this.foo = "bar";
  192. },
  193. teardown: function() {
  194. deepEqual(this.foo, "bar");
  195. }
  196. });
  197. test("scope check", function() {
  198. expect(2);
  199. deepEqual(this.foo, "bar");
  200. });
  201. module("simple testEnvironment setup", {
  202. foo: "bar",
  203. bugid: "#5311" // example of meta-data
  204. });
  205. test("scope check", function() {
  206. deepEqual(this.foo, "bar");
  207. });
  208. test("modify testEnvironment",function() {
  209. expect(0);
  210. this.foo="hamster";
  211. });
  212. test("testEnvironment reset for next test",function() {
  213. deepEqual(this.foo, "bar");
  214. });
  215. module("testEnvironment with object", {
  216. options:{
  217. recipe:"soup",
  218. ingredients:["hamster","onions"]
  219. }
  220. });
  221. test("scope check", function() {
  222. deepEqual(this.options, {recipe:"soup",ingredients:["hamster","onions"]}) ;
  223. });
  224. test("modify testEnvironment",function() {
  225. expect(0);
  226. // since we do a shallow copy, the testEnvironment can be modified
  227. this.options.ingredients.push("carrots");
  228. });
  229. test("testEnvironment reset for next test",function() {
  230. deepEqual(this.options, {recipe:"soup",ingredients:["hamster","onions","carrots"]}, "Is this a bug or a feature? Could do a deep copy") ;
  231. });
  232. module("testEnvironment tests");
  233. function makeurl() {
  234. var testEnv = QUnit.current_testEnvironment;
  235. var url = testEnv.url || 'http://example.com/search';
  236. var q = testEnv.q || 'a search test';
  237. return url + '?q='+encodeURIComponent(q);
  238. }
  239. test("makeurl working",function() {
  240. equal( QUnit.current_testEnvironment, this, 'The current testEnvironment is global');
  241. equal( makeurl(), 'http://example.com/search?q=a%20search%20test', 'makeurl returns a default url if nothing specified in the testEnvironment');
  242. });
  243. module("testEnvironment with makeurl settings", {
  244. url: 'http://google.com/',
  245. q: 'another_search_test'
  246. });
  247. test("makeurl working with settings from testEnvironment", function() {
  248. equal( makeurl(), 'http://google.com/?q=another_search_test', 'rather than passing arguments, we use test metadata to from the url');
  249. });
  250. module("jsDump");
  251. test("jsDump output", function() {
  252. equal( QUnit.jsDump.parse([1, 2]), "[\n 1,\n 2\n]" );
  253. equal( QUnit.jsDump.parse({top: 5, left: 0}), "{\n \"left\": 0,\n \"top\": 5\n}" );
  254. if (typeof document !== 'undefined' && document.getElementById("qunit-header")) {
  255. equal( QUnit.jsDump.parse(document.getElementById("qunit-header")), "<h1 id=\"qunit-header\"></h1>" );
  256. equal( QUnit.jsDump.parse(document.getElementsByTagName("h1")), "[\n <h1 id=\"qunit-header\"></h1>\n]" );
  257. }
  258. });
  259. module("assertions");
  260. test("raises",function() {
  261. function CustomError( message ) {
  262. this.message = message;
  263. }
  264. CustomError.prototype.toString = function() {
  265. return this.message;
  266. };
  267. throws(
  268. function() {
  269. throw "my error"
  270. }
  271. );
  272. throws(
  273. function() {
  274. throw "my error"
  275. },
  276. "simple string throw, no 'expected' value given"
  277. );
  278. throws(
  279. function() {
  280. throw new CustomError();
  281. },
  282. CustomError,
  283. 'thrown error is an instance of CustomError'
  284. );
  285. throws(
  286. function() {
  287. throw new CustomError("some error description");
  288. },
  289. /description/,
  290. "use a regex to match against the stringified error"
  291. );
  292. throws(
  293. function() {
  294. throw new CustomError("some error description");
  295. },
  296. function( err ) {
  297. if ( (err instanceof CustomError) && /description/.test(err) ) {
  298. return true;
  299. }
  300. },
  301. "custom validation function"
  302. );
  303. throws(
  304. function() {
  305. ( window.execScript || function( data ) {
  306. window["eval"].call( window, data );
  307. })( "throw 'error'" );
  308. },
  309. 'globally-executed errors caught'
  310. );
  311. this.CustomError = CustomError;
  312. throws(
  313. function() {
  314. throw new this.CustomError("some error description");
  315. },
  316. /description/,
  317. "throw error from property of 'this' context"
  318. );
  319. raises(
  320. function() {
  321. throw "error"
  322. },
  323. "simple throw, asserting with deprecated raises() function"
  324. );
  325. });
  326. if (typeof document !== "undefined") {
  327. module("fixture");
  328. test("setup", function() {
  329. expect(0);
  330. document.getElementById("qunit-fixture").innerHTML = "foobar";
  331. });
  332. test("basics", function() {
  333. equal( document.getElementById("qunit-fixture").innerHTML, "test markup", "automatically reset" );
  334. });
  335. test("running test name displayed", function() {
  336. expect(2);
  337. var displaying = document.getElementById("qunit-testresult");
  338. ok( /running test name displayed/.test(displaying.innerHTML), "Expect test name to be found in displayed text" );
  339. ok( /fixture/.test(displaying.innerHTML), "Expect module name to be found in displayed text" );
  340. });
  341. }
  342. module("custom assertions");
  343. (function() {
  344. function mod2(value, expected, message) {
  345. var actual = value % 2;
  346. QUnit.push(actual == expected, actual, expected, message);
  347. }
  348. test("mod2", function() {
  349. mod2(2, 0, "2 % 2 == 0");
  350. mod2(3, 1, "3 % 2 == 1");
  351. });
  352. })();
  353. module("recursions");
  354. function Wrap(x) {
  355. this.wrap = x;
  356. if (x == undefined) this.first = true;
  357. }
  358. function chainwrap(depth, first, prev) {
  359. depth = depth || 0;
  360. var last = prev || new Wrap();
  361. first = first || last;
  362. if (depth == 1) {
  363. first.wrap = last;
  364. }
  365. if (depth > 1) {
  366. last = chainwrap(depth-1, first, new Wrap(last));
  367. }
  368. return last;
  369. }
  370. test("check jsDump recursion", function() {
  371. expect(4);
  372. var noref = chainwrap(0);
  373. var nodump = QUnit.jsDump.parse(noref);
  374. equal(nodump, '{\n "first": true,\n "wrap": undefined\n}');
  375. var selfref = chainwrap(1);
  376. var selfdump = QUnit.jsDump.parse(selfref);
  377. equal(selfdump, '{\n "first": true,\n "wrap": recursion(-1)\n}');
  378. var parentref = chainwrap(2);
  379. var parentdump = QUnit.jsDump.parse(parentref);
  380. equal(parentdump, '{\n "wrap": {\n "first": true,\n "wrap": recursion(-2)\n }\n}');
  381. var circref = chainwrap(10);
  382. var circdump = QUnit.jsDump.parse(circref);
  383. ok(new RegExp("recursion\\(-10\\)").test(circdump), "(" +circdump + ") should show -10 recursion level");
  384. });
  385. test("check (deep-)equal recursion", function() {
  386. var noRecursion = chainwrap(0);
  387. equal(noRecursion, noRecursion, "I should be equal to me.");
  388. deepEqual(noRecursion, noRecursion, "... and so in depth.");
  389. var selfref = chainwrap(1);
  390. equal(selfref, selfref, "Even so if I nest myself.");
  391. deepEqual(selfref, selfref, "... into the depth.");
  392. var circref = chainwrap(10);
  393. equal(circref, circref, "Or hide that through some levels of indirection.");
  394. deepEqual(circref, circref, "... and checked on all levels!");
  395. });
  396. test('Circular reference with arrays', function() {
  397. // pure array self-ref
  398. var arr = [];
  399. arr.push(arr);
  400. var arrdump = QUnit.jsDump.parse(arr);
  401. equal(arrdump, '[\n recursion(-1)\n]');
  402. equal(arr, arr[0], 'no endless stack when trying to dump arrays with circular ref');
  403. // mix obj-arr circular ref
  404. var obj = {};
  405. var childarr = [obj];
  406. obj.childarr = childarr;
  407. var objdump = QUnit.jsDump.parse(obj);
  408. var childarrdump = QUnit.jsDump.parse(childarr);
  409. equal(objdump, '{\n "childarr": [\n recursion(-2)\n ]\n}');
  410. equal(childarrdump, '[\n {\n "childarr": recursion(-2)\n }\n]');
  411. equal(obj.childarr, childarr, 'no endless stack when trying to dump array/object mix with circular ref');
  412. equal(childarr[0], obj, 'no endless stack when trying to dump array/object mix with circular ref');
  413. });
  414. test('Circular reference - test reported by soniciq in #105', function() {
  415. var MyObject = function() {};
  416. MyObject.prototype.parent = function(obj) {
  417. if (obj === undefined) { return this._parent; }
  418. this._parent = obj;
  419. };
  420. MyObject.prototype.children = function(obj) {
  421. if (obj === undefined) { return this._children; }
  422. this._children = obj;
  423. };
  424. var a = new MyObject(),
  425. b = new MyObject();
  426. var barr = [b];
  427. a.children(barr);
  428. b.parent(a);
  429. equal(a.children(), barr);
  430. deepEqual(a.children(), [b]);
  431. });
  432. (function() {
  433. var reset = QUnit.reset;
  434. module("reset");
  435. test("reset runs assertions", function() {
  436. expect(0);
  437. QUnit.reset = function() {
  438. ok( false, "reset should not modify test status" );
  439. reset.apply( this, arguments );
  440. };
  441. });
  442. test("reset runs assertions, cleanup", function() {
  443. expect(0);
  444. QUnit.reset = reset;
  445. });
  446. })();
  447. if (typeof setTimeout !== 'undefined') {
  448. function testAfterDone(){
  449. var testName = "ensure has correct number of assertions";
  450. function secondAfterDoneTest(){
  451. QUnit.config.done = [];
  452. //QUnit.done = function(){};
  453. //because when this does happen, the assertion count parameter doesn't actually
  454. //work we use this test to check the assertion count.
  455. module("check previous test's assertion counts");
  456. test('count previous two test\'s assertions', function(){
  457. var spans = document.getElementsByTagName('span'),
  458. tests = [],
  459. countNodes;
  460. //find these two tests
  461. for (var i = 0; i < spans.length; i++) {
  462. if (spans[i].innerHTML.indexOf(testName) !== -1) {
  463. tests.push(spans[i]);
  464. }
  465. }
  466. //walk dom to counts
  467. countNodes = tests[0].nextSibling.nextSibling.getElementsByTagName('b');
  468. equal(countNodes[1].innerHTML, "99");
  469. countNodes = tests[1].nextSibling.nextSibling.getElementsByTagName('b');
  470. equal(countNodes[1].innerHTML, "99");
  471. });
  472. }
  473. QUnit.config.done = [];
  474. QUnit.done(secondAfterDoneTest);
  475. module("Synchronous test after load of page");
  476. asyncTest('Async test', function(){
  477. start();
  478. for (var i = 1; i < 100; i++) {
  479. ok(i);
  480. }
  481. });
  482. test(testName, 99, function(){
  483. for (var i = 1; i < 100; i++) {
  484. ok(i);
  485. }
  486. });
  487. //we need two of these types of tests in order to ensure that assertions
  488. //don't move between tests.
  489. test(testName + ' 2', 99, function(){
  490. for (var i = 1; i < 100; i++) {
  491. ok(i);
  492. }
  493. });
  494. }
  495. QUnit.done(testAfterDone);
  496. }