aboutsummaryrefslogtreecommitdiff
path: root/includes/js/dijit/tests/i18n/test_i18n.js
blob: bc44e6384004f0763f7a7f750bcabe89ea4807d5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
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 += " <a style='font-size:0.8em' href='javascript:alert(\"Expected: " + escapeEx(exp) + "\\n Result: " + escapeEx(test_node.value) + "\")'>Compare (Escaped)</a>";
	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 ? "" : "<br>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("<tr>");
	dojo.doc.write("<td>" + desc + "</td>");
	dojo.doc.write("<td>");
	dojo.doc.write("<input id='test_display_" + formatWidgetCount + "' type='text' value='" + value + "' ");
	dojo.doc.write("dojoType='" + dojoType + "' ");
	for(var attr in dojoAttrs){
		dojo.doc.write(attr + "=\"" + dojoAttrs[attr] + "\" ");
	}
	dojo.doc.write("/>");
	dojo.doc.write("</td>");
	dojo.doc.write("<td><input id='test_display_expected_" + formatWidgetCount + "' value='" + expValue + "'></td>");
	dojo.doc.write("<td id='test_display_result_" + formatWidgetCount + "'></td>");
	dojo.doc.write("<td style='white-space:normal'>" + comment + "</td>");
	dojo.doc.write("</tr>");
	formatWidgetCount++;
}
/*
[
	{attrs: {currency: "CNY", lang: "zh-cn"}, desc: "", value:"-123456789.46", expValue: "", comment: ""},
	...
]
*/
function genFormatTestCases(title, dojoType, testCases){
	dojo.doc.write("<h2 class=testTitle>" + title + "</h2>");
	dojo.doc.write("<table border=1>");
	dojo.doc.write("<tr>");
	dojo.doc.write("<td class=title><b>Test Description</b></td>");
	dojo.doc.write("<td class=title><b>Test</b></td>");
	dojo.doc.write("<td class=title><b>Expected</b></td>");
	dojo.doc.write("<td class=title><b>Result</b></td>");
	dojo.doc.write("<td class=title><b>Comment</b></td>");
	dojo.doc.write("</tr>");

	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("</table>");
}

function genValidateTestCase(desc, dojoType, dojoAttrs, input, value, comment, isWrong){
	dojo.doc.write("<tr>");
	dojo.doc.write("<td>" + desc + "</td>");
	dojo.doc.write("<td>");
	dojo.doc.write("<input id='test_validate_" + validateWidgetCount + "' type='text' ");
	dojo.doc.write("dojoType='" + dojoType + "' ");
	for(var attr in dojoAttrs){
		dojo.doc.write(attr + "=\"" + dojoAttrs[attr] + "\" ");
	}
	dojo.doc.write("/>");
	dojo.doc.write("</td>");
	dojo.doc.write("<td><input id='test_validate_input_" + validateWidgetCount + "' value='" + input + "'></td>");
	dojo.doc.write("<td id='test_display_value_" + validateWidgetCount + "'></td>");
	dojo.doc.write("<td id='test_validate_expected_" + validateWidgetCount + "'>" + (isWrong ? "Wrong" : "Correct") + "</td>");
	dojo.doc.write("<td id='test_validate_result_" + validateWidgetCount + "'></td>");
	dojo.doc.write("<td style='white-space:normal'>" + comment + "</td>");
	dojo.doc.write("</tr>");
	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("<h2 class=testTitle>" + title + "</h2>");
	dojo.doc.write("<table border=1>");
	dojo.doc.write("<tr>");
	dojo.doc.write("<td class=title><b>Test Description</b></td>");
	dojo.doc.write("<td class=title><b>Test</b></td>");
	dojo.doc.write("<td class=title><b>Input</b></td>");
	dojo.doc.write("<td class=title><b>Parsed Value</b></td>");
	dojo.doc.write("<td class=title><b>Expected</b></td>");
	dojo.doc.write("<td class=title><b>Result</b></td>");
	dojo.doc.write("<td class=title><b>Comment</b></td>");
	dojo.doc.write("</tr>");

	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("</table>");
}