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
|
-- test/test-supple.lua
--
-- Supple - Tests for the core module
--
-- Copyright 2012 Daniel Silverstone <dsilvers@digital-scurf.org>
--
-- For Licence terms, see COPYING
--
-- Step one, start coverage
local luacov = require 'luacov'
local objects = require 'supple.objects'
local capi = require 'supple.capi'
local testnames = {}
local real_assert = assert
local total_asserts = 0
local function assert(...)
local retval = real_assert(...)
total_asserts = total_asserts + 1
return retval
end
local function add_test(suite, name, value)
rawset(suite, name, value)
testnames[#testnames+1] = name
end
local suite = setmetatable({}, {__newindex = add_test})
function suite.give_integral_types()
assert(objects.give(nil) == nil, "Nil didn't pass through")
assert(objects.give(true) == true, "True didn't pass through")
assert(objects.give(false) == false, "False didn't pass through")
assert(objects.give(123) == 123, "Number didn't pass through")
assert(objects.give("") == "", "String didn't pass through")
end
function suite.set_name_works()
local unique_name = tostring({})
objects.set_name(unique_name)
local expn = objects.give({})
assert(expn.tag:find(unique_name), "set_name not passing through")
end
function suite.give_table()
local tab = {}
objects.set_name("give_table")
local expn = objects.give(tab)
assert(tab ~= expn, "Table passed through")
assert(expn.tag:match("^give_table:"), "Tag didn't come through")
local expn2 = objects.give(tab)
assert(expn ~= expn2, "Expansion was the same")
assert(expn.tag == expn2.tag, "Expansion tags did not match")
end
function suite.receive_integral_types()
assert(objects.receive(nil) == nil, "Nil didn't pass through")
assert(objects.receive(true) == true, "True didn't pass through")
assert(objects.receive(false) == false, "False didn't pass through")
assert(objects.receive(123) == 123, "Number didn't pass through")
assert(objects.receive("") == "", "String didn't pass through")
end
function suite.receive_function()
local last_cb = nil
local function cb(tag, name, ...)
last_cb = {
tag = tag,
name = name,
args = {...}
}
return last_cb
end
local unique_id = tostring({})
objects.set_proc_call(cb)
local obj = objects.receive {
tag = unique_id,
type = "function"
}
assert(capi.rawtype(obj) == "userdata", "Not a proxy")
assert(capi.type(obj) == "function", "Not a proxied function")
assert(obj("fish") == last_cb, "Call method not propagated")
assert(type(last_cb) == "table", "Call didn't reach callback")
assert(last_cb.tag == unique_id, "Tag not propagated")
assert(last_cb.name == "__call", "__call not propagated")
assert(last_cb.args[1] == obj, "obj not propagated")
assert(last_cb.args[2] == "fish", "args not propagated")
local obj2 = objects.receive {
tag = unique_id
}
assert(obj == obj2, "Same object should be same object")
end
function suite.receive_one_of_ours()
local tab = {}
local ours = objects.give(tab)
local back = objects.receive({tag=ours.tag})
assert(tab == back, "Reception of one of ours didn't work")
end
function suite.give_one_of_theirs()
local function cb(...)
end
objects.set_proc_call(cb)
local unique_id = "give_one_of_theirs:"..tostring({})
local obj = objects.receive {
tag = unique_id,
type = "function"
}
local out = objects.give(obj)
assert(type(out) == "table", "giving a proxy didn't tabulate")
assert(out.tag == unique_id, "giving back one of theirs wasn't right")
end
function suite.receive_table()
local last_cb = nil
local function cb(tag, name, ...)
last_cb = {
tag = tag,
name = name,
args = {...}
}
return last_cb
end
local unique_id = tostring({})
objects.set_proc_call(cb)
local obj = objects.receive {
tag = unique_id,
type = "table"
}
assert(capi.rawtype(obj) == "userdata", "Not a proxy")
assert(capi.type(obj) == "table", "Not a proxied function")
assert(obj.thingy == last_cb, "Didn't proxy index")
assert(last_cb.name == "__index", "didn't proxy")
assert(last_cb.args[2] == "thingy", "proxy wasn't right")
end
function suite.received_object_gc()
local last_cb = nil
local function cb(tag, name, ...)
last_cb = {
tag = tag,
name = name,
}
return last_cb
end
local unique_id = tostring({})
objects.set_proc_call(cb)
local obj = objects.receive {
tag = unique_id,
type = "table"
}
assert(capi.rawtype(obj) == "userdata", "Not a proxy")
assert(capi.type(obj) == "table", "Not a proxied function")
-- deliberately lose the object
obj = nil
collectgarbage()
assert(last_cb.name == "__gc", "GC didn't go through")
end
local count_ok = 0
for _, testname in ipairs(testnames) do
-- print("Run: " .. testname)
local ok, err = xpcall(suite[testname], debug.traceback)
if not ok then
print(err)
print()
else
count_ok = count_ok + 1
end
end
print(tostring(count_ok) .. "/" .. tostring(#testnames) .. " [" .. tostring(total_asserts) .. "] OK")
os.exit(count_ok == #testnames and 0 or 1)
|