aboutsummaryrefslogtreecommitdiffstats
path: root/tests/mason-core/functional/list_spec.lua
blob: 3dba6295a34062de4c4f920616404c7e07767ca0 (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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
local Optional = require "mason-core.optional"
local _ = require "mason-core.functional"
local spy = require "luassert.spy"

describe("functional: list", function()
    it("should produce list without nils", function()
        assert.same({ 1, 2, 3, 4 }, _.list_not_nil(nil, 1, 2, nil, 3, nil, 4, nil))
    end)

    it("makes a shallow copy of a list", function()
        local list = { "BLUE", { nested = "TABLE" }, "RED" }
        local list_copy = _.list_copy(list)
        assert.same({ "BLUE", { nested = "TABLE" }, "RED" }, list_copy)
        assert.is_false(list == list_copy)
        assert.is_true(list[2] == list_copy[2])
    end)

    it("reverses lists", function()
        local colors = { "BLUE", "YELLOW", "RED" }
        assert.same({
            "RED",
            "YELLOW",
            "BLUE",
        }, _.reverse(colors))
        -- should not modify in-place
        assert.same({ "BLUE", "YELLOW", "RED" }, colors)
    end)

    it("maps over list", function()
        local colors = { "BLUE", "YELLOW", "RED" }
        assert.same(
            {
                "LIGHT_BLUE",
                "LIGHT_YELLOW",
                "LIGHT_RED",
            },
            _.map(function(color)
                return "LIGHT_" .. color
            end, colors)
        )
        -- should not modify in-place
        assert.same({ "BLUE", "YELLOW", "RED" }, colors)
    end)

    it("filter_map over list", function()
        local colors = { "BROWN", "BLUE", "YELLOW", "GREEN", "CYAN" }
        assert.same(
            {
                "BROWN EYES",
                "BLUE EYES",
                "GREEN EYES",
            },
            _.filter_map(function(color)
                if _.any_pass({ _.equals "BROWN", _.equals "BLUE", _.equals "GREEN" }, color) then
                    return Optional.of(("%s EYES"):format(color))
                else
                    return Optional.empty()
                end
            end, colors)
        )
    end)

    it("finds first item that fulfills predicate", function()
        local predicate = spy.new(function(item)
            return item == "Waldo"
        end)

        assert.equals(
            "Waldo",
            _.find_first(predicate, {
                "Where",
                "On Earth",
                "Is",
                "Waldo",
                "?",
            })
        )
        assert.spy(predicate).was.called(4)
    end)

    it("determines whether any item in the list fulfills predicate", function()
        local predicate = spy.new(function(item)
            return item == "On Earth"
        end)

        assert.is_true(_.any(predicate, {
            "Where",
            "On Earth",
            "Is",
            "Waldo",
            "?",
        }))

        assert.spy(predicate).was.called(2)
    end)

    it("should check that all items in list fulfills predicate", function()
        assert.is_true(_.all(_.is "string", {
            "Where",
            "On Earth",
            "Is",
            "Waldo",
            "?",
        }))

        local predicate = spy.new(_.is "string")

        assert.is_false(_.all(predicate, {
            "Five",
            "Plus",
            42,
            "Equals",
            47,
        }))
        assert.spy(predicate).was_called(3)
    end)

    it("should iterate list in .each", function()
        local list = { "BLUE", "YELLOW", "RED" }
        local iterate_fn = spy.new()
        _.each(iterate_fn, list)
        assert.spy(iterate_fn).was_called(3)
        assert.spy(iterate_fn).was_called_with("BLUE", 1)
        assert.spy(iterate_fn).was_called_with("YELLOW", 2)
        assert.spy(iterate_fn).was_called_with("RED", 3)
    end)

    it("should concat list tables", function()
        local list = { "monstera", "tulipa", "carnation" }
        assert.same({ "monstera", "tulipa", "carnation", "rose", "daisy" }, _.concat(list, { "rose", "daisy" }))
        assert.same({ "monstera", "tulipa", "carnation" }, list) -- does not mutate list
    end)

    it("should concat strings", function()
        assert.equals("FooBar", _.concat("Foo", "Bar"))
    end)

    it("should zip list into table", function()
        local fnkey = function() end
        assert.same({
            a = "a",
            [fnkey] = 1,
        }, _.zip_table({ "a", fnkey }, { "a", 1 }))
    end)

    it("should get nth item", function()
        assert.equals("first", _.nth(1, { "first", "middle", "last" }))
        assert.equals("last", _.nth(-1, { "first", "middle", "last" }))
        assert.equals("middle", _.nth(-2, { "first", "middle", "last" }))
        assert.equals("a", _.nth(1, "abc"))
        assert.equals("c", _.nth(-1, "abc"))
        assert.equals("b", _.nth(-2, "abc"))
        assert.is_nil(_.nth(0, { "value" }))
        assert.equals("", _.nth(0, "abc"))
    end)

    it("should get length", function()
        assert.equals(0, _.length {})
        assert.equals(0, _.length { nil })
        assert.equals(0, _.length { obj = "doesnt count" })
        assert.equals(0, _.length "")
        assert.equals(1, _.length { "" })
        assert.equals(4, _.length "fire")
    end)

    it("should sort by comparator", function()
        local list = {
            {
                name = "William",
            },
            {
                name = "Boman",
            },
        }
        assert.same({
            {
                name = "Boman",
            },
            {
                name = "William",
            },
        }, _.sort_by(_.prop "name", list))

        -- Should not mutate original list
        assert.same({
            {
                name = "William",
            },
            {
                name = "Boman",
            },
        }, list)
    end)

    it("should append to list", function()
        local list = { "Earth", "Wind" }
        assert.same({ "Earth", "Wind", { "Fire" } }, _.append({ "Fire" }, list))

        -- Does not mutate original list
        assert.same({ "Earth", "Wind" }, list)
    end)

    it("should prepend to list", function()
        local list = { "Fire" }
        assert.same({ { "Earth", "Wind" }, "Fire" }, _.prepend({ "Earth", "Wind" }, list))

        -- Does not mutate original list
        assert.same({ "Fire" }, list)
    end)

    it("joins lists", function()
        assert.equals("Hello, John", _.join(", ", { "Hello", "John" }))
    end)

    it("should uniq_by lists", function()
        local list = { "Person.", "Woman.", "Man.", "Person.", "Woman.", "Camera.", "TV." }
        assert.same({ "Person.", "Woman.", "Man.", "Camera.", "TV." }, _.uniq_by(_.identity, list))
    end)

    it("should partition lists", function()
        local words = { "person", "Woman", "Man", "camera", "TV" }
        assert.same({
            { "Woman", "Man", "TV" },
            { "person", "camera" },
        }, _.partition(_.matches "%u", words))
    end)

    it("should return head", function()
        assert.equals("Head", _.head { "Head", "Tail", "Tail" })
    end)

    it("should return last", function()
        assert.equals("Last", _.last { "Head", "List", "Last" })
    end)

    it("should take n items", function()
        local list = { "First", "Second", "Third", "I", "Have", "Poor", "Imagination" }
        assert.same({ "First", "Second", "Third" }, _.take(3, list))
        assert.same({}, _.take(0, list))
        assert.same({ "First", "Second", "Third", "I", "Have", "Poor", "Imagination" }, _.take(10000, list))
    end)

    it("should drop n items", function()
        local list = { "First", "Second", "Third", "I", "Have", "Poor", "Imagination" }
        assert.same({ "I", "Have", "Poor", "Imagination" }, _.drop(3, list))
        assert.same({ "First", "Second", "Third", "I", "Have", "Poor", "Imagination" }, _.drop(0, list))
        assert.same({}, _.drop(10000, list))
    end)

    it("should drop last n items", function()
        local list = { "First", "Second", "Third", "I", "Have", "Poor", "Imagination" }
        assert.same({ "First", "Second", "Third" }, _.drop_last(4, list))
        assert.same({ "First", "Second", "Third", "I", "Have", "Poor", "Imagination" }, _.drop_last(0, list))
        assert.same({}, _.drop_last(10000, list))
    end)

    it("should reduce lists", function()
        local add = spy.new(_.add)
        assert.equals(15, _.reduce(add, 0, { 1, 2, 3, 4, 5 }))
        assert.spy(add).was_called(5)
        assert.spy(add).was_called_with(0, 1)
        assert.spy(add).was_called_with(1, 2)
        assert.spy(add).was_called_with(3, 3)
        assert.spy(add).was_called_with(6, 4)
        assert.spy(add).was_called_with(10, 5)
    end)

    it("should split lists", function()
        assert.same({
            { 1, 2, 3 },
            { 4, 5, 6 },
            { 7 },
        }, _.split_every(3, { 1, 2, 3, 4, 5, 6, 7 }))

        assert.same({ { 1, 2, 3 } }, _.split_every(5, { 1, 2, 3 }))
        assert.same({ { 1 }, { 2 }, { 3 } }, _.split_every(1, { 1, 2, 3 }))

        assert.has_error(function()
            _.split_every(0, {})
        end)
    end)

    it("should index_by lists", function()
        assert.same(
            {
                apple = { fruit = "apple", color = "red" },
                banana = { fruit = "banana", color = "yellow" },
            },
            _.index_by(_.prop "fruit", {
                { fruit = "apple", color = "red" },
                { fruit = "banana", color = "yellow" },
            })
        )
    end)

    it("should flatten tables", function()
        assert.same({ 1, 2, 3 }, _.flatten { 1, 2, 3 })
        assert.same({ 1, 2, 3, "a" }, _.flatten { 1, { 2 }, { 3 }, "a" })
        assert.same({ 1, 2, 3, 4, 5 }, _.flatten { 1, { { 2, 3 }, { 4 } }, { 5 } })
    end)

    -- Note: this is not necessarily a requirement, but it is expected to behave this way as of writing.
    it("should flatten keyed tables", function()
        assert.same(
            {
                "-xvf",
                "file",
            },
            _.flatten {
                { "-xvf", { "file" } },
                cmd = "tar",
                env = {
                    LC_ALL = "latin",
                },
            }
        )
    end)
end)

describe("list immutability", function()
    it("should not mutate lists", function()
        local og_list = setmetatable({ "a", "b", "c" }, {
            __newindex = function()
                error "Tried to newindex"
            end,
        })

        _.reverse(og_list)
        _.list_copy(og_list)
        _.filter(_.F, og_list)
        _.map(_.to_upper, og_list)
        _.filter_map(_.always(Optional.empty()), og_list)
        _.each(_.length, og_list)
        _.concat(og_list, { "d", "e" })
        _.append("d", og_list)
        _.prepend("0", og_list)
        _.zip_table({ "first", "second", "third" }, og_list)
        _.nth(1, og_list)
        _.head(og_list)
        _.last(og_list)
        _.length(og_list)
        _.flatten(og_list)
        _.sort_by(_.identity, og_list)
        _.uniq_by(_.identity, og_list)
        _.join(".", og_list)
        _.partition(_.equals "a", og_list)
        _.take(2, og_list)
        _.drop(2, og_list)
        _.drop_last(2, og_list)
        _.reduce(_.concat, "", og_list)

        assert.same({ "a", "b", "c" }, og_list)
    end)
end)