Skip to content

Commit 5129fa9

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, { msg = "Invalid number of items" }) MiniTest.expect.no_equality(x, y, { msg = "Unexpected line counts" }) MiniTest.expect.reference_screenshot(screen, path, { msg = "Buffer not matching" }) ```
1 parent 80a1149 commit 5129fa9

File tree

3 files changed

+37
-4
lines changed

3 files changed

+37
-4
lines changed

doc/mini-test.txt

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -525,25 +525,29 @@ Usage ~
525525
<
526526
------------------------------------------------------------------------------
527527
*MiniTest.expect.equality()*
528-
`MiniTest.expect.equality`({left}, {right})
528+
`MiniTest.expect.equality`({left}, {right}, {opts})
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+
{opts} `(table|nil)` Options:
537+
- <msg> `(string)` Fails with the supplied failure message. Default: nil
536538

537539
------------------------------------------------------------------------------
538540
*MiniTest.expect.no_equality()*
539-
`MiniTest.expect.no_equality`({left}, {right})
541+
`MiniTest.expect.no_equality`({left}, {right}, {opts})
540542
Expect no equality of two objects
541543

542544
Equality is tested via |vim.deep_equal()|.
543545

544546
Parameters ~
545547
{left} `(any)` First object.
546548
{right} `(any)` Second object.
549+
{opts} `(table|nil)` Options:
550+
- <msg> `(string)` Fails with the supplied failure message. Default: nil
547551

548552
------------------------------------------------------------------------------
549553
*MiniTest.expect.error()*
@@ -589,6 +593,7 @@ Parameters ~
589593
if `false` - do not ignore any. Default: `false`.
590594
- <directory> `(string)` - directory where automatically constructed `path`
591595
is located. Default: "tests/screenshots".
596+
- <msg> `(string)` - Fails with the supplied failure message. Default: nil
592597

593598
------------------------------------------------------------------------------
594599
*MiniTest.new_expectation()*

lua/mini/test.lua

Lines changed: 13 additions & 2 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 opts table|nil Options:
670+
--- - <msg> `(string)` Fails with the supplied failure message. Default: nil
671+
MiniTest.expect.equality = function(left, right, opts)
670672
if vim.deep_equal(left, right) then return true end
671673

672674
local context = string.format('Left: %s\nRight: %s', vim.inspect(left), vim.inspect(right))
675+
opts = opts or {}
676+
if opts.msg then context = string.format('%s\n%s', opts.msg, context) end
673677
H.error_expect('equality', context)
674678
end
675679

@@ -679,10 +683,15 @@ end
679683
---
680684
---@param left any First object.
681685
---@param right any Second object.
682-
MiniTest.expect.no_equality = function(left, right)
686+
---@param opts table|nil Options:
687+
--- - <msg> `(string)` Fails with the supplied failure message. Default: nil
688+
MiniTest.expect.no_equality = function(left, right, opts)
689+
opts = opts or {}
683690
if not vim.deep_equal(left, right) then return true end
684691

685692
local context = string.format('Object: %s', vim.inspect(left))
693+
opts = opts or {}
694+
if opts.msg then context = string.format('%s\n%s', opts.msg, context) end
686695
H.error_expect('*no* equality', context)
687696
end
688697

@@ -739,6 +748,7 @@ end
739748
--- if `false` - do not ignore any. Default: `false`.
740749
--- - <directory> `(string)` - directory where automatically constructed `path`
741750
--- is located. Default: "tests/screenshots".
751+
--- - <msg> `(string)` - Fails with the supplied failure message. Default: nil
742752
MiniTest.expect.reference_screenshot = function(screenshot, path, opts)
743753
if screenshot == nil then return true end
744754

@@ -794,6 +804,7 @@ MiniTest.expect.reference_screenshot = function(screenshot, path, opts)
794804
local cause = same_text and cause_attr or cause_text
795805
local subject = 'screenshot equality to reference at ' .. vim.inspect(path)
796806
local context = string.format('%s\nReference:\n%s\n\nObserved:\n%s', cause, tostring(reference), tostring(screenshot))
807+
if opts.msg then context = string.format('%s\n%s', opts.msg, context) end
797808
H.error_expect(subject, context)
798809
end
799810

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, opts) expect.error(fn, '.*Test msg.*', x, y, opts) end
757+
758+
validate(MiniTest.expect.equality, 1, 2, { msg = 'Test msg' })
759+
validate(MiniTest.expect.no_equality, 1, 1, { msg = '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, { msg = '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)