aboutsummaryrefslogtreecommitdiffstats
path: root/spec/util_array_spec.lua
blob: ff049d0e8fc55b832ca2ccc15c2727045a8c38f8 (plain)
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
local array = require "util.array";
describe("util.array", function ()
	describe("creation", function ()
		describe("from table", function ()
			it("works", function ()
				local a = array({"a", "b", "c"});
				assert.same({"a", "b", "c"}, a);
			end);
		end);

		describe("from iterator", function ()
			it("works", function ()
				-- collects the first value, ie the keys
				local a = array(ipairs({true, true, true}));
				assert.same({1, 2, 3}, a);
			end);
		end);

		describe("collect", function ()
			it("works", function ()
				-- collects the first value, ie the keys
				local a = array.collect(ipairs({true, true, true}));
				assert.same({1, 2, 3}, a);
			end);
		end);

	end);

	describe("metatable", function ()
		describe("operator", function ()
			describe("addition", function ()
				it("works", function ()
					local a = array({ "a", "b" });
					local b = array({ "c", "d" });
					assert.same({"a", "b", "c", "d"}, a + b);
				end);
			end);

			describe("equality", function ()
				it("works", function ()
					local a1 = array({ "a", "b" });
					local a2 = array({ "a", "b" });
					local b = array({ "c", "d" });
					assert.truthy(a1 == a2);
					assert.falsy(a1 == b);
					assert.falsy(a1 == { "a", "b" }, "Behavior of metatables changed in Lua 5.3");
				end);
			end);

			describe("division", function ()
				it("works", function ()
					local a = array({ "a", "b", "c" });
					local b = a / function (i) if i ~= "b" then return i .. "x" end end;
					assert.same({ "ax", "cx" }, b);
				end);
			end);

		end);
	end);

	describe("methods", function ()
		describe("map", function ()
			it("works", function ()
				local a = array({ "a", "b", "c" });
				local b = a:map(string.upper);
				assert.same({ "A", "B", "C" }, b);
			end);
		end);

		describe("filter", function ()
			it("works", function ()
				local a = array({ "a", "b", "c" });
				a:filter(function (i) return i ~= "b" end);
				assert.same({ "a", "c" }, a);
			end);
		end);

		describe("sort", function ()
			it("works", function ()
				local a = array({ 5, 4, 3, 1, 2, });
				a:sort();
				assert.same({ 1, 2, 3, 4, 5, }, a);
			end);
		end);

		describe("unique", function ()
			it("works", function ()
				local a = array({ "a", "b", "c", "c", "a", "b" });
				a:unique();
				assert.same({ "a", "b", "c" }, a);
			end);
		end);

		describe("pluck", function ()
			it("works", function ()
				local a = array({ { a = 1, b = -1 }, { a = 2, b = -2 }, });
				a:pluck("a");
				assert.same({ 1, 2 }, a);
			end);
		end);


		describe("reverse", function ()
			it("works", function ()
				local a = array({ "a", "b", "c" });
				a:reverse();
				assert.same({ "c", "b", "a" }, a);
			end);
		end);

		-- TODO :shuffle

		describe("append", function ()
			it("works", function ()
				local a = array({ "a", "b", "c" });
				a:append(array({ "d", "e", }));
				assert.same({ "a", "b", "c", "d", "e" }, a);
			end);
		end);

		describe("push", function ()
			it("works", function ()
				local a = array({ "a", "b", "c" });
				a:push("d"):push("e");
				assert.same({ "a", "b", "c", "d", "e" }, a);
			end);
		end);

		describe("pop", function ()
			it("works", function ()
				local a = array({ "a", "b", "c" });
				assert.equal("c", a:pop());
				assert.same({ "a", "b", }, a);
			end);
		end);

		describe("concat", function ()
			it("works", function ()
				local a = array({ "a", "b", "c" });
				assert.equal("a,b,c", a:concat(","));
			end);
		end);

		describe("length", function ()
			it("works", function ()
				local a = array({ "a", "b", "c" });
				assert.equal(3, a:length());
			end);
		end);

		describe("slice", function ()
			it("works", function ()
				local a = array({ "a", "b", "c" });
				assert.equal(array.slice(a, 1, 2), array{ "a", "b" });
				assert.equal(array.slice(a, 1, 3), array{ "a", "b", "c" });
				assert.equal(array.slice(a, 2, 3), array{ "b", "c" });
				assert.equal(array.slice(a, 2), array{ "b", "c" });
				assert.equal(array.slice(a, -4), array{ "a", "b", "c" });
				assert.equal(array.slice(a, -3), array{ "a", "b", "c" });
				assert.equal(array.slice(a, -2), array{ "b", "c" });
				assert.equal(array.slice(a, -1), array{ "c" });
			end);

			it("can mutate", function ()
				local a = array({ "a", "b", "c" });
				assert.equal(a:slice(-1), array{"c"});
				assert.equal(a, array{"c"});
			end);
		end);
	end);

	-- TODO The various array.foo(array ina, array outa) functions
end);