test_i18n.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. var validateValues = [];
  2. var formatWidgetCount = 0;
  3. var validateWidgetCount = 0;
  4. function getElementsById(id) {
  5. var result = [];
  6. if (!id || typeof(id) != "string") {
  7. return result;
  8. }
  9. var ae = document.getElementsByTagName(dojo.byId(id).tagName);
  10. for (var i = 0; i < ae.length; i++) {
  11. if (ae[i].id == id) {
  12. result.push(ae[i]);
  13. }
  14. }
  15. return result;
  16. }
  17. function getString(n) {
  18. return n && n.toString();
  19. }
  20. function startTest(t) {
  21. startTestFormat(t);
  22. startTestValidate(t);
  23. }
  24. function escapeEx(s) {
  25. var result = "";
  26. for (var i = 0; i < s.length; i++) {
  27. var c = s.charAt(i);
  28. switch (c) {
  29. case '"' :
  30. result += '\\"';
  31. break;
  32. case "'" :
  33. result += "\\'";
  34. break;
  35. default :
  36. result += escape(c);
  37. break;
  38. }
  39. }
  40. return result;
  41. }
  42. function getAllTestCases() {
  43. var allTestCases = [];
  44. for (var i = 0; i < formatWidgetCount; i++) {
  45. allTestCases.push({
  46. name : "format-" + i,
  47. runTest : new Function("t", "startTestFormat(" + i + ", t)")
  48. });
  49. }
  50. for (var i = 0; i < validateWidgetCount; i++) {
  51. allTestCases.push({
  52. name : "validate-" + i,
  53. runTest : new Function("t", "startTestValidate(" + i
  54. + ", t)")
  55. });
  56. }
  57. return allTestCases;
  58. }
  59. function startTestFormat(i, t) {
  60. var test_node = dojo.doc.getElementById("test_display_" + i);
  61. var exp = dojo.doc.getElementById("test_display_expected_" + i).value;
  62. var res_node = dojo.doc.getElementById("test_display_result_" + i);
  63. res_node.innerHTML = test_node.value;
  64. res_node.style.backgroundColor = (test_node.value == exp) ? "#AFA" : "#FAA";
  65. res_node.innerHTML += " <a style='font-size:0.8em' href='javascript:alert(\"Expected: "
  66. + escapeEx(exp)
  67. + "\\n Result: "
  68. + escapeEx(test_node.value)
  69. + "\")'>Compare (Escaped)</a>";
  70. t.is(exp, test_node.value);
  71. }
  72. function startTestValidate(i, t) {
  73. /*
  74. * The dijit.byNode has an issue: cannot handle same id.
  75. */
  76. var test_node = dojo.doc.getElementById("test_validate_" + i);
  77. var inp_node = dojo.doc.getElementById("test_validate_input_" + i);
  78. var exp = dojo.doc.getElementById("test_validate_expected_" + i).innerHTML;
  79. var res_node = dojo.doc.getElementById("test_validate_result_" + i);
  80. var val_node = dojo.doc.getElementById("test_display_value_" + i);
  81. test_node.value = inp_node.value;
  82. /*
  83. * The dijit.byNode has an issue.
  84. */
  85. var widget = null;
  86. var node = test_node;
  87. while ((widget = dijit.byNode(node)) == null) {
  88. node = node.parentNode;
  89. if (!node) {
  90. break;
  91. }
  92. }
  93. if (widget) {
  94. widget.focus();
  95. var expected = validateValues[i];
  96. var result = widget.getValue();
  97. if (validateValues[i].processValue) {
  98. expected = validateValues[i].processValue(expected);
  99. result = validateValues[i].processValue(result);
  100. }
  101. var parseCorrect = getString(expected) == getString(result);
  102. val_node.style.backgroundColor = parseCorrect ? "#AFA" : "#FAA";
  103. val_node.innerHTML = getString(result)
  104. + (parseCorrect ? "" : "<br>Expected: " + getString(expected));
  105. res_node.innerHTML = widget.isValid && !widget.isValid()
  106. ? "Wrong"
  107. : "Correct";
  108. res_node.style.backgroundColor = res_node.innerHTML == exp
  109. ? "#AFA"
  110. : "#FAA";
  111. t.is(getString(expected), getString(result));
  112. }
  113. }
  114. function genFormatTestCase(desc, dojoType, dojoAttrs, value, expValue, comment) {
  115. dojo.doc.write("<tr>");
  116. dojo.doc.write("<td>" + desc + "</td>");
  117. dojo.doc.write("<td>");
  118. dojo.doc.write("<input id='test_display_" + formatWidgetCount
  119. + "' type='text' value='" + value + "' ");
  120. dojo.doc.write("dojoType='" + dojoType + "' ");
  121. for (var attr in dojoAttrs) {
  122. dojo.doc.write(attr + "=\"" + dojoAttrs[attr] + "\" ");
  123. }
  124. dojo.doc.write("/>");
  125. dojo.doc.write("</td>");
  126. dojo.doc.write("<td><input id='test_display_expected_" + formatWidgetCount
  127. + "' value='" + expValue + "'></td>");
  128. dojo.doc.write("<td id='test_display_result_" + formatWidgetCount
  129. + "'></td>");
  130. dojo.doc.write("<td style='white-space:normal'>" + comment + "</td>");
  131. dojo.doc.write("</tr>");
  132. formatWidgetCount++;
  133. }
  134. /*
  135. * [ {attrs: {currency: "CNY", lang: "zh-cn"}, desc: "", value:"-123456789.46",
  136. * expValue: "", comment: ""}, ... ]
  137. */
  138. function genFormatTestCases(title, dojoType, testCases) {
  139. dojo.doc.write("<h2 class=testTitle>" + title + "</h2>");
  140. dojo.doc.write("<table border=1>");
  141. dojo.doc.write("<tr>");
  142. dojo.doc.write("<td class=title><b>Test Description</b></td>");
  143. dojo.doc.write("<td class=title><b>Test</b></td>");
  144. dojo.doc.write("<td class=title><b>Expected</b></td>");
  145. dojo.doc.write("<td class=title><b>Result</b></td>");
  146. dojo.doc.write("<td class=title><b>Comment</b></td>");
  147. dojo.doc.write("</tr>");
  148. for (var i = 0; i < testCases.length; i++) {
  149. var testCase = testCases[i];
  150. genFormatTestCase(testCase.desc, dojoType, testCase.attrs,
  151. testCase.value, testCase.expValue, testCase.comment);
  152. }
  153. dojo.doc.write("</table>");
  154. }
  155. function genValidateTestCase(desc, dojoType, dojoAttrs, input, value, comment,
  156. isWrong) {
  157. dojo.doc.write("<tr>");
  158. dojo.doc.write("<td>" + desc + "</td>");
  159. dojo.doc.write("<td>");
  160. dojo.doc.write("<input id='test_validate_" + validateWidgetCount
  161. + "' type='text' ");
  162. dojo.doc.write("dojoType='" + dojoType + "' ");
  163. for (var attr in dojoAttrs) {
  164. dojo.doc.write(attr + "=\"" + dojoAttrs[attr] + "\" ");
  165. }
  166. dojo.doc.write("/>");
  167. dojo.doc.write("</td>");
  168. dojo.doc.write("<td><input id='test_validate_input_" + validateWidgetCount
  169. + "' value='" + input + "'></td>");
  170. dojo.doc.write("<td id='test_display_value_" + validateWidgetCount
  171. + "'></td>");
  172. dojo.doc.write("<td id='test_validate_expected_" + validateWidgetCount
  173. + "'>" + (isWrong ? "Wrong" : "Correct") + "</td>");
  174. dojo.doc.write("<td id='test_validate_result_" + validateWidgetCount
  175. + "'></td>");
  176. dojo.doc.write("<td style='white-space:normal'>" + comment + "</td>");
  177. dojo.doc.write("</tr>");
  178. validateValues.push(value);
  179. validateWidgetCount++;
  180. }
  181. /*
  182. * [ {attrs: {currency: "CNY", lang: "zh-cn"}, desc: "", value:false, expValue:
  183. * "-123456789.46", comment: ""}, ... ]
  184. */
  185. function genValidateTestCases(title, dojoType, testCases) {
  186. dojo.doc.write("<h2 class=testTitle>" + title + "</h2>");
  187. dojo.doc.write("<table border=1>");
  188. dojo.doc.write("<tr>");
  189. dojo.doc.write("<td class=title><b>Test Description</b></td>");
  190. dojo.doc.write("<td class=title><b>Test</b></td>");
  191. dojo.doc.write("<td class=title><b>Input</b></td>");
  192. dojo.doc.write("<td class=title><b>Parsed Value</b></td>");
  193. dojo.doc.write("<td class=title><b>Expected</b></td>");
  194. dojo.doc.write("<td class=title><b>Result</b></td>");
  195. dojo.doc.write("<td class=title><b>Comment</b></td>");
  196. dojo.doc.write("</tr>");
  197. for (var i = 0; i < testCases.length; i++) {
  198. var testCase = testCases[i];
  199. genValidateTestCase(testCase.desc, dojoType, testCase.attrs,
  200. testCase.expValue, testCase.value, testCase.comment,
  201. testCase.isWrong);
  202. }
  203. dojo.doc.write("</table>");
  204. }