Test.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /***
  2. MochiKit.Test 1.4
  3. See <http://mochikit.com/> for documentation, downloads, license, etc.
  4. (c) 2005 Bob Ippolito. All rights Reserved.
  5. ***/
  6. if (typeof(dojo) != 'undefined') {
  7. dojo.provide('MochiKit.Test');
  8. dojo.require('MochiKit.Base');
  9. }
  10. if (typeof(JSAN) != 'undefined') {
  11. JSAN.use("MochiKit.Base", []);
  12. }
  13. try {
  14. if (typeof(MochiKit.Base) == 'undefined') {
  15. throw "";
  16. }
  17. } catch (e) {
  18. throw "MochiKit.Test depends on MochiKit.Base!";
  19. }
  20. if (typeof(MochiKit.Test) == 'undefined') {
  21. MochiKit.Test = {};
  22. }
  23. MochiKit.Test.NAME = "MochiKit.Test";
  24. MochiKit.Test.VERSION = "1.4";
  25. MochiKit.Test.__repr__ = function () {
  26. return "[" + this.NAME + " " + this.VERSION + "]";
  27. };
  28. MochiKit.Test.toString = function () {
  29. return this.__repr__();
  30. };
  31. MochiKit.Test.EXPORT = ["runTests"];
  32. MochiKit.Test.EXPORT_OK = [];
  33. MochiKit.Test.runTests = function (obj) {
  34. if (typeof(obj) == "string") {
  35. obj = JSAN.use(obj);
  36. }
  37. var suite = new MochiKit.Test.Suite();
  38. suite.run(obj);
  39. };
  40. MochiKit.Test.Suite = function () {
  41. this.testIndex = 0;
  42. MochiKit.Base.bindMethods(this);
  43. };
  44. MochiKit.Test.Suite.prototype = {
  45. run: function (obj) {
  46. try {
  47. obj(this);
  48. } catch (e) {
  49. this.traceback(e);
  50. }
  51. },
  52. traceback: function (e) {
  53. var items = MochiKit.Iter.sorted(MochiKit.Base.items(e));
  54. print("not ok " + this.testIndex + " - Error thrown");
  55. for (var i = 0; i < items.length; i++) {
  56. var kv = items[i];
  57. if (kv[0] == "stack") {
  58. kv[1] = kv[1].split(/\n/)[0];
  59. }
  60. this.print("# " + kv.join(": "));
  61. }
  62. },
  63. print: function (s) {
  64. print(s);
  65. },
  66. is: function (got, expected, /* optional */message) {
  67. var res = 1;
  68. var msg = null;
  69. try {
  70. res = MochiKit.Base.compare(got, expected);
  71. } catch (e) {
  72. msg = "Can not compare " + typeof(got) + ":" + typeof(expected);
  73. }
  74. if (res) {
  75. msg = "Expected value did not compare equal";
  76. }
  77. if (!res) {
  78. return this.testResult(true, message);
  79. }
  80. return this.testResult(false, message,
  81. [[msg], ["got:", got], ["expected:", expected]]);
  82. },
  83. testResult: function (pass, msg, failures) {
  84. this.testIndex += 1;
  85. if (pass) {
  86. this.print("ok " + this.testIndex + " - " + msg);
  87. return;
  88. }
  89. this.print("not ok " + this.testIndex + " - " + msg);
  90. if (failures) {
  91. for (var i = 0; i < failures.length; i++) {
  92. this.print("# " + failures[i].join(" "));
  93. }
  94. }
  95. },
  96. isDeeply: function (got, expected, /* optional */message) {
  97. var m = MochiKit.Base;
  98. var res = 1;
  99. try {
  100. res = m.compare(got, expected);
  101. } catch (e) {
  102. // pass
  103. }
  104. if (res === 0) {
  105. return this.ok(true, message);
  106. }
  107. var gk = m.keys(got);
  108. var ek = m.keys(expected);
  109. gk.sort();
  110. ek.sort();
  111. if (m.compare(gk, ek)) {
  112. // differing keys
  113. var cmp = {};
  114. var i;
  115. for (i = 0; i < gk.length; i++) {
  116. cmp[gk[i]] = "got";
  117. }
  118. for (i = 0; i < ek.length; i++) {
  119. if (ek[i] in cmp) {
  120. delete cmp[ek[i]];
  121. } else {
  122. cmp[ek[i]] = "expected";
  123. }
  124. }
  125. var diffkeys = m.keys(cmp);
  126. diffkeys.sort();
  127. var gotkeys = [];
  128. var expkeys = [];
  129. while (diffkeys.length) {
  130. var k = diffkeys.shift();
  131. if (k in Object.prototype) {
  132. continue;
  133. }
  134. (cmp[k] == "got" ? gotkeys : expkeys).push(k);
  135. }
  136. }
  137. return this.testResult((!res), msg,
  138. (msg ? [["got:", got], ["expected:", expected]] : undefined)
  139. );
  140. },
  141. ok: function (res, message) {
  142. return this.testResult(res, message);
  143. }
  144. };
  145. MochiKit.Test.__new__ = function () {
  146. var m = MochiKit.Base;
  147. this.EXPORT_TAGS = {
  148. ":common": this.EXPORT,
  149. ":all": m.concat(this.EXPORT, this.EXPORT_OK)
  150. };
  151. m.nameFunctions(this);
  152. };
  153. MochiKit.Test.__new__();
  154. MochiKit.Base._exportSymbols(this, MochiKit.Test);