Skip to content

Commit

Permalink
Fixing the error that occurs when a failing assertion that uses the m…
Browse files Browse the repository at this point in the history
…atcher `match._` tries to format that matcher into the message.
  • Loading branch information
stringTrimmer authored and Tieske committed Aug 31, 2023
1 parent 8c9385b commit 376fdb4
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 3 deletions.
2 changes: 2 additions & 0 deletions spec/formatters_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,8 @@ describe("Test Formatters", function()
local nostringformatted = assert:format({nostring, ["n"] = 1})[1]
assert.is.same("(matcher) no.string()",
nostringformatted)
local anythingmatcherformatted = assert:format({match._, ["n"] = 1})[1]
assert.is.same("(matcher) _ *anything*", anythingmatcherformatted)
end)

it("checks arglist formatting", function()
Expand Down
24 changes: 23 additions & 1 deletion spec/spies_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,18 @@ describe("Tests dealing with spies", function()
.. " %[1%] = 'test' } } }.\n"
.. "Expected:\n"
.. "%(values list%) %(%(number%) 5, %(number%) 6%)")
end)
-- assertion can format the `_` anything matcher appropriately in the fail message
assert.error_matches(
function() assert.spy(s).returned_with(5, _) end,
"Function never returned matching arguments.\n"
.. "Returned %(last call if any%):\n"
.. "%(values list%) %(%(table: 0x%x+%) {\n"
.. " %[foo%] = {\n"
.. " %[bar%] = {\n"
.. " %[1%] = 'test' } } }.\n"
.. "Expected:\n"
.. "%(values list%) %(%(number%) 5, %(matcher%) _ %*anything%*%)")
end) -- (matcher) _ *anything*

it("checks called() and called_with() assertions", function()
local s = spy.new(function() end)
Expand Down Expand Up @@ -97,6 +108,17 @@ describe("Tests dealing with spies", function()
.. " %[1%] = 'test' } } }%)\n"
.. "Expected:\n"
.. "%(values list%) %(%(number%) 5, %(number%) 6%)")
-- assertion can format the `_` anything matcher appropriately in the fail message
assert.error_matches(
function() assert.spy(s).was.called_with(5, _) end,
"Function was never called with matching arguments.\n"
.. "Called with %(last call if any%):\n"
.. "%(values list%) %(%(table: 0x%x+%) {\n"
.. " %[foo%] = {\n"
.. " %[bar%] = {\n"
.. " %[1%] = 'test' } } }%)\n"
.. "Expected:\n"
.. "%(values list%) %(%(number%) 5, %(matcher%) _ %*anything%*%)")
end)

it("checks called() and called_with() assertions using refs", function()
Expand Down
2 changes: 1 addition & 1 deletion src/formatters/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ local function fmt_matcher(arg)
for idx = 1, arg.arguments.n do
table.insert(args, assert:format({ arg.arguments[idx], n = 1, })[1])
end
return string.format("(matcher) %s%s(%s)",
return string.format("(matcher) " .. (arg.name == "_" and "_ *anything*" or "%s%s(%s)"),
not_inverted[arg.mod],
tostring(arg.name),
table.concat(args, ", "))
Expand Down
7 changes: 6 additions & 1 deletion src/match.lua
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,12 @@ local state_mt = {
}

local match = {
_ = setmetatable({mod=true, callback=function() return true end}, matcher_mt),
_ = setmetatable({
name = "_",
mod = true,
callback = function() return true end,
arguments = { n = 0 },
}, matcher_mt),

state = function() return setmetatable({mod=true, tokens={}}, state_mt) end,

Expand Down

0 comments on commit 376fdb4

Please sign in to comment.