在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):Olivine-Labs/luassert开源软件地址(OpenSource Url):https://github.com/Olivine-Labs/luassert开源编程语言(OpenSource Language):Lua 100.0%开源软件介绍(OpenSource Introduction):Luassertluassert extends Lua's built-in assertions to provide additional tests and the ability to create your own. You can modify chains of assertions with Check out busted for extended examples. assert = require("luassert")
assert.True(true)
assert.is.True(true)
assert.is_true(true)
assert.is_not.True(false)
assert.is.Not.True(false)
assert.is_not_true(false)
assert.are.equal(1, 1)
assert.has.errors(function() error("this should fail") end) Extend your own: local assert = require("luassert")
local say = require("say") --our i18n lib, installed through luarocks, included as a luassert dependency
local function has_property(state, arguments)
local property = arguments[1]
local table = arguments[2]
for key, value in pairs(table) do
if key == property then
return true
end
end
return false
end
say:set_namespace("en")
say:set("assertion.has_property.positive", "Expected property %s in:\n%s")
say:set("assertion.has_property.negative", "Expected property %s to not be in:\n%s")
assert:register("assertion", "has_property", has_property, "assertion.has_property.positive", "assertion.has_property.negative")
assert.has_property("name", { name = "jack" })
When writing your own assertions you can also use modifiers to set specific objects to work against. An example
is the Which can be used as; local arr = { "one", "two", "three" }
assert.array(arr).has.no.holes() -- checks the array to not contain holes --> passes
assert.array(arr).has.no.holes(4) -- sets explicit length to 4 --> fails Implementation notes:
local f = function() end
assert.message("the function 'f' did not return 2 arguments").returned_arguments(2, f()) MatchersArgument matching can be performed on spies/stubs with the ability to create your own. This provides flexible argument matching for local assert = require 'luassert'
local match = require 'luassert.match'
local spy = require 'luassert.spy'
local s = spy.new(function() end)
s('foo')
s(1)
s({}, 'foo')
assert.spy(s).was.called_with(match._) -- arg1 is anything
assert.spy(s).was.called_with(match.is_string()) -- arg1 is a string
assert.spy(s).was.called_with(match.is_number()) -- arg1 is a number
assert.spy(s).was.called_with(match.is_not_true()) -- arg1 is not true
assert.spy(s).was.called_with(match.is_table(), match.is_string()) -- arg1 is a table, arg2 is a string
assert.spy(s).was.called_with(match.has_match('.oo')) -- arg1 contains pattern ".oo"
assert.spy(s).was.called_with({}, 'foo') -- you can still match without using matchers Extend your own: local function is_even(state, arguments)
return function(value)
return (value % 2) == 0
end
end
local function is_gt(state, arguments)
local expected = arguments[1]
return function(value)
return value > expected
end
end
assert:register("matcher", "even", is_even)
assert:register("matcher", "gt", is_gt) local assert = require 'luassert'
local match = require 'luassert.match'
local spy = require 'luassert.spy'
local s = spy.new(function() end)
s(7)
assert.spy(s).was.called_with(match.is_number()) -- arg1 was a number
assert.spy(s).was.called_with(match.is_not_even()) -- arg1 was not even
assert.spy(s).was.called_with(match.is_gt(5)) -- arg1 was greater than 5 Composite matchers have the form: match.all_of(m1, m2, ...) -- argument matches all of the matchers m1 to mn
match.any_of(m1, m2, ...) -- argument matches at least one of the matchers m1 to mn
match.none_of(m1, m2, ...) -- argument does not match any of the matchers m1 to mn If you're creating a spy for methods that mutate any properties on local t = { cnt = 0, }
function t:incrby(i) self.cnt = self.cnt + i end
local s = spy.on(t, "incrby")
t:incrby(2)
assert.spy(s).was_called_with(match.is_ref(t), 2) SnapshotsTo be able to revert changes created by tests, inserting spies and stubs for example, luassert supports 'snapshots'. A snapshot includes the following;
Example: describe("Showing use of snapshots", function()
local snapshot
before_each(function()
snapshot = assert:snapshot()
end)
after_each(function()
snapshot:revert()
end)
it("does some test", function()
-- spies or stubs registered here, parameters changed, or formatters added
-- will be undone in the after_each() handler.
end)
end) ParametersTo register state information 'parameters' can be used. The parameter is included in a snapshot and can hence be restored in between tests. For an example see Example: assert:set_parameter("my_param_name", 1)
local s = assert:snapshot()
assert:set_parameter("my_param_name", 2)
s:revert()
assert.are.equal(1, assert:get_parameter("my_param_name")) Customizing argument formattingluassert comes preloaded with argument formatters for common Lua types, but it is easy to roll your own. Customizing them is especially useful for limiting table depth and for userdata types. Configuring table depth displayThe default table formatter allows you to customize the levels displayed by setting the Example: describe("Tests different levels of table display", function()
local testtable = {
hello = "hola",
world = "mundo",
liqour = {
"beer", "wine", "water"
},
fruit = {
native = { "apple", "strawberry", "grape" },
tropical = { "banana", "orange", "mango" },
},
}
it("tests display of 0 levels", function()
assert:set_parameter("TableFormatLevel", 0)
assert.are.same(testtable, {})
end)
it("tests display of 2 levels", function()
assert:set_parameter("TableFormatLevel", 2)
assert.are.same(testtable, {})
end)
end) Will display the following output with the table pretty-printed to the requested depth:
Customized formattersThe formatters are functions taking a single argument that needs to be converted to a string representation. The formatter should examine the value provided, if it can format the value, it should return the formatted string, otherwise it should return Example using the included binary string formatter: local binstring = require("luassert.formatters.binarystring")
describe("Tests using a binary string formatter", function()
setup(function()
assert:add_formatter(binstring)
end)
teardown(function()
assert:remove_formatter(binstring)
end)
it("tests a string comparison with binary formatting", function()
local s1, s2 = "", ""
for n = 65,88 do
s1 = s1 .. string.char(n)
s2 = string.char(n) .. s2
end
assert.are.same(s1, s2)
end)
end) Because this formatter formats string values, and is added last, it will take precedence over the regular string formatter. The results will be:
|
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论