aboutsummaryrefslogtreecommitdiffstats
path: root/tests/mason-core/functional/table_spec.lua
blob: 357f7120e3b386461cfcdf9719d177691345cfdb (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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
local _ = require "mason-core.functional"

describe("functional: table", function()
    it("retrieves property of table", function()
        assert.equals("hello", _.prop("a", { a = "hello" }))
    end)

    it("retrieves nested property of table", function()
        assert.equals("hello", _.path({ "a", "greeting" }, { a = { greeting = "hello" } }))
    end)

    it("picks properties of table", function()
        local function fn() end
        assert.same(
            {
                ["key1"] = 1,
                [fn] = 2,
            },
            _.pick({ "key1", fn }, {
                ["key1"] = 1,
                [fn] = 2,
                [3] = 3,
            })
        )
    end)

    it("converts table to pairs", function()
        assert.same(
            _.sort_by(_.nth(1), {
                {
                    "skies",
                    "cloudy",
                },
                {
                    "temperature",
                    "20°",
                },
            }),
            _.sort_by(_.nth(1), _.to_pairs { skies = "cloudy", temperature = "20°" })
        )
    end)

    it("converts pairs to table", function()
        assert.same(
            { skies = "cloudy", temperature = "20°" },
            _.from_pairs {
                {
                    "skies",
                    "cloudy",
                },
                {
                    "temperature",
                    "20°",
                },
            }
        )
    end)

    it("should invert tables", function()
        assert.same(
            {
                val1 = "key1",
                val2 = "key2",
            },
            _.invert {
                key1 = "val1",
                key2 = "val2",
            }
        )
    end)

    it("should evolve table", function()
        assert.same(
            {
                non_existent = nil,
                firstname = "JOHN",
                lastname = "DOE",
                age = 42,
            },
            _.evolve({
                non_existent = _.always "hello",
                firstname = _.to_upper,
                lastname = _.to_upper,
                age = _.add(2),
            }, {
                firstname = "John",
                lastname = "Doe",
                age = 40,
            })
        )
    end)

    it("should merge left", function()
        assert.same(
            {
                firstname = "John",
                lastname = "Doe",
            },
            _.merge_left({
                firstname = "John",
            }, {
                firstname = "Jane",
                lastname = "Doe",
            })
        )
    end)

    it("should dissoc keys", function()
        assert.same({
            a = "a",
            c = "c",
        }, _.dissoc("b", { a = "a", b = "b", c = "c" }))
    end)
end)