Subversion Repositories Applications.papyrus

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
2150 mathias 1
if(!dojo._hasResource["dojox.string.tests.sprintf"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
2
dojo._hasResource["dojox.string.tests.sprintf"] = true;
3
dojo.provide("dojox.string.tests.sprintf");
4
 
5
dojo.require("dojox.string.sprintf");
6
dojo.require("dojo.string");
7
 
8
 
9
// Mapping using the %(var) format
10
 
11
// Flags:
12
//	(space): Preceeds a positive number with a blank space
13
//	+: Preceeds a positive number with a + sign
14
//	0: Pads numbers using zeroes
15
//	-: Left justify a number (they're right justified by default)
16
//	#: Alternate view for the specifier
17
 
18
tests.register("dojox.string.tests.sprintf", [
19
	{
20
		name: "Flag: (space)",
21
		runTest: function(t){
22
			var sprintf = dojox.string.sprintf;
23
 
24
			t.is(" 42", sprintf("% d", 42));
25
			t.is("-42", sprintf("% d", -42));
26
			t.is("   42", sprintf("% 5d", 42));
27
			t.is("  -42", sprintf("% 5d", -42));
28
			t.is("             42", sprintf("% 15d", 42));
29
			t.is("            -42", sprintf("% 15d", -42));
30
		}
31
	},
32
	{
33
		name: "Flag: +",
34
		runTest: function(t){
35
			var sprintf = dojox.string.sprintf;
36
 
37
			t.is("+42", sprintf("%+d", 42));
38
			t.is("-42", sprintf("%+d", -42));
39
			t.is("  +42", sprintf("%+5d", 42));
40
			t.is("  -42", sprintf("%+5d", -42));
41
			t.is("            +42", sprintf("%+15d", 42));
42
			t.is("            -42", sprintf("%+15d", -42));
43
		}
44
	},
45
	{
46
		name: "Flag: 0",
47
		runTest: function(t){
48
			var sprintf = dojox.string.sprintf;
49
 
50
			t.is("42", sprintf("%0d", 42));
51
			t.is("-42", sprintf("%0d", -42));
52
			t.is("00042", sprintf("%05d", 42));
53
			t.is("00-42", sprintf("%05d", -42));
54
			t.is("000000000000042", sprintf("%015d", 42));
55
			t.is("000000000000-42", sprintf("%015d", -42));
56
		}
57
	},
58
	{
59
		name: "Flag: -",
60
		runTest: function(t){
61
			var sprintf = dojox.string.sprintf;
62
 
63
			t.is("42", sprintf("%-d", 42));
64
			t.is("-42", sprintf("%-d", -42));
65
			t.is("42   ", sprintf("%-5d", 42));
66
			t.is("-42  ", sprintf("%-5d", -42));
67
			t.is("42             ", sprintf("%-15d", 42));
68
			t.is("-42            ", sprintf("%-15d", -42));
69
 
70
			t.is("42", sprintf("%-0d", 42));
71
			t.is("-42", sprintf("%-0d", -42));
72
			t.is("42   ", sprintf("%-05d", 42));
73
			t.is("-42  ", sprintf("%-05d", -42));
74
			t.is("42             ", sprintf("%-015d", 42));
75
			t.is("-42            ", sprintf("%-015d", -42));
76
 
77
			t.is("42", sprintf("%0-d", 42));
78
			t.is("-42", sprintf("%0-d", -42));
79
			t.is("42   ", sprintf("%0-5d", 42));
80
			t.is("-42  ", sprintf("%0-5d", -42));
81
			t.is("42             ", sprintf("%0-15d", 42));
82
			t.is("-42            ", sprintf("%0-15d", -42));
83
		}
84
	},
85
	{
86
		name: "Precision",
87
		runTest: function(t){
88
			var sprintf = dojox.string.sprintf;
89
 
90
			t.is("42", sprintf("%d", 42.8952));
91
			t.is("42", sprintf("%.2d", 42.8952)); // Note: the %d format is an int
92
			t.is("42", sprintf("%.2i", 42.8952));
93
			t.is("42.90", sprintf("%.2f", 42.8952));
94
			t.is("42.90", sprintf("%.2F", 42.8952));
95
			t.is("42.8952000000", sprintf("%.10f", 42.8952));
96
			t.is("42.90", sprintf("%1.2f", 42.8952));
97
			t.is(" 42.90", sprintf("%6.2f", 42.8952));
98
			t.is("042.90", sprintf("%06.2f", 42.8952));
99
			t.is("+42.90", sprintf("%+6.2f", 42.8952));
100
			t.is("42.8952000000", sprintf("%5.10f", 42.8952));
101
		}
102
	},
103
	{
104
		name: "Bases",
105
		runTest: function(t){
106
			var sprintf = dojox.string.sprintf;
107
 
108
			t.is("\x7f", sprintf("%c", 0x7f));
109
 
110
			var error = false;
111
			try {
112
				sprintf("%c", -100);
113
			}catch(e){
114
				t.is("invalid character code passed to %c in sprintf", e.message);
115
				error = true;
116
			}
117
			t.t(error);
118
 
119
			error = false;
120
			try {
121
				sprintf("%c", 0x200000);
122
			}catch(e){
123
				t.is("invalid character code passed to %c in sprintf", e.message);
124
				error = true;
125
			}
126
			t.t(error);
127
		}
128
	},
129
	{
130
		name: "Mapping",
131
		runTest: function(t){
132
			var sprintf = dojox.string.sprintf;
133
 
134
			// %1$s format
135
			t.is("%1$", sprintf("%1$"));
136
			t.is("%0$s", sprintf("%0$s"));
137
			t.is("Hot Pocket", sprintf("%1$s %2$s", "Hot", "Pocket"));
138
			t.is("12.0 Hot Pockets", sprintf("%1$.1f %2$s %3$ss", 12, "Hot", "Pocket"));
139
			t.is(" 42", sprintf("%1$*.f", "42", 3));
140
 
141
			error = false;
142
			try {
143
				sprintf("%2$*s", "Hot Pocket");
144
			}catch(e){
145
				t.is("got 1 printf arguments, insufficient for '%2$*s'", e.message);
146
				error = true;
147
			}
148
			t.t(error);
149
 
150
			// %(map)s format
151
			t.is("%(foo", sprintf("%(foo", {}));
152
			t.is("Hot Pocket", sprintf("%(temperature)s %(crevace)s", {
153
				temperature: "Hot",
154
				crevace: "Pocket"
155
			}));
156
			t.is("12.0 Hot Pockets", sprintf("%(quantity).1f %(temperature)s %(crevace)ss", {
157
				quantity: 12,
158
				temperature: "Hot",
159
				crevace: "Pocket"
160
			}));
161
 
162
			var error = false;
163
			try {
164
				sprintf("%(foo)s", 42);
165
			}catch(e){
166
				t.is("format requires a mapping", e.message);
167
				error = true;
168
			}
169
			t.t(error);
170
 
171
			error = false;
172
			try {
173
				sprintf("%(foo)s %(bar)s", "foo", 42);
174
			}catch(e){
175
				t.is("format requires a mapping", e.message);
176
				error = true;
177
			}
178
			t.t(error);
179
 
180
			error = false;
181
			try {
182
				sprintf("%(foo)*s", {
183
					foo: "Hot Pocket"
184
				});
185
			}catch(e){
186
				t.is("* width not supported in mapped formats", e.message);
187
				error = true;
188
			}
189
			t.t(error);
190
		}
191
	},
192
	{
193
		name: "Positionals",
194
		runTest: function(t){
195
			var sprintf = dojox.string.sprintf;
196
 
197
			t.is(" foo", sprintf("%*s", "foo", 4));
198
			t.is("      3.14", sprintf("%*.*f", 3.14159265, 10, 2));
199
			t.is("0000003.14", sprintf("%0*.*f", 3.14159265, 10, 2));
200
			t.is("3.14      ", sprintf("%-*.*f", 3.14159265, 10, 2));
201
 
202
			var error = false;
203
			try {
204
				sprintf("%*s", "foo", "bar");
205
			}catch(e){
206
				t.is("the argument for * width at position 2 is not a number in %*s", e.message);
207
				error = true;
208
			}
209
			t.t(error);
210
 
211
			error = false;
212
			try {
213
				sprintf("%10.*f", "foo", 42);
214
			}catch(e){
215
				t.is("format argument 'foo' not a float; parseFloat returned NaN", e.message);
216
				error = true;
217
			}
218
			t.t(error);
219
		}
220
	},
221
	{
222
		name: "vs. Formatter",
223
		runTest: function(t){
224
			var sprintf = dojox.string.sprintf;
225
 
226
			for(var i = 0; i < 1000; i++){
227
				sprintf("%d %s Pockets", i, "Hot");
228
			}
229
		}
230
	},
231
	{
232
		name: "Formatter",
233
		runTest: function(t){
234
			var Formatter = dojox.string.sprintf.Formatter;
235
 
236
			var str = new Formatter("%d %s Pockets");
237
			for(var i = 0; i < 1000; i++){
238
				str.format(i, "Hot");
239
			}
240
		}
241
	},
242
	{
243
		name: "Miscellaneous",
244
		runTest: function(t) {
245
			var sprintf = dojox.string.sprintf;
246
 
247
			t.is("+hello+", sprintf("+%s+", "hello"));
248
			t.is("+10+", sprintf("+%d+", 10));
249
			t.is("a", sprintf("%c", "a"));
250
			t.is('"', sprintf("%c", 34));
251
			t.is('$', sprintf("%c", 36));
252
			t.is("10", sprintf("%d", 10));
253
 
254
			var error = false;
255
			try {
256
				sprintf("%s%s", 42);
257
			}catch(e){
258
				t.is("got 1 printf arguments, insufficient for '%s%s'", e.message);
259
				error = true;
260
			}
261
			t.t(error);
262
 
263
			error = false;
264
			try {
265
				sprintf("%c");
266
			}catch(e){
267
				t.is("got 0 printf arguments, insufficient for '%c'", e.message);
268
				error = true;
269
			}
270
			t.t(error);
271
 
272
			t.is("%10", sprintf("%10", 42));
273
		}
274
	}
275
]);
276
 
277
}