diff options
Diffstat (limited to 'test/test-supple.capi.lua')
-rw-r--r-- | test/test-supple.capi.lua | 68 |
1 files changed, 68 insertions, 0 deletions
diff --git a/test/test-supple.capi.lua b/test/test-supple.capi.lua index 7438874..f46c3eb 100644 --- a/test/test-supple.capi.lua +++ b/test/test-supple.capi.lua @@ -170,6 +170,74 @@ function suite.capi_new_proxy_table() assert(tostring(proxy):match("^userdata: "), "Proxy didn't tostring nicely") end +function suite.capi_test_next_on_table() + local t = { foo = "bar" } + local k, v = capi.next(t) + assert(k == "foo", "Key was not 'foo'") + assert(v == "bar", "Value was not 'bar'") + assert(capi.next(t, "foo") == nil, "Next after 'foo' was not nil") +end + +function suite.capi_test_next_on_proxied_table() + local proxied = { foo = "bar" } + local proxy, mt = capi.new_proxy("table") + mt.__next = function(self, key) + return next(proxied, key) + end + local k, v = capi.next(proxy) + assert(k == "foo", "Key was not 'foo'") + assert(v == "bar", "Value was not 'bar'") + assert(capi.next(proxy, "foo") == nil, "Next after 'foo' was not nil") +end + +function suite.capi_test_ipairs_on_table() + local tab = { "a", "b" } + for i, v in capi.ipairs(tab) do + assert(i > 0 and i < 3, "Index out of range") + assert((i == 1 and v == "a") or + (i == 2 and v == "b"), + "Key/value pair wrong") + end +end + +function suite.capi_test_ipairs_on_proxied_table() + local proxied = { "a", "b" } + local tab, mt = capi.new_proxy("table") + mt.__index = function(self, idx) + return proxied[idx] + end + for i, v in capi.ipairs(tab) do + assert(i > 0 and i < 3, "Index out of range") + assert((i == 1 and v == "a") or + (i == 2 and v == "b"), + "Key/value pair wrong") + end +end + +function suite.capi_test_pairs_on_table() + local tab = { foo = "bar", baz = "meta" } + for k, v in capi.pairs(tab) do + assert(k == "foo" or k == "baz", "Key not right") + assert((k == "foo" and v == "bar") or + (k == "baz" and v == "meta"), + "key/value pair wrong") + end +end + +function suite.capi_test_pairs_on_proxied_table() + local proxied = { foo = "bar", baz = "meta" } + local tab, mt = capi.new_proxy("table") + mt.__next = function(self, idx) + return next(proxied, idx) + end + for k, v in capi.pairs(tab) do + assert(k == "foo" or k == "baz", "Key not right") + assert((k == "foo" and v == "bar") or + (k == "baz" and v == "meta"), + "key/value pair wrong") + end +end + local count_ok = 0 for _, testname in ipairs(testnames) do -- print("Run: " .. testname) |