File: helpers.lua

package info (click to toggle)
lua-vips 1.1.12-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 772 kB
  • sloc: makefile: 2
file content (43 lines) | stat: -rw-r--r-- 1,287 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
-- Pre-load the vips module
require "vips"

local assert = require "luassert.assert"
local say = require "say"

-- TODO: Wait for https://github.com/Olivine-Labs/luassert/issues/148
-- Meanwhile, we're applying patch #150 here.

-- Pre-load the ffi module, such that it becomes part of the environment
-- and Busted will not try to GC and reload it. The ffi is not suited
-- for that and will occasionally segfault if done so.
local ffi = require "ffi"

-- Patch ffi.cdef to only be called once with each definition, as it
-- will error on re-registering.
local old_cdef = ffi.cdef
local exists = {}
ffi.cdef = function(def)
    if exists[def] then
        return
    end
    exists[def] = true
    return old_cdef(def)
end

local function almost_equal(_, arguments)
    local threshold = arguments[3] or 0.001

    if type(arguments[1]) ~= "number" or type(arguments[2]) ~= "number" then
        return false
    end

    return math.abs(arguments[1] - arguments[2]) < threshold
end

say:set("assertion.almost_equal.positive",
    "Expected %s to almost equal %s")
say:set("assertion.almost_equal.negative",
    "Expected %s to not almost equal %s")
assert:register("assertion", "almost_equal", almost_equal,
    "assertion.almost_equal.positive",
    "assertion.almost_equal.negative")