PageRenderTime 55ms CodeModel.GetById 32ms RepoModel.GetById 1ms app.codeStats 0ms

/websocket/public/test/qunit/test/test.js

https://bitbucket.org/proppy/playground
JavaScript | 264 lines | 228 code | 35 blank | 1 comment | 7 complexity | 39f2a0c06b57801f11d27cacb39d7877 MD5 | raw file
Possible License(s): AGPL-3.0, GPL-2.0, Apache-2.0, GPL-3.0, 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. module("async setup test", {
  60. setup: function() {
  61. stop();
  62. setTimeout(function(){
  63. ok(true);
  64. start();
  65. }, 500);
  66. }
  67. });
  68. asyncTest("module with async setup", function() {
  69. expect(2);
  70. ok(true);
  71. start();
  72. });
  73. module("async teardown test", {
  74. teardown: function() {
  75. stop();
  76. setTimeout(function(){
  77. ok(true);
  78. start();
  79. }, 500);
  80. }
  81. });
  82. asyncTest("module with async teardown", function() {
  83. expect(2);
  84. ok(true);
  85. start();
  86. });
  87. module("asyncTest");
  88. asyncTest("asyncTest", function() {
  89. expect(2);
  90. ok(true);
  91. setTimeout(function() {
  92. state = "done";
  93. ok(true);
  94. start();
  95. }, 13);
  96. });
  97. asyncTest("asyncTest", 2, function() {
  98. ok(true);
  99. setTimeout(function() {
  100. state = "done";
  101. ok(true);
  102. start();
  103. }, 13);
  104. });
  105. }
  106. module("save scope", {
  107. setup: function() {
  108. this.foo = "bar";
  109. },
  110. teardown: function() {
  111. deepEqual(this.foo, "bar");
  112. }
  113. });
  114. test("scope check", function() {
  115. expect(2);
  116. deepEqual(this.foo, "bar");
  117. });
  118. module("simple testEnvironment setup", {
  119. foo: "bar",
  120. bugid: "#5311" // example of meta-data
  121. });
  122. test("scope check", function() {
  123. deepEqual(this.foo, "bar");
  124. });
  125. test("modify testEnvironment",function() {
  126. this.foo="hamster";
  127. });
  128. test("testEnvironment reset for next test",function() {
  129. deepEqual(this.foo, "bar");
  130. });
  131. module("testEnvironment with object", {
  132. options:{
  133. recipe:"soup",
  134. ingredients:["hamster","onions"]
  135. }
  136. });
  137. test("scope check", function() {
  138. deepEqual(this.options, {recipe:"soup",ingredients:["hamster","onions"]}) ;
  139. });
  140. test("modify testEnvironment",function() {
  141. // since we do a shallow copy, the testEnvironment can be modified
  142. this.options.ingredients.push("carrots");
  143. });
  144. test("testEnvironment reset for next test",function() {
  145. deepEqual(this.options, {recipe:"soup",ingredients:["hamster","onions","carrots"]}, "Is this a bug or a feature? Could do a deep copy") ;
  146. });
  147. module("testEnvironment tests");
  148. function makeurl() {
  149. var testEnv = QUnit.current_testEnvironment;
  150. var url = testEnv.url || 'http://example.com/search';
  151. var q = testEnv.q || 'a search test';
  152. return url + '?q='+encodeURIComponent(q);
  153. }
  154. test("makeurl working",function() {
  155. equal( QUnit.current_testEnvironment, this, 'The current testEnvironment is global');
  156. equal( makeurl(), 'http://example.com/search?q=a%20search%20test', 'makeurl returns a default url if nothing specified in the testEnvironment');
  157. });
  158. module("testEnvironment with makeurl settings", {
  159. url: 'http://google.com/',
  160. q: 'another_search_test'
  161. });
  162. test("makeurl working with settings from testEnvironment", function() {
  163. equal( makeurl(), 'http://google.com/?q=another_search_test', 'rather than passing arguments, we use test metadata to form the url');
  164. });
  165. test("each test can extend the module testEnvironment", {
  166. q:'hamstersoup'
  167. }, function() {
  168. equal( makeurl(), 'http://google.com/?q=hamstersoup', 'url from module, q from test');
  169. });
  170. module("jsDump");
  171. test("jsDump output", function() {
  172. equals( QUnit.jsDump.parse([1, 2]), "[\n 1,\n 2\n]" );
  173. equals( QUnit.jsDump.parse({top: 5, left: 0}), "{\n \"top\": 5,\n \"left\": 0\n}" );
  174. if (typeof document !== 'undefined' && document.getElementById("qunit-header")) {
  175. equals( QUnit.jsDump.parse(document.getElementById("qunit-header")), "<h1 id=\"qunit-header\"></h1>" );
  176. equals( QUnit.jsDump.parse(document.getElementsByTagName("h1")), "[\n <h1 id=\"qunit-header\"></h1>\n]" );
  177. }
  178. });
  179. module("assertions");
  180. test("raises", function() {
  181. function thrower1() {
  182. throw 'Errored!';
  183. }
  184. function thrower2() {
  185. throw new TypeError("Type!");
  186. }
  187. function thrower3() {
  188. throw {message:"Custom!"};
  189. }
  190. raises(thrower1, 'Errored!', 'throwing string');
  191. raises(thrower2, 'Type!', 'throwing TypeError instance');
  192. raises(thrower3, 'Custom!', 'throwing custom object');
  193. });
  194. if (typeof document !== "undefined") {
  195. module("fixture");
  196. test("setup", function() {
  197. document.getElementById("qunit-fixture").innerHTML = "foobar";
  198. });
  199. test("basics", function() {
  200. equal( document.getElementById("qunit-fixture").innerHTML, "test markup", "automatically reset" );
  201. });
  202. }
  203. module("custom assertions");
  204. (function() {
  205. function mod2(value, expected, message) {
  206. var actual = value % 2;
  207. QUnit.push(actual == expected, actual, expected, message);
  208. }
  209. test("mod2", function() {
  210. mod2(2, 0, "2 % 2 == 0");
  211. mod2(3, 1, "3 % 2 == 1");
  212. })
  213. })();
  214. (function() {
  215. var reset = QUnit.reset;
  216. function afterTest() {
  217. ok( false, "reset should not modify test status" );
  218. }
  219. module("reset");
  220. test("reset runs assertions", function() {
  221. QUnit.reset = function() {
  222. afterTest();
  223. reset.apply( this, arguments );
  224. };
  225. });
  226. test("reset runs assertions2", function() {
  227. QUnit.reset = reset;
  228. });
  229. })();