Skip to content

Commit 5612643

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 5612643

File tree

3 files changed

+40
-6
lines changed

3 files changed

+40
-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: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -666,10 +666,14 @@ 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
675+
context = string.format('%s\n%s', msg, context)
676+
end
673677
H.error_expect('equality', context)
674678
end
675679

@@ -679,10 +683,14 @@ end
679683
---
680684
---@param left any First object.
681685
---@param right any Second object.
682-
MiniTest.expect.no_equality = function(left, right)
686+
---@param msg string|nil Fails with the supplied failure message.
687+
MiniTest.expect.no_equality = function(left, right, msg)
683688
if not vim.deep_equal(left, right) then return true end
684689

685690
local context = string.format('Object: %s', vim.inspect(left))
691+
if msg then
692+
context = string.format('%s\n%s', msg, context)
693+
end
686694
H.error_expect('*no* equality', context)
687695
end
688696

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

745754
local default_opts = { force = false, ignore_text = false, ignore_attr = false, directory = 'tests/screenshots' }
@@ -794,6 +803,9 @@ MiniTest.expect.reference_screenshot = function(screenshot, path, opts)
794803
local cause = same_text and cause_attr or cause_text
795804
local subject = 'screenshot equality to reference at ' .. vim.inspect(path)
796805
local context = string.format('%s\nReference:\n%s\n\nObserved:\n%s', cause, tostring(reference), tostring(screenshot))
806+
if msg then
807+
context = string.format('%s\n%s', msg, context)
808+
end
797809
H.error_expect(subject, context)
798810
end
799811

tests/test_test.lua

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -752,6 +752,15 @@ 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)
757+
expect.error(fn, '.*Test msg.*', x, y, msg)
758+
end
759+
760+
validate(MiniTest.expect.equality, 1, 2, "Test msg")
761+
validate(MiniTest.expect.no_equality, 1, 1, "Test msg")
762+
end
763+
755764
T['expect']['error()'] = new_set()
756765

757766
T['expect']['error()']['works'] = function()
@@ -1061,6 +1070,16 @@ T['expect']['reference_screenshot()']['works with multibyte characters'] = funct
10611070
expect.no_error(function() MiniTest.expect.reference_screenshot(child.get_screenshot()) end)
10621071
end
10631072

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

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

0 commit comments

Comments
 (0)