diff options
author | chai <chaifix@163.com> | 2021-10-30 11:32:16 +0800 |
---|---|---|
committer | chai <chaifix@163.com> | 2021-10-30 11:32:16 +0800 |
commit | 42ec7286b2d36a9ba22925f816a17cb1cc2aa5ce (patch) | |
tree | 24bc7009457a8d7500f264e89946dc20d069294f /Data/Libraries/Penlight/spec/utils-npairs_spec.lua | |
parent | 164885fd98d48703bd771f802d79557b7db97431 (diff) |
+ Penlight
Diffstat (limited to 'Data/Libraries/Penlight/spec/utils-npairs_spec.lua')
-rw-r--r-- | Data/Libraries/Penlight/spec/utils-npairs_spec.lua | 105 |
1 files changed, 105 insertions, 0 deletions
diff --git a/Data/Libraries/Penlight/spec/utils-npairs_spec.lua b/Data/Libraries/Penlight/spec/utils-npairs_spec.lua new file mode 100644 index 0000000..7e3c5d4 --- /dev/null +++ b/Data/Libraries/Penlight/spec/utils-npairs_spec.lua @@ -0,0 +1,105 @@ +local utils = require("pl.utils") + +describe("pl.utils", function () + + describe("npairs", function () + local npairs = utils.npairs + + it("start index defaults to 1", function() + local t1 = { 1, 2, 3 } + local t2 = {} + for i, v in npairs(t1, nil, 2) do t2[i] = v end + assert.are.same({ 1, 2 }, t2) + end) + + + it("end index defaults to `t.n`", function() + local t1 = { n = 2, 1, 2, 3 } + local t2 = {} + for i, v in npairs(t1) do t2[i] = v end + assert.are.same({1, 2}, t2) + end) + + + it("step size defaults to 1", function() + local t1 = { 1, 2, 3 } + local t2 = {} + for i, v in npairs(t1) do t2[i] = v end + assert.are.same({1, 2, 3}, t2) + end) + + + it("step size cannot be 0", function() + local t1 = { 1, 2, 3 } + assert.has.error(function() + npairs(t1, nil, nil, 0) + end, "iterator step-size cannot be 0") + end) + + + it("end index defaults to `#t` if there is no `t.n`", function() + local t1 = { 1, 2, 3 } + local t2 = {} + for i, v in npairs(t1) do t2[i] = v end + assert.are.same({1, 2, 3}, t2) + end) + + + it("returns nothing if start index is beyond end index", function() + local t1 = { 1, 2, 3 } + local t2 = {} + for i, v in npairs(t1, 5, 3) do t2[i] = v end + assert.are.same({}, t2) + end) + + + it("returns nothing if start index is beyond end index, with negative step size", function() + local t1 = { 1, 2, 3 } + local t2 = {} + for i, v in npairs(t1, 3, 1, -1) do t2[#t2+1] = v end + assert.are.same({ 3, 2, 1}, t2) + end) + + + it("returns 1 key/value if end == start index", function() + local t1 = { 1, 2, 3 } + local t2 = {} + for i, v in npairs(t1, 2, 2) do t2[i] = v end + assert.are.same({ [2] = 2 }, t2) + end) + + + it("returns negative to positive ranges", function() + local t1 = { [-5] = -5, [-4] = -4, [-3] = -3, [-2] = -2, [-1] = -1, [0] = 0, 1, 2, 3 } + local t2 = {} + for i, v in npairs(t1, -4, 1) do t2[i] = v end + assert.are.same({ [-4] = -4, [-3] = -3, [-2] = -2, [-1] = -1, [0] = 0, 1 }, t2) + end) + + + it("returns nil values with the range", function() + local t1 = { n = 3 } + local t2 = {} + for i, v in npairs(t1) do t2[i] = tostring(v) end + assert.are.same({ "nil", "nil", "nil" }, t2) + end) + + + it("honours positive step size", function() + local t1 = { [-5] = -5, [-4] = -4, [-3] = -3, [-2] = -2, [-1] = -1, [0] = 0, 1, 2, 3 } + local t2 = {} + for i, v in npairs(t1, -4, 1, 2) do t2[#t2+1] = v end + assert.are.same({ -4, -2, 0}, t2) + end) + + + it("honours negative step size", function() + local t1 = { [-5] = -5, [-4] = -4, [-3] = -3, [-2] = -2, [-1] = -1, [0] = 0, 1, 2, 3 } + local t2 = {} + for i, v in npairs(t1, 0, -5, -2) do t2[#t2+1] = v end + assert.are.same({ 0, -2, -4 }, t2) + end) + + end) + +end) |