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
|
local Data = require "nvim-lsp-installer.data"
local spy = require "luassert.spy"
describe("data", function()
it("creates enums", function()
local colors = Data.enum {
"BLUE",
"YELLOW",
}
assert.equal(
vim.inspect {
["BLUE"] = "BLUE",
["YELLOW"] = "YELLOW",
},
vim.inspect(colors)
)
end)
it("creates sets", function()
local colors = Data.set_of {
"BLUE",
"YELLOW",
"BLUE",
"RED",
}
assert.equal(
vim.inspect {
["BLUE"] = true,
["YELLOW"] = true,
["RED"] = true,
},
vim.inspect(colors)
)
end)
it("reverses lists", function()
local colors = { "BLUE", "YELLOW", "RED" }
assert.equal(
vim.inspect {
"RED",
"YELLOW",
"BLUE",
},
vim.inspect(Data.list_reverse(colors))
)
-- should not modify in-place
assert.equal(vim.inspect { "BLUE", "YELLOW", "RED" }, vim.inspect(colors))
end)
it("maps over list", function()
local colors = { "BLUE", "YELLOW", "RED" }
assert.equal(
vim.inspect {
"LIGHT_BLUE1",
"LIGHT_YELLOW2",
"LIGHT_RED3",
},
vim.inspect(Data.list_map(function(color, i)
return "LIGHT_" .. color .. i
end, colors))
)
-- should not modify in-place
assert.equal(vim.inspect { "BLUE", "YELLOW", "RED" }, vim.inspect(colors))
end)
it("coalesces first non-nil value", function()
assert.equal("Hello World!", Data.coalesce(nil, nil, "Hello World!", ""))
end)
it("makes a shallow copy of a list", function()
local list = { "BLUE", { nested = "TABLE" }, "RED" }
local list_copy = Data.list_copy(list)
assert.equal(vim.inspect { "BLUE", { nested = "TABLE" }, "RED" }, vim.inspect(list_copy))
assert.is_not.is_true(list == list_copy)
assert.is_true(list[2] == list_copy[2])
end)
it("finds first item that fulfills predicate", function()
local predicate = spy.new(function(item)
return item == "Waldo"
end)
assert.equal(
"Waldo",
Data.list_find_first({
"Where",
"On Earth",
"Is",
"Waldo",
"?",
}, predicate)
)
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(Data.list_any({
"Where",
"On Earth",
"Is",
"Waldo",
"?",
}, predicate))
assert.spy(predicate).was.called(2)
end)
it("memoizes functions with default cache mechanism", function()
local expensive_function = spy.new(function(s)
return s
end)
local memoized_fn = Data.memoize(expensive_function)
assert.equal("key", memoized_fn "key")
assert.equal("key", memoized_fn "key")
assert.equal("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 = Data.memoize(expensive_function, function(arg1, arg2)
return arg1 .. arg2
end)
assert.equal("key1key2", memoized_fn("key1", "key2"))
assert.equal("key1key2", memoized_fn("key1", "key2"))
assert.equal("key1key3", memoized_fn("key1", "key3"))
assert.spy(expensive_function).was_called(2)
end)
end)
|