-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathplurify.tests.html
executable file
·160 lines (139 loc) · 5.29 KB
/
plurify.tests.html
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
<html>
<head>
<script type="text/javascript" src="3rd_party/json2.js"></script>
<style type="text/css">
table {
border-collapse: collapse;
}
td {
border: 1px solid black;
font-family: verdana,tahoma,arial;
font-size: 12px;
padding: 3px;
}
thead {
font-weight: bold;
}
.fail {
color: red;
}
.success {
color: green;
}
</style>
</head>
<body>
<script type="text/javascript">
var tests = [
{expectedOutput: "Missing format string."},
{input: null, arguments: null, expectedOutput: "Missing format string."},
{input: "xxx", arguments: null, expectedOutput: "xxx"},
{input: "x{0}x", arguments: ["y"], expectedOutput: "xyx"},
{input: "x{0}x{1}x{2}x", arguments: [1, 2, 3], expectedOutput: "x1x2x3x"},
{input: "{firstName} {lastName}", arguments: {firstName: "John", lastName: "Doe"}, expectedOutput: "John Doe"},
{input: "x\\{0}x", arguments: null, expectedOutput: "x{0}x"},
{input: "x\\\\{0}x", arguments: ["y"], expectedOutput: "x\\yx"},
{input: "x\\\\\\{0}x", arguments: null, expectedOutput: "x\\{0}x"},
{input: "x\\\\\\\\\\{0}x", arguments: null, expectedOutput: "x\\\\{0}x"},
{input: "{data.firstName} {data.lastName}", arguments: {data: {firstName: "John", lastName: "Doe"}}, expectedOutput: "John Doe"},
{input: "x{0.value}x", arguments: [{value: "y"}], expectedOutput: "xyx"},
{input: "x{0:toLowerCase}x", arguments: ["Y"], expectedOutput: "xyx"},
{input: "x{0:toUpperCase}x", arguments: ["y"], expectedOutput: "xYx"},
{input: "x{0:wrap}x", arguments: ["y"], expectedOutput: "x*y*x"},
{input: "{data.lastName:toUpperCase}, {data.firstName}", arguments: {data: {firstName: "John", lastName: "Doe"}}, expectedOutput: "DOE, John"},
{input: "x{ 0 }x", arguments: ["y"], expectedOutput: "xyx"},
{input: "{ firstName } { lastName }", arguments: {firstName: "John", lastName: "Doe"}, expectedOutput: "John Doe"},
{input: "x{0: toLowerCase }x", arguments: ["Y"], expectedOutput: "xyx"},
{input: "x{0", expectedOutput: "Unterminated format item: {0."},
{input: "x\\\\{0", expectedOutput: "Unterminated format item: {0."},
{input: "x{0:invalid}x", arguments: ["y"], expectedOutput: "Invalid operation: invalid."},
{input: "x{0}x", expectedOutput: "Invalid parameter name: 0."},
{input: "x{0}x", arguments: [], expectedOutput: "Invalid parameter name: 0."},
{input: "x{abc}x", arguments: {}, expectedOutput: "Invalid parameter name: abc."},
{input: "x{abc.def}x", arguments: {}, expectedOutput: "Invalid parameter name: abc.def."},
{input: "x{abc.def}x", arguments: {abc: "X"}, expectedOutput: "Invalid parameter name: abc.def."},
{input: "x{0}x", arguments: [null], expectedOutput: "xx"},
{input: "x{0:toLowerCase}x", arguments: [null], expectedOutput: "xx"}
];
function runTests(summaryId, resultsId) {
plurify.operations["wrap"] = function(x) {
return "*" + x + "*";
}
var startDate = new Date();
for(var i = 0; i < tests.length; ++i) {
var test = tests[i];
try {
test.output = plurify(test.input, test.arguments);
}
catch(e) {
test.output = e;
}
}
var duration = new Date() - startDate;
var failed = 0;
var succeeded = 0;
var resultHtml = [];
for(var i = 0; i < tests.length; ++i) {
var test = tests[i];
var success = test.expectedOutput == test.output;
if(success) {
++succeeded;
}
else {
++failed;
}
resultHtml.push("<tr class='" + (success ? "success" : "fail") + "'>");
resultHtml.push("<td>" + test.input + "</td>");
resultHtml.push("<td>" + JSON.stringify(test.arguments) + "</td>");
resultHtml.push("<td>" + test.expectedOutput + "</td>");
resultHtml.push("<td>" + test.output + "</td>");
resultHtml.push("</tr>");
}
var ieHackNode = document.createElement("div");
ieHackNode.innerHTML = "<table>" + resultHtml.join("") + "</table>";
var results = document.getElementById(resultsId);
results.parentNode.replaceChild(ieHackNode.firstChild.firstChild, results);
var summary = document.getElementById(summaryId);
summary.className = failed > 0 ? "fail" : "success";
summary.innerHTML = succeeded + " succeeded, " + failed + " failed (in " + duration + "ms)";
}
</script>
<table>
<thead>
<tr><td>"Normal"</td><td>Minified</td></tr>
</thead>
<tbody>
<tr>
<td>
<!-- "normal" version -->
<h1 id="summary"></h1>
<table>
<thead>
<tr><td>Input</td><td>Arguments</td><td>Expected Output</td><td>Output</td></tr>
</thead>
<tbody id="results"></tbody>
</table>
<script type="text/javascript" src="plurify.js"></script>
<script type="text/javascript">
runTests("summary", "results");
</script>
</td>
<td>
<!-- minified version -->
<h1 id="summary-minified"></h1>
<table>
<thead>
<tr><td>Input</td><td>Arguments</td><td>Expected Output</td><td>Output</td></tr>
</thead>
<tbody id="results-minified"></tbody>
</table>
<script type="text/javascript" src="plurify-min.js"></script>
<script type="text/javascript">
runTests("summary-minified", "results-minified");
</script>
</td>
</tr>
</tbody>
</table>
</body>
</html>