Comparison

spec/util_array_spec.lua @ 11787:3ae6fa901a8b

util.array: Add :slice() method + tests Behaviour follows the same logic as string.sub (so yes, 1-indexed).
author Matthew Wild <mwild1@gmail.com>
date Sun, 12 Sep 2021 10:50:20 +0100
parent 10590:257dc26e8e65
child 13245:ffe4adbd2af9
comparison
equal deleted inserted replaced
11786:39164ea2ab9e 11787:3ae6fa901a8b
146 local a = array({ "a", "b", "c" }); 146 local a = array({ "a", "b", "c" });
147 assert.equal(3, a:length()); 147 assert.equal(3, a:length());
148 end); 148 end);
149 end); 149 end);
150 150
151 describe("slice", function ()
152 it("works", function ()
153 local a = array({ "a", "b", "c" });
154 assert.equal(array.slice(a, 1, 2), array{ "a", "b" });
155 assert.equal(array.slice(a, 1, 3), array{ "a", "b", "c" });
156 assert.equal(array.slice(a, 2, 3), array{ "b", "c" });
157 assert.equal(array.slice(a, 2), array{ "b", "c" });
158 assert.equal(array.slice(a, -4), array{ "a", "b", "c" });
159 assert.equal(array.slice(a, -3), array{ "a", "b", "c" });
160 assert.equal(array.slice(a, -2), array{ "b", "c" });
161 assert.equal(array.slice(a, -1), array{ "c" });
162 end);
163
164 it("can mutate", function ()
165 local a = array({ "a", "b", "c" });
166 assert.equal(a:slice(-1), array{"c"});
167 assert.equal(a, array{"c"});
168 end);
169 end);
151 end); 170 end);
152 171
153 -- TODO The various array.foo(array ina, array outa) functions 172 -- TODO The various array.foo(array ina, array outa) functions
154 end); 173 end);
155 174