Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"Assertion" Standard Library #426

Merged
merged 3 commits into from
Nov 15, 2023
Merged

"Assertion" Standard Library #426

merged 3 commits into from
Nov 15, 2023

Conversation

well-in-that-case
Copy link
Member

@well-in-that-case well-in-that-case commented Nov 14, 2023

Adds the following assertions:

  • isnil
  • istrue
  • isfalse
  • falsy
  • truthy
  • notnil
  • equal
    • Supports tables.
  • nequal
    • Supports tables.
  • lessthan
  • lessthaneq
  • greaterthan
  • greaterthaneq
  • noerror
  • haserror
  • containserror
    • Checks for a substring inside of an error message.
  • type
    • Supports vararg.

The main purpose of the library is descriptive errors which allow one execution pass to make the issue obvious.

pluto: test.pluto:2 -> Assertion Error: (assert.equal)
        Intended Value: string(5) "*****"
        Received Value: string(4) "****"

pluto: test.pluto:2 -> Assertion Error: (assert.equal)
        Intended Value: { [1] = string(2) "my", [2] = string(5) "split", [3] = string(6) "string", }
        Received Value: { [1] = string(2) "my", [2] = string(5) "split", [3] = string(5) "thing", }

pluto: test.pluto:2 -> Assertion Error: (assert.containserror)
        Absent String: length
        Error Message: argument 'count' for string.duplicate must be larger than zero

pluto: test.pluto:2 -> Assertion Error: (assert.type, argument #5)
        Intended Type: number
        Received Value: string(5) "hello"

Name shadowing is not a worry because __call redirects to a custom reimplementation of _G.assert

local assert = require("assert")
assert(false, "uh oh")
pluto: test.pluto:2 -> Assertion Error: (assert.assert)
         uh oh

To-Do:

  • deepCompare always fails when the first table is empty.
    • The cause is obvious, only t1 gets iterated. Considering adding a table.compare first.
  • A test suite that allows us to easily observe the error messages of every assert.

Resolving the file:line of the original assertion call is a little awkward since we pick the first source file we find. We always have the correct information in the traceback, problem is figuring out which file it may be.

@well-in-that-case well-in-that-case added the enhancement New feature or request label Nov 14, 2023
@well-in-that-case well-in-that-case marked this pull request as ready for review November 15, 2023 01:16
@well-in-that-case well-in-that-case merged commit f86ca42 into 0.8.0 Nov 15, 2023
6 checks passed
@well-in-that-case well-in-that-case mentioned this pull request Nov 15, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants