lunarmodules / busted

Elegant Lua unit testing.
https://lunarmodules.github.io/busted/
MIT License
1.4k stars 185 forks source link

Registering your own functional asserts #611

Closed ktalebian closed 5 years ago

ktalebian commented 5 years ago

I want to register a new that(variable).* so I can add my own assertions, for example:

assert.that(value).contains('foo')

I was doing something like:

local assert = require('luassert')

local _M = {}

local function that(state, arguments)
    return {
        contains = _M.contains(arguments[1])
    }
end

_M.contains = function(arg)
  return function(str)
    return arg:match(str)
  end
end

assert:register('assertion', 'that', that)

This does not work; how can I do register a function?

Tieske commented 5 years ago

That's actually a luassert question and not busted. But please have a look at the array example: https://github.com/Olivine-Labs/luassert/blob/master/src/array.lua . It has exactly what you want.

ktalebian commented 5 years ago

Ah thank you!