-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathutils.test.lua
More file actions
222 lines (183 loc) · 7.83 KB
/
Copy pathutils.test.lua
File metadata and controls
222 lines (183 loc) · 7.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
local utils = require("mods").utils
local args_repr = utils.args_repr
local fmt = string.format
describe("mods.utils", function()
local tests
local fn = function() end
---------------
--- quote() ---
---------------
-- stylua: ignore
tests = {
--------------input--------------|--------------expected---------------
{ "foo" , '"foo"' },
{ "" , '""' },
{ "" , '""' },
{ '"' , [['"']] },
{ "'" , [["'"]] },
{ "it's ok" , [["it's ok"]] },
{ [[a "b" c]] , [['a "b" c']] },
{ "a 'b' c" , [["a 'b' c"]] },
{ "bar_baz" , '"bar_baz"' },
{ [[he said "hi"]] , [['he said "hi"']] },
{ "hello world" , '"hello world"' },
{ [[back\slash]] , [["back\slash"]] },
{ [['mix "quotes" and 'single']] , [["'mix \"quotes\" and 'single'"]] },
}
for i = 1, #tests do
local input, expected = unpack(tests[i] --[[@as {[1]:string, [2]:string}]], 1, 2)
it(fmt("quote(%q) returns correct result", input), function()
assert.are_equal(expected, utils.quote(input))
end)
end
-----------------
--- keypath() ---
-----------------
-- stylua: ignore
tests = {
-------------params-------------|-------------expected--------------
{ { } , "" },
{ { "ctx", "end" } , 'ctx["end"]' },
{ { "ctx", "invalid-key" } , 'ctx["invalid-key"]' },
{ { "ctx", "users", 1, "name" } , "ctx.users[1].name" },
{ { "t" } , "t" },
{ { "t", "a", "b", "c" } , "t.a.b.c" },
{ { fn, "end" } , fmt('[%s]["end"]', tostring(fn)) },
}
for i = 1, #tests do
local params, expected = unpack(tests[i] --[[@as {[1]:any[], [2]:string}]], 1, 2)
it(fmt("keypath(%s) returns correct result", args_repr(params)), function()
assert.are_equal(expected, utils.keypath(unpack(params)))
end)
end
-------------------
--- args_repr() ---
-------------------
-- stylua: ignore
tests = {
---------input----------|------expected--------
{ nil , "" },
{ { } , "" },
{ { "a", 1, true } , '"a", 1, true' },
{ { "x", "y", "z", {} } , '"x", "y", "z", {}' },
}
for i = 1, #tests do
local input, expected = unpack(tests[i] --[[@as {[1]:any[], [2]:string}]], 1, 2)
it(fmt("args_repr(%s) returns correct result", args_repr(input)), function()
assert.are_equal(expected, utils.args_repr(input))
end)
end
it("args_repr() omits metatables from inspected tables", function()
local input = { setmetatable({ "a" }, { __index = { "b" } }) }
assert.are_equal('{"a"}', utils.args_repr(input))
end)
describe("assert_arg()", function()
it("returns same value when validation passes", function()
assert.are_equal("abc", utils.assert_arg(1, "abc", "string"))
assert.are_equal(123, utils.assert_arg(2, 123, "number"))
end)
it("returns nil when optional and value is nil", function()
assert.is_nil(utils.assert_arg(1, nil, "string", true))
end)
it("defaults to truthy check when type is omitted", function()
assert.are_equal("ok", utils.assert_arg(1, "ok"))
assert.has_error(function()
utils.assert_arg(2, false)
end, "bad argument #2 (truthy value expected, got false)")
end)
it("throws with bad argument prefix on validation failure", function()
assert.has_error(function()
utils.assert_arg(3, 123, "string")
end, "bad argument #3 (string expected, got number)")
end)
end)
describe("validate()", function()
it("errors with label prefix on validation failure", function()
assert.has_error(function()
utils.validate("count", "x", "number")
end, "count: number expected, got string")
end)
it("uses keypath when label is a table", function()
assert.has_error(function()
utils.validate({ "ctx", "users", 1, "name" }, 123, "string")
end, "ctx.users[1].name: string expected, got number")
end)
it("does not error when optional and value is nil", function()
assert.no_error(function()
utils.validate("name", nil, "string", true)
end)
end)
it("passes custom message template to validate when provided", function()
assert.has_error(function()
utils.validate("count", "x", "number", false, "need {{expected}}, got {{got}}")
end, "count: need number, got string")
end)
end)
describe("lazy_module()", function()
local function is_module_loaded(name)
return package.loaded[name] ~= nil
end
it("does not load the module until a field is accessed", function()
local modname = "mods.stringcase"
package.loaded[modname] = nil
local proxy = utils.lazy_module(modname)
assert.is_false(is_module_loaded(modname))
_ = proxy.missing
assert.is_true(is_module_loaded(modname))
end)
it("proxies reads and writes to the loaded module", function()
local modname = "mods.is"
local proxy = utils.lazy_module(modname)
local loaded = require(modname) --[[@as mods.is]]
local k, v = "_lazy_module_proxy_value", 3
assert.is_nil(proxy[k])
assert.same(loaded.boolean, proxy.boolean)
assert.is_nil(rawget(proxy, "boolean"))
proxy[k] = v
assert.are_equal(v, proxy[k])
assert.are_equal(v, loaded[k])
assert.is_nil(rawget(proxy, k))
proxy[k] = nil
assert.is_nil(proxy[k])
assert.is_nil(loaded[k])
loaded[k] = v
assert.are_equal(v, loaded[k])
assert.are_equal(v, proxy[k])
assert.is_nil(rawget(proxy, k))
loaded[k] = nil
assert.is_nil(loaded[k])
assert.is_nil(proxy[k])
end)
it("supports writes before any read access", function()
local proxy = utils.lazy_module "mods.is"
local loaded = require "mods.is" --[[@as mods.is]]
local k, v = "_lazy_module_first_write", 11
proxy[k] = v
assert.are_equal(v, loaded[k])
assert.are_equal(v, proxy[k])
assert.is_nil(rawget(proxy, k))
end)
it("rewrites its metamethods to the loaded module", function()
local proxy = utils.lazy_module("mods.validate")
_ = proxy.boolean
assert.are_equal(require("mods.validate"), getmetatable(proxy).__index)
assert.are_equal(require("mods.validate"), getmetatable(proxy).__newindex)
end)
it("supports __call when the loaded module is callable", function()
local proxy = utils.lazy_module("mods.validate")
local loaded = require("mods.validate")
assert.is_true(proxy(true, "truthy"))
assert.is_false(proxy(false, "truthy"))
-- `__call` is cached after the first proxy call.
assert.are_equal(getmetatable(loaded).__call, getmetatable(proxy).__call)
end)
it("supports __call when the loaded module is a function", function()
local proxy = utils.lazy_module("mods.template")
local loaded = require("mods.template")
assert.are_equal(loaded("{{name}}", { name = "Ada" }), proxy("{{name}}", { name = "Ada" }))
assert.are_equal(loaded("{{name}}", { name = "Grace" }), proxy("{{name}}", { name = "Grace" }))
assert.is_nil(getmetatable(proxy).__index)
assert.is_nil(getmetatable(proxy).__newindex)
end)
end)
end)