Subversion Repositories Applications.papyrus

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
2150 mathias 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 escapeEx(s){
31
	var result = "";
32
	for(var i = 0; i < s.length; i++){
33
		var c = s.charAt(i);
34
		switch (c){
35
			case '"':
36
				result += '\\"';
37
				break;
38
			case "'":
39
				result += "\\'";
40
				break;
41
			default:
42
				result += escape(c);
43
				break;
44
		}
45
	}
46
	return result;
47
}
48
 
49
function getAllTestCases(){
50
	var allTestCases = [];
51
	for(var i = 0; i < formatWidgetCount; i++){
52
		allTestCases.push({
53
			name: "format-" + i,
54
			runTest: new Function("t", "startTestFormat(" + i + ", t)")
55
		});
56
	}
57
	for(var i = 0; i < validateWidgetCount; i++){
58
		allTestCases.push({
59
			name: "validate-" + i,
60
			runTest: new Function("t", "startTestValidate(" + i + ", t)")
61
		});
62
	}
63
	return allTestCases;
64
}
65
 
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);
74
}
75
 
76
function startTestValidate(i, t){
77
	/*
78
	 * The dijit.byNode has an issue: cannot handle same id.
79
	 */
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);
85
 
86
	test_node.value = inp_node.value;
87
	/*
88
	 * The dijit.byNode has an issue.
89
	 */
90
	var widget = null;
91
	var node = test_node;
92
	while ((widget = dijit.byNode(node)) == null){
93
		node = node.parentNode;
94
		if(!node){
95
			break;
96
		}
97
	}
98
 
99
	if(widget){
100
		widget.focus();
101
 
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);
107
		}
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));
111
 
112
		res_node.innerHTML = widget.isValid && !widget.isValid() ? "Wrong" : "Correct";
113
		res_node.style.backgroundColor = res_node.innerHTML == exp ? "#AFA" : "#FAA";
114
 
115
		t.is(getString(expected), getString(result));
116
	}
117
}
118
 
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] + "\" ");
127
	}
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>");
134
	formatWidgetCount++;
135
}
136
/*
137
[
138
	{attrs: {currency: "CNY", lang: "zh-cn"}, desc: "", value:"-123456789.46", expValue: "", comment: ""},
139
	...
140
]
141
*/
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>");
152
 
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);
156
	}
157
 
158
	dojo.doc.write("</table>");
159
}
160
 
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] + "\" ");
169
	}
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++;
180
}
181
/*
182
[
183
	{attrs: {currency: "CNY", lang: "zh-cn"}, desc: "", value:false, expValue: "-123456789.46", comment: ""},
184
	...
185
]
186
*/
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>");
199
 
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);
203
	}
204
 
205
	dojo.doc.write("</table>");
206
}