Created
August 29, 2014 11:44
-
-
Save kikito/4e2cb64d870116b50680 to your computer and use it in GitHub Desktop.
Lua Busted table contains assertion
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local s = require("say") | |
local function contains(container, contained) | |
if container == contained then return true end | |
local t1,t2 = type(container), type(contained) | |
if t1 ~= t2 then return false end | |
if t1 == 'table' then | |
for k,v in pairs(contained) do | |
if not contains(container[k], v) then return false end | |
end | |
return true | |
end | |
return false | |
end | |
local function contains_for_luassert(state, arguments) | |
return contains(arguments[1], arguments[2]) | |
end | |
s:set("assertion.contains.positive", "Expected %s\n to contain \n%s") | |
s:set("assertion.contains.negative", "Expected %s\n to NOT contain \n%s") | |
assert:register("assertion", "contains", contains_for_luassert, "assertion.contains.positive", "assertion.contains.negative") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'assert_contains' | |
describe("contains", function() | |
it("works in the basic case", function() | |
assert.contains({a=1,b=2,c=3}, {a=1}) | |
assert.contains({a=1,b=2,c=3}, {a=1, c=3}) | |
assert.not_contains({a=1,b=2,c=3}, {c=5}) | |
assert.not_contains({a=1,b=2,c=3}, {x=5}) | |
assert.not_contains({a=1,b=2,c=3}, {a=1, b=5, c=3}) | |
end) | |
it("works in the complex case", function() | |
assert.contains({a={b=1}, c=1}, {a={b=1}}) | |
end) | |
end) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment