aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKim Alvefur <zash@zash.se>2021-12-11 20:38:54 +0100
committerKim Alvefur <zash@zash.se>2021-12-11 20:38:54 +0100
commit3d0844a4f5dbc7f8eb71d888c9b117e68de0825c (patch)
tree9bd959e48e0f1fc4a5a7e5f6584b00bf6eb16a6a
parent0ddd204a2715415ff1d6c9c69c7c871f17745b62 (diff)
downloadprosody-3d0844a4f5dbc7f8eb71d888c9b117e68de0825c.tar.gz
prosody-3d0844a4f5dbc7f8eb71d888c9b117e68de0825c.zip
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.
-rw-r--r--spec/util_format_spec.lua842
-rw-r--r--tools/generate_format_spec.lua49
2 files changed, 891 insertions, 0 deletions
diff --git a/spec/util_format_spec.lua b/spec/util_format_spec.lua
index f19697df..bc3a5867 100644
--- a/spec/util_format_spec.lua
+++ b/spec/util_format_spec.lua
@@ -1,4 +1,6 @@
local format = require "util.format".format;
+-- There are eight basic types in Lua:
+-- nil, boolean, number, string, function, userdata, thread, and table
describe("util.format", function()
describe("#format()", function()
@@ -34,5 +36,845 @@ describe("util.format", function()
assert.string(format("%p", {}));
end)
end
+
+ -- Tests generated with loops!
+ describe("nil", function ()
+ describe("to %c", function ()
+ it("works", function ()
+ assert.equal("(nil)", format("%c", nil))
+ end);
+ end);
+
+ describe("to %d", function ()
+ it("works", function ()
+ assert.equal("(nil)", format("%d", nil))
+ end);
+ end);
+
+ describe("to %i", function ()
+ it("works", function ()
+ assert.equal("(nil)", format("%i", nil))
+ end);
+ end);
+
+ describe("to %o", function ()
+ it("works", function ()
+ assert.equal("(nil)", format("%o", nil))
+ end);
+ end);
+
+ describe("to %u", function ()
+ it("works", function ()
+ assert.equal("(nil)", format("%u", nil))
+ end);
+ end);
+
+ describe("to %x", function ()
+ it("works", function ()
+ assert.equal("(nil)", format("%x", nil))
+ end);
+ end);
+
+ describe("to %X", function ()
+ it("works", function ()
+ assert.equal("(nil)", format("%X", nil))
+ end);
+ end);
+
+ describe("to %a", function ()
+ it("works", function ()
+ assert.equal("(nil)", format("%a", nil))
+ end);
+ end);
+
+ describe("to %A", function ()
+ it("works", function ()
+ assert.equal("(nil)", format("%A", nil))
+ end);
+ end);
+
+ describe("to %e", function ()
+ it("works", function ()
+ assert.equal("(nil)", format("%e", nil))
+ end);
+ end);
+
+ describe("to %E", function ()
+ it("works", function ()
+ assert.equal("(nil)", format("%E", nil))
+ end);
+ end);
+
+ describe("to %f", function ()
+ it("works", function ()
+ assert.equal("(nil)", format("%f", nil))
+ end);
+ end);
+
+ describe("to %g", function ()
+ it("works", function ()
+ assert.equal("(nil)", format("%g", nil))
+ end);
+ end);
+
+ describe("to %G", function ()
+ it("works", function ()
+ assert.equal("(nil)", format("%G", nil))
+ end);
+ end);
+
+ describe("to %q", function ()
+ it("works", function ()
+ assert.equal("(nil)", format("%q", nil))
+ end);
+ end);
+
+ describe("to %s", function ()
+ it("works", function ()
+ assert.equal("(nil)", format("%s", nil))
+ end);
+ end);
+
+ end);
+
+ describe("boolean", function ()
+ describe("to %c", function ()
+ it("works", function ()
+ assert.equal("[true]", format("%c", true))
+ assert.equal("[false]", format("%c", false))
+ end);
+ end);
+
+ describe("to %d", function ()
+ it("works", function ()
+ assert.equal("[true]", format("%d", true))
+ assert.equal("[false]", format("%d", false))
+ end);
+ end);
+
+ describe("to %i", function ()
+ it("works", function ()
+ assert.equal("[true]", format("%i", true))
+ assert.equal("[false]", format("%i", false))
+ end);
+ end);
+
+ describe("to %o", function ()
+ it("works", function ()
+ assert.equal("[true]", format("%o", true))
+ assert.equal("[false]", format("%o", false))
+ end);
+ end);
+
+ describe("to %u", function ()
+ it("works", function ()
+ assert.equal("[true]", format("%u", true))
+ assert.equal("[false]", format("%u", false))
+ end);
+ end);
+
+ describe("to %x", function ()
+ it("works", function ()
+ assert.equal("[true]", format("%x", true))
+ assert.equal("[false]", format("%x", false))
+ end);
+ end);
+
+ describe("to %X", function ()
+ it("works", function ()
+ assert.equal("[true]", format("%X", true))
+ assert.equal("[false]", format("%X", false))
+ end);
+ end);
+
+ describe("to %a", function ()
+ it("works", function ()
+ assert.equal("[true]", format("%a", true))
+ assert.equal("[false]", format("%a", false))
+ end);
+ end);
+
+ describe("to %A", function ()
+ it("works", function ()
+ assert.equal("[true]", format("%A", true))
+ assert.equal("[false]", format("%A", false))
+ end);
+ end);
+
+ describe("to %e", function ()
+ it("works", function ()
+ assert.equal("[true]", format("%e", true))
+ assert.equal("[false]", format("%e", false))
+ end);
+ end);
+
+ describe("to %E", function ()
+ it("works", function ()
+ assert.equal("[true]", format("%E", true))
+ assert.equal("[false]", format("%E", false))
+ end);
+ end);
+
+ describe("to %f", function ()
+ it("works", function ()
+ assert.equal("[true]", format("%f", true))
+ assert.equal("[false]", format("%f", false))
+ end);
+ end);
+
+ describe("to %g", function ()
+ it("works", function ()
+ assert.equal("[true]", format("%g", true))
+ assert.equal("[false]", format("%g", false))
+ end);
+ end);
+
+ describe("to %G", function ()
+ it("works", function ()
+ assert.equal("[true]", format("%G", true))
+ assert.equal("[false]", format("%G", false))
+ end);
+ end);
+
+ describe("to %q", function ()
+ it("works", function ()
+ assert.equal("true", format("%q", true))
+ assert.equal("false", format("%q", false))
+ end);
+ end);
+
+ describe("to %s", function ()
+ it("works", function ()
+ assert.equal("true", format("%s", true))
+ assert.equal("false", format("%s", false))
+ end);
+ end);
+
+ end);
+
+ describe("number", function ()
+ describe("to %c", function ()
+ it("works", function ()
+ assert.equal("a", format("%c", 97))
+ assert.equal("[1.5]", format("%c", 1.5))
+ assert.equal("[7.3786976294838e+19]", format("%c", 73786976294838206464))
+ assert.equal("[inf]", format("%c", math.huge))
+ end);
+ end);
+
+ describe("to %d", function ()
+ it("works", function ()
+ assert.equal("97", format("%d", 97))
+ assert.equal("-12345", format("%d", -12345))
+ assert.equal("[1.5]", format("%d", 1.5))
+ assert.equal("[7.3786976294838e+19]", format("%d", 73786976294838206464))
+ assert.equal("[inf]", format("%d", math.huge))
+ assert.equal("2147483647", format("%d", 2147483647))
+ end);
+ end);
+
+ describe("to %i", function ()
+ it("works", function ()
+ assert.equal("97", format("%i", 97))
+ assert.equal("-12345", format("%i", -12345))
+ assert.equal("[1.5]", format("%i", 1.5))
+ assert.equal("[7.3786976294838e+19]", format("%i", 73786976294838206464))
+ assert.equal("[inf]", format("%i", math.huge))
+ assert.equal("2147483647", format("%i", 2147483647))
+ end);
+ end);
+
+ describe("to %o", function ()
+ it("works", function ()
+ assert.equal("141", format("%o", 97))
+ assert.equal("1777777777777777747707", format("%o", -12345))
+ assert.equal("[1.5]", format("%o", 1.5))
+ assert.equal("[7.3786976294838e+19]", format("%o", 73786976294838206464))
+ assert.equal("[inf]", format("%o", math.huge))
+ assert.equal("17777777777", format("%o", 2147483647))
+ end);
+ end);
+
+ describe("to %u", function ()
+ it("works", function ()
+ assert.equal("97", format("%u", 97))
+ assert.equal("18446744073709539271", format("%u", -12345))
+ assert.equal("[1.5]", format("%u", 1.5))
+ assert.equal("[7.3786976294838e+19]", format("%u", 73786976294838206464))
+ assert.equal("[inf]", format("%u", math.huge))
+ assert.equal("2147483647", format("%u", 2147483647))
+ end);
+ end);
+
+ describe("to %x", function ()
+ it("works", function ()
+ assert.equal("61", format("%x", 97))
+ assert.equal("ffffffffffffcfc7", format("%x", -12345))
+ assert.equal("[1.5]", format("%x", 1.5))
+ assert.equal("[7.3786976294838e+19]", format("%x", 73786976294838206464))
+ assert.equal("[inf]", format("%x", math.huge))
+ assert.equal("7fffffff", format("%x", 2147483647))
+ end);
+ end);
+
+ describe("to %X", function ()
+ it("works", function ()
+ assert.equal("61", format("%X", 97))
+ assert.equal("FFFFFFFFFFFFCFC7", format("%X", -12345))
+ assert.equal("[1.5]", format("%X", 1.5))
+ assert.equal("[7.3786976294838e+19]", format("%X", 73786976294838206464))
+ assert.equal("[inf]", format("%X", math.huge))
+ assert.equal("7FFFFFFF", format("%X", 2147483647))
+ end);
+ end);
+
+ if _VERSION > "Lua 5.1" then -- COMPAT no %a or %A in Lua 5.1
+ describe("to %a", function ()
+ it("works", function ()
+ assert.equal("0x1.84p+6", format("%a", 97))
+ assert.equal("-0x1.81c8p+13", format("%a", -12345))
+ assert.equal("0x1.8p+0", format("%a", 1.5))
+ assert.equal("0x1p+66", format("%a", 73786976294838206464))
+ assert.equal("inf", format("%a", math.huge))
+ assert.equal("0x1.fffffffcp+30", format("%a", 2147483647))
+ end);
+ end);
+
+ describe("to %A", function ()
+ it("works", function ()
+ assert.equal("0X1.84P+6", format("%A", 97))
+ assert.equal("-0X1.81C8P+13", format("%A", -12345))
+ assert.equal("0X1.8P+0", format("%A", 1.5))
+ assert.equal("0X1P+66", format("%A", 73786976294838206464))
+ assert.equal("INF", format("%A", math.huge))
+ assert.equal("0X1.FFFFFFFCP+30", format("%A", 2147483647))
+ end);
+ end);
+ end
+
+ describe("to %e", function ()
+ it("works", function ()
+ assert.equal("9.700000e+01", format("%e", 97))
+ assert.equal("-1.234500e+04", format("%e", -12345))
+ assert.equal("1.500000e+00", format("%e", 1.5))
+ assert.equal("7.378698e+19", format("%e", 73786976294838206464))
+ assert.equal("inf", format("%e", math.huge))
+ assert.equal("2.147484e+09", format("%e", 2147483647))
+ end);
+ end);
+
+ describe("to %E", function ()
+ it("works", function ()
+ assert.equal("9.700000E+01", format("%E", 97))
+ assert.equal("-1.234500E+04", format("%E", -12345))
+ assert.equal("1.500000E+00", format("%E", 1.5))
+ assert.equal("7.378698E+19", format("%E", 73786976294838206464))
+ assert.equal("INF", format("%E", math.huge))
+ assert.equal("2.147484E+09", format("%E", 2147483647))
+ end);
+ end);
+
+ describe("to %f", function ()
+ it("works", function ()
+ assert.equal("97.000000", format("%f", 97))
+ assert.equal("-12345.000000", format("%f", -12345))
+ assert.equal("1.500000", format("%f", 1.5))
+ assert.equal("73786976294838206464.000000", format("%f", 73786976294838206464))
+ assert.equal("inf", format("%f", math.huge))
+ assert.equal("2147483647.000000", format("%f", 2147483647))
+ end);
+ end);
+
+ describe("to %g", function ()
+ it("works", function ()
+ assert.equal("97", format("%g", 97))
+ assert.equal("-12345", format("%g", -12345))
+ assert.equal("1.5", format("%g", 1.5))
+ assert.equal("7.3787e+19", format("%g", 73786976294838206464))
+ assert.equal("inf", format("%g", math.huge))
+ assert.equal("2.14748e+09", format("%g", 2147483647))
+ end);
+ end);
+
+ describe("to %G", function ()
+ it("works", function ()
+ assert.equal("97", format("%G", 97))
+ assert.equal("-12345", format("%G", -12345))
+ assert.equal("1.5", format("%G", 1.5))
+ assert.equal("7.3787E+19", format("%G", 73786976294838206464))
+ assert.equal("INF", format("%G", math.huge))
+ assert.equal("2.14748E+09", format("%G", 2147483647))
+ end);
+ end);
+
+ describe("to %q", function ()
+ it("works", function ()
+ assert.equal("97", format("%q", 97))
+ assert.equal("-12345", format("%q", -12345))
+ assert.equal("1.5", format("%q", 1.5))
+ assert.equal("7.37869762948382065e+19", format("%q", 73786976294838206464))
+ assert.equal("(1/0)", format("%q", math.huge))
+ assert.equal("2147483647", format("%q", 2147483647))
+ end);
+ end);
+
+ describe("to %s", function ()
+ it("works", function ()
+ assert.equal("97", format("%s", 97))
+ assert.equal("-12345", format("%s", -12345))
+ assert.equal("1.5", format("%s", 1.5))
+ assert.equal("7.3786976294838e+19", format("%s", 73786976294838206464))
+ assert.equal("inf", format("%s", math.huge))
+ assert.equal("2147483647", format("%s", 2147483647))
+ end);
+ end);
+
+ end);
+
+ describe("string", function ()
+ describe("to %c", function ()
+ it("works", function ()
+ assert.equal("[hello]", format("%c", "hello"))
+ assert.equal("[foo \226\144\129\226\144\130\226\144\131 bar]", format("%c", "foo \001\002\003 bar"))
+ assert.equal("[nödåtgärd]", format("%c", "n\195\182d\195\165tg\195\164rd"))
+ assert.equal("\"n\\195\\182d\\195\\165tg\\195\"", format("%c", "n\195\182d\195\165tg\195"))
+ end);
+ end);
+
+ describe("to %d", function ()
+ it("works", function ()
+ assert.equal("[hello]", format("%d", "hello"))
+ assert.equal("[foo \226\144\129\226\144\130\226\144\131 bar]", format("%d", "foo \001\002\003 bar"))
+ assert.equal("[nödåtgärd]", format("%d", "n\195\182d\195\165tg\195\164rd"))
+ assert.equal("\"n\\195\\182d\\195\\165tg\\195\"", format("%d", "n\195\182d\195\165tg\195"))
+ end);
+ end);
+
+ describe("to %i", function ()
+ it("works", function ()
+ assert.equal("[hello]", format("%i", "hello"))
+ assert.equal("[foo \226\144\129\226\144\130\226\144\131 bar]", format("%i", "foo \001\002\003 bar"))
+ assert.equal("[nödåtgärd]", format("%i", "n\195\182d\195\165tg\195\164rd"))
+ assert.equal("\"n\\195\\182d\\195\\165tg\\195\"", format("%i", "n\195\182d\195\165tg\195"))
+ end);
+ end);
+
+ describe("to %o", function ()
+ it("works", function ()
+ assert.equal("[hello]", format("%o", "hello"))
+ assert.equal("[foo \226\144\129\226\144\130\226\144\131 bar]", format("%o", "foo \001\002\003 bar"))
+ assert.equal("[nödåtgärd]", format("%o", "n\195\182d\195\165tg\195\164rd"))
+ assert.equal("\"n\\195\\182d\\195\\165tg\\195\"", format("%o", "n\195\182d\195\165tg\195"))
+ end);
+ end);
+
+ describe("to %u", function ()
+ it("works", function ()
+ assert.equal("[hello]", format("%u", "hello"))
+ assert.equal("[foo \226\144\129\226\144\130\226\144\131 bar]", format("%u", "foo \001\002\003 bar"))
+ assert.equal("[nödåtgärd]", format("%u", "n\195\182d\195\165tg\195\164rd"))
+ assert.equal("\"n\\195\\182d\\195\\165tg\\195\"", format("%u", "n\195\182d\195\165tg\195"))
+ end);
+ end);
+
+ describe("to %x", function ()
+ it("works", function ()
+ assert.equal("[hello]", format("%x", "hello"))
+ assert.equal("[foo \226\144\129\226\144\130\226\144\131 bar]", format("%x", "foo \001\002\003 bar"))
+ assert.equal("[nödåtgärd]", format("%x", "n\195\182d\195\165tg\195\164rd"))
+ assert.equal("\"n\\195\\182d\\195\\165tg\\195\"", format("%x", "n\195\182d\195\165tg\195"))
+ end);
+ end);
+
+ describe("to %X", function ()
+ it("works", function ()
+ assert.equal("[hello]", format("%X", "hello"))
+ assert.equal("[foo \226\144\129\226\144\130\226\144\131 bar]", format("%X", "foo \001\002\003 bar"))
+ assert.equal("[nödåtgärd]", format("%X", "n\195\182d\195\165tg\195\164rd"))
+ assert.equal("\"n\\195\\182d\\195\\165tg\\195\"", format("%X", "n\195\182d\195\165tg\195"))
+ end);
+ end);
+
+ describe("to %a", function ()
+ it("works", function ()
+ assert.equal("[hello]", format("%a", "hello"))
+ assert.equal("[foo \226\144\129\226\144\130\226\144\131 bar]", format("%a", "foo \001\002\003 bar"))
+ assert.equal("[nödåtgärd]", format("%a", "n\195\182d\195\165tg\195\164rd"))
+ assert.equal("\"n\\195\\182d\\195\\165tg\\195\"", format("%a", "n\195\182d\195\165tg\195"))
+ end);
+ end);
+
+ describe("to %A", function ()
+ it("works", function ()
+ assert.equal("[hello]", format("%A", "hello"))
+ assert.equal("[foo \226\144\129\226\144\130\226\144\131 bar]", format("%A", "foo \001\002\003 bar"))
+ assert.equal("[nödåtgärd]", format("%A", "n\195\182d\195\165tg\195\164rd"))
+ assert.equal("\"n\\195\\182d\\195\\165tg\\195\"", format("%A", "n\195\182d\195\165tg\195"))
+ end);
+ end);
+
+ describe("to %e", function ()
+ it("works", function ()
+ assert.equal("[hello]", format("%e", "hello"))
+ assert.equal("[foo \226\144\129\226\144\130\226\144\131 bar]", format("%e", "foo \001\002\003 bar"))
+ assert.equal("[nödåtgärd]", format("%e", "n\195\182d\195\165tg\195\164rd"))
+ assert.equal("\"n\\195\\182d\\195\\165tg\\195\"", format("%e", "n\195\182d\195\165tg\195"))
+ end);
+ end);
+
+ describe("to %E", function ()
+ it("works", function ()
+ assert.equal("[hello]", format("%E", "hello"))
+ assert.equal("[foo \226\144\129\226\144\130\226\144\131 bar]", format("%E", "foo \001\002\003 bar"))
+ assert.equal("[nödåtgärd]", format("%E", "n\195\182d\195\165tg\195\164rd"))
+ assert.equal("\"n\\195\\182d\\195\\165tg\\195\"", format("%E", "n\195\182d\195\165tg\195"))
+ end);
+ end);
+
+ describe("to %f", function ()
+ it("works", function ()
+ assert.equal("[hello]", format("%f", "hello"))
+ assert.equal("[foo \226\144\129\226\144\130\226\144\131 bar]", format("%f", "foo \001\002\003 bar"))
+ assert.equal("[nödåtgärd]", format("%f", "n\195\182d\195\165tg\195\164rd"))
+ assert.equal("\"n\\195\\182d\\195\\165tg\\195\"", format("%f", "n\195\182d\195\165tg\195"))
+ end);
+ end);
+
+ describe("to %g", function ()
+ it("works", function ()
+ assert.equal("[hello]", format("%g", "hello"))
+ assert.equal("[foo \226\144\129\226\144\130\226\144\131 bar]", format("%g", "foo \001\002\003 bar"))
+ assert.equal("[nödåtgärd]", format("%g", "n\195\182d\195\165tg\195\164rd"))
+ assert.equal("\"n\\195\\182d\\195\\165tg\\195\"", format("%g", "n\195\182d\195\165tg\195"))
+ end);
+ end);
+
+ describe("to %G", function ()
+ it("works", function ()
+ assert.equal("[hello]", format("%G", "hello"))
+ assert.equal("[foo \226\144\129\226\144\130\226\144\131 bar]", format("%G", "foo \001\002\003 bar"))
+ assert.equal("[nödåtgärd]", format("%G", "n\195\182d\195\165tg\195\164rd"))
+ assert.equal("\"n\\195\\182d\\195\\165tg\\195\"", format("%G", "n\195\182d\195\165tg\195"))
+ end);
+ end);
+
+ describe("to %q", function ()
+ it("works", function ()
+ assert.equal("\"hello\"", format("%q", "hello"))
+ assert.equal("\"foo \226\144\129\226\144\130\226\144\131 bar\"", format("%q", "foo \001\002\003 bar"))
+ assert.equal("\"nödåtgärd\"", format("%q", "n\195\182d\195\165tg\195\164rd"))
+ assert.equal("\"n\\195\\182d\\195\\165tg\\195\"", format("%q", "n\195\182d\195\165tg\195"))
+ end);
+ end);
+
+ describe("to %s", function ()
+ it("works", function ()
+ assert.equal("hello", format("%s", "hello"))
+ assert.equal("foo \226\144\129\226\144\130\226\144\131 bar", format("%s", "foo \001\002\003 bar"))
+ assert.equal("nödåtgärd", format("%s", "n\195\182d\195\165tg\195\164rd"))
+ assert.equal("\"n\\195\\182d\\195\\165tg\\195\"", format("%s", "n\195\182d\195\165tg\195"))
+ end);
+ end);
+
+ end);
+
+ describe("function", function ()
+ describe("to %c", function ()
+ it("works", function ()
+ assert.matches("[function: 0[xX]%x+]", format("%c", function() end))
+ end);
+ end);
+
+ describe("to %d", function ()
+ it("works", function ()
+ assert.matches("[function: 0[xX]%x+]", format("%d", function() end))
+ end);
+ end);
+
+ describe("to %i", function ()
+ it("works", function ()
+ assert.matches("[function: 0[xX]%x+]", format("%i", function() end))
+ end);
+ end);
+
+ describe("to %o", function ()
+ it("works", function ()
+ assert.matches("[function: 0[xX]%x+]", format("%o", function() end))
+ end);
+ end);
+
+ describe("to %u", function ()
+ it("works", function ()
+ assert.matches("[function: 0[xX]%x+]", format("%u", function() end))
+ end);
+ end);
+
+ describe("to %x", function ()
+ it("works", function ()
+ assert.matches("[function: 0[xX]%x+]", format("%x", function() end))
+ end);
+ end);
+
+ describe("to %X", function ()
+ it("works", function ()
+ assert.matches("[function: 0[xX]%x+]", format("%X", function() end))
+ end);
+ end);
+
+ describe("to %a", function ()
+ it("works", function ()
+ assert.matches("[function: 0[xX]%x+]", format("%a", function() end))
+ end);
+ end);
+
+ describe("to %A", function ()
+ it("works", function ()
+ assert.matches("[function: 0[xX]%x+]", format("%A", function() end))
+ end);
+ end);
+
+ describe("to %e", function ()
+ it("works", function ()
+ assert.matches("[function: 0[xX]%x+]", format("%e", function() end))
+ end);
+ end);
+
+ describe("to %E", function ()
+ it("works", function ()
+ assert.matches("[function: 0[xX]%x+]", format("%E", function() end))
+ end);
+ end);
+
+ describe("to %f", function ()
+ it("works", function ()
+ assert.matches("[function: 0[xX]%x+]", format("%f", function() end))
+ end);
+ end);
+
+ describe("to %g", function ()
+ it("works", function ()
+ assert.matches("[function: 0[xX]%x+]", format("%g", function() end))
+ end);
+ end);
+
+ describe("to %G", function ()
+ it("works", function ()
+ assert.matches("[function: 0[xX]%x+]", format("%G", function() end))
+ end);
+ end);
+
+ describe("to %q", function ()
+ it("works", function ()
+ assert.matches('{__type="function",__error="fail"}', format("%q", function() end))
+ end);
+ end);
+
+ describe("to %s", function ()
+ it("works", function ()
+ assert.matches("function: 0[xX]%x+", format("%s", function() end))
+ end);
+ end);
+
+ end);
+
+ describe("thread", function ()
+ describe("to %c", function ()
+ it("works", function ()
+ assert.matches("[thread: 0[xX]%x+]", format("%c", coroutine.create(function() end)))
+ end);
+ end);
+
+ describe("to %d", function ()
+ it("works", function ()
+ assert.matches("[thread: 0[xX]%x+]", format("%d", coroutine.create(function() end)))
+ end);
+ end);
+
+ describe("to %i", function ()
+ it("works", function ()
+ assert.matches("[thread: 0[xX]%x+]", format("%i", coroutine.create(function() end)))
+ end);
+ end);
+
+ describe("to %o", function ()
+ it("works", function ()
+ assert.matches("[thread: 0[xX]%x+]", format("%o", coroutine.create(function() end)))
+ end);
+ end);
+
+ describe("to %u", function ()
+ it("works", function ()
+ assert.matches("[thread: 0[xX]%x+]", format("%u", coroutine.create(function() end)))
+ end);
+ end);
+
+ describe("to %x", function ()
+ it("works", function ()
+ assert.matches("[thread: 0[xX]%x+]", format("%x", coroutine.create(function() end)))
+ end);
+ end);
+
+ describe("to %X", function ()
+ it("works", function ()
+ assert.matches("[thread: 0[xX]%x+]", format("%X", coroutine.create(function() end)))
+ end);
+ end);
+
+ describe("to %a", function ()
+ it("works", function ()
+ assert.matches("[thread: 0[xX]%x+]", format("%a", coroutine.create(function() end)))
+ end);
+ end);
+
+ describe("to %A", function ()
+ it("works", function ()
+ assert.matches("[thread: 0[xX]%x+]", format("%A", coroutine.create(function() end)))
+ end);
+ end);
+
+ describe("to %e", function ()
+ it("works", function ()
+ assert.matches("[thread: 0[xX]%x+]", format("%e", coroutine.create(function() end)))
+ end);
+ end);
+
+ describe("to %E", function ()
+ it("works", function ()
+ assert.matches("[thread: 0[xX]%x+]", format("%E", coroutine.create(function() end)))
+ end);
+ end);
+
+ describe("to %f", function ()
+ it("works", function ()
+ assert.matches("[thread: 0[xX]%x+]", format("%f", coroutine.create(function() end)))
+ end);
+ end);
+
+ describe("to %g", function ()
+ it("works", function ()
+ assert.matches("[thread: 0[xX]%x+]", format("%g", coroutine.create(function() end)))
+ end);
+ end);
+
+ describe("to %G", function ()
+ it("works", function ()
+ assert.matches("[thread: 0[xX]%x+]", format("%G", coroutine.create(function() end)))
+ end);
+ end);
+
+ describe("to %q", function ()
+ it("works", function ()
+ assert.matches('{__type="thread",__error="fail"}', format("%q", coroutine.create(function() end)))
+ end);
+ end);
+
+ describe("to %s", function ()
+ it("works", function ()
+ assert.matches("thread: 0[xX]%x+", format("%s", coroutine.create(function() end)))
+ end);
+ end);
+
+ end);
+
+ describe("table", function ()
+ describe("to %c", function ()
+ it("works", function ()
+ assert.matches("[table: 0[xX]%x+]", format("%c", { }))
+ end);
+ end);
+
+ describe("to %d", function ()
+ it("works", function ()
+ assert.matches("[table: 0[xX]%x+]", format("%d", { }))
+ end);
+ end);
+
+ describe("to %i", function ()
+ it("works", function ()
+ assert.matches("[table: 0[xX]%x+]", format("%i", { }))
+ end);
+ end);
+
+ describe("to %o", function ()
+ it("works", function ()
+ assert.matches("[table: 0[xX]%x+]", format("%o", { }))
+ end);
+ end);
+
+ describe("to %u", function ()
+ it("works", function ()
+ assert.matches("[table: 0[xX]%x+]", format("%u", { }))
+ end);
+ end);
+
+ describe("to %x", function ()
+ it("works", function ()
+ assert.matches("[table: 0[xX]%x+]", format("%x", { }))
+ end);
+ end);
+
+ describe("to %X", function ()
+ it("works", function ()
+ assert.matches("[table: 0[xX]%x+]", format("%X", { }))
+ end);
+ end);
+
+ describe("to %a", function ()
+ it("works", function ()
+ assert.matches("[table: 0[xX]%x+]", format("%a", { }))
+ end);
+ end);
+
+ describe("to %A", function ()
+ it("works", function ()
+ assert.matches("[table: 0[xX]%x+]", format("%A", { }))
+ end);
+ end);
+
+ describe("to %e", function ()
+ it("works", function ()
+ assert.matches("[table: 0[xX]%x+]", format("%e", { }))
+ end);
+ end);
+
+ describe("to %E", function ()
+ it("works", function ()
+ assert.matches("[table: 0[xX]%x+]", format("%E", { }))
+ end);
+ end);
+
+ describe("to %f", function ()
+ it("works", function ()
+ assert.matches("[table: 0[xX]%x+]", format("%f", { }))
+ end);
+ end);
+
+ describe("to %g", function ()
+ it("works", function ()
+ assert.matches("[table: 0[xX]%x+]", format("%g", { }))
+ end);
+ end);
+
+ describe("to %G", function ()
+ it("works", function ()
+ assert.matches("[table: 0[xX]%x+]", format("%G", { }))
+ end);
+ end);
+
+ describe("to %q", function ()
+ it("works", function ()
+ assert.matches("{ }", format("%q", { }))
+ end);
+ end);
+
+ describe("to %s", function ()
+ it("works", function ()
+ assert.matches("table: 0[xX]%x+", format("%s", { }))
+ end);
+ end);
+
+ end);
+
+
end);
end);
diff --git a/tools/generate_format_spec.lua b/tools/generate_format_spec.lua
new file mode 100644
index 00000000..66b68079
--- /dev/null
+++ b/tools/generate_format_spec.lua
@@ -0,0 +1,49 @@
+local format = require"util.format".format;
+local dump = require "util.serialization".new("oneline")
+local types = {
+ "nil";
+ "boolean";
+ "number";
+ "string";
+ "function";
+ -- "userdata";
+ "thread";
+ "table";
+};
+local example_values = {
+ ["nil"] = { n = 1; nil };
+ ["boolean"] = { true; false };
+ ["number"] = { 97, -12345, 1.5; 73786976294838206464, math.huge, 2147483647 };
+ ["string"] = { "hello", "foo \1\2\3 bar", "nödåtgärd", string.sub("nödåtgärd", 1, -4) };
+ ["function"] = { function() end };
+ -- ["userdata"] = {};
+ ["thread"] = { coroutine.create(function() end) };
+ ["table"] = { {} };
+};
+local example_strings = setmetatable({
+ ["nil"] = { "nil" };
+ ["function"] = { "function() end" };
+ ["number"] = { "97", "-12345", "1.5"; "73786976294838206464", "math.huge", "2147483647" };
+ ["thread"] = { "coroutine.create(function() end)" };
+}, { __index = function() return {} end });
+for _, lua_type in ipairs(types) do
+ print(string.format("\t\tdescribe(\"%s\", function ()", lua_type));
+ local examples = example_values[lua_type];
+ for fmt in ("cdiouxXaAeEfgGqs"):gmatch(".") do
+ print(string.format("\t\t\tdescribe(\"to %%%s\", function ()", fmt));
+ print("\t\t\t\tit(\"works\", function ()");
+ for i=1, examples.n or #examples do
+ local example = examples[i];
+ if not tostring(example):match("%w+: 0[xX]%x+") then
+ 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)));
+ else
+ 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)));
+ end
+ end
+ print("\t\t\t\tend);");
+ print("\t\t\tend);");
+ print()
+ end
+ print("\t\tend);");
+ print()
+end