Annotate

tools/generate_format_spec.lua @ 12034:ee94ac51b2dd

util.format: ALL THE TESTS!!! The more tests I made, the more Lua 5.1 quirks I discovered. Tests generated using a tool plus some touch-up.
author Kim Alvefur <zash@zash.se>
date Sat, 11 Dec 2021 20:38:54 +0100
child 12037:82f6a0b0a425
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
12034
ee94ac51b2dd util.format: ALL THE TESTS!!!
Kim Alvefur <zash@zash.se>
parents:
diff changeset
1 local format = require"util.format".format;
ee94ac51b2dd util.format: ALL THE TESTS!!!
Kim Alvefur <zash@zash.se>
parents:
diff changeset
2 local dump = require "util.serialization".new("oneline")
ee94ac51b2dd util.format: ALL THE TESTS!!!
Kim Alvefur <zash@zash.se>
parents:
diff changeset
3 local types = {
ee94ac51b2dd util.format: ALL THE TESTS!!!
Kim Alvefur <zash@zash.se>
parents:
diff changeset
4 "nil";
ee94ac51b2dd util.format: ALL THE TESTS!!!
Kim Alvefur <zash@zash.se>
parents:
diff changeset
5 "boolean";
ee94ac51b2dd util.format: ALL THE TESTS!!!
Kim Alvefur <zash@zash.se>
parents:
diff changeset
6 "number";
ee94ac51b2dd util.format: ALL THE TESTS!!!
Kim Alvefur <zash@zash.se>
parents:
diff changeset
7 "string";
ee94ac51b2dd util.format: ALL THE TESTS!!!
Kim Alvefur <zash@zash.se>
parents:
diff changeset
8 "function";
ee94ac51b2dd util.format: ALL THE TESTS!!!
Kim Alvefur <zash@zash.se>
parents:
diff changeset
9 -- "userdata";
ee94ac51b2dd util.format: ALL THE TESTS!!!
Kim Alvefur <zash@zash.se>
parents:
diff changeset
10 "thread";
ee94ac51b2dd util.format: ALL THE TESTS!!!
Kim Alvefur <zash@zash.se>
parents:
diff changeset
11 "table";
ee94ac51b2dd util.format: ALL THE TESTS!!!
Kim Alvefur <zash@zash.se>
parents:
diff changeset
12 };
ee94ac51b2dd util.format: ALL THE TESTS!!!
Kim Alvefur <zash@zash.se>
parents:
diff changeset
13 local example_values = {
ee94ac51b2dd util.format: ALL THE TESTS!!!
Kim Alvefur <zash@zash.se>
parents:
diff changeset
14 ["nil"] = { n = 1; nil };
ee94ac51b2dd util.format: ALL THE TESTS!!!
Kim Alvefur <zash@zash.se>
parents:
diff changeset
15 ["boolean"] = { true; false };
ee94ac51b2dd util.format: ALL THE TESTS!!!
Kim Alvefur <zash@zash.se>
parents:
diff changeset
16 ["number"] = { 97, -12345, 1.5; 73786976294838206464, math.huge, 2147483647 };
ee94ac51b2dd util.format: ALL THE TESTS!!!
Kim Alvefur <zash@zash.se>
parents:
diff changeset
17 ["string"] = { "hello", "foo \1\2\3 bar", "nödåtgärd", string.sub("nödåtgärd", 1, -4) };
ee94ac51b2dd util.format: ALL THE TESTS!!!
Kim Alvefur <zash@zash.se>
parents:
diff changeset
18 ["function"] = { function() end };
ee94ac51b2dd util.format: ALL THE TESTS!!!
Kim Alvefur <zash@zash.se>
parents:
diff changeset
19 -- ["userdata"] = {};
ee94ac51b2dd util.format: ALL THE TESTS!!!
Kim Alvefur <zash@zash.se>
parents:
diff changeset
20 ["thread"] = { coroutine.create(function() end) };
ee94ac51b2dd util.format: ALL THE TESTS!!!
Kim Alvefur <zash@zash.se>
parents:
diff changeset
21 ["table"] = { {} };
ee94ac51b2dd util.format: ALL THE TESTS!!!
Kim Alvefur <zash@zash.se>
parents:
diff changeset
22 };
ee94ac51b2dd util.format: ALL THE TESTS!!!
Kim Alvefur <zash@zash.se>
parents:
diff changeset
23 local example_strings = setmetatable({
ee94ac51b2dd util.format: ALL THE TESTS!!!
Kim Alvefur <zash@zash.se>
parents:
diff changeset
24 ["nil"] = { "nil" };
ee94ac51b2dd util.format: ALL THE TESTS!!!
Kim Alvefur <zash@zash.se>
parents:
diff changeset
25 ["function"] = { "function() end" };
ee94ac51b2dd util.format: ALL THE TESTS!!!
Kim Alvefur <zash@zash.se>
parents:
diff changeset
26 ["number"] = { "97", "-12345", "1.5"; "73786976294838206464", "math.huge", "2147483647" };
ee94ac51b2dd util.format: ALL THE TESTS!!!
Kim Alvefur <zash@zash.se>
parents:
diff changeset
27 ["thread"] = { "coroutine.create(function() end)" };
ee94ac51b2dd util.format: ALL THE TESTS!!!
Kim Alvefur <zash@zash.se>
parents:
diff changeset
28 }, { __index = function() return {} end });
ee94ac51b2dd util.format: ALL THE TESTS!!!
Kim Alvefur <zash@zash.se>
parents:
diff changeset
29 for _, lua_type in ipairs(types) do
ee94ac51b2dd util.format: ALL THE TESTS!!!
Kim Alvefur <zash@zash.se>
parents:
diff changeset
30 print(string.format("\t\tdescribe(\"%s\", function ()", lua_type));
ee94ac51b2dd util.format: ALL THE TESTS!!!
Kim Alvefur <zash@zash.se>
parents:
diff changeset
31 local examples = example_values[lua_type];
ee94ac51b2dd util.format: ALL THE TESTS!!!
Kim Alvefur <zash@zash.se>
parents:
diff changeset
32 for fmt in ("cdiouxXaAeEfgGqs"):gmatch(".") do
ee94ac51b2dd util.format: ALL THE TESTS!!!
Kim Alvefur <zash@zash.se>
parents:
diff changeset
33 print(string.format("\t\t\tdescribe(\"to %%%s\", function ()", fmt));
ee94ac51b2dd util.format: ALL THE TESTS!!!
Kim Alvefur <zash@zash.se>
parents:
diff changeset
34 print("\t\t\t\tit(\"works\", function ()");
ee94ac51b2dd util.format: ALL THE TESTS!!!
Kim Alvefur <zash@zash.se>
parents:
diff changeset
35 for i=1, examples.n or #examples do
ee94ac51b2dd util.format: ALL THE TESTS!!!
Kim Alvefur <zash@zash.se>
parents:
diff changeset
36 local example = examples[i];
ee94ac51b2dd util.format: ALL THE TESTS!!!
Kim Alvefur <zash@zash.se>
parents:
diff changeset
37 if not tostring(example):match("%w+: 0[xX]%x+") then
ee94ac51b2dd util.format: ALL THE TESTS!!!
Kim Alvefur <zash@zash.se>
parents:
diff changeset
38 print(string.format("\t\t\t\t\tassert.equal(%q, format(%q, %s))", format("%"..fmt, example), "%" .. fmt, example_strings[lua_type][i] or dump(example)));
ee94ac51b2dd util.format: ALL THE TESTS!!!
Kim Alvefur <zash@zash.se>
parents:
diff changeset
39 else
ee94ac51b2dd util.format: ALL THE TESTS!!!
Kim Alvefur <zash@zash.se>
parents:
diff changeset
40 print(string.format("\t\t\t\t\tassert.matches(\"[%s: 0[xX]%%x+]\", format(%q, %s))", lua_type, "%" .. fmt, example_strings[lua_type][i] or dump(example)));
ee94ac51b2dd util.format: ALL THE TESTS!!!
Kim Alvefur <zash@zash.se>
parents:
diff changeset
41 end
ee94ac51b2dd util.format: ALL THE TESTS!!!
Kim Alvefur <zash@zash.se>
parents:
diff changeset
42 end
ee94ac51b2dd util.format: ALL THE TESTS!!!
Kim Alvefur <zash@zash.se>
parents:
diff changeset
43 print("\t\t\t\tend);");
ee94ac51b2dd util.format: ALL THE TESTS!!!
Kim Alvefur <zash@zash.se>
parents:
diff changeset
44 print("\t\t\tend);");
ee94ac51b2dd util.format: ALL THE TESTS!!!
Kim Alvefur <zash@zash.se>
parents:
diff changeset
45 print()
ee94ac51b2dd util.format: ALL THE TESTS!!!
Kim Alvefur <zash@zash.se>
parents:
diff changeset
46 end
ee94ac51b2dd util.format: ALL THE TESTS!!!
Kim Alvefur <zash@zash.se>
parents:
diff changeset
47 print("\t\tend);");
ee94ac51b2dd util.format: ALL THE TESTS!!!
Kim Alvefur <zash@zash.se>
parents:
diff changeset
48 print()
ee94ac51b2dd util.format: ALL THE TESTS!!!
Kim Alvefur <zash@zash.se>
parents:
diff changeset
49 end