Skip to content

Commit

Permalink
Merge pull request #113 from TAServers/74-diff-renderer
Browse files Browse the repository at this point in the history
74 - Add diff renderer and improve reporting
  • Loading branch information
Derpius authored Dec 27, 2023
2 parents bdc45ac + efef146 commit c0f015c
Show file tree
Hide file tree
Showing 27 changed files with 953 additions and 283 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions packages/lest/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,20 @@ The valid change types are:
- `Fixed` for any bug fixes.
- `Security` in case of vulnerabilities.

## [3.1.0] - [#113](https://github.com/TAServers/lest/pull/113)

### Added

- Added pretty diff rendering to `toEqual` matcher to show differences between deeply nested values

### Changed

- `toBe` matcher now generates its message more in line with Jest to make comparing values easier
- All values displayed in test failure messages are now properly serialised to mostly valid Lua, including the contents of tables
- Test failure messages no longer include the expected and received values in the `expect(...).matcherName(...)` signature
- This matches the behaviour of Jest and avoids duplicating information between the signature and failure message
- Test failure messages are no longer highlighted in red to give more control to matchers over how individual messages are formatted

## [3.0.0] - [#102](https://github.com/TAServers/lest/issues/102)

### Added
Expand Down
2 changes: 2 additions & 0 deletions packages/lest/lest.config.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
package.path = "./src/lua/?.lua;./src/lua/?/index.lua;./?.lua"

return {
testMatch = { "tests/lua/.+%.test%.lua", "src/lua/.+%.test%.lua" },
}
6 changes: 3 additions & 3 deletions packages/lest/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@taservers/lest",
"version": "3.0.0",
"version": "3.1.0",
"license": "MIT",
"description": "Painless Lua testing.",
"homepage": "https://taservers.github.io/lest/",
Expand All @@ -16,10 +16,10 @@
"lest": "./dist/bin/lest.js"
},
"scripts": {
"build:lua": "luabundler bundle src/lua/lest.lua -p \"./?.lua\" -o dist/lua/lest.lua",
"build:lua": "luabundler bundle src/lua/lest.lua -p \"./src/lua/?.lua\" -p \"./src/lua/?/index.lua\" -p \"./?.lua\" -o dist/lua/lest.lua",
"build:ts": "tsc -p tsconfig.build.json",
"build": "npm run build:lua && npm run build:ts",
"test:lua": "lua src/lua/lest.lua",
"test:lua": "npm run build:lua && lua dist/lua/lest.lua",
"test:ts": "jest",
"test": "npm run test:lua && npm run test:ts",
"clean": "rimraf dist",
Expand Down
15 changes: 9 additions & 6 deletions packages/lest/src/lua/asserts/matchers.lua
Original file line number Diff line number Diff line change
@@ -1,29 +1,32 @@
--- Asserts the matcher passed.
---@param result lest.MatcherResult
local function assertPass(result)
assert(result.pass, debug.traceback("Expected matcher to pass", 2))
if not result.pass then
error("Expected matcher to pass", 2)
end
end

--- Asserts the matcher failed.
---@param result lest.MatcherResult
local function assertFail(result)
assert(not result.pass, debug.traceback("Expected matcher to fail", 2))
if result.pass then
error("Expected matcher to fail", 2)
end
end

--- Asserts the matcher returned the given message.
---@param result lest.MatcherResult
local function assertMessage(result, message)
assert(
result.message == message,
debug.traceback(
if result.message ~= message then
error(
string.format(
"Expected message: %s\nReceived message: %s",
message,
result.message
),
2
)
)
end
end

return {
Expand Down
8 changes: 4 additions & 4 deletions packages/lest/src/lua/asserts/type.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
local TypeError = require("src.lua.errors.type")
local prettyValue = require("src.lua.utils.prettyValue")
local TypeError = require("errors.type")
local serialiseValue = require("utils.serialise-value")

--- Asserts the object is of the specified type
---
Expand All @@ -9,7 +9,7 @@ local prettyValue = require("src.lua.utils.prettyValue")
---@param label? string
---@param level? number
return function(object, typeStringOrMeta, label, level)
label = label or prettyValue(object)
label = label or serialiseValue(object)
level = (level or 1) + 1

if
Expand All @@ -34,7 +34,7 @@ return function(object, typeStringOrMeta, label, level)
string.format(
"Expected %s to be an instance of %s",
label,
prettyValue(typeStringOrMeta)
serialiseValue(typeStringOrMeta)
)
),
level
Expand Down
9 changes: 2 additions & 7 deletions packages/lest/src/lua/errors/testresult.lua
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
local COLOURS = require("src.lua.utils.consoleColours")
local registerError = require("src.lua.errors.register")
local registerError = require("errors.register")
return registerError("TestResultError", function(message, signature)
return {
message = message,
signature = signature,
}
end, {
__tostring = function(self)
return string.format(
"%s\n\n%s",
self.signature,
COLOURS.FAIL(self.message)
)
return string.format("%s\n\n%s", self.signature, self.message)
end,
})
22 changes: 7 additions & 15 deletions packages/lest/src/lua/expect.lua
Original file line number Diff line number Diff line change
@@ -1,30 +1,22 @@
local COLOURS = require("src.lua.utils.consoleColours")
local COLOURS = require("utils.consoleColours")

local matchers = require("src.lua.matchers")
local prettyValue = require("src.lua.utils.prettyValue")
local TestResultError = require("src.lua.errors.testresult")
local matchers = require("matchers")
local TestResultError = require("errors.testresult")

--- Builds a signature for the expect call
---@param name string -- Name of the matcher that was used
---@param args any -- Arguments passed to the matcher
---@param received any
---@param inverted boolean -- True if the condition was inverted
---@return string
local function buildSignature(name, args, received, inverted)
local stringArgs = {}
for i, arg in ipairs(args) do
stringArgs[i] = prettyValue(arg)
end

local function buildSignature(name, inverted)
return string.format(
"%s%s%s%s.%s%s%s%s",
COLOURS.DIMMED("expect("),
COLOURS.RECEIVED(prettyValue(received)),
COLOURS.RECEIVED("received"),
COLOURS.DIMMED(")"),
inverted and ".never" or "",
COLOURS.HIGHLIGHT(name),
COLOURS.DIMMED("("),
COLOURS.EXPECTED(table.concat(stringArgs, ", ")),
COLOURS.EXPECTED("expected"),
COLOURS.DIMMED(")")
)
end
Expand All @@ -51,7 +43,7 @@ local function bindMatcher(name, matcher, received, inverted)
error(
TestResultError(
tostring(result.message),
buildSignature(name, { ... }, received, inverted)
buildSignature(name, inverted)
)
)
end
Expand Down
31 changes: 11 additions & 20 deletions packages/lest/src/lua/matchers/equality.lua
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
local prettyValue = require("src.lua.utils.prettyValue")
local deepEqual = require("src.lua.utils.deepEqual")
local serialiseValue = require("utils.serialise-value")
local deepEqual = require("utils.deepEqual")
local renderDiff = require("utils.render-diff")

---@type lest.Matcher
local function toBe(ctx, received, expected)
return {
pass = received == expected,
message = string.format(
"Expected %s to%sbe %s",
prettyValue(received),
ctx.inverted and " not " or " ",
prettyValue(expected)
),
message = renderDiff(expected, received, false, ctx.inverted),
}
end

Expand All @@ -20,7 +16,7 @@ local function toBeDefined(ctx, received)
pass = received ~= nil,
message = string.format(
"Expected %s to be %sdefined",
prettyValue(received),
serialiseValue(received),
ctx.inverted and "un" or ""
),
}
Expand All @@ -32,7 +28,7 @@ local function toBeUndefined(ctx, received)
pass = received == nil,
message = string.format(
"Expected %s to be %sdefined",
prettyValue(received),
serialiseValue(received),
ctx.inverted and "" or "un"
),
}
Expand All @@ -41,12 +37,7 @@ end
local function toEqual(ctx, received, expected)
return {
pass = deepEqual(received, expected),
message = string.format(
"Expected %s to%sdeeply equal %s",
prettyValue(received),
ctx.inverted and " not " or " ",
prettyValue(expected)
),
message = renderDiff(expected, received, true, ctx.inverted),
}
end

Expand All @@ -56,7 +47,7 @@ local function toBeTruthy(ctx, received)
pass = not not received,
message = string.format(
"Expected %s to%sbe truthy",
prettyValue(received),
serialiseValue(received),
ctx.inverted and " not " or " "
),
}
Expand All @@ -68,7 +59,7 @@ local function toBeFalsy(ctx, received)
pass = not received,
message = string.format(
"Expected %s to%sbe falsy",
prettyValue(received),
serialiseValue(received),
ctx.inverted and " not " or " "
),
}
Expand All @@ -82,9 +73,9 @@ local function toBeInstanceOf(ctx, received, metatable)
pass = getmetatable(received) == metatable,
message = string.format(
"Expected %s to%sbe an instance of %s",
prettyValue(received),
serialiseValue(received),
ctx.inverted and " not " or " ",
prettyValue(metatable)
serialiseValue(metatable)
),
}
end
Expand Down
Loading

0 comments on commit c0f015c

Please sign in to comment.