Annotate

spec/util_array_spec.lua @ 11432:113f3912c7cb

util: Add Teal interface definition files Enables writing code in Teal that is aware of the interfaces and function prototypes in these other utils. Could also be used to do type checks on Lua sources, but this tends to have a lot of noise.
author Kim Alvefur <zash@zash.se>
date Tue, 09 Mar 2021 14:36:46 +0100
parent 10590:257dc26e8e65
child 11787:3ae6fa901a8b
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 ()
10397
921e8b00778e util.array: Fix typo in test
Kim Alvefur <zash@zash.se>
parents: 10100
diff changeset
4 describe("from table", function ()
10100
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);
10590
257dc26e8e65 util.array: Add a test case for a behavior change in Lua 5.3
Kim Alvefur <zash@zash.se>
parents: 10397
diff changeset
46 assert.falsy(a1 == { "a", "b" }, "Behavior of metatables changed in Lua 5.3");
10100
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 end);
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
49
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
50 describe("division", function ()
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
51 it("works", function ()
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
52 local a = array({ "a", "b", "c" });
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
53 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
54 assert.same({ "ax", "cx" }, b);
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 end);
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
57
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 end);
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
60
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
61 describe("methods", function ()
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
62 describe("map", function ()
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
63 it("works", function ()
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
64 local a = array({ "a", "b", "c" });
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
65 local b = a:map(string.upper);
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
66 assert.same({ "A", "B", "C" }, b);
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 end);
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
69
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
70 describe("filter", function ()
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
71 it("works", function ()
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
72 local a = array({ "a", "b", "c" });
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
73 a:filter(function (i) return i ~= "b" end);
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
74 assert.same({ "a", "c" }, a);
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 end);
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
77
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
78 describe("sort", function ()
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
79 it("works", function ()
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
80 local a = array({ 5, 4, 3, 1, 2, });
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
81 a:sort();
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
82 assert.same({ 1, 2, 3, 4, 5, }, a);
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 end);
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
85
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
86 describe("unique", function ()
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
87 it("works", function ()
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
88 local a = array({ "a", "b", "c", "c", "a", "b" });
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
89 a:unique();
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
90 assert.same({ "a", "b", "c" }, a);
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 end);
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
93
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
94 describe("pluck", function ()
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
95 it("works", function ()
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
96 local a = array({ { a = 1, b = -1 }, { a = 2, b = -2 }, });
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
97 a:pluck("a");
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
98 assert.same({ 1, 2 }, a);
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 end);
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
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
103 describe("reverse", function ()
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
104 it("works", function ()
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
105 local a = array({ "a", "b", "c" });
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
106 a:reverse();
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
107 assert.same({ "c", "b", "a" }, a);
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 end);
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
110
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
111 -- TODO :shuffle
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
112
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
113 describe("append", function ()
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
114 it("works", function ()
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
115 local a = array({ "a", "b", "c" });
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
116 a:append(array({ "d", "e", }));
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
117 assert.same({ "a", "b", "c", "d", "e" }, a);
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 end);
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
120
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
121 describe("push", function ()
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
122 it("works", function ()
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
123 local a = array({ "a", "b", "c" });
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
124 a:push("d"):push("e");
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
125 assert.same({ "a", "b", "c", "d", "e" }, a);
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 end);
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
128
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
129 describe("pop", function ()
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
130 it("works", function ()
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
131 local a = array({ "a", "b", "c" });
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
132 assert.equal("c", a:pop());
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
133 assert.same({ "a", "b", }, a);
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 end);
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
136
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
137 describe("concat", function ()
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
138 it("works", function ()
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
139 local a = array({ "a", "b", "c" });
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
140 assert.equal("a,b,c", a:concat(","));
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 end);
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
143
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
144 describe("length", function ()
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
145 it("works", function ()
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
146 local a = array({ "a", "b", "c" });
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
147 assert.equal(3, a:length());
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 end);
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
150
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
151 end);
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
152
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
153 -- TODO The various array.foo(array ina, array outa) functions
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
154 end);
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
155