aboutsummaryrefslogtreecommitdiffstats
path: root/tests/mason-core/functional/relation_spec.lua
blob: a8207b557b46e2721f8ac128f911bd144a9c6d43 (plain) (blame)
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
local _ = require "mason-core.functional"

describe("functional: relation", function()
    it("should check equality", function()
        local tbl = {}
        local is_tbl = _.equals(tbl)
        local is_a = _.equals "a"
        local is_42 = _.equals(42)

        assert.is_true(is_tbl(tbl))
        assert.is_true(is_a "a")
        assert.is_true(is_42(42))
        assert.is_false(is_a "b")
        assert.is_false(is_42(32))
    end)

    it("should check property equality", function()
        local fn_key = function() end
        local tbl = { a = "a", b = "b", number = 42, [fn_key] = "fun" }
        assert.is_true(_.prop_eq("a", "a", tbl))
        assert.is_true(_.prop_eq(fn_key, "fun", tbl))
        assert.is_true(_.prop_eq(fn_key) "fun"(tbl))
    end)

    it("should check whether property satisfies predicate", function()
        local obj = {
            low = 0,
            med = 10,
            high = 15,
        }

        assert.is_false(_.prop_satisfies(_.gt(10), "low", obj))
        assert.is_false(_.prop_satisfies(_.gt(10), "med")(obj))
        assert.is_true(_.prop_satisfies(_.gt(10)) "high"(obj))
    end)

    it("should check whether nested property satisfies predicate", function()
        local obj = {
            low = { value = 0 },
            med = { value = 10 },
            high = { value = 15 },
        }

        assert.is_false(_.path_satisfies(_.gt(10), { "low", "value" }, obj))
        assert.is_false(_.path_satisfies(_.gt(10), { "med", "value" })(obj))
        assert.is_true(_.path_satisfies(_.gt(10)) { "high", "value" }(obj))
    end)

    it("should subtract numbers", function()
        assert.equals(42, _.min(42, 84))
        assert.equals(-1, _.min(11, 10))
    end)

    it("should add numbers", function()
        assert.equals(1337, _.add(1300, 37))
        assert.equals(-10, _.add(90, -100))
    end)
end)