DOMTestCase.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. /*
  2. Copyright (c) 2001-2004 World Wide Web Consortium,
  3. (Massachusetts Institute of Technology, Institut National de
  4. Recherche en Informatique et en Automatique, Keio University). All
  5. Rights Reserved. This program is distributed under the W3C's Software
  6. Intellectual Property License. This program is distributed in the
  7. hope that it will be useful, but WITHOUT ANY WARRANTY; without even
  8. the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  9. PURPOSE.
  10. See W3C License http://www.w3.org/Consortium/Legal/ for more details.
  11. */
  12. exports.assertSize = function(descr, expected, actual) {
  13. var actualSize;
  14. assertNotNull(descr, actual);
  15. actualSize = actual.length;
  16. assertEquals(descr, expected, actualSize);
  17. }
  18. exports.assertEqualsAutoCase = function(context, descr, expected, actual) {
  19. if (builder.contentType == "text/html") {
  20. if(context == "attribute") {
  21. assertEquals(descr, expected.toLowerCase(), actual.toLowerCase());
  22. } else {
  23. assertEquals(descr, expected.toUpperCase(), actual);
  24. }
  25. } else {
  26. assertEquals(descr, expected, actual);
  27. }
  28. }
  29. exports.assertEqualsCollectionAutoCase = function(context, descr, expected, actual) {
  30. //
  31. // if they aren't the same size, they aren't equal
  32. assertEquals(descr, expected.length, actual.length);
  33. //
  34. // if there length is the same, then every entry in the expected list
  35. // must appear once and only once in the actual list
  36. var expectedLen = expected.length;
  37. var expectedValue;
  38. var actualLen = actual.length;
  39. var i;
  40. var j;
  41. var matches;
  42. for(i = 0; i < expectedLen; i++) {
  43. matches = 0;
  44. expectedValue = expected[i];
  45. for(j = 0; j < actualLen; j++) {
  46. if (builder.contentType == "text/html") {
  47. if (context == "attribute") {
  48. if (expectedValue.toLowerCase() == actual[j].toLowerCase()) {
  49. matches++;
  50. }
  51. } else {
  52. if (expectedValue.toUpperCase() == actual[j]) {
  53. matches++;
  54. }
  55. }
  56. } else {
  57. if(expectedValue == actual[j]) {
  58. matches++;
  59. }
  60. }
  61. }
  62. if(matches == 0) {
  63. throw new MjsUnitAssertionError(descr + ": No match found for " + expectedValue);
  64. }
  65. if(matches > 1) {
  66. throw new MjsUnitAssertionError(descr + ": Multiple matches found for " + expectedValue);
  67. }
  68. }
  69. }
  70. exports.assertEqualsCollection = function(test, expected, actual, descr) {
  71. //
  72. // if they aren't the same size, they aren't equal
  73. test.strictEqual(expected.length, actual.length, descr);
  74. //
  75. // if there length is the same, then every entry in the expected list
  76. // must appear once and only once in the actual list
  77. var expectedLen = expected.length;
  78. var expectedValue;
  79. var actualLen = actual.length;
  80. var i;
  81. var j;
  82. var matches;
  83. for(i = 0; i < expectedLen; i++) {
  84. matches = 0;
  85. expectedValue = expected[i];
  86. for(j = 0; j < actualLen; j++) {
  87. if(expectedValue == actual[j]) {
  88. matches++;
  89. }
  90. }
  91. test.ok(matches !== 0, 'No match found for ' + expectedValue);
  92. test.ok(matches < 2, 'To many matches found for ' + expectedValue);
  93. }
  94. }
  95. exports.assertEqualsListAutoCase = function(context, descr, expected, actual) {
  96. var minLength = expected.length;
  97. if (actual.length < minLength) {
  98. minLength = actual.length;
  99. }
  100. //
  101. for(var i = 0; i < minLength; i++) {
  102. assertEqualsAutoCase(context, descr, expected[i], actual[i]);
  103. }
  104. //
  105. // if they aren't the same size, they aren't equal
  106. assertEquals(descr, expected.length, actual.length);
  107. }
  108. exports.assertEqualsList = exports.arrayEqual = function(test, expected, actual) {
  109. //
  110. // if they aren't the same size, they aren't equal
  111. test.equal(expected.length, actual.length, "Array lengths are not the same!");
  112. var minLength = expected.length;
  113. if (actual.length < minLength) {
  114. minLength = actual.length;
  115. }
  116. //
  117. for(var i = 0; i < minLength; i++) {
  118. test.ok(expected[i] === actual[i], "Arrays not equal!" + expected[i] + ' vs ' + actual[i]);
  119. }
  120. }
  121. exports.assertInstanceOf = function(descr, type, obj) {
  122. if(type == "Attr") {
  123. assertEquals(descr,2,obj.nodeType);
  124. var specd = obj.specified;
  125. }
  126. }
  127. exports.assertSame = function(descr, expected, actual) {
  128. if(expected != actual) {
  129. assertEquals(descr, expected.nodeType, actual.nodeType);
  130. assertEquals(descr, expected.nodeValue, actual.nodeValue);
  131. }
  132. }
  133. exports.assertURIEquals = function(assertID, scheme, path, host, file, name, query, fragment, isAbsolute, actual) {
  134. //
  135. // URI must be non-null
  136. assertNotNull(assertID, actual);
  137. var uri = actual;
  138. var lastPound = actual.lastIndexOf("#");
  139. var actualFragment = "";
  140. if(lastPound != -1) {
  141. //
  142. // substring before pound
  143. //
  144. uri = actual.substring(0,lastPound);
  145. actualFragment = actual.substring(lastPound+1);
  146. }
  147. if(fragment != null) assertEquals(assertID,fragment, actualFragment);
  148. var lastQuestion = uri.lastIndexOf("?");
  149. var actualQuery = "";
  150. if(lastQuestion != -1) {
  151. //
  152. // substring before pound
  153. //
  154. uri = actual.substring(0,lastQuestion);
  155. actualQuery = actual.substring(lastQuestion+1);
  156. }
  157. if(query != null) assertEquals(assertID, query, actualQuery);
  158. var firstColon = uri.indexOf(":");
  159. var firstSlash = uri.indexOf("/");
  160. var actualPath = uri;
  161. var actualScheme = "";
  162. if(firstColon != -1 && firstColon < firstSlash) {
  163. actualScheme = uri.substring(0,firstColon);
  164. actualPath = uri.substring(firstColon + 1);
  165. }
  166. if(scheme != null) {
  167. assertEquals(assertID, scheme, actualScheme);
  168. }
  169. if(path != null) {
  170. assertEquals(assertID, path, actualPath);
  171. }
  172. if(host != null) {
  173. var actualHost = "";
  174. if(actualPath.substring(0,2) == "//") {
  175. var termSlash = actualPath.substring(2).indexOf("/") + 2;
  176. actualHost = actualPath.substring(0,termSlash);
  177. }
  178. assertEquals(assertID, host, actualHost);
  179. }
  180. if(file != null || name != null) {
  181. var actualFile = actualPath;
  182. var finalSlash = actualPath.lastIndexOf("/");
  183. if(finalSlash != -1) {
  184. actualFile = actualPath.substring(finalSlash+1);
  185. }
  186. if (file != null) {
  187. assertEquals(assertID, file, actualFile);
  188. }
  189. if (name != null) {
  190. var actualName = actualFile;
  191. var finalDot = actualFile.lastIndexOf(".");
  192. if (finalDot != -1) {
  193. actualName = actualName.substring(0, finalDot);
  194. }
  195. assertEquals(assertID, name, actualName);
  196. }
  197. }
  198. if(isAbsolute != null) {
  199. assertEquals(assertID, isAbsolute, actualPath.substring(0,1) == "/");
  200. }
  201. }
  202. // size() used by assertSize element
  203. function size(collection)
  204. {
  205. return collection.length;
  206. }
  207. function same(expected, actual)
  208. {
  209. return expected === actual;
  210. }
  211. function getSuffix(contentType) {
  212. switch(contentType) {
  213. case "text/html":
  214. return ".html";
  215. case "text/xml":
  216. return ".xml";
  217. case "application/xhtml+xml":
  218. return ".xhtml";
  219. case "image/svg+xml":
  220. return ".svg";
  221. case "text/mathml":
  222. return ".mml";
  223. }
  224. return ".html";
  225. }
  226. exports.toLowerArray = function(src) {
  227. var newArray = new Array();
  228. var i;
  229. for (i = 0; i < src.length; i++) {
  230. newArray[i] = src[i].toLowerCase();
  231. }
  232. return newArray;
  233. }
  234. exports.equalsAutoCase = function(context, expected, actual) {
  235. if (builder.contentType == "text/html") {
  236. if (context == "attribute") {
  237. return expected.toLowerCase() == actual;
  238. }
  239. return expected.toUpperCase() == actual;
  240. }
  241. return expected == actual;
  242. }