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