summaryrefslogtreecommitdiff
path: root/test/test-supple.capi.lua
diff options
context:
space:
mode:
authorDaniel Silverstone <dsilvers@digital-scurf.org>2012-07-21 16:32:07 +0100
committerDaniel Silverstone <dsilvers@digital-scurf.org>2012-07-21 16:32:07 +0100
commitb5460017f1e88baf283ebfaad341cd094f5041ff (patch)
tree445386ab7dbce5dbd3e97078225bb9be3e939960 /test/test-supple.capi.lua
parentb4d2d633cd88308487aa6e2b689a4328ce6023c1 (diff)
downloadsupple-b5460017f1e88baf283ebfaad341cd094f5041ff.tar.gz
Lots of stuff
Diffstat (limited to 'test/test-supple.capi.lua')
-rw-r--r--test/test-supple.capi.lua134
1 files changed, 133 insertions, 1 deletions
diff --git a/test/test-supple.capi.lua b/test/test-supple.capi.lua
index 30dbf81..7438874 100644
--- a/test/test-supple.capi.lua
+++ b/test/test-supple.capi.lua
@@ -1,4 +1,4 @@
--- test/test-supple.lua
+-- test/test-supple.capi.lua
--
-- Supple - Tests for the capi module
--
@@ -38,6 +38,138 @@ function suite.capi_has_abi()
assert(capi._ABI, "No _ABI found in the CAPI")
end
+function suite.capi_explain_non_table_etc()
+ local ok = pcall(capi.explain, nil, "");
+ assert(ok == false, "Explained nil unexpectedly")
+ ok = pcall(capi.explain, true, "");
+ assert(ok == false, "Explained true unexpectedly")
+ ok = pcall(capi.explain, false, "");
+ assert(ok == false, "Explained false unexpectedly")
+ ok = pcall(capi.explain, 123, "");
+ assert(ok == false, "Explained number unexpectedly")
+ ok = pcall(capi.explain, "", "");
+ assert(ok == false, "Explained string unexpectedly")
+ ok = pcall(capi.explain, coroutine.create(function()end), "");
+ assert(ok == false, "Explained coroutine unexpectedly")
+end
+
+function suite.capi_explain_bad_tag()
+ local ok = pcall(capi.explain, {}, nil)
+ assert(ok == false, "Explained with nil tag unexpectedly")
+ ok = pcall(capi.explain, {}, 1233)
+ assert(ok == false, "Explained with number tag unexpectedly")
+ ok = pcall(capi.explain, {}, true)
+ assert(ok == false, "Explained with true tag unexpectedly")
+ ok = pcall(capi.explain, {}, false)
+ assert(ok == false, "Explained with false tag unexpectedly")
+ ok = pcall(capi.explain, {}, {})
+ assert(ok == false, "Explained with table tag unexpectedly")
+ ok = pcall(capi.explain, {}, function()end)
+ assert(ok == false, "Explained with function tag unexpectedly")
+ ok = pcall(capi.explain, {}, coroutine.create(function()end))
+ assert(ok == false, "Explained with coroutine tag unexpectedly")
+end
+
+function suite.capi_explain_simple_table()
+ local expn = capi.explain({}, "fish")
+ assert(type(expn) == "table", "Explanation wasn't a table")
+ assert(type(expn.tag) == "string", "Tag not a string")
+ assert(expn.tag == "fish", "Tag wasn't as passed in")
+ assert(type(expn.type) == "string", "Type wasn't a string")
+ assert(expn.type == "table", "Type didn't declare it is a table")
+ assert(expn.methods == nil, "Methods table was present in some form")
+end
+
+function suite.capi_explain_table_with_mode()
+ local tab = setmetatable({}, { __mode="k"})
+ local expn = capi.explain(tab, "fish")
+ assert(type(expn) == "table", "Explanation wasn't a table")
+ assert(type(expn.tag) == "string", "Tag not a string")
+ assert(expn.tag == "fish", "Tag wasn't as passed in")
+ assert(type(expn.type) == "string", "Type wasn't a string")
+ assert(expn.type == "table", "Type didn't declare it is a table")
+ assert(type(expn.methods) == "table", "Methods were not present")
+ assert(next(expn.methods) == nil, "Methods had something in")
+end
+
+function suite.capi_explain_table_with_index()
+ local tab = setmetatable({}, { __index = {} })
+ local expn = capi.explain(tab, "fish")
+ assert(type(expn) == "table", "Explanation wasn't a table")
+ assert(type(expn.tag) == "string", "Tag not a string")
+ assert(expn.tag == "fish", "Tag wasn't as passed in")
+ assert(type(expn.type) == "string", "Type wasn't a string")
+ assert(expn.type == "table", "Type didn't declare it is a table")
+ assert(type(expn.methods) == "table", "Methods were not present")
+ assert(expn.methods[1] == "__index", "Missing __index")
+ assert(expn.methods[2] == nil, "Something other than __index")
+end
+
+function suite.capi_explain_function()
+ local expn = capi.explain(function()end, "fish")
+ assert(type(expn) == "table", "Explanation wasn't a table")
+ assert(type(expn.tag) == "string", "Tag not a string")
+ assert(expn.tag == "fish", "Tag wasn't as passed in")
+ assert(type(expn.type) == "string", "Type wasn't a string")
+ assert(expn.type == "function", "Type didn't declare it is a table")
+ assert(expn.methods == nil, "Methods table was present in some form")
+end
+
+function suite.capi_type_chains()
+ assert(capi.type({}) == "table", "Plain table wasn't a table")
+ local t = setmetatable({}, { __type = function() return "jeff" end })
+ assert(capi.type(t) == "jeff", "Chained table wasn't jeff")
+end
+
+function suite.capi_rawtype()
+ assert(capi.rawtype() == "nil", "nil was't nil")
+ assert(capi.rawtype({}) == "table", "table wasn't")
+ assert(capi.rawtype("") == "string", "string wasn't")
+ assert(capi.rawtype(false) == "boolean", "boolean wasn't")
+ assert(capi.rawtype(function()end) == "function", "function wasn't")
+ assert(capi.rawtype(coroutine.create(function()end)) == "thread",
+ "coroutine wasn't")
+ assert(capi.rawtype(capi.new_proxy("table")) == "userdata",
+ "userdata wasn't")
+end
+
+function suite.capi_new_proxy_fails()
+ local ok = pcall(capi.new_proxy)
+ assert(ok == false, "Made a new proxy with a nil type string")
+ ok = pcall(capi.new_proxy, {})
+ assert(ok == false, "Made a new proxy with a table type string")
+end
+
+function suite.capi_new_proxy_function()
+ local proxy, mt = capi.new_proxy("function")
+ local sparetype = mt.__type
+ mt.__type = nil
+ assert(capi.type(proxy) == "userdata", "Proxy wasn't userdata")
+ mt.__type = sparetype
+ assert(capi.type(proxy) == "function", "Proxy did not override type")
+ assert(tostring(proxy):match("^function: "), "Proxy didn't tostring nicely")
+end
+
+function suite.capi_new_proxy_table()
+ local proxy, mt = capi.new_proxy("table")
+ local sparetype = mt.__type
+ mt.__type = nil
+ assert(type(proxy) == "userdata", "Proxy wasn't userdata")
+ mt.__type = sparetype
+ assert(capi.type(proxy) == "table", "Proxy did not override type")
+ assert(tostring(proxy):match("^table: "), "Proxy didn't tostring nicely")
+end
+
+function suite.capi_new_proxy_table()
+ local proxy, mt = capi.new_proxy("userdata")
+ local sparetype = mt.__type
+ mt.__type = nil
+ assert(type(proxy) == "userdata", "Proxy wasn't userdata")
+ mt.__type = sparetype
+ assert(capi.type(proxy) == "userdata", "Proxy did not override type")
+ assert(tostring(proxy):match("^userdata: "), "Proxy didn't tostring nicely")
+end
+
local count_ok = 0
for _, testname in ipairs(testnames) do
-- print("Run: " .. testname)