Skip to content

Commit 6d1c3b5

Browse files
committed
feat(test): add optional message to failed assertions
Allow user to add an optional message to failed assertions in mini test. This allow providing more context to specific failures when they arise. Usage: ```lua MiniTest.expect.equality(x, y, "Invalid number of items") MiniTest.expect.no_equality(x, y, "Unexpected line counts") MiniTest.expect.reference_screenshot(screen, path, nil, "Buffer not matching") ```
1 parent 80a1149 commit 6d1c3b5

File tree

3 files changed

+32
-6
lines changed

3 files changed

+32
-6
lines changed

doc/mini-test.txt

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -525,25 +525,27 @@ Usage ~
525525
<
526526
------------------------------------------------------------------------------
527527
*MiniTest.expect.equality()*
528-
`MiniTest.expect.equality`({left}, {right})
528+
`MiniTest.expect.equality`({left}, {right}, {msg})
529529
Expect equality of two objects
530530

531531
Equality is tested via |vim.deep_equal()|.
532532

533533
Parameters ~
534534
{left} `(any)` First object.
535535
{right} `(any)` Second object.
536+
{msg} `(string|nil)` Fails with the supplied failure message.
536537

537538
------------------------------------------------------------------------------
538539
*MiniTest.expect.no_equality()*
539-
`MiniTest.expect.no_equality`({left}, {right})
540+
`MiniTest.expect.no_equality`({left}, {right}, {msg})
540541
Expect no equality of two objects
541542

542543
Equality is tested via |vim.deep_equal()|.
543544

544545
Parameters ~
545546
{left} `(any)` First object.
546547
{right} `(any)` Second object.
548+
{msg} `(string|nil)` Fails with the supplied failure message.
547549

548550
------------------------------------------------------------------------------
549551
*MiniTest.expect.error()*
@@ -567,7 +569,7 @@ Parameters ~
567569

568570
------------------------------------------------------------------------------
569571
*MiniTest.expect.reference_screenshot()*
570-
`MiniTest.expect.reference_screenshot`({screenshot}, {path}, {opts})
572+
`MiniTest.expect.reference_screenshot`({screenshot}, {path}, {opts}, {msg})
571573
Expect equality to reference screenshot
572574

573575
Parameters ~
@@ -589,6 +591,7 @@ Parameters ~
589591
if `false` - do not ignore any. Default: `false`.
590592
- <directory> `(string)` - directory where automatically constructed `path`
591593
is located. Default: "tests/screenshots".
594+
{msg} `(string|nil)` Fails with the supplied failure message.
592595

593596
------------------------------------------------------------------------------
594597
*MiniTest.new_expectation()*

lua/mini/test.lua

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -666,10 +666,12 @@ MiniTest.expect = {}
666666
---
667667
---@param left any First object.
668668
---@param right any Second object.
669-
MiniTest.expect.equality = function(left, right)
669+
---@param msg string|nil Fails with the supplied failure message.
670+
MiniTest.expect.equality = function(left, right, msg)
670671
if vim.deep_equal(left, right) then return true end
671672

672673
local context = string.format('Left: %s\nRight: %s', vim.inspect(left), vim.inspect(right))
674+
if msg then context = string.format('%s\n%s', msg, context) end
673675
H.error_expect('equality', context)
674676
end
675677

@@ -679,10 +681,12 @@ end
679681
---
680682
---@param left any First object.
681683
---@param right any Second object.
682-
MiniTest.expect.no_equality = function(left, right)
684+
---@param msg string|nil Fails with the supplied failure message.
685+
MiniTest.expect.no_equality = function(left, right, msg)
683686
if not vim.deep_equal(left, right) then return true end
684687

685688
local context = string.format('Object: %s', vim.inspect(left))
689+
if msg then context = string.format('%s\n%s', msg, context) end
686690
H.error_expect('*no* equality', context)
687691
end
688692

@@ -739,7 +743,8 @@ end
739743
--- if `false` - do not ignore any. Default: `false`.
740744
--- - <directory> `(string)` - directory where automatically constructed `path`
741745
--- is located. Default: "tests/screenshots".
742-
MiniTest.expect.reference_screenshot = function(screenshot, path, opts)
746+
---@param msg string|nil Fails with the supplied failure message.
747+
MiniTest.expect.reference_screenshot = function(screenshot, path, opts, msg)
743748
if screenshot == nil then return true end
744749

745750
local default_opts = { force = false, ignore_text = false, ignore_attr = false, directory = 'tests/screenshots' }
@@ -794,6 +799,7 @@ MiniTest.expect.reference_screenshot = function(screenshot, path, opts)
794799
local cause = same_text and cause_attr or cause_text
795800
local subject = 'screenshot equality to reference at ' .. vim.inspect(path)
796801
local context = string.format('%s\nReference:\n%s\n\nObserved:\n%s', cause, tostring(reference), tostring(screenshot))
802+
if msg then context = string.format('%s\n%s', msg, context) end
797803
H.error_expect(subject, context)
798804
end
799805

tests/test_test.lua

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -752,6 +752,13 @@ T['expect']['equality()/no_equality()']['return `true` on success'] = function()
752752
eq(MiniTest.expect.no_equality(1, 2), true)
753753
end
754754

755+
T['expect']['equality()/no_equality()']['print message on failure'] = function()
756+
local validate = function(fn, x, y, msg) expect.error(fn, '.*Test msg.*', x, y, msg) end
757+
758+
validate(MiniTest.expect.equality, 1, 2, 'Test msg')
759+
validate(MiniTest.expect.no_equality, 1, 1, 'Test msg')
760+
end
761+
755762
T['expect']['error()'] = new_set()
756763

757764
T['expect']['error()']['works'] = function()
@@ -1061,6 +1068,16 @@ T['expect']['reference_screenshot()']['works with multibyte characters'] = funct
10611068
expect.no_error(function() MiniTest.expect.reference_screenshot(child.get_screenshot()) end)
10621069
end
10631070

1071+
T['expect']['reference_screenshot()']['print message on failure'] = function()
1072+
local path = get_ref_path('reference-screenshot')
1073+
child.set_size(5, 12)
1074+
set_lines({ 'bbb' })
1075+
expect.error(
1076+
function() MiniTest.expect.reference_screenshot(child.get_screenshot(), path, nil, 'Test msg') end,
1077+
'screenshot equality to reference at ' .. vim.pesc(vim.inspect(path)) .. '.*Test msg.*Reference:.*Observed:'
1078+
)
1079+
end
1080+
10641081
T['new_expectation()'] = new_set()
10651082

10661083
T['new_expectation()']['works'] = function()

0 commit comments

Comments
 (0)