Welcome to mirror list, hosted at ThFree Co, Russian Federation.

test-utils.lua « tests - github.com/stevedonovan/Penlight.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 90634e205c8c7fe3daf28adff2d769016d2fcba4 (plain)
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
local utils = require 'pl.utils'
local path = require 'pl.path'
local test = require 'pl.test'
local asserteq, T = test.asserteq, test.tuple


local function quote(s)
    if utils.is_windows then
        return '"'..s..'"'
    else
        return "'"..s.."'"
    end
end

-- construct command to run external lua, we need to to be able to run some
-- tests on the same lua engine, but also need to pass on the LuaCov flag
-- if it was used, to make sure we report the proper coverage.
local cmd = "-e "
do
    local i = 0
    while arg[i-1] do
      local a = arg[i-1]
      if a:find("package%.path") and a:sub(1,1) ~= "'" then
        a = quote(a)
      end
      cmd = a .. " " .. cmd
      i = i - 1
    end
end


--- quitting
do
    local luacode = quote("require([[pl.utils]]).quit([[hello world]])")
    local success, code, stdout, stderr = utils.executeex(cmd..luacode)
    asserteq(success, false)
    if utils.is_windows then
        asserteq(code, -1)
    else
        asserteq(code, 255)
    end
    asserteq(stdout, "")
    asserteq(stderr, "hello world\n")

    local luacode = quote("require([[pl.utils]]).quit(2, [[hello world]])")
    local success, code, stdout, stderr = utils.executeex(cmd..luacode)
    asserteq(success, false)
    asserteq(code, 2)
    asserteq(stdout, "")
    asserteq(stderr, "hello world\n")

    local luacode = quote("require([[pl.utils]]).quit(2, [[hello %s]], 42)")
    local success, code, stdout, stderr = utils.executeex(cmd..luacode)
    asserteq(success, false)
    asserteq(code, 2)
    asserteq(stdout, "")
    asserteq(stderr, "hello 42\n")

    local luacode = quote("require([[pl.utils]]).quit(2)")
    local success, code, stdout, stderr = utils.executeex(cmd..luacode)
    asserteq(success, false)
    asserteq(code, 2)
    asserteq(stdout, "")
    asserteq(stderr, "")
end

----- importing module tables wholesale ---
utils.import(math)
asserteq(type(sin),"function")
asserteq(type(abs),"function")

--- useful patterns
local P = utils.patterns
asserteq(("+0.1e10"):match(P.FLOAT) ~= nil, true)
asserteq(("-23430"):match(P.INTEGER) ~= nil, true)
asserteq(("my_little_pony99"):match(P.IDEN) ~= nil, true)

--- escaping magic chars
local escape = utils.escape
asserteq(escape '[a]','%[a%]')
asserteq(escape '$(bonzo)','%$%(bonzo%)')

--- choose
asserteq(utils.choose(true, 1, 2), 1)
asserteq(utils.choose(false, 1, 2), 2)

--- splitting strings ---
local split = utils.split
asserteq(split("hello dolly"),{"hello","dolly"})
asserteq(split("hello,dolly",","),{"hello","dolly"})
asserteq(split("hello,dolly,",","),{"hello","dolly"})

local first,second = utils.splitv("hello:dolly",":")
asserteq(T(first,second),T("hello","dolly"))

----- table of values to table of strings
asserteq(utils.array_tostring{1,2,3},{"1","2","3"})
-- writing into existing table
local tmp = {}
utils.array_tostring({1,2,3},tmp)
asserteq(tmp,{"1","2","3"})

--- memoizing a function
local kount = 0
local f = utils.memoize(function(x)
    kount = kount + 1
    return x*x
end)
asserteq(f(2),4)
asserteq(f(10),100)
asserteq(f(2),4)
-- actual function only called twice
asserteq(kount,2)

-- string lambdas
local L = utils.string_lambda
local g = L"|x| x:sub(1,1)"
asserteq(g("hello"),"h")

local f = L"|x,y| x - y"
asserteq(f(10,2),8)

-- alternative form for _one_ argument
asserteq(L("2 * _")(4), 8)

local List = require 'pl.List'
local ls = List{10,20,30}

-- string lambdas can be used throughout Penlight
asserteq(ls:map"_+1", {11,21,31})

-- because they use this common function
local function test_fn_arg(f)
    f = utils.function_arg(1,f)
    asserteq(f(10),11)
end

test_fn_arg (function (x) return x + 1 end)
test_fn_arg  '_ + 1'
test.assertraise(function() test_fn_arg {} end, 'not a callable object')
test.assertraise(function() test_fn_arg (0) end, 'must be callable')

-- partial application

local f1 = utils.bind1(f,10)
asserteq(f1(2), 8)

local f2 = utils.bind2(f,2)
asserteq(f2(10), 8)

--- extended type checking

local is_type = utils.is_type
-- anything without a metatable works as regular type() function
asserteq(is_type("one","string"),true)
asserteq(is_type({},"table"),true)

-- but otherwise the type of an object is considered to be its metatable
asserteq(is_type(ls,List),true)

-- compatibility functions
local chunk = utils.load 'return 42'
asserteq(chunk(),42)

chunk = utils.load 'a = 42'
chunk()
asserteq(a,42)

local t = {}
chunk = utils.load ('b = 42','<str>','t',t)
chunk()
asserteq(t.b,42)

chunk,err = utils.load ('a = ?','<str>')
assert(err,[[[string "<str>"]:1: unexpected symbol near '?']])

asserteq(utils.quote_arg("foo"), [[foo]])
if path.is_windows then
    asserteq(utils.quote_arg(""), '^"^"')
    asserteq(utils.quote_arg('"'), '^"')
    asserteq(utils.quote_arg([[ \]]), [[^" \\^"]])
    asserteq(utils.quote_arg([[foo\\ bar\\" baz\]]), [[^"foo\\ bar\\\\\^" baz\\^"]])
    asserteq(utils.quote_arg("%path% ^^!()"), [[^"^%path^% ^^^^^!()^"]])
else
    asserteq(utils.quote_arg(""), "''")
    asserteq(utils.quote_arg("'"), [[''\''']])
    asserteq(utils.quote_arg([['a\'b]]), [[''\''a\'\''b']])
end

-- packing and unpacking arguments in a nil-safe way
local t = utils.pack(nil, nil, "hello", nil)
asserteq(t.n, 4) -- the last nil does count as an argument

local arg1, arg2, arg3, arg4 = utils.unpack(t)
assert(arg1 == nil)
assert(arg2 == nil)
asserteq("hello", arg3)
assert(arg4 == nil)


-- Assert arguments assert_arg
local ok, err = pcall(function()
    utils.assert_arg(4,'!@#$%^&*','string',require("pl.path").isdir,'not a directory')
end)
asserteq(ok, false)
asserteq(err:match("(argument .+)$"), "argument 4: '!@#$%^&*' not a directory")

local ok, err = pcall(function()
    utils.assert_arg(1, "hello", "table")
end)
asserteq(ok, false)
asserteq(err:match("(argument .+)$"), "argument 1 expected a 'table', got a 'string'")

local ok, err = pcall(function()
    return utils.assert_arg(1, "hello", "string")
end)
asserteq(ok, true)
asserteq(err, "hello")

-- assert_string
local success, err = pcall(utils.assert_string, 2, 5)
asserteq(success, false)
asserteq(err:match("(argument .+)$"), "argument 2 expected a 'string', got a 'number'")

local x = utils.assert_string(2, "5")
asserteq(x, "5")


do
    -- printf -- without template
    local luacode = quote("require([[pl.utils]]).printf([[hello world]])")
    local success, code, stdout, stderr = utils.executeex(cmd..luacode)
    asserteq(success, true)
    asserteq(code, 0)
    asserteq(stdout, "hello world")
    asserteq(stderr, "")

    -- printf -- with template
    local luacode = quote("require([[pl.utils]]).printf([[hello %s]], [[world]])")
    local success, code, stdout, stderr = utils.executeex(cmd..luacode)
    asserteq(success, true)
    asserteq(code, 0)
    asserteq(stdout, "hello world")
    asserteq(stderr, "")

    -- printf -- with bad template
    local luacode = quote("require([[pl.utils]]).printf(42)")
    local success, code, stdout, stderr = utils.executeex(cmd..luacode)
    asserteq(success, false)
    asserteq(code, 1)
    asserteq(stdout, "")
    assert(stderr:find("argument 1 expected a 'string', got a 'number'"))
end

do
    -- on_error, raise  -- default
    utils.on_error("default")
    local ok, err = utils.raise("some error")
    asserteq(ok, nil)
    asserteq(err, "some error")
    local ok, err = pcall(utils.on_error, "bad one")
    asserteq(ok, false)
    asserteq(err, "Bad argument expected string; 'default', 'quit', or 'error'. Got 'bad one'")

    -- on_error, raise  -- error
    utils.on_error("error")
    local ok, err = pcall(utils.raise, "some error")
    asserteq(ok, false)
    asserteq(err, "some error")
    local ok, err = pcall(utils.on_error, "bad one")
    asserteq(ok, false)
    assert(err:find("Bad argument expected string; 'default', 'quit', or 'error'. Got 'bad one'"))

    -- on_error, raise  -- quit
    utils.on_error("quit")
    local luacode = quote("local u=require([[pl.utils]]) u.on_error([[quit]]) u.raise([[some error]])")
    local success, code, stdout, stderr = utils.executeex(cmd..luacode)
    asserteq(success, false)
    if utils.is_windows then
        asserteq(code, -1)
    else
        asserteq(code, 255)
    end
    asserteq(stdout, "")
    asserteq(stderr, "some error\n")

    local luacode = quote("local u=require([[pl.utils]]) u.on_error([[quit]]) u.on_error([[bad one]])")
    local success, code, stdout, stderr = utils.executeex(cmd..luacode)
    asserteq(success, false)
    if utils.is_windows then
        asserteq(code, -1)
    else
        asserteq(code, 255)
    end
    asserteq(stdout, "")
    asserteq(stderr, "Bad argument expected string; 'default', 'quit', or 'error'. Got 'bad one'\n")

    utils.on_error("default") -- cleanup by restoring behaviour after on_error + raise tests
end

do
    -- readlines
    local f = utils.readlines("tests/test-utils.lua")
    asserteq(type(f), "table")
    local v = "some extraordinary string this is only in this file for test purposes so we can go and find it"
    local found = false
    for i, line in ipairs(f) do
      if line:find(v) then
        found = true
        break
      end
    end
    asserteq(found, true)
end