PageRenderTime 44ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/node_modules/url/node_modules/punycode/vendor/qunit/test/test.js

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