PageRenderTime 56ms CodeModel.GetById 31ms RepoModel.GetById 1ms app.codeStats 0ms

/history.js/vendor/qunit/test/test.js

https://bitbucket.org/Moepl/tfmforum
JavaScript | 324 lines | 278 code | 44 blank | 2 comment | 9 complexity | 095d7f5fdbf7cea6b23d8433bc24931d MD5 | raw file
Possible License(s): MIT
  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. test("sync", 2, function() {
  106. stop();
  107. setTimeout(function() {
  108. ok(true);
  109. start();
  110. }, 13);
  111. stop();
  112. setTimeout(function() {
  113. ok(true);
  114. start();
  115. }, 125);
  116. });
  117. }
  118. module("save scope", {
  119. setup: function() {
  120. this.foo = "bar";
  121. },
  122. teardown: function() {
  123. deepEqual(this.foo, "bar");
  124. }
  125. });
  126. test("scope check", function() {
  127. expect(2);
  128. deepEqual(this.foo, "bar");
  129. });
  130. module("simple testEnvironment setup", {
  131. foo: "bar",
  132. bugid: "#5311" // example of meta-data
  133. });
  134. test("scope check", function() {
  135. deepEqual(this.foo, "bar");
  136. });
  137. test("modify testEnvironment",function() {
  138. this.foo="hamster";
  139. });
  140. test("testEnvironment reset for next test",function() {
  141. deepEqual(this.foo, "bar");
  142. });
  143. module("testEnvironment with object", {
  144. options:{
  145. recipe:"soup",
  146. ingredients:["hamster","onions"]
  147. }
  148. });
  149. test("scope check", function() {
  150. deepEqual(this.options, {recipe:"soup",ingredients:["hamster","onions"]}) ;
  151. });
  152. test("modify testEnvironment",function() {
  153. // since we do a shallow copy, the testEnvironment can be modified
  154. this.options.ingredients.push("carrots");
  155. });
  156. test("testEnvironment reset for next test",function() {
  157. deepEqual(this.options, {recipe:"soup",ingredients:["hamster","onions","carrots"]}, "Is this a bug or a feature? Could do a deep copy") ;
  158. });
  159. module("testEnvironment tests");
  160. function makeurl() {
  161. var testEnv = QUnit.current_testEnvironment;
  162. var url = testEnv.url || 'http://example.com/search';
  163. var q = testEnv.q || 'a search test';
  164. return url + '?q='+encodeURIComponent(q);
  165. }
  166. test("makeurl working",function() {
  167. equal( QUnit.current_testEnvironment, this, 'The current testEnvironment is global');
  168. equal( makeurl(), 'http://example.com/search?q=a%20search%20test', 'makeurl returns a default url if nothing specified in the testEnvironment');
  169. });
  170. module("testEnvironment with makeurl settings", {
  171. url: 'http://google.com/',
  172. q: 'another_search_test'
  173. });
  174. test("makeurl working with settings from testEnvironment", function() {
  175. equal( makeurl(), 'http://google.com/?q=another_search_test', 'rather than passing arguments, we use test metadata to form the url');
  176. });
  177. test("each test can extend the module testEnvironment", {
  178. q:'hamstersoup'
  179. }, function() {
  180. equal( makeurl(), 'http://google.com/?q=hamstersoup', 'url from module, q from test');
  181. });
  182. module("jsDump");
  183. test("jsDump output", function() {
  184. equals( QUnit.jsDump.parse([1, 2]), "[\n 1,\n 2\n]" );
  185. equals( QUnit.jsDump.parse({top: 5, left: 0}), "{\n \"top\": 5,\n \"left\": 0\n}" );
  186. if (typeof document !== 'undefined' && document.getElementById("qunit-header")) {
  187. equals( QUnit.jsDump.parse(document.getElementById("qunit-header")), "<h1 id=\"qunit-header\"></h1>" );
  188. equals( QUnit.jsDump.parse(document.getElementsByTagName("h1")), "[\n <h1 id=\"qunit-header\"></h1>\n]" );
  189. }
  190. });
  191. module("assertions");
  192. test("raises",function() {
  193. function CustomError( message ) {
  194. this.message = message;
  195. }
  196. CustomError.prototype.toString = function() {
  197. return this.message;
  198. };
  199. raises(
  200. function() {
  201. throw "error"
  202. }
  203. );
  204. raises(
  205. function() {
  206. throw "error"
  207. },
  208. 'raises with just a message, no expected'
  209. );
  210. raises(
  211. function() {
  212. throw new CustomError();
  213. },
  214. CustomError,
  215. 'raised error is an instance of CustomError'
  216. );
  217. raises(
  218. function() {
  219. throw new CustomError("some error description");
  220. },
  221. /description/,
  222. "raised error message contains 'description'"
  223. );
  224. raises(
  225. function() {
  226. throw new CustomError("some error description");
  227. },
  228. function( err ) {
  229. if ( (err instanceof CustomError) && /description/.test(err) ) {
  230. return true;
  231. }
  232. },
  233. "custom validation function"
  234. );
  235. });
  236. if (typeof document !== "undefined") {
  237. module("fixture");
  238. test("setup", function() {
  239. document.getElementById("qunit-fixture").innerHTML = "foobar";
  240. });
  241. test("basics", function() {
  242. equal( document.getElementById("qunit-fixture").innerHTML, "test markup", "automatically reset" );
  243. });
  244. }
  245. module("custom assertions");
  246. (function() {
  247. function mod2(value, expected, message) {
  248. var actual = value % 2;
  249. QUnit.push(actual == expected, actual, expected, message);
  250. }
  251. test("mod2", function() {
  252. mod2(2, 0, "2 % 2 == 0");
  253. mod2(3, 1, "3 % 2 == 1");
  254. })
  255. })();
  256. (function() {
  257. var reset = QUnit.reset;
  258. function afterTest() {
  259. ok( false, "reset should not modify test status" );
  260. }
  261. module("reset");
  262. test("reset runs assertions", function() {
  263. QUnit.reset = function() {
  264. afterTest();
  265. reset.apply( this, arguments );
  266. };
  267. });
  268. test("reset runs assertions2", function() {
  269. QUnit.reset = reset;
  270. });
  271. })();
  272. module("noglobals", {
  273. teardown: function() {
  274. delete window.badGlobalVariableIntroducedInTest;
  275. }
  276. });
  277. test("let teardown clean up globals", function() {
  278. // this test will always pass if run without ?noglobals=true
  279. window.badGlobalVariableIntroducedInTest = true;
  280. });