-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.lua
executable file
·98 lines (83 loc) · 2.09 KB
/
test.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
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
#!/usr/bin/env luvit
local table = require 'table'
local timer = require 'timer'
local twisted = require "./init.lua"
local bourbon = require './bourbon'
local tests = {}
tests.test_sync = function(test, asserts)
local f = twisted.inline_callbacks(function()
local var = 1
var = twisted.yield(var+1)
var = twisted.yield(var+1)
return {nil, var}
end)
f(function(err, res)
asserts.equal(err, nil, "got err")
test.done()
end)
end
tests.test_err = function(test, asserts)
local f = twisted.inline_callbacks(function()
asdf()
end)
f(function(err, res)
asserts.not_nil(err, 'need an err')
test.done()
end)
end
tests.test_async = function(test, asserts)
local f = twisted.inline_callbacks(function()
local func = function(cb)
timer.setTimeout(10, cb, 2)
end
local res = twisted.yield(func)
asserts.equal(res, 2)
end)
f(function(err, res)
asserts.equal(err, nil)
test.done()
end)
end
tests.test_async_with_args = function(test, asserts)
local f = twisted.inline_callbacks(function()
local func = function(a, b, c, cb)
timer.setTimeout(10, cb, c)
end
local res = twisted.yield(func, 1, nil, 3)
asserts.equal(res, 3)
end)
f(function(err, res)
asserts.equal(err, nil)
test.done()
end)
end
tests.test_sentinel = function(test, asserts)
local f = twisted.inline_callbacks(function()
local func = function(a, b, cb)
asserts.not_nil(cb, 'should be a function')
timer.setTimeout(10, cb, 2)
end
local res = twisted.yield(func, 1, nil, twisted.SENTINEL)
asserts.equal(res, 2)
end)
f(function(err, res)
asserts.equal(err, nil)
test.done()
end)
end
tests.test_return = function(test, asserts)
local f = twisted.inline_callbacks(function()
local func = function(a, cb)
asserts.not_nil(cb, 'should be a function')
timer.setTimeout(10, cb, a)
end
local res = twisted.yield(func, 1, twisted.SENTINEL)
return {nil, res}
end)
f(function(err, res)
asserts.equal(err, nil)
asserts.equal(res, 1)
test.done()
end)
end
bourbon.run(tests)