Skip to content

fix: use ipairs in functional.all and add basic test coverage #659

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions lua/plenary/functional.lua
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,14 @@ function f.any(fun, iterable)
end

function f.all(fun, iterable)
for k, v in pairs(iterable) do
for k, v in ipairs(iterable) do
if not fun(k, v) then
return false
end
end

return true
end

function f.if_nil(val, was_nil, was_not_nil)
if val == nil then
return was_nil
Expand Down
42 changes: 41 additions & 1 deletion tests/plenary/functional_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,44 @@ describe("functional", function()
assert.is.equal(expected, f.partial(args, 1, 2, 3, 4)())
end)
end)
end)

describe("all", function()
it("returns true when all elements satisfy predicate", function()
assert.is_true(f.all(function(_, x) return x < 10 end, {1, 2, 3, 4}))
end)

it("returns false if any element fails predicate", function()
assert.is_false(f.all(function(_, x) return x < 10 end, {1, 2, 30, 4}))
end)

it("returns true on empty list", function()
assert.is_true(f.all(function(_, x) return false end, {}))
end)
end)

describe("any", function()
it("returns true if any element satisfies predicate", function()
assert.is_true(f.any(function(_, v) return v > 10 end, {1, 2, 12, 4}))
end)

it("returns false when no elements match", function()
assert.is_false(f.any(function(_, v) return v > 10 end, {1, 2, 3, 4}))
end)

it("returns false on empty list", function()
assert.is_false(f.any(function(_, v) return true end, {}))
end)
end)

describe("if_nil", function()
it("returns was_nil value if nil", function()
assert.equals("fallback", f.if_nil(nil, "fallback", "not nil"))
end)

it("returns was_not_nil value if not nil", function()
assert.equals("not nil", f.if_nil(5, "fallback", "not nil"))
end)
end)
end)