Skip to content

Commit

Permalink
add modifiers a, an, and the
Browse files Browse the repository at this point in the history
This enables assertions such as:

`assert.is.a.string()`
`assert.are.the.same()`
`assert.has_an_error()`
  • Loading branch information
tmillr authored and Tieske committed Sep 23, 2024
1 parent b7b52b6 commit 7b4f451
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
17 changes: 17 additions & 0 deletions spec/assertions_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -348,46 +348,63 @@ describe("Test Assertions", function()

it("Checks '_' chaining of modifiers and assertions", function()
assert.is_string("abc")
assert.is_a_string("abc")
assert.is_true(true)
assert.is_not_string(123)
assert.is_not_a_string(123)
assert.is_nil(nil)
assert.is_not_nil({})
assert.is_not_true(false)
assert.is_not_false(true)
assert.are_the_same({a=1}, {a=1})
assert.are_not_the_same({a=1}, {b=1})

-- verify that failing assertions actually fail
assert.has_error(function() assert.is_string(1) end)
assert.has_error(function() assert.is_true(false) end)
assert.has_error(function() assert.is_not_string('string!') end)
assert.has_error(function() assert.is_not_a_string('string!') end)
assert.has_error(function() assert.is_nil({}) end)
assert.has_error(function() assert.is_not_nil(nil) end)
assert.has_error(function() assert.is_not_true(true) end)
assert.has_error(function() assert.is_not_false(false) end)
assert.has_error(function() assert.are_the_same({a=1}, {b=1}) end)
assert.has_a_error(function() assert.are_not_the_same({a=1}, {a=1}) end)
assert.has_an_error(function() assert.are_not_the_same({a=1}, {a=1}) end)
end)

it("Checks '.' chaining of modifiers and assertions", function()
assert.is.string("abc")
assert.is.a.string("abc")
assert.is.True(true)
assert.is.Not.string(123)
assert.is.Not.a.string(123)
assert.is.Nil(nil)
assert.is.Not.Nil({})
assert.is.Not.True(false)
assert.is.Not.False(true)
assert.equals.Not(true, false)
assert.equals.Not.Not(true, true)
assert.Not.equals.Not(true, true)
assert.are.the.same({a=1}, {a=1})
assert.are.Not.the.same({a=1}, {b=1})

-- verify that failing assertions actually fail
assert.has.error(function() assert.is.string(1) end)
assert.has.error(function() assert.is.a.string(1) end)
assert.has.error(function() assert.is.True(false) end)
assert.has.error(function() assert.is.Not.string('string!') end)
assert.has.error(function() assert.is.Not.a.string('string!') end)
assert.has.error(function() assert.is.Nil({}) end)
assert.has.error(function() assert.is.Not.Nil(nil) end)
assert.has.error(function() assert.is.Not.True(true) end)
assert.has.error(function() assert.is.Not.False(false) end)
assert.has.error(function() assert.equals.Not(true, true) end)
assert.has.error(function() assert.equals.Not.Not(true, false) end)
assert.has.error(function() assert.Not.equals.Not(true, false) end)
assert.has.error(function() assert.are.the.same({a=1}, {b=1}) end)
assert.has.a.error(function() assert.are.Not.the.same({a=1}, {a=1}) end)
assert.has.an.error(function() assert.are.Not.the.same({a=1}, {a=1}) end)
end)

it("Checks number of returned arguments", function()
Expand Down
3 changes: 3 additions & 0 deletions src/modifiers.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ local function is_not(state)
return state
end

assert:register("modifier", "a", is)
assert:register("modifier", "an", is)
assert:register("modifier", "the", is)
assert:register("modifier", "is", is)
assert:register("modifier", "are", is)
assert:register("modifier", "was", is)
Expand Down

0 comments on commit 7b4f451

Please sign in to comment.