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