Skip to content
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

add odd? and even? to the loop tag #232

Merged
merged 2 commits into from
May 24, 2024
Merged
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
40 changes: 40 additions & 0 deletions spec/marten/template/tag/for/loop_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -101,4 +101,44 @@ describe Marten::Template::Tag::For::Loop do
loop.revindex0.should eq 8
end
end

describe "#odd?" do
it "returns true if the index is odd" do
loop = Marten::Template::Tag::For::Loop.new(items_size: 10)
loop.index = 1
loop.odd?.should eq true

loop.index = 3
loop.odd?.should eq true
end

it "return false if the index is even" do
loop = Marten::Template::Tag::For::Loop.new(items_size: 10)
loop.index = 2
loop.odd?.should eq false
Comment on lines +117 to +118
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would expect this to be in another spec example (eg. returns false if the index is not odd).


loop.index = 4
loop.odd?.should eq false
end
end

describe "#even?" do
it "returns true if the index is even" do
loop = Marten::Template::Tag::For::Loop.new(items_size: 10)
loop.index = 0
loop.even?.should eq true

loop.index = 2
loop.even?.should eq true
end

it "return false if the index is odd" do
loop = Marten::Template::Tag::For::Loop.new(items_size: 10)
loop.index = 1
loop.even?.should eq false
Comment on lines +137 to +138
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same remark here: this could be in its own spec example (returns false if the index is not even).


loop.index = 3
loop.even?.should eq false
end
end
end
8 changes: 8 additions & 0 deletions src/marten/template/tag/for/loop.cr
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,14 @@ module Marten
def revindex0
@items_size - @index - 1
end

def odd?
@index.odd?
end

def even?
@index.even?
end
end
end
end
Expand Down
Loading