From e44a7e37b6c7b5961adaffc62b9042b8d442938e Mon Sep 17 00:00:00 2001 From: mensonge Date: Thu, 13 Nov 2008 09:49:11 +0000 Subject: New feature: basic Ajax suggestion for tags and implementation of Dojo toolkit git-svn-id: https://semanticscuttle.svn.sourceforge.net/svnroot/semanticscuttle/trunk@151 b3834d28-1941-0410-a4f8-b48e95affb8f --- includes/js/dijit/tests/i18n/test_i18n.js | 206 ++++++++++++++++++++++++++++++ 1 file changed, 206 insertions(+) create mode 100644 includes/js/dijit/tests/i18n/test_i18n.js (limited to 'includes/js/dijit/tests/i18n/test_i18n.js') diff --git a/includes/js/dijit/tests/i18n/test_i18n.js b/includes/js/dijit/tests/i18n/test_i18n.js new file mode 100644 index 0000000..bc44e63 --- /dev/null +++ b/includes/js/dijit/tests/i18n/test_i18n.js @@ -0,0 +1,206 @@ +var validateValues = []; +var formatWidgetCount = 0; +var validateWidgetCount = 0; + +function getElementsById(id){ + var result = []; + + if(!id || typeof(id) != "string"){ + return result; + } + + var ae = document.getElementsByTagName(dojo.byId(id).tagName); + for(var i = 0; i < ae.length; i++){ + if(ae[i].id == id){ + result.push(ae[i]); + } + } + return result; +} + +function getString(n){ + return n && n.toString(); +} + +function startTest(t){ + startTestFormat(t); + startTestValidate(t); +} + +function escapeEx(s){ + var result = ""; + for(var i = 0; i < s.length; i++){ + var c = s.charAt(i); + switch (c){ + case '"': + result += '\\"'; + break; + case "'": + result += "\\'"; + break; + default: + result += escape(c); + break; + } + } + return result; +} + +function getAllTestCases(){ + var allTestCases = []; + for(var i = 0; i < formatWidgetCount; i++){ + allTestCases.push({ + name: "format-" + i, + runTest: new Function("t", "startTestFormat(" + i + ", t)") + }); + } + for(var i = 0; i < validateWidgetCount; i++){ + allTestCases.push({ + name: "validate-" + i, + runTest: new Function("t", "startTestValidate(" + i + ", t)") + }); + } + return allTestCases; +} + +function startTestFormat(i, t){ + var test_node = dojo.doc.getElementById("test_display_" + i); + var exp = dojo.doc.getElementById("test_display_expected_" + i).value; + var res_node = dojo.doc.getElementById("test_display_result_" + i); + res_node.innerHTML = test_node.value; + res_node.style.backgroundColor = (test_node.value == exp) ? "#AFA" : "#FAA"; + res_node.innerHTML += " Compare (Escaped)"; + t.is(exp, test_node.value); +} + +function startTestValidate(i, t){ + /* + * The dijit.byNode has an issue: cannot handle same id. + */ + var test_node = dojo.doc.getElementById("test_validate_" + i); + var inp_node = dojo.doc.getElementById("test_validate_input_" + i); + var exp = dojo.doc.getElementById("test_validate_expected_" + i).innerHTML; + var res_node = dojo.doc.getElementById("test_validate_result_" + i); + var val_node = dojo.doc.getElementById("test_display_value_" + i); + + test_node.value = inp_node.value; + /* + * The dijit.byNode has an issue. + */ + var widget = null; + var node = test_node; + while ((widget = dijit.byNode(node)) == null){ + node = node.parentNode; + if(!node){ + break; + } + } + + if(widget){ + widget.focus(); + + var expected = validateValues[i]; + var result = widget.getValue(); + if(validateValues[i].processValue){ + expected = validateValues[i].processValue(expected); + result = validateValues[i].processValue(result); + } + var parseCorrect = getString(expected) == getString(result); + val_node.style.backgroundColor = parseCorrect ? "#AFA" : "#FAA"; + val_node.innerHTML = getString(result) + (parseCorrect ? "" : "
Expected: " + getString(expected)); + + res_node.innerHTML = widget.isValid && !widget.isValid() ? "Wrong" : "Correct"; + res_node.style.backgroundColor = res_node.innerHTML == exp ? "#AFA" : "#FAA"; + + t.is(getString(expected), getString(result)); + } +} + +function genFormatTestCase(desc, dojoType, dojoAttrs, value, expValue, comment){ + dojo.doc.write(""); + dojo.doc.write("" + desc + ""); + dojo.doc.write(""); + dojo.doc.write(""); + dojo.doc.write(""); + dojo.doc.write(""); + dojo.doc.write(""); + dojo.doc.write("" + comment + ""); + dojo.doc.write(""); + formatWidgetCount++; +} +/* +[ + {attrs: {currency: "CNY", lang: "zh-cn"}, desc: "", value:"-123456789.46", expValue: "", comment: ""}, + ... +] +*/ +function genFormatTestCases(title, dojoType, testCases){ + dojo.doc.write("

" + title + "

"); + dojo.doc.write(""); + dojo.doc.write(""); + dojo.doc.write(""); + dojo.doc.write(""); + dojo.doc.write(""); + dojo.doc.write(""); + dojo.doc.write(""); + dojo.doc.write(""); + + for(var i = 0; i < testCases.length; i++){ + var testCase = testCases[i]; + genFormatTestCase(testCase.desc, dojoType, testCase.attrs, testCase.value, testCase.expValue, testCase.comment); + } + + dojo.doc.write("
Test DescriptionTestExpectedResultComment
"); +} + +function genValidateTestCase(desc, dojoType, dojoAttrs, input, value, comment, isWrong){ + dojo.doc.write(""); + dojo.doc.write("" + desc + ""); + dojo.doc.write(""); + dojo.doc.write(""); + dojo.doc.write(""); + dojo.doc.write(""); + dojo.doc.write(""); + dojo.doc.write("" + (isWrong ? "Wrong" : "Correct") + ""); + dojo.doc.write(""); + dojo.doc.write("" + comment + ""); + dojo.doc.write(""); + validateValues.push(value); + validateWidgetCount++; +} +/* +[ + {attrs: {currency: "CNY", lang: "zh-cn"}, desc: "", value:false, expValue: "-123456789.46", comment: ""}, + ... +] +*/ +function genValidateTestCases(title, dojoType, testCases){ + dojo.doc.write("

" + title + "

"); + dojo.doc.write(""); + dojo.doc.write(""); + dojo.doc.write(""); + dojo.doc.write(""); + dojo.doc.write(""); + dojo.doc.write(""); + dojo.doc.write(""); + dojo.doc.write(""); + dojo.doc.write(""); + dojo.doc.write(""); + + for(var i = 0; i < testCases.length; i++){ + var testCase = testCases[i]; + genValidateTestCase(testCase.desc, dojoType, testCase.attrs, testCase.expValue, testCase.value, testCase.comment, testCase.isWrong); + } + + dojo.doc.write("
Test DescriptionTestInputParsed ValueExpectedResultComment
"); +} -- cgit v1.2.3