-
Notifications
You must be signed in to change notification settings - Fork 187
/
Copy pathtest-callbacks.lua
59 lines (48 loc) · 1.67 KB
/
test-callbacks.lua
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
return require('lib/tap')(function (test)
-- Notes:
-- * When using a callable table as a callback, it will get itself as the first param when it is called.
-- This matches what happens when calling the callable table normally.
-- * expect wraps its argument in a function and returns the function, so expect should not be called on
-- a callable table directly.
test("luv_handle_t: function", function (print, p, expect, uv)
local handle = uv.new_timer()
local function onclose()
p("closed", handle)
end
local function ontimeout()
p("timeout", handle)
uv.close(handle, expect(onclose))
end
uv.timer_start(handle, 10, 0, expect(ontimeout))
end)
test("luv_handle_t: callable table", function (print, p, expect, uv)
local handle = uv.new_timer()
local function onclose(self)
p("closed", self, handle)
end
local onCloseTable = setmetatable({}, {__call=expect(onclose)})
local function ontimeout(self)
p("timeout", self, handle)
uv.close(handle, onCloseTable)
end
local onTimeoutTable = setmetatable({}, {__call=expect(ontimeout)})
uv.timer_start(handle, 10, 0, onTimeoutTable)
end)
test("luv_req_t: function", function (print, p, expect, uv)
local fn = function(err, stat)
assert(not err)
assert(stat)
end
assert(uv.fs_stat('.', expect(fn)))
end)
test("luv_req_t: callable table", function (print, p, expect, uv)
local callable
local fn = function(self, err, stat)
assert(self == callable)
assert(not err, err)
assert(stat)
end
callable = setmetatable({}, {__call=expect(fn)})
assert(uv.fs_stat('.', callable))
end)
end)