Software /
code /
prosody
Annotate
spec/util_format_spec.lua @ 12036:2ce06f788093
util.format: Fix some formats expecting positive numbers in Lua 5.2
Amazing how string.format behaves differently under each Lua version
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Sat, 11 Dec 2021 20:54:37 +0100 |
parent | 12034:ee94ac51b2dd |
child | 12039:e0a8c5b1ab4f |
rev | line source |
---|---|
8383 | 1 local format = require "util.format".format; |
12034 | 2 -- There are eight basic types in Lua: |
3 -- nil, boolean, number, string, function, userdata, thread, and table | |
8383 | 4 |
5 describe("util.format", function() | |
6 describe("#format()", function() | |
7 it("should work", function() | |
8619
b96b0141cb61
util.format: Fix tests to have expected value first
Kim Alvefur <zash@zash.se>
parents:
8383
diff
changeset
|
8 assert.equal("hello", format("%s", "hello")); |
11644
fc1b8fe94d04
util.format: Change formatting of nil values to avoid looking like XML
Kim Alvefur <zash@zash.se>
parents:
11638
diff
changeset
|
9 assert.equal("(nil)", format("%s")); |
fc1b8fe94d04
util.format: Change formatting of nil values to avoid looking like XML
Kim Alvefur <zash@zash.se>
parents:
11638
diff
changeset
|
10 assert.equal("(nil)", format("%d")); |
fc1b8fe94d04
util.format: Change formatting of nil values to avoid looking like XML
Kim Alvefur <zash@zash.se>
parents:
11638
diff
changeset
|
11 assert.equal("(nil)", format("%q")); |
fc1b8fe94d04
util.format: Change formatting of nil values to avoid looking like XML
Kim Alvefur <zash@zash.se>
parents:
11638
diff
changeset
|
12 assert.equal(" [(nil)]", format("", nil)); |
8619
b96b0141cb61
util.format: Fix tests to have expected value first
Kim Alvefur <zash@zash.se>
parents:
8383
diff
changeset
|
13 assert.equal("true", format("%s", true)); |
b96b0141cb61
util.format: Fix tests to have expected value first
Kim Alvefur <zash@zash.se>
parents:
8383
diff
changeset
|
14 assert.equal("[true]", format("%d", true)); |
b96b0141cb61
util.format: Fix tests to have expected value first
Kim Alvefur <zash@zash.se>
parents:
8383
diff
changeset
|
15 assert.equal("% [true]", format("%%", true)); |
9693
6ed0d6224d64
util.format: Serialize values for the %q format
Kim Alvefur <zash@zash.se>
parents:
9656
diff
changeset
|
16 assert.equal("{ }", format("%q", { })); |
10034
4fca92d60040
util.format: Handle formats expecting an integer in Lua 5.3+ (fixes #1371)
Kim Alvefur <zash@zash.se>
parents:
9693
diff
changeset
|
17 assert.equal("[1.5]", format("%d", 1.5)); |
10035
386f085820e6
util.format: Handle integer formats the same way on Lua versions without integer support
Kim Alvefur <zash@zash.se>
parents:
10034
diff
changeset
|
18 assert.equal("[7.3786976294838e+19]", format("%d", 73786976294838206464)); |
8383 | 19 end); |
11638
5f4a657136bc
util.format: Escape ASCII control characters in output
Kim Alvefur <zash@zash.se>
parents:
10035
diff
changeset
|
20 |
5f4a657136bc
util.format: Escape ASCII control characters in output
Kim Alvefur <zash@zash.se>
parents:
10035
diff
changeset
|
21 it("escapes ascii control stuff", function () |
5f4a657136bc
util.format: Escape ASCII control characters in output
Kim Alvefur <zash@zash.se>
parents:
10035
diff
changeset
|
22 assert.equal("␁", format("%s", "\1")); |
12032
3db09eb4c43b
util.format: Ensure sanitation of strings passed to wrong format
Kim Alvefur <zash@zash.se>
parents:
12031
diff
changeset
|
23 assert.equal("[␁]", format("%d", "\1")); |
11638
5f4a657136bc
util.format: Escape ASCII control characters in output
Kim Alvefur <zash@zash.se>
parents:
10035
diff
changeset
|
24 end); |
5f4a657136bc
util.format: Escape ASCII control characters in output
Kim Alvefur <zash@zash.se>
parents:
10035
diff
changeset
|
25 |
12031
87bc26f23d9b
util.format: Escape invalid UTF-8 by passing trough serialization
Kim Alvefur <zash@zash.se>
parents:
11644
diff
changeset
|
26 it("escapes invalid UTF-8", function () |
87bc26f23d9b
util.format: Escape invalid UTF-8 by passing trough serialization
Kim Alvefur <zash@zash.se>
parents:
11644
diff
changeset
|
27 assert.equal("\"Hello w\\195rld\"", format("%s", "Hello w\195rld")); |
87bc26f23d9b
util.format: Escape invalid UTF-8 by passing trough serialization
Kim Alvefur <zash@zash.se>
parents:
11644
diff
changeset
|
28 end); |
87bc26f23d9b
util.format: Escape invalid UTF-8 by passing trough serialization
Kim Alvefur <zash@zash.se>
parents:
11644
diff
changeset
|
29 |
12033
161f8268c4b3
util.format: Also handle the %p format added in Lua 5.4
Kim Alvefur <zash@zash.se>
parents:
12032
diff
changeset
|
30 if _VERSION >= "Lua 5.4" then |
161f8268c4b3
util.format: Also handle the %p format added in Lua 5.4
Kim Alvefur <zash@zash.se>
parents:
12032
diff
changeset
|
31 it("handles %p formats", function () |
161f8268c4b3
util.format: Also handle the %p format added in Lua 5.4
Kim Alvefur <zash@zash.se>
parents:
12032
diff
changeset
|
32 assert.matches("a 0x%x+ b", format("%s %p %s", "a", {}, "b")); |
161f8268c4b3
util.format: Also handle the %p format added in Lua 5.4
Kim Alvefur <zash@zash.se>
parents:
12032
diff
changeset
|
33 end) |
161f8268c4b3
util.format: Also handle the %p format added in Lua 5.4
Kim Alvefur <zash@zash.se>
parents:
12032
diff
changeset
|
34 else |
161f8268c4b3
util.format: Also handle the %p format added in Lua 5.4
Kim Alvefur <zash@zash.se>
parents:
12032
diff
changeset
|
35 it("does something with %p formats", function () |
161f8268c4b3
util.format: Also handle the %p format added in Lua 5.4
Kim Alvefur <zash@zash.se>
parents:
12032
diff
changeset
|
36 assert.string(format("%p", {})); |
161f8268c4b3
util.format: Also handle the %p format added in Lua 5.4
Kim Alvefur <zash@zash.se>
parents:
12032
diff
changeset
|
37 end) |
161f8268c4b3
util.format: Also handle the %p format added in Lua 5.4
Kim Alvefur <zash@zash.se>
parents:
12032
diff
changeset
|
38 end |
12034 | 39 |
40 -- Tests generated with loops! | |
41 describe("nil", function () | |
42 describe("to %c", function () | |
43 it("works", function () | |
44 assert.equal("(nil)", format("%c", nil)) | |
45 end); | |
46 end); | |
47 | |
48 describe("to %d", function () | |
49 it("works", function () | |
50 assert.equal("(nil)", format("%d", nil)) | |
51 end); | |
52 end); | |
53 | |
54 describe("to %i", function () | |
55 it("works", function () | |
56 assert.equal("(nil)", format("%i", nil)) | |
57 end); | |
58 end); | |
59 | |
60 describe("to %o", function () | |
61 it("works", function () | |
62 assert.equal("(nil)", format("%o", nil)) | |
63 end); | |
64 end); | |
65 | |
66 describe("to %u", function () | |
67 it("works", function () | |
68 assert.equal("(nil)", format("%u", nil)) | |
69 end); | |
70 end); | |
71 | |
72 describe("to %x", function () | |
73 it("works", function () | |
74 assert.equal("(nil)", format("%x", nil)) | |
75 end); | |
76 end); | |
77 | |
78 describe("to %X", function () | |
79 it("works", function () | |
80 assert.equal("(nil)", format("%X", nil)) | |
81 end); | |
82 end); | |
83 | |
84 describe("to %a", function () | |
85 it("works", function () | |
86 assert.equal("(nil)", format("%a", nil)) | |
87 end); | |
88 end); | |
89 | |
90 describe("to %A", function () | |
91 it("works", function () | |
92 assert.equal("(nil)", format("%A", nil)) | |
93 end); | |
94 end); | |
95 | |
96 describe("to %e", function () | |
97 it("works", function () | |
98 assert.equal("(nil)", format("%e", nil)) | |
99 end); | |
100 end); | |
101 | |
102 describe("to %E", function () | |
103 it("works", function () | |
104 assert.equal("(nil)", format("%E", nil)) | |
105 end); | |
106 end); | |
107 | |
108 describe("to %f", function () | |
109 it("works", function () | |
110 assert.equal("(nil)", format("%f", nil)) | |
111 end); | |
112 end); | |
113 | |
114 describe("to %g", function () | |
115 it("works", function () | |
116 assert.equal("(nil)", format("%g", nil)) | |
117 end); | |
118 end); | |
119 | |
120 describe("to %G", function () | |
121 it("works", function () | |
122 assert.equal("(nil)", format("%G", nil)) | |
123 end); | |
124 end); | |
125 | |
126 describe("to %q", function () | |
127 it("works", function () | |
128 assert.equal("(nil)", format("%q", nil)) | |
129 end); | |
130 end); | |
131 | |
132 describe("to %s", function () | |
133 it("works", function () | |
134 assert.equal("(nil)", format("%s", nil)) | |
135 end); | |
136 end); | |
137 | |
138 end); | |
139 | |
140 describe("boolean", function () | |
141 describe("to %c", function () | |
142 it("works", function () | |
143 assert.equal("[true]", format("%c", true)) | |
144 assert.equal("[false]", format("%c", false)) | |
145 end); | |
146 end); | |
147 | |
148 describe("to %d", function () | |
149 it("works", function () | |
150 assert.equal("[true]", format("%d", true)) | |
151 assert.equal("[false]", format("%d", false)) | |
152 end); | |
153 end); | |
154 | |
155 describe("to %i", function () | |
156 it("works", function () | |
157 assert.equal("[true]", format("%i", true)) | |
158 assert.equal("[false]", format("%i", false)) | |
159 end); | |
160 end); | |
161 | |
162 describe("to %o", function () | |
163 it("works", function () | |
164 assert.equal("[true]", format("%o", true)) | |
165 assert.equal("[false]", format("%o", false)) | |
166 end); | |
167 end); | |
168 | |
169 describe("to %u", function () | |
170 it("works", function () | |
171 assert.equal("[true]", format("%u", true)) | |
172 assert.equal("[false]", format("%u", false)) | |
173 end); | |
174 end); | |
175 | |
176 describe("to %x", function () | |
177 it("works", function () | |
178 assert.equal("[true]", format("%x", true)) | |
179 assert.equal("[false]", format("%x", false)) | |
180 end); | |
181 end); | |
182 | |
183 describe("to %X", function () | |
184 it("works", function () | |
185 assert.equal("[true]", format("%X", true)) | |
186 assert.equal("[false]", format("%X", false)) | |
187 end); | |
188 end); | |
189 | |
190 describe("to %a", function () | |
191 it("works", function () | |
192 assert.equal("[true]", format("%a", true)) | |
193 assert.equal("[false]", format("%a", false)) | |
194 end); | |
195 end); | |
196 | |
197 describe("to %A", function () | |
198 it("works", function () | |
199 assert.equal("[true]", format("%A", true)) | |
200 assert.equal("[false]", format("%A", false)) | |
201 end); | |
202 end); | |
203 | |
204 describe("to %e", function () | |
205 it("works", function () | |
206 assert.equal("[true]", format("%e", true)) | |
207 assert.equal("[false]", format("%e", false)) | |
208 end); | |
209 end); | |
210 | |
211 describe("to %E", function () | |
212 it("works", function () | |
213 assert.equal("[true]", format("%E", true)) | |
214 assert.equal("[false]", format("%E", false)) | |
215 end); | |
216 end); | |
217 | |
218 describe("to %f", function () | |
219 it("works", function () | |
220 assert.equal("[true]", format("%f", true)) | |
221 assert.equal("[false]", format("%f", false)) | |
222 end); | |
223 end); | |
224 | |
225 describe("to %g", function () | |
226 it("works", function () | |
227 assert.equal("[true]", format("%g", true)) | |
228 assert.equal("[false]", format("%g", false)) | |
229 end); | |
230 end); | |
231 | |
232 describe("to %G", function () | |
233 it("works", function () | |
234 assert.equal("[true]", format("%G", true)) | |
235 assert.equal("[false]", format("%G", false)) | |
236 end); | |
237 end); | |
238 | |
239 describe("to %q", function () | |
240 it("works", function () | |
241 assert.equal("true", format("%q", true)) | |
242 assert.equal("false", format("%q", false)) | |
243 end); | |
244 end); | |
245 | |
246 describe("to %s", function () | |
247 it("works", function () | |
248 assert.equal("true", format("%s", true)) | |
249 assert.equal("false", format("%s", false)) | |
250 end); | |
251 end); | |
252 | |
253 end); | |
254 | |
255 describe("number", function () | |
256 describe("to %c", function () | |
257 it("works", function () | |
258 assert.equal("a", format("%c", 97)) | |
259 assert.equal("[1.5]", format("%c", 1.5)) | |
260 assert.equal("[7.3786976294838e+19]", format("%c", 73786976294838206464)) | |
261 assert.equal("[inf]", format("%c", math.huge)) | |
262 end); | |
263 end); | |
264 | |
265 describe("to %d", function () | |
266 it("works", function () | |
267 assert.equal("97", format("%d", 97)) | |
268 assert.equal("-12345", format("%d", -12345)) | |
269 assert.equal("[1.5]", format("%d", 1.5)) | |
270 assert.equal("[7.3786976294838e+19]", format("%d", 73786976294838206464)) | |
271 assert.equal("[inf]", format("%d", math.huge)) | |
272 assert.equal("2147483647", format("%d", 2147483647)) | |
273 end); | |
274 end); | |
275 | |
276 describe("to %i", function () | |
277 it("works", function () | |
278 assert.equal("97", format("%i", 97)) | |
279 assert.equal("-12345", format("%i", -12345)) | |
280 assert.equal("[1.5]", format("%i", 1.5)) | |
281 assert.equal("[7.3786976294838e+19]", format("%i", 73786976294838206464)) | |
282 assert.equal("[inf]", format("%i", math.huge)) | |
283 assert.equal("2147483647", format("%i", 2147483647)) | |
284 end); | |
285 end); | |
286 | |
287 describe("to %o", function () | |
288 it("works", function () | |
289 assert.equal("141", format("%o", 97)) | |
12036
2ce06f788093
util.format: Fix some formats expecting positive numbers in Lua 5.2
Kim Alvefur <zash@zash.se>
parents:
12034
diff
changeset
|
290 assert.equal("[-12345]", format("%o", -12345)) |
12034 | 291 assert.equal("[1.5]", format("%o", 1.5)) |
292 assert.equal("[7.3786976294838e+19]", format("%o", 73786976294838206464)) | |
293 assert.equal("[inf]", format("%o", math.huge)) | |
294 assert.equal("17777777777", format("%o", 2147483647)) | |
295 end); | |
296 end); | |
297 | |
298 describe("to %u", function () | |
299 it("works", function () | |
300 assert.equal("97", format("%u", 97)) | |
12036
2ce06f788093
util.format: Fix some formats expecting positive numbers in Lua 5.2
Kim Alvefur <zash@zash.se>
parents:
12034
diff
changeset
|
301 assert.equal("[-12345]", format("%u", -12345)) |
12034 | 302 assert.equal("[1.5]", format("%u", 1.5)) |
303 assert.equal("[7.3786976294838e+19]", format("%u", 73786976294838206464)) | |
304 assert.equal("[inf]", format("%u", math.huge)) | |
305 assert.equal("2147483647", format("%u", 2147483647)) | |
306 end); | |
307 end); | |
308 | |
309 describe("to %x", function () | |
310 it("works", function () | |
311 assert.equal("61", format("%x", 97)) | |
12036
2ce06f788093
util.format: Fix some formats expecting positive numbers in Lua 5.2
Kim Alvefur <zash@zash.se>
parents:
12034
diff
changeset
|
312 assert.equal("[-12345]", format("%x", -12345)) |
12034 | 313 assert.equal("[1.5]", format("%x", 1.5)) |
314 assert.equal("[7.3786976294838e+19]", format("%x", 73786976294838206464)) | |
315 assert.equal("[inf]", format("%x", math.huge)) | |
316 assert.equal("7fffffff", format("%x", 2147483647)) | |
317 end); | |
318 end); | |
319 | |
320 describe("to %X", function () | |
321 it("works", function () | |
322 assert.equal("61", format("%X", 97)) | |
12036
2ce06f788093
util.format: Fix some formats expecting positive numbers in Lua 5.2
Kim Alvefur <zash@zash.se>
parents:
12034
diff
changeset
|
323 assert.equal("[-12345]", format("%X", -12345)) |
12034 | 324 assert.equal("[1.5]", format("%X", 1.5)) |
325 assert.equal("[7.3786976294838e+19]", format("%X", 73786976294838206464)) | |
326 assert.equal("[inf]", format("%X", math.huge)) | |
327 assert.equal("7FFFFFFF", format("%X", 2147483647)) | |
328 end); | |
329 end); | |
330 | |
331 if _VERSION > "Lua 5.1" then -- COMPAT no %a or %A in Lua 5.1 | |
332 describe("to %a", function () | |
333 it("works", function () | |
334 assert.equal("0x1.84p+6", format("%a", 97)) | |
335 assert.equal("-0x1.81c8p+13", format("%a", -12345)) | |
336 assert.equal("0x1.8p+0", format("%a", 1.5)) | |
337 assert.equal("0x1p+66", format("%a", 73786976294838206464)) | |
338 assert.equal("inf", format("%a", math.huge)) | |
339 assert.equal("0x1.fffffffcp+30", format("%a", 2147483647)) | |
340 end); | |
341 end); | |
342 | |
343 describe("to %A", function () | |
344 it("works", function () | |
345 assert.equal("0X1.84P+6", format("%A", 97)) | |
346 assert.equal("-0X1.81C8P+13", format("%A", -12345)) | |
347 assert.equal("0X1.8P+0", format("%A", 1.5)) | |
348 assert.equal("0X1P+66", format("%A", 73786976294838206464)) | |
349 assert.equal("INF", format("%A", math.huge)) | |
350 assert.equal("0X1.FFFFFFFCP+30", format("%A", 2147483647)) | |
351 end); | |
352 end); | |
353 end | |
354 | |
355 describe("to %e", function () | |
356 it("works", function () | |
357 assert.equal("9.700000e+01", format("%e", 97)) | |
358 assert.equal("-1.234500e+04", format("%e", -12345)) | |
359 assert.equal("1.500000e+00", format("%e", 1.5)) | |
360 assert.equal("7.378698e+19", format("%e", 73786976294838206464)) | |
361 assert.equal("inf", format("%e", math.huge)) | |
362 assert.equal("2.147484e+09", format("%e", 2147483647)) | |
363 end); | |
364 end); | |
365 | |
366 describe("to %E", function () | |
367 it("works", function () | |
368 assert.equal("9.700000E+01", format("%E", 97)) | |
369 assert.equal("-1.234500E+04", format("%E", -12345)) | |
370 assert.equal("1.500000E+00", format("%E", 1.5)) | |
371 assert.equal("7.378698E+19", format("%E", 73786976294838206464)) | |
372 assert.equal("INF", format("%E", math.huge)) | |
373 assert.equal("2.147484E+09", format("%E", 2147483647)) | |
374 end); | |
375 end); | |
376 | |
377 describe("to %f", function () | |
378 it("works", function () | |
379 assert.equal("97.000000", format("%f", 97)) | |
380 assert.equal("-12345.000000", format("%f", -12345)) | |
381 assert.equal("1.500000", format("%f", 1.5)) | |
382 assert.equal("73786976294838206464.000000", format("%f", 73786976294838206464)) | |
383 assert.equal("inf", format("%f", math.huge)) | |
384 assert.equal("2147483647.000000", format("%f", 2147483647)) | |
385 end); | |
386 end); | |
387 | |
388 describe("to %g", function () | |
389 it("works", function () | |
390 assert.equal("97", format("%g", 97)) | |
391 assert.equal("-12345", format("%g", -12345)) | |
392 assert.equal("1.5", format("%g", 1.5)) | |
393 assert.equal("7.3787e+19", format("%g", 73786976294838206464)) | |
394 assert.equal("inf", format("%g", math.huge)) | |
395 assert.equal("2.14748e+09", format("%g", 2147483647)) | |
396 end); | |
397 end); | |
398 | |
399 describe("to %G", function () | |
400 it("works", function () | |
401 assert.equal("97", format("%G", 97)) | |
402 assert.equal("-12345", format("%G", -12345)) | |
403 assert.equal("1.5", format("%G", 1.5)) | |
404 assert.equal("7.3787E+19", format("%G", 73786976294838206464)) | |
405 assert.equal("INF", format("%G", math.huge)) | |
406 assert.equal("2.14748E+09", format("%G", 2147483647)) | |
407 end); | |
408 end); | |
409 | |
410 describe("to %q", function () | |
411 it("works", function () | |
412 assert.equal("97", format("%q", 97)) | |
413 assert.equal("-12345", format("%q", -12345)) | |
414 assert.equal("1.5", format("%q", 1.5)) | |
415 assert.equal("7.37869762948382065e+19", format("%q", 73786976294838206464)) | |
416 assert.equal("(1/0)", format("%q", math.huge)) | |
417 assert.equal("2147483647", format("%q", 2147483647)) | |
418 end); | |
419 end); | |
420 | |
421 describe("to %s", function () | |
422 it("works", function () | |
423 assert.equal("97", format("%s", 97)) | |
424 assert.equal("-12345", format("%s", -12345)) | |
425 assert.equal("1.5", format("%s", 1.5)) | |
426 assert.equal("7.3786976294838e+19", format("%s", 73786976294838206464)) | |
427 assert.equal("inf", format("%s", math.huge)) | |
428 assert.equal("2147483647", format("%s", 2147483647)) | |
429 end); | |
430 end); | |
431 | |
432 end); | |
433 | |
434 describe("string", function () | |
435 describe("to %c", function () | |
436 it("works", function () | |
437 assert.equal("[hello]", format("%c", "hello")) | |
438 assert.equal("[foo \226\144\129\226\144\130\226\144\131 bar]", format("%c", "foo \001\002\003 bar")) | |
439 assert.equal("[nödåtgärd]", format("%c", "n\195\182d\195\165tg\195\164rd")) | |
440 assert.equal("\"n\\195\\182d\\195\\165tg\\195\"", format("%c", "n\195\182d\195\165tg\195")) | |
441 end); | |
442 end); | |
443 | |
444 describe("to %d", function () | |
445 it("works", function () | |
446 assert.equal("[hello]", format("%d", "hello")) | |
447 assert.equal("[foo \226\144\129\226\144\130\226\144\131 bar]", format("%d", "foo \001\002\003 bar")) | |
448 assert.equal("[nödåtgärd]", format("%d", "n\195\182d\195\165tg\195\164rd")) | |
449 assert.equal("\"n\\195\\182d\\195\\165tg\\195\"", format("%d", "n\195\182d\195\165tg\195")) | |
450 end); | |
451 end); | |
452 | |
453 describe("to %i", function () | |
454 it("works", function () | |
455 assert.equal("[hello]", format("%i", "hello")) | |
456 assert.equal("[foo \226\144\129\226\144\130\226\144\131 bar]", format("%i", "foo \001\002\003 bar")) | |
457 assert.equal("[nödåtgärd]", format("%i", "n\195\182d\195\165tg\195\164rd")) | |
458 assert.equal("\"n\\195\\182d\\195\\165tg\\195\"", format("%i", "n\195\182d\195\165tg\195")) | |
459 end); | |
460 end); | |
461 | |
462 describe("to %o", function () | |
463 it("works", function () | |
464 assert.equal("[hello]", format("%o", "hello")) | |
465 assert.equal("[foo \226\144\129\226\144\130\226\144\131 bar]", format("%o", "foo \001\002\003 bar")) | |
466 assert.equal("[nödåtgärd]", format("%o", "n\195\182d\195\165tg\195\164rd")) | |
467 assert.equal("\"n\\195\\182d\\195\\165tg\\195\"", format("%o", "n\195\182d\195\165tg\195")) | |
468 end); | |
469 end); | |
470 | |
471 describe("to %u", function () | |
472 it("works", function () | |
473 assert.equal("[hello]", format("%u", "hello")) | |
474 assert.equal("[foo \226\144\129\226\144\130\226\144\131 bar]", format("%u", "foo \001\002\003 bar")) | |
475 assert.equal("[nödåtgärd]", format("%u", "n\195\182d\195\165tg\195\164rd")) | |
476 assert.equal("\"n\\195\\182d\\195\\165tg\\195\"", format("%u", "n\195\182d\195\165tg\195")) | |
477 end); | |
478 end); | |
479 | |
480 describe("to %x", function () | |
481 it("works", function () | |
482 assert.equal("[hello]", format("%x", "hello")) | |
483 assert.equal("[foo \226\144\129\226\144\130\226\144\131 bar]", format("%x", "foo \001\002\003 bar")) | |
484 assert.equal("[nödåtgärd]", format("%x", "n\195\182d\195\165tg\195\164rd")) | |
485 assert.equal("\"n\\195\\182d\\195\\165tg\\195\"", format("%x", "n\195\182d\195\165tg\195")) | |
486 end); | |
487 end); | |
488 | |
489 describe("to %X", function () | |
490 it("works", function () | |
491 assert.equal("[hello]", format("%X", "hello")) | |
492 assert.equal("[foo \226\144\129\226\144\130\226\144\131 bar]", format("%X", "foo \001\002\003 bar")) | |
493 assert.equal("[nödåtgärd]", format("%X", "n\195\182d\195\165tg\195\164rd")) | |
494 assert.equal("\"n\\195\\182d\\195\\165tg\\195\"", format("%X", "n\195\182d\195\165tg\195")) | |
495 end); | |
496 end); | |
497 | |
498 describe("to %a", function () | |
499 it("works", function () | |
500 assert.equal("[hello]", format("%a", "hello")) | |
501 assert.equal("[foo \226\144\129\226\144\130\226\144\131 bar]", format("%a", "foo \001\002\003 bar")) | |
502 assert.equal("[nödåtgärd]", format("%a", "n\195\182d\195\165tg\195\164rd")) | |
503 assert.equal("\"n\\195\\182d\\195\\165tg\\195\"", format("%a", "n\195\182d\195\165tg\195")) | |
504 end); | |
505 end); | |
506 | |
507 describe("to %A", function () | |
508 it("works", function () | |
509 assert.equal("[hello]", format("%A", "hello")) | |
510 assert.equal("[foo \226\144\129\226\144\130\226\144\131 bar]", format("%A", "foo \001\002\003 bar")) | |
511 assert.equal("[nödåtgärd]", format("%A", "n\195\182d\195\165tg\195\164rd")) | |
512 assert.equal("\"n\\195\\182d\\195\\165tg\\195\"", format("%A", "n\195\182d\195\165tg\195")) | |
513 end); | |
514 end); | |
515 | |
516 describe("to %e", function () | |
517 it("works", function () | |
518 assert.equal("[hello]", format("%e", "hello")) | |
519 assert.equal("[foo \226\144\129\226\144\130\226\144\131 bar]", format("%e", "foo \001\002\003 bar")) | |
520 assert.equal("[nödåtgärd]", format("%e", "n\195\182d\195\165tg\195\164rd")) | |
521 assert.equal("\"n\\195\\182d\\195\\165tg\\195\"", format("%e", "n\195\182d\195\165tg\195")) | |
522 end); | |
523 end); | |
524 | |
525 describe("to %E", function () | |
526 it("works", function () | |
527 assert.equal("[hello]", format("%E", "hello")) | |
528 assert.equal("[foo \226\144\129\226\144\130\226\144\131 bar]", format("%E", "foo \001\002\003 bar")) | |
529 assert.equal("[nödåtgärd]", format("%E", "n\195\182d\195\165tg\195\164rd")) | |
530 assert.equal("\"n\\195\\182d\\195\\165tg\\195\"", format("%E", "n\195\182d\195\165tg\195")) | |
531 end); | |
532 end); | |
533 | |
534 describe("to %f", function () | |
535 it("works", function () | |
536 assert.equal("[hello]", format("%f", "hello")) | |
537 assert.equal("[foo \226\144\129\226\144\130\226\144\131 bar]", format("%f", "foo \001\002\003 bar")) | |
538 assert.equal("[nödåtgärd]", format("%f", "n\195\182d\195\165tg\195\164rd")) | |
539 assert.equal("\"n\\195\\182d\\195\\165tg\\195\"", format("%f", "n\195\182d\195\165tg\195")) | |
540 end); | |
541 end); | |
542 | |
543 describe("to %g", function () | |
544 it("works", function () | |
545 assert.equal("[hello]", format("%g", "hello")) | |
546 assert.equal("[foo \226\144\129\226\144\130\226\144\131 bar]", format("%g", "foo \001\002\003 bar")) | |
547 assert.equal("[nödåtgärd]", format("%g", "n\195\182d\195\165tg\195\164rd")) | |
548 assert.equal("\"n\\195\\182d\\195\\165tg\\195\"", format("%g", "n\195\182d\195\165tg\195")) | |
549 end); | |
550 end); | |
551 | |
552 describe("to %G", function () | |
553 it("works", function () | |
554 assert.equal("[hello]", format("%G", "hello")) | |
555 assert.equal("[foo \226\144\129\226\144\130\226\144\131 bar]", format("%G", "foo \001\002\003 bar")) | |
556 assert.equal("[nödåtgärd]", format("%G", "n\195\182d\195\165tg\195\164rd")) | |
557 assert.equal("\"n\\195\\182d\\195\\165tg\\195\"", format("%G", "n\195\182d\195\165tg\195")) | |
558 end); | |
559 end); | |
560 | |
561 describe("to %q", function () | |
562 it("works", function () | |
563 assert.equal("\"hello\"", format("%q", "hello")) | |
564 assert.equal("\"foo \226\144\129\226\144\130\226\144\131 bar\"", format("%q", "foo \001\002\003 bar")) | |
565 assert.equal("\"nödåtgärd\"", format("%q", "n\195\182d\195\165tg\195\164rd")) | |
566 assert.equal("\"n\\195\\182d\\195\\165tg\\195\"", format("%q", "n\195\182d\195\165tg\195")) | |
567 end); | |
568 end); | |
569 | |
570 describe("to %s", function () | |
571 it("works", function () | |
572 assert.equal("hello", format("%s", "hello")) | |
573 assert.equal("foo \226\144\129\226\144\130\226\144\131 bar", format("%s", "foo \001\002\003 bar")) | |
574 assert.equal("nödåtgärd", format("%s", "n\195\182d\195\165tg\195\164rd")) | |
575 assert.equal("\"n\\195\\182d\\195\\165tg\\195\"", format("%s", "n\195\182d\195\165tg\195")) | |
576 end); | |
577 end); | |
578 | |
579 end); | |
580 | |
581 describe("function", function () | |
582 describe("to %c", function () | |
583 it("works", function () | |
584 assert.matches("[function: 0[xX]%x+]", format("%c", function() end)) | |
585 end); | |
586 end); | |
587 | |
588 describe("to %d", function () | |
589 it("works", function () | |
590 assert.matches("[function: 0[xX]%x+]", format("%d", function() end)) | |
591 end); | |
592 end); | |
593 | |
594 describe("to %i", function () | |
595 it("works", function () | |
596 assert.matches("[function: 0[xX]%x+]", format("%i", function() end)) | |
597 end); | |
598 end); | |
599 | |
600 describe("to %o", function () | |
601 it("works", function () | |
602 assert.matches("[function: 0[xX]%x+]", format("%o", function() end)) | |
603 end); | |
604 end); | |
605 | |
606 describe("to %u", function () | |
607 it("works", function () | |
608 assert.matches("[function: 0[xX]%x+]", format("%u", function() end)) | |
609 end); | |
610 end); | |
611 | |
612 describe("to %x", function () | |
613 it("works", function () | |
614 assert.matches("[function: 0[xX]%x+]", format("%x", function() end)) | |
615 end); | |
616 end); | |
617 | |
618 describe("to %X", function () | |
619 it("works", function () | |
620 assert.matches("[function: 0[xX]%x+]", format("%X", function() end)) | |
621 end); | |
622 end); | |
623 | |
624 describe("to %a", function () | |
625 it("works", function () | |
626 assert.matches("[function: 0[xX]%x+]", format("%a", function() end)) | |
627 end); | |
628 end); | |
629 | |
630 describe("to %A", function () | |
631 it("works", function () | |
632 assert.matches("[function: 0[xX]%x+]", format("%A", function() end)) | |
633 end); | |
634 end); | |
635 | |
636 describe("to %e", function () | |
637 it("works", function () | |
638 assert.matches("[function: 0[xX]%x+]", format("%e", function() end)) | |
639 end); | |
640 end); | |
641 | |
642 describe("to %E", function () | |
643 it("works", function () | |
644 assert.matches("[function: 0[xX]%x+]", format("%E", function() end)) | |
645 end); | |
646 end); | |
647 | |
648 describe("to %f", function () | |
649 it("works", function () | |
650 assert.matches("[function: 0[xX]%x+]", format("%f", function() end)) | |
651 end); | |
652 end); | |
653 | |
654 describe("to %g", function () | |
655 it("works", function () | |
656 assert.matches("[function: 0[xX]%x+]", format("%g", function() end)) | |
657 end); | |
658 end); | |
659 | |
660 describe("to %G", function () | |
661 it("works", function () | |
662 assert.matches("[function: 0[xX]%x+]", format("%G", function() end)) | |
663 end); | |
664 end); | |
665 | |
666 describe("to %q", function () | |
667 it("works", function () | |
668 assert.matches('{__type="function",__error="fail"}', format("%q", function() end)) | |
669 end); | |
670 end); | |
671 | |
672 describe("to %s", function () | |
673 it("works", function () | |
674 assert.matches("function: 0[xX]%x+", format("%s", function() end)) | |
675 end); | |
676 end); | |
677 | |
678 end); | |
679 | |
680 describe("thread", function () | |
681 describe("to %c", function () | |
682 it("works", function () | |
683 assert.matches("[thread: 0[xX]%x+]", format("%c", coroutine.create(function() end))) | |
684 end); | |
685 end); | |
686 | |
687 describe("to %d", function () | |
688 it("works", function () | |
689 assert.matches("[thread: 0[xX]%x+]", format("%d", coroutine.create(function() end))) | |
690 end); | |
691 end); | |
692 | |
693 describe("to %i", function () | |
694 it("works", function () | |
695 assert.matches("[thread: 0[xX]%x+]", format("%i", coroutine.create(function() end))) | |
696 end); | |
697 end); | |
698 | |
699 describe("to %o", function () | |
700 it("works", function () | |
701 assert.matches("[thread: 0[xX]%x+]", format("%o", coroutine.create(function() end))) | |
702 end); | |
703 end); | |
704 | |
705 describe("to %u", function () | |
706 it("works", function () | |
707 assert.matches("[thread: 0[xX]%x+]", format("%u", coroutine.create(function() end))) | |
708 end); | |
709 end); | |
710 | |
711 describe("to %x", function () | |
712 it("works", function () | |
713 assert.matches("[thread: 0[xX]%x+]", format("%x", coroutine.create(function() end))) | |
714 end); | |
715 end); | |
716 | |
717 describe("to %X", function () | |
718 it("works", function () | |
719 assert.matches("[thread: 0[xX]%x+]", format("%X", coroutine.create(function() end))) | |
720 end); | |
721 end); | |
722 | |
723 describe("to %a", function () | |
724 it("works", function () | |
725 assert.matches("[thread: 0[xX]%x+]", format("%a", coroutine.create(function() end))) | |
726 end); | |
727 end); | |
728 | |
729 describe("to %A", function () | |
730 it("works", function () | |
731 assert.matches("[thread: 0[xX]%x+]", format("%A", coroutine.create(function() end))) | |
732 end); | |
733 end); | |
734 | |
735 describe("to %e", function () | |
736 it("works", function () | |
737 assert.matches("[thread: 0[xX]%x+]", format("%e", coroutine.create(function() end))) | |
738 end); | |
739 end); | |
740 | |
741 describe("to %E", function () | |
742 it("works", function () | |
743 assert.matches("[thread: 0[xX]%x+]", format("%E", coroutine.create(function() end))) | |
744 end); | |
745 end); | |
746 | |
747 describe("to %f", function () | |
748 it("works", function () | |
749 assert.matches("[thread: 0[xX]%x+]", format("%f", coroutine.create(function() end))) | |
750 end); | |
751 end); | |
752 | |
753 describe("to %g", function () | |
754 it("works", function () | |
755 assert.matches("[thread: 0[xX]%x+]", format("%g", coroutine.create(function() end))) | |
756 end); | |
757 end); | |
758 | |
759 describe("to %G", function () | |
760 it("works", function () | |
761 assert.matches("[thread: 0[xX]%x+]", format("%G", coroutine.create(function() end))) | |
762 end); | |
763 end); | |
764 | |
765 describe("to %q", function () | |
766 it("works", function () | |
767 assert.matches('{__type="thread",__error="fail"}', format("%q", coroutine.create(function() end))) | |
768 end); | |
769 end); | |
770 | |
771 describe("to %s", function () | |
772 it("works", function () | |
773 assert.matches("thread: 0[xX]%x+", format("%s", coroutine.create(function() end))) | |
774 end); | |
775 end); | |
776 | |
777 end); | |
778 | |
779 describe("table", function () | |
780 describe("to %c", function () | |
781 it("works", function () | |
782 assert.matches("[table: 0[xX]%x+]", format("%c", { })) | |
783 end); | |
784 end); | |
785 | |
786 describe("to %d", function () | |
787 it("works", function () | |
788 assert.matches("[table: 0[xX]%x+]", format("%d", { })) | |
789 end); | |
790 end); | |
791 | |
792 describe("to %i", function () | |
793 it("works", function () | |
794 assert.matches("[table: 0[xX]%x+]", format("%i", { })) | |
795 end); | |
796 end); | |
797 | |
798 describe("to %o", function () | |
799 it("works", function () | |
800 assert.matches("[table: 0[xX]%x+]", format("%o", { })) | |
801 end); | |
802 end); | |
803 | |
804 describe("to %u", function () | |
805 it("works", function () | |
806 assert.matches("[table: 0[xX]%x+]", format("%u", { })) | |
807 end); | |
808 end); | |
809 | |
810 describe("to %x", function () | |
811 it("works", function () | |
812 assert.matches("[table: 0[xX]%x+]", format("%x", { })) | |
813 end); | |
814 end); | |
815 | |
816 describe("to %X", function () | |
817 it("works", function () | |
818 assert.matches("[table: 0[xX]%x+]", format("%X", { })) | |
819 end); | |
820 end); | |
821 | |
822 describe("to %a", function () | |
823 it("works", function () | |
824 assert.matches("[table: 0[xX]%x+]", format("%a", { })) | |
825 end); | |
826 end); | |
827 | |
828 describe("to %A", function () | |
829 it("works", function () | |
830 assert.matches("[table: 0[xX]%x+]", format("%A", { })) | |
831 end); | |
832 end); | |
833 | |
834 describe("to %e", function () | |
835 it("works", function () | |
836 assert.matches("[table: 0[xX]%x+]", format("%e", { })) | |
837 end); | |
838 end); | |
839 | |
840 describe("to %E", function () | |
841 it("works", function () | |
842 assert.matches("[table: 0[xX]%x+]", format("%E", { })) | |
843 end); | |
844 end); | |
845 | |
846 describe("to %f", function () | |
847 it("works", function () | |
848 assert.matches("[table: 0[xX]%x+]", format("%f", { })) | |
849 end); | |
850 end); | |
851 | |
852 describe("to %g", function () | |
853 it("works", function () | |
854 assert.matches("[table: 0[xX]%x+]", format("%g", { })) | |
855 end); | |
856 end); | |
857 | |
858 describe("to %G", function () | |
859 it("works", function () | |
860 assert.matches("[table: 0[xX]%x+]", format("%G", { })) | |
861 end); | |
862 end); | |
863 | |
864 describe("to %q", function () | |
865 it("works", function () | |
866 assert.matches("{ }", format("%q", { })) | |
867 end); | |
868 end); | |
869 | |
870 describe("to %s", function () | |
871 it("works", function () | |
872 assert.matches("table: 0[xX]%x+", format("%s", { })) | |
873 end); | |
874 end); | |
875 | |
876 end); | |
877 | |
878 | |
8383 | 879 end); |
880 end); |