1 var validateValues = [];
2 var formatWidgetCount = 0;
3 var validateWidgetCount = 0;
5 function getElementsById(id){
8 if(!id || typeof(id) != "string"){
12 var ae = document.getElementsByTagName(dojo.byId(id).tagName);
13 for(var i = 0; i < ae.length; i++){
21 function getString(n){
22 return n && n.toString();
25 function startTest(t){
32 for(var i = 0; i < s.length; i++){
49 function getAllTestCases(){
50 var allTestCases = [];
51 for(var i = 0; i < formatWidgetCount; i++){
54 runTest: new Function("t", "startTestFormat(" + i + ", t)")
57 for(var i = 0; i < validateWidgetCount; i++){
59 name: "validate-" + i,
60 runTest: new Function("t", "startTestValidate(" + i + ", t)")
66 function startTestFormat(i, t){
67 var test_node = dojo.doc.getElementById("test_display_" + i);
68 var exp = dojo.doc.getElementById("test_display_expected_" + i).value;
69 var res_node = dojo.doc.getElementById("test_display_result_" + i);
70 res_node.innerHTML = test_node.value;
71 res_node.style.backgroundColor = (test_node.value == exp) ? "#AFA" : "#FAA";
72 res_node.innerHTML += " <a style='font-size:0.8em' href='javascript:alert(\"Expected: " + escapeEx(exp) + "\\n Result: " + escapeEx(test_node.value) + "\")'>Compare (Escaped)</a>";
73 t.is(exp, test_node.value);
76 function startTestValidate(i, t){
78 * The dijit.byNode has an issue: cannot handle same id.
80 var test_node = dojo.doc.getElementById("test_validate_" + i);
81 var inp_node = dojo.doc.getElementById("test_validate_input_" + i);
82 var exp = dojo.doc.getElementById("test_validate_expected_" + i).innerHTML;
83 var res_node = dojo.doc.getElementById("test_validate_result_" + i);
84 var val_node = dojo.doc.getElementById("test_display_value_" + i);
86 test_node.value = inp_node.value;
88 * The dijit.byNode has an issue.
92 while ((widget = dijit.byNode(node)) == null){
93 node = node.parentNode;
102 var expected = validateValues[i];
103 var result = widget.getValue();
104 if(validateValues[i].processValue){
105 expected = validateValues[i].processValue(expected);
106 result = validateValues[i].processValue(result);
108 var parseCorrect = getString(expected) == getString(result);
109 val_node.style.backgroundColor = parseCorrect ? "#AFA" : "#FAA";
110 val_node.innerHTML = getString(result) + (parseCorrect ? "" : "<br>Expected: " + getString(expected));
112 res_node.innerHTML = widget.isValid && !widget.isValid() ? "Wrong" : "Correct";
113 res_node.style.backgroundColor = res_node.innerHTML == exp ? "#AFA" : "#FAA";
115 t.is(getString(expected), getString(result));
119 function genFormatTestCase(desc, dojoType, dojoAttrs, value, expValue, comment){
120 dojo.doc.write("<tr>");
121 dojo.doc.write("<td>" + desc + "</td>");
122 dojo.doc.write("<td>");
123 dojo.doc.write("<input id='test_display_" + formatWidgetCount + "' type='text' value='" + value + "' ");
124 dojo.doc.write("dojoType='" + dojoType + "' ");
125 for(var attr in dojoAttrs){
126 dojo.doc.write(attr + "=\"" + dojoAttrs[attr] + "\" ");
128 dojo.doc.write("/>");
129 dojo.doc.write("</td>");
130 dojo.doc.write("<td><input id='test_display_expected_" + formatWidgetCount + "' value='" + expValue + "'></td>");
131 dojo.doc.write("<td id='test_display_result_" + formatWidgetCount + "'></td>");
132 dojo.doc.write("<td style='white-space:normal'>" + comment + "</td>");
133 dojo.doc.write("</tr>");
138 {attrs: {currency: "CNY", lang: "zh-cn"}, desc: "", value:"-123456789.46", expValue: "", comment: ""},
142 function genFormatTestCases(title, dojoType, testCases){
143 dojo.doc.write("<h2 class=testTitle>" + title + "</h2>");
144 dojo.doc.write("<table border=1>");
145 dojo.doc.write("<tr>");
146 dojo.doc.write("<td class=title><b>Test Description</b></td>");
147 dojo.doc.write("<td class=title><b>Test</b></td>");
148 dojo.doc.write("<td class=title><b>Expected</b></td>");
149 dojo.doc.write("<td class=title><b>Result</b></td>");
150 dojo.doc.write("<td class=title><b>Comment</b></td>");
151 dojo.doc.write("</tr>");
153 for(var i = 0; i < testCases.length; i++){
154 var testCase = testCases[i];
155 genFormatTestCase(testCase.desc, dojoType, testCase.attrs, testCase.value, testCase.expValue, testCase.comment);
158 dojo.doc.write("</table>");
161 function genValidateTestCase(desc, dojoType, dojoAttrs, input, value, comment, isWrong){
162 dojo.doc.write("<tr>");
163 dojo.doc.write("<td>" + desc + "</td>");
164 dojo.doc.write("<td>");
165 dojo.doc.write("<input id='test_validate_" + validateWidgetCount + "' type='text' ");
166 dojo.doc.write("dojoType='" + dojoType + "' ");
167 for(var attr in dojoAttrs){
168 dojo.doc.write(attr + "=\"" + dojoAttrs[attr] + "\" ");
170 dojo.doc.write("/>");
171 dojo.doc.write("</td>");
172 dojo.doc.write("<td><input id='test_validate_input_" + validateWidgetCount + "' value='" + input + "'></td>");
173 dojo.doc.write("<td id='test_display_value_" + validateWidgetCount + "'></td>");
174 dojo.doc.write("<td id='test_validate_expected_" + validateWidgetCount + "'>" + (isWrong ? "Wrong" : "Correct") + "</td>");
175 dojo.doc.write("<td id='test_validate_result_" + validateWidgetCount + "'></td>");
176 dojo.doc.write("<td style='white-space:normal'>" + comment + "</td>");
177 dojo.doc.write("</tr>");
178 validateValues.push(value);
179 validateWidgetCount++;
183 {attrs: {currency: "CNY", lang: "zh-cn"}, desc: "", value:false, expValue: "-123456789.46", comment: ""},
187 function genValidateTestCases(title, dojoType, testCases){
188 dojo.doc.write("<h2 class=testTitle>" + title + "</h2>");
189 dojo.doc.write("<table border=1>");
190 dojo.doc.write("<tr>");
191 dojo.doc.write("<td class=title><b>Test Description</b></td>");
192 dojo.doc.write("<td class=title><b>Test</b></td>");
193 dojo.doc.write("<td class=title><b>Input</b></td>");
194 dojo.doc.write("<td class=title><b>Parsed Value</b></td>");
195 dojo.doc.write("<td class=title><b>Expected</b></td>");
196 dojo.doc.write("<td class=title><b>Result</b></td>");
197 dojo.doc.write("<td class=title><b>Comment</b></td>");
198 dojo.doc.write("</tr>");
200 for(var i = 0; i < testCases.length; i++){
201 var testCase = testCases[i];
202 genValidateTestCase(testCase.desc, dojoType, testCase.attrs, testCase.expValue, testCase.value, testCase.comment, testCase.isWrong);
205 dojo.doc.write("</table>");