-
Notifications
You must be signed in to change notification settings - Fork 243
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(utils) add npairs nil-safe numeric iterator
It honours the `table.n` size field.
- Loading branch information
Showing
4 changed files
with
167 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() | ||
for i, v in npairs(t1, nil, nil, 0) do end | ||
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) |