mirror of
https://github.com/bjc/prosody.git
synced 2025-04-03 21:27:38 +03:00
util.array: Add :slice() method + tests
Behaviour follows the same logic as string.sub (so yes, 1-indexed).
This commit is contained in:
parent
eba0bacfda
commit
edb6956ad5
2 changed files with 53 additions and 0 deletions
|
@ -148,6 +148,25 @@ describe("util.array", function ()
|
|||
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
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue