aboutsummaryrefslogtreecommitdiffstats
path: root/tests/mason-core/functional/function_spec.lua
blob: 7a589aba0567ca2db78fc9c538667e917da8be1b (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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
local _ = require "mason-core.functional"
local match = require "luassert.match"
local spy = require "luassert.spy"

describe("functional: function", function()
    it("curries functions", function()
        local function sum(...)
            local res = 0
            for i = 1, select("#", ...) do
                res = res + select(i, ...)
            end
            return res
        end
        local arity0 = _.curryN(sum, 0)
        local arity1 = _.curryN(sum, 1)
        local arity2 = _.curryN(sum, 2)
        local arity3 = _.curryN(sum, 3)

        assert.equals(0, arity0(42))
        assert.equals(42, arity1(42))
        assert.equals(3, arity2(1)(2))
        assert.equals(3, arity2(1, 2))
        assert.equals(6, arity3(1)(2)(3))
        assert.equals(6, arity3(1, 2, 3))

        -- should discard superfluous args
        assert.equals(0, arity1(0, 10, 20, 30))
    end)

    it("coalesces first non-nil value", function()
        assert.equals("Hello World!", _.coalesce(nil, nil, "Hello World!", ""))
    end)

    it("should compose functions", function()
        local function add(x)
            return function(y)
                return y + x
            end
        end
        local function subtract(x)
            return function(y)
                return y - x
            end
        end
        local function multiply(x)
            return function(y)
                return y * x
            end
        end

        local big_maths = _.compose(add(1), subtract(3), multiply(5))

        assert.equals(23, big_maths(5))
    end)

    it("should not allow composing no functions", function()
        local e = assert.has_error(function()
            _.compose()
        end)
        assert.equals("compose requires at least one function", e)
    end)

    it("should partially apply functions", function()
        local funcy = spy.new()
        local partially_funcy = _.partial(funcy, "a", "b", "c")
        partially_funcy("d", "e", "f")
        assert.spy(funcy).was_called_with("a", "b", "c", "d", "e", "f")
    end)

    it("should partially apply functions with nil arguments", function()
        local funcy = spy.new()
        local partially_funcy = _.partial(funcy, "a", nil, "c")
        partially_funcy("d", nil, "f")
        assert.spy(funcy).was_called_with("a", nil, "c", "d", nil, "f")
    end)

    it("memoizes functions with default cache mechanism", function()
        local expensive_function = spy.new(function(s)
            return s
        end)
        local memoized_fn = _.memoize(expensive_function)
        assert.equals("key", memoized_fn "key")
        assert.equals("key", memoized_fn "key")
        assert.equals("new_key", memoized_fn "new_key")
        assert.spy(expensive_function).was_called(2)
    end)

    it("memoizes function with custom cache mechanism", function()
        local expensive_function = spy.new(function(arg1, arg2)
            return arg1 .. arg2
        end)
        local memoized_fn = _.memoize(expensive_function, function(arg1, arg2)
            return arg1 .. arg2
        end)
        assert.equals("key1key2", memoized_fn("key1", "key2"))
        assert.equals("key1key2", memoized_fn("key1", "key2"))
        assert.equals("key1key3", memoized_fn("key1", "key3"))
        assert.spy(expensive_function).was_called(2)
    end)

    it("should evaluate functions lazily", function()
        local impl = spy.new(function()
            return {}, {}
        end)
        local lazy_fn = _.lazy(impl)
        assert.spy(impl).was_called(0)
        local a, b = lazy_fn()
        assert.spy(impl).was_called(1)
        assert.is_true(match.is_table()(a))
        assert.is_true(match.is_table()(b))
        local new_a, new_b = lazy_fn()
        assert.spy(impl).was_called(1)
        assert.is_true(match.is_ref(a)(new_a))
        assert.is_true(match.is_ref(b)(new_b))
    end)

    it("should support nil return values in lazy functions", function()
        local lazy_fn = _.lazy(function()
            return nil, 2
        end)
        local a, b = lazy_fn()
        assert.is_nil(a)
        assert.equals(2, b)
    end)

    it("should provide identity value", function()
        local obj = {}
        assert.equals(2, _.identity(2))
        assert.equals(obj, _.identity(obj))
    end)

    it("should always return bound value", function()
        local obj = {}
        assert.equals(2, _.always(2)())
        assert.equals(obj, _.always(obj)())
    end)

    it("true is true and false is false", function()
        assert.is_true(_.T())
        assert.is_false(_.F())
    end)

    it("should tap values", function()
        local fn = spy.new()
        assert.equals(42, _.tap(fn, 42))
        assert.spy(fn).was_called()
        assert.spy(fn).was_called_with(42)
    end)

    it("should apply function", function()
        local max = spy.new(math.max)
        local max_fn = _.apply(max)
        assert.equals(42, max_fn { 1, 2, 3, 4, 5, 6, 7, 8, 9, 42, 10, 8, 4 })
        assert.spy(max).was_called(1)
        assert.spy(max).was_called_with(1, 2, 3, 4, 5, 6, 7, 8, 9, 42, 10, 8, 4)
    end)

    it("should apply value to function", function()
        local agent = spy.new()
        _.apply_to("007", agent)
        assert.spy(agent).was_called(1)
        assert.spy(agent).was_called_with "007"
    end)

    it("should converge on function", function()
        local target = spy.new()
        _.converge(target, { _.head, _.last }, { "These", "Are", "Some", "Words", "Ain't", "That", "Pretty", "Nuts" })
        assert.spy(target).was_called(1)
        assert.spy(target).was_called_with("These", "Nuts")
    end)

    it("should apply spec", function()
        local apply = _.apply_spec {
            sum = _.add(2),
            list = { _.add(2), _.add(6) },
            nested = {
                sum = _.min(2),
            },
        }
        assert.same({
            sum = 4,
            list = { 4, 8 },
            nested = {
                sum = 0,
            },
        }, apply(2))
    end)
end)