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 test for page_number greater than max pages #52

Merged
merged 2 commits into from
Mar 4, 2017
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
20 changes: 13 additions & 7 deletions lib/scrivener/html.ex
Original file line number Diff line number Diff line change
Expand Up @@ -299,27 +299,33 @@ defmodule Scrivener.HTML do

# Computing page number ranges
defp page_number_list(list, page, total, distance) when is_integer(distance) and distance >= 1 do
list ++ Enum.to_list(beginning_distance(page, distance)..end_distance(page, total, distance))
list ++ Enum.to_list(beginning_distance(page, total, distance)..end_distance(page, total, distance))
end
defp page_number_list(_list, _page, _total, _distance) do
raise "Scrivener.HTML: Distance cannot be less than one."
end

# Beginning distance computation
defp beginning_distance(page, distance) when page - distance < 1 do
# defp beginning_distance(page, total, distance) when page + distance > total and page < total do
# page - distance
# end
Copy link
Owner

Choose a reason for hiding this comment

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

Can you remove this commented out one?

defp beginning_distance(page, _total, distance) when page - distance < 1 do
page - (distance + (page - distance - 1))
end
defp beginning_distance(page, distance) do
defp beginning_distance(page, total, distance) when page <= total do
page - distance
end
defp beginning_distance(page, total, distance) when page > total do
total - distance
end

# End distance computation
defp end_distance(page, 0, _distance) do
page
end
defp end_distance(page, total, distance) when page + distance >= total do
defp end_distance(page, total, distance) when page + distance >= total and total != 0 do
total
end
defp end_distance(page, 0, _distance) do
1
end
defp end_distance(page, _total, distance) do
page + distance
end
Expand Down
4 changes: 4 additions & 0 deletions test/scrivener/html_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ defmodule Scrivener.HTMLTest do
assert pages(5..10) == links_with_opts total_pages: 10, page_number: 10
end

test "page value larger than total pages" do
assert pages(5..10) == links_with_opts total_pages: 10, page_number: 100
end

end

describe "raw_pagination_links next" do
Expand Down