diff options
author | Matthew Wild <mwild1@gmail.com> | 2021-09-12 10:50:20 +0100 |
---|---|---|
committer | Matthew Wild <mwild1@gmail.com> | 2021-09-12 10:50:20 +0100 |
commit | edb6956ad524febe58d5575d0bf42b4a7ddabc1c (patch) | |
tree | 444448cf660a0c4887605d6d3066648443b4cfbe /spec | |
parent | eba0bacfdaeaddd47c323f6681c79d57228d93a5 (diff) | |
download | prosody-edb6956ad524febe58d5575d0bf42b4a7ddabc1c.tar.gz prosody-edb6956ad524febe58d5575d0bf42b4a7ddabc1c.zip |
util.array: Add :slice() method + tests
Behaviour follows the same logic as string.sub (so yes, 1-indexed).
Diffstat (limited to 'spec')
-rw-r--r-- | spec/util_array_spec.lua | 19 |
1 files changed, 19 insertions, 0 deletions
diff --git a/spec/util_array_spec.lua b/spec/util_array_spec.lua index 1d9da947..ff049d0e 100644 --- a/spec/util_array_spec.lua +++ b/spec/util_array_spec.lua @@ -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 |