Annotate

spec/util_array_spec.lua @ 10391:986349fc0f9e

Merge 0.11->trunk
author Kim Alvefur <zash@zash.se>
date Mon, 04 Nov 2019 00:36:49 +0100
parent 10100:5ca9c4917885
child 10397:921e8b00778e
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
10100
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
1 local array = require "util.array";
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
2 describe("util.array", function ()
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
3 describe("creation", function ()
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
4 describe("from tablle", function ()
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
5 it("works", function ()
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
6 local a = array({"a", "b", "c"});
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
7 assert.same({"a", "b", "c"}, a);
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
8 end);
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
9 end);
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
10
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
11 describe("from iterator", function ()
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
12 it("works", function ()
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
13 -- collects the first value, ie the keys
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
14 local a = array(ipairs({true, true, true}));
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
15 assert.same({1, 2, 3}, a);
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
16 end);
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
17 end);
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
18
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
19 describe("collect", function ()
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
20 it("works", function ()
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
21 -- collects the first value, ie the keys
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
22 local a = array.collect(ipairs({true, true, true}));
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
23 assert.same({1, 2, 3}, a);
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
24 end);
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
25 end);
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
26
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
27 end);
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
28
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
29 describe("metatable", function ()
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
30 describe("operator", function ()
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
31 describe("addition", function ()
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
32 it("works", function ()
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
33 local a = array({ "a", "b" });
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
34 local b = array({ "c", "d" });
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
35 assert.same({"a", "b", "c", "d"}, a + b);
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
36 end);
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
37 end);
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
38
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
39 describe("equality", function ()
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
40 it("works", function ()
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
41 local a1 = array({ "a", "b" });
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
42 local a2 = array({ "a", "b" });
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
43 local b = array({ "c", "d" });
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
44 assert.truthy(a1 == a2);
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
45 assert.falsy(a1 == b);
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
46 end);
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
47 end);
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
48
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
49 describe("division", function ()
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
50 it("works", function ()
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
51 local a = array({ "a", "b", "c" });
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
52 local b = a / function (i) if i ~= "b" then return i .. "x" end end;
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
53 assert.same({ "ax", "cx" }, b);
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
54 end);
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
55 end);
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
56
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
57 end);
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
58 end);
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
59
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
60 describe("methods", function ()
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
61 describe("map", function ()
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
62 it("works", function ()
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
63 local a = array({ "a", "b", "c" });
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
64 local b = a:map(string.upper);
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
65 assert.same({ "A", "B", "C" }, b);
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
66 end);
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
67 end);
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
68
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
69 describe("filter", function ()
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
70 it("works", function ()
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
71 local a = array({ "a", "b", "c" });
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
72 a:filter(function (i) return i ~= "b" end);
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
73 assert.same({ "a", "c" }, a);
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
74 end);
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
75 end);
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
76
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
77 describe("sort", function ()
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
78 it("works", function ()
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
79 local a = array({ 5, 4, 3, 1, 2, });
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
80 a:sort();
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
81 assert.same({ 1, 2, 3, 4, 5, }, a);
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
82 end);
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
83 end);
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
84
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
85 describe("unique", function ()
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
86 it("works", function ()
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
87 local a = array({ "a", "b", "c", "c", "a", "b" });
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
88 a:unique();
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
89 assert.same({ "a", "b", "c" }, a);
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
90 end);
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
91 end);
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
92
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
93 describe("pluck", function ()
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
94 it("works", function ()
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
95 local a = array({ { a = 1, b = -1 }, { a = 2, b = -2 }, });
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
96 a:pluck("a");
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
97 assert.same({ 1, 2 }, a);
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
98 end);
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
99 end);
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
100
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
101
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
102 describe("reverse", function ()
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
103 it("works", function ()
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
104 local a = array({ "a", "b", "c" });
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
105 a:reverse();
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
106 assert.same({ "c", "b", "a" }, a);
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
107 end);
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
108 end);
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
109
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
110 -- TODO :shuffle
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
111
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
112 describe("append", function ()
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
113 it("works", function ()
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
114 local a = array({ "a", "b", "c" });
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
115 a:append(array({ "d", "e", }));
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
116 assert.same({ "a", "b", "c", "d", "e" }, a);
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
117 end);
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
118 end);
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
119
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
120 describe("push", function ()
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
121 it("works", function ()
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
122 local a = array({ "a", "b", "c" });
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
123 a:push("d"):push("e");
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
124 assert.same({ "a", "b", "c", "d", "e" }, a);
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
125 end);
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
126 end);
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
127
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
128 describe("pop", function ()
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
129 it("works", function ()
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
130 local a = array({ "a", "b", "c" });
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
131 assert.equal("c", a:pop());
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
132 assert.same({ "a", "b", }, a);
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
133 end);
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
134 end);
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
135
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
136 describe("concat", function ()
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
137 it("works", function ()
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
138 local a = array({ "a", "b", "c" });
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
139 assert.equal("a,b,c", a:concat(","));
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
140 end);
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
141 end);
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
142
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
143 describe("length", function ()
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
144 it("works", function ()
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
145 local a = array({ "a", "b", "c" });
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
146 assert.equal(3, a:length());
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
147 end);
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
148 end);
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
149
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
150 end);
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
151
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
152 -- TODO The various array.foo(array ina, array outa) functions
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
153 end);
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
154