diff --git a/test/api_test.rb b/test/api_test.rb deleted file mode 100644 index bf19116ec..000000000 --- a/test/api_test.rb +++ /dev/null @@ -1,37 +0,0 @@ -require "test_helper" - -class ApiTest < Minitest::Test - - def setup - @pagy = Pagy.new count: 100, page: 4 - end - - def test_version_number - refute_nil Pagy::VERSION - end - - def test_initialization - assert_instance_of Pagy, @pagy - assert_instance_of Pagy, Pagy.new(count: 100) - assert_instance_of Pagy, Pagy.new(count: '100') - assert_instance_of Pagy, Pagy.new(count: 100, page: '2') - assert_instance_of Pagy, Pagy.new(count: 100, page: '') - assert_instance_of Pagy, Pagy.new(count: 100, items: '10') - assert_raises(ArgumentError) { Pagy.new({}) } - assert_raises(ArgumentError) { Pagy.new(count: 100, page: 0) } - assert_raises(ArgumentError) { Pagy.new(count: 100, page: 2, items: 0) } - assert_raises(ArgumentError) { Pagy.new(count: 100, page: 2, size: [1,2,3]).series } - assert_raises(ArgumentError) { Pagy.new(count: 100, page: 2, size: [1,2,3,'4']).series } - assert_raises(Pagy::OutOfRangeError) { Pagy.new(count: 100, page: '11') } - assert_raises(Pagy::OutOfRangeError) { Pagy.new(count: 100, page: 12 ) } - end - - def test_respond_to_accessors - [:count, :page, :items, :vars, # input - :offset, :pages, :last, :from, :to, :prev, :next, :series # output - ].each do |meth| - assert_respond_to @pagy, meth - end - end - -end diff --git a/test/frontend_test.rb b/test/frontend_test.rb deleted file mode 100644 index 98b34e9a8..000000000 --- a/test/frontend_test.rb +++ /dev/null @@ -1,72 +0,0 @@ -require 'test_helper' -require 'rack' - -class FrontendTest < Minitest::Test - - class TestView - include Pagy::Frontend - - def request - Rack::Request.new('SCRIPT_NAME' => '/foo') - end - end - - def setup - @frontend = TestView.new - @array = (1..103).to_a.extend(Pagy::Array::PageMethod) - end - - def test_pagy_nav_page_1 - pagy, _ = @array.pagy(1) - - assert_equal( - '', - @frontend.pagy_nav(pagy) - ) - end - - def test_pagy_nav_page_3 - pagy, _ = @array.pagy(3) - - assert_equal( - '', - @frontend.pagy_nav(pagy) - ) - end - - def test_pagy_nav_page_6 - pagy, _ = @array.pagy(6) - - assert_equal( - '', - @frontend.pagy_nav(pagy) - ) - end -end diff --git a/test/i18n_test.rb b/test/i18n_test.rb deleted file mode 100644 index a0679f1c2..000000000 --- a/test/i18n_test.rb +++ /dev/null @@ -1,64 +0,0 @@ -require "test_helper" - -class I18nTest < Minitest::Test - - class TestI18n; - include Pagy::Frontend - end - - def setup - @i18n = TestI18n.new - end - - def test_data - assert_equal "‹ Prev", Pagy::Frontend::I18N_DATA['pagy']['nav']['prev'] - assert_equal "…", Pagy::Frontend::I18N_DATA['pagy']['nav']['gap'] - end - - def test_translation - assert_equal "‹ Prev", @i18n.pagy_t('pagy.nav.prev') - - assert_equal "items", @i18n.pagy_t('pagy.info.item_name', count: 0) - assert_equal "item", @i18n.pagy_t('pagy.info.item_name', count: 1) - assert_equal "items", @i18n.pagy_t('pagy.info.item_name', count: 10) - - assert_equal "No %{item_name} found", - @i18n.pagy_t('pagy.info.single_page', count: 0) - assert_equal "Displaying 1 %{item_name}", - @i18n.pagy_t('pagy.info.single_page', count: 1) - assert_equal "Displaying all 10 %{item_name}", - @i18n.pagy_t('pagy.info.single_page', count: 10) - assert_equal "Displaying %{item_name} %{from}-%{to} of 10 in total", - @i18n.pagy_t('pagy.info.multiple_pages', count: 10) - end - - def test_missing - assert_equal 'translation missing: "pagy.nav.not_here"', @i18n.pagy_t('pagy.nav.not_here') - end - - def test_render_info_no_118n_key - pagy = Pagy.new count: 0 - assert_equal "No items found", @i18n.pagy_info(pagy) - pagy = Pagy.new count: 1 - assert_equal "Displaying 1 item", @i18n.pagy_info(pagy) - pagy = Pagy.new count: 13 - assert_equal "Displaying all 13 items", @i18n.pagy_info(pagy) - pagy = Pagy.new count: 100, page: 3 - assert_equal "Displaying items 41-60 of 100 in total", @i18n.pagy_info(pagy) - end - - def test_render_info_with_existing_118n_key - Pagy::Frontend::I18N_DATA['pagy']['info']['product'] = { 'zero' => 'Products', - 'one' => 'Product', - 'other' => 'Products' } - pagy = Pagy.new count: 0, item_path: 'pagy.info.product' - assert_equal "No Products found", @i18n.pagy_info(pagy) - pagy = Pagy.new count: 1, item_path: 'pagy.info.product' - assert_equal "Displaying 1 Product", @i18n.pagy_info(pagy) - pagy = Pagy.new count: 13, item_path: 'pagy.info.product' - assert_equal "Displaying all 13 Products", @i18n.pagy_info(pagy) - pagy = Pagy.new count: 100, page: 3, item_path: 'pagy.info.product' - assert_equal "Displaying Products 41-60 of 100 in total", @i18n.pagy_info(pagy) - end - -end diff --git a/test/metrics_test.rb b/test/metrics_test.rb deleted file mode 100644 index 39cc1eb8b..000000000 --- a/test/metrics_test.rb +++ /dev/null @@ -1,233 +0,0 @@ -require "test_helper" - -class MetricsTest < Minitest::Test - - def setup - @vars = { items: 10, size: [3,2,2,3] } - @array = (1..103).to_a.extend(Pagy::Array::PageMethod) - end - - def test_count_0 - pagy = Pagy.new @vars.merge(count: 0) - assert_equal 1, pagy.pages - assert_equal 1, pagy.last - assert_equal 0, pagy.offset - assert_equal 0, pagy.from - assert_equal 0, pagy.to - assert_nil pagy.prev - assert_nil pagy.next - assert_equal ["1"], pagy.series - end - - def test_single_page - pagy = Pagy.new @vars.merge(count: 8) - assert_equal 1, pagy.pages - assert_equal 1, pagy.last - assert_equal 0, pagy.offset - assert_equal 1, pagy.from - assert_equal 8, pagy.to - assert_nil pagy.prev - assert_nil pagy.next - assert_equal ["1"], pagy.series - end - - def test_1_of_2_pages - pagy = Pagy.new @vars.merge(count: 15) - assert_equal 2, pagy.pages - assert_equal 2, pagy.last - assert_equal 0, pagy.offset - assert_equal 1, pagy.from - assert_equal 10, pagy.to - assert_nil pagy.prev - assert_equal 2, pagy.next - assert_equal ["1", 2], pagy.series - end - - def test_2_of_2_pages - pagy = Pagy.new @vars.merge(count: 15, page: 2) - assert_equal 2, pagy.pages - assert_equal 2, pagy.last - assert_equal 10, pagy.offset - assert_equal 11, pagy.from - assert_equal 15, pagy.to - assert_equal 1, pagy.prev - assert_nil pagy.next - assert_equal [1, "2"], pagy.series - end - - def test_page_1 - pagy, paged = @array.pagy(1, @vars) - assert_equal (1..10).to_a, paged - assert_instance_of Pagy, pagy - assert_equal 1, pagy.from - assert_equal 10, pagy.to - assert_nil pagy.prev - assert_equal 1, pagy.page - assert_equal 2, pagy.next - assert_equal ["1", 2, 3, :gap, 9, 10, 11], pagy.series - end - - def test_page_2 - pagy, paged = @array.pagy(2, @vars) - assert_equal (11..20).to_a, paged - assert_instance_of Pagy, pagy - assert_equal 11, pagy.from - assert_equal 20, pagy.to - assert_equal 1, pagy.prev - assert_equal 2, pagy.page - assert_equal 3, pagy.next - assert_equal [1, "2", 3, 4, :gap, 9, 10, 11], pagy.series - end - - def test_page_3 - pagy, paged = @array.pagy(3, @vars) - assert_equal (21..30).to_a, paged - assert_instance_of Pagy, pagy - assert_equal 21, pagy.from - assert_equal 30, pagy.to - assert_equal 2, pagy.prev - assert_equal 3, pagy.page - assert_equal 4, pagy.next - assert_equal [1, 2, "3", 4, 5, :gap, 9, 10, 11], pagy.series - end - - def test_page_4 - pagy, paged = @array.pagy(4, @vars) - assert_equal (31..40).to_a, paged - assert_instance_of Pagy, pagy - assert_equal 31, pagy.from - assert_equal 40, pagy.to - assert_equal 3, pagy.prev - assert_equal 4, pagy.page - assert_equal 5, pagy.next - assert_equal [1, 2, 3, "4", 5, 6, :gap, 9, 10, 11], pagy.series - end - - def test_page_5 - pagy, paged = @array.pagy(5, @vars) - assert_equal (41..50).to_a, paged - assert_instance_of Pagy, pagy - assert_equal 41, pagy.from - assert_equal 50, pagy.to - assert_equal 4, pagy.prev - assert_equal 5, pagy.page - assert_equal 6, pagy.next - assert_equal [1, 2, 3, 4, "5", 6, 7, 8, 9, 10, 11], pagy.series - end - - def test_page_6 - pagy, paged = @array.pagy(6, @vars) - assert_equal (51..60).to_a, paged - assert_equal 51, pagy.from - assert_equal 60, pagy.to - assert_instance_of Pagy, pagy - assert_equal 5, pagy.prev - assert_equal 6, pagy.page - assert_equal 7, pagy.next - assert_equal [1, 2, 3, 4, 5, "6", 7, 8, 9, 10, 11], pagy.series - end - - def test_page_7 - pagy, paged = @array.pagy(7, @vars) - assert_equal (61..70).to_a, paged - assert_instance_of Pagy, pagy - assert_equal 61, pagy.from - assert_equal 70, pagy.to - assert_equal 6, pagy.prev - assert_equal 7, pagy.page - assert_equal 8, pagy.next - assert_equal [1, 2, 3, 4, 5, 6, "7", 8, 9, 10, 11], pagy.series - end - - def test_page_8 - pagy, paged = @array.pagy(8, @vars) - assert_equal (71..80).to_a, paged - assert_instance_of Pagy, pagy - assert_equal 71, pagy.from - assert_equal 80, pagy.to - assert_equal 7, pagy.prev - assert_equal 8, pagy.page - assert_equal 9, pagy.next - assert_equal [1, 2, 3, :gap, 6, 7, "8", 9, 10, 11], pagy.series - end - - def test_page_9 - pagy, paged = @array.pagy(9, @vars) - assert_equal (81..90).to_a, paged - assert_instance_of Pagy, pagy - assert_equal 81, pagy.from - assert_equal 90, pagy.to - assert_equal 8, pagy.prev - assert_equal 9, pagy.page - assert_equal 10, pagy.next - assert_equal [1, 2, 3, :gap, 7, 8, "9", 10, 11], pagy.series - end - - def test_page_10 - pagy, paged = @array.pagy(10, @vars) - assert_equal (91..100).to_a, paged - assert_instance_of Pagy, pagy - assert_equal 91, pagy.from - assert_equal 100, pagy.to - assert_equal 9, pagy.prev - assert_equal 10, pagy.page - assert_equal 11, pagy.next - assert_equal [1, 2, 3, :gap, 8, 9, "10", 11], pagy.series - end - - def test_page_11 - pagy, paged = @array.pagy(11, @vars) - assert_equal (101..103).to_a, paged - assert_instance_of Pagy, pagy - assert_equal 101, pagy.from - assert_equal 103, pagy.to - assert_equal 10, pagy.prev - assert_equal 11, pagy.page - assert_nil pagy.next - assert_equal [1, 2, 3, :gap, 9, 10, "11"], pagy.series - end - - def test_other_output - pagy = Pagy.new @vars.merge(count: 103, page: 2) - assert_equal 103, pagy.count - assert_equal 10, pagy.items - assert_equal 10, pagy.offset - assert_equal 11 , pagy.pages - assert_equal 11, pagy.last - end - - def test_initial_offset_page_1 - pagy = Pagy.new(count: 87, page:1, outset:10, items: 10) - assert_equal 10, pagy.offset - assert_equal 10, pagy.items - assert_equal 1, pagy.from - assert_equal 10, pagy.to - assert_equal 9, pagy.pages - end - - def test_initial_offset_page_9 - pagy = Pagy.new(count: 87, page:9, outset:10, items: 10) - assert_equal 90, pagy.offset - assert_equal 7, pagy.items - assert_equal 81, pagy.from - assert_equal 87, pagy.to - assert_equal 9, pagy.pages - end - - def test_items_of_last_page_of_one - pagy = Pagy.new items: 10, count: 0 - assert_equal 0, pagy.items - pagy = Pagy.new items: 10, count: 4 - assert_equal 4, pagy.items - pagy = Pagy.new items: 10, count: 10 - assert_equal 10, pagy.items - end - - def test_items_of_last_page_of_many - pagy = Pagy.new items: 10, count: 14, page: 2 - assert_equal 4, pagy.items - pagy = Pagy.new items: 10, count: 20, page: 2 - assert_equal 10, pagy.items - end - -end diff --git a/test/pagy/frontend_test.rb b/test/pagy/frontend_test.rb new file mode 100644 index 000000000..7f7b53250 --- /dev/null +++ b/test/pagy/frontend_test.rb @@ -0,0 +1,129 @@ +require_relative '../test_helper' +require 'rack' + +describe Pagy::Frontend do + + class TestView + include Pagy::Frontend + + def request + Rack::Request.new('SCRIPT_NAME' => '/foo') + end + end + + let(:frontend) { TestView.new } + + describe "#pagy_nav" do + before do + @array = (1..103).to_a.extend(Pagy::Array::PageMethod) + end + + def test_pagy_nav_page_1 + pagy, _ = @array.pagy(1) + + assert_equal( + '', + frontend.pagy_nav(pagy) + ) + end + + def test_pagy_nav_page_3 + pagy, _ = @array.pagy(3) + + assert_equal( + '', + frontend.pagy_nav(pagy) + ) + end + + def test_pagy_nav_page_6 + pagy, _ = @array.pagy(6) + + assert_equal( + '', + frontend.pagy_nav(pagy) + ) + end + end + + describe "#pagy_t" do + def test_data + assert_equal "‹ Prev", Pagy::Frontend::I18N_DATA['pagy']['nav']['prev'] + assert_equal "…", Pagy::Frontend::I18N_DATA['pagy']['nav']['gap'] + end + + def test_translation + assert_equal "‹ Prev", frontend.pagy_t('pagy.nav.prev') + + assert_equal "items", frontend.pagy_t('pagy.info.item_name', count: 0) + assert_equal "item", frontend.pagy_t('pagy.info.item_name', count: 1) + assert_equal "items", frontend.pagy_t('pagy.info.item_name', count: 10) + + assert_equal "No %{item_name} found", + frontend.pagy_t('pagy.info.single_page', count: 0) + assert_equal "Displaying 1 %{item_name}", + frontend.pagy_t('pagy.info.single_page', count: 1) + assert_equal "Displaying all 10 %{item_name}", + frontend.pagy_t('pagy.info.single_page', count: 10) + assert_equal "Displaying %{item_name} %{from}-%{to} of 10 in total", + frontend.pagy_t('pagy.info.multiple_pages', count: 10) + end + + def test_missing + assert_equal 'translation missing: "pagy.nav.not_here"', frontend.pagy_t('pagy.nav.not_here') + end + + def test_render_info_no_118n_key + pagy = Pagy.new count: 0 + assert_equal "No items found", frontend.pagy_info(pagy) + pagy = Pagy.new count: 1 + assert_equal "Displaying 1 item", frontend.pagy_info(pagy) + pagy = Pagy.new count: 13 + assert_equal "Displaying all 13 items", frontend.pagy_info(pagy) + pagy = Pagy.new count: 100, page: 3 + assert_equal "Displaying items 41-60 of 100 in total", frontend.pagy_info(pagy) + end + + def test_render_info_with_existing_118n_key + Pagy::Frontend::I18N_DATA['pagy']['info']['product'] = { 'zero' => 'Products', + 'one' => 'Product', + 'other' => 'Products' } + pagy = Pagy.new count: 0, item_path: 'pagy.info.product' + assert_equal "No Products found", frontend.pagy_info(pagy) + pagy = Pagy.new count: 1, item_path: 'pagy.info.product' + assert_equal "Displaying 1 Product", frontend.pagy_info(pagy) + pagy = Pagy.new count: 13, item_path: 'pagy.info.product' + assert_equal "Displaying all 13 Products", frontend.pagy_info(pagy) + pagy = Pagy.new count: 100, page: 3, item_path: 'pagy.info.product' + assert_equal "Displaying Products 41-60 of 100 in total", frontend.pagy_info(pagy) + end + end + +end diff --git a/test/pagy_test.rb b/test/pagy_test.rb new file mode 100644 index 000000000..537c3ee7b --- /dev/null +++ b/test/pagy_test.rb @@ -0,0 +1,370 @@ +require_relative 'test_helper' + +describe Pagy do + let(:pagy) { Pagy.new count: 100, page: 4 } + + def test_version_number + refute_nil Pagy::VERSION + end + + describe "#initialize" do + def test_initialization + assert_instance_of Pagy, pagy + assert_instance_of Pagy, Pagy.new(count: 100) + assert_instance_of Pagy, Pagy.new(count: '100') + assert_instance_of Pagy, Pagy.new(count: 100, page: '2') + assert_instance_of Pagy, Pagy.new(count: 100, page: '') + assert_instance_of Pagy, Pagy.new(count: 100, items: '10') + assert_raises(ArgumentError) { Pagy.new({}) } + assert_raises(ArgumentError) { Pagy.new(count: 100, page: 0) } + assert_raises(ArgumentError) { Pagy.new(count: 100, page: 2, items: 0) } + assert_raises(ArgumentError) { Pagy.new(count: 100, page: 2, size: [1,2,3]).series } + assert_raises(ArgumentError) { Pagy.new(count: 100, page: 2, size: [1,2,3,'4']).series } + assert_raises(Pagy::OutOfRangeError) { Pagy.new(count: 100, page: '11') } + assert_raises(Pagy::OutOfRangeError) { Pagy.new(count: 100, page: 12 ) } + end + end + + describe "accessors" do + def test_respond_to_accessors + [ + :count, :page, :items, :vars, # input + :offset, :pages, :last, :from, :to, :prev, :next, :series # output + ].each do |meth| + assert_respond_to pagy, meth + end + end + end + + describe "#series" do + def setup + @vars0 = { count: 103, + items: 10, + size: [0,2,2,0] } + + @vars1 = { count: 103, + items: 10, + size: [3,0,0,3] } + + @vars2 = { count: 103, + items: 10, + size: [3,2,0,0] } + + end + + def series_tests(page, *expected) + expected.each_with_index do |e, i| + vars = instance_variable_get(:"@vars#{i}").merge(page: page) + assert_equal e, Pagy.new(vars).series + end + end + + def test_page_1 + series_tests 1, + ["1", 2, 3, :gap], + ["1", 2, 3, :gap, 9, 10, 11], + ["1", 2, 3, :gap] + end + + def test_page_2 + series_tests 2, + [1, "2", 3, 4, :gap], + [1, "2", 3, :gap, 9, 10, 11], + [1, "2", 3, :gap] + end + + def test_page_3 + series_tests 3, + [1, 2, "3", 4, 5, :gap], + [1, 2, "3", :gap, 9, 10, 11], + [1, 2, "3", :gap] + end + + def test_page_4 + series_tests 4, + [1, 2, 3, "4", 5, 6, :gap], + [1, 2, 3, "4", :gap, 9, 10, 11], + [1, 2, 3, "4", :gap] + end + + def test_page_5 + series_tests 5, + [:gap, 3, 4, "5", 6, 7, :gap], + [1, 2, 3, 4, "5", :gap, 9, 10, 11], + [1, 2, 3, 4, "5", :gap] + end + + def test_page_6 + series_tests 6, + [:gap, 4, 5, "6", 7, 8, :gap], + [1, 2, 3, :gap, "6", :gap, 9, 10, 11], + [1, 2, 3, 4, 5, "6", :gap] + end + + def test_page_7 + series_tests 7, + [:gap, 5, 6, "7", 8, 9, :gap], + [1, 2, 3, :gap, "7", 8, 9, 10, 11], + [1, 2, 3, 4, 5, 6, "7", :gap] + end + + def test_page_8 + series_tests 8, + [:gap, 6, 7, "8", 9, 10, 11], + [1, 2, 3, :gap, "8", 9, 10, 11], + [1, 2, 3, :gap, 6, 7, "8", :gap] + end + + def test_page_9 + series_tests 9, + [:gap, 7, 8, "9", 10, 11], + [1, 2, 3, :gap, "9", 10, 11], + [1, 2, 3, :gap, 7, 8, "9", :gap] + end + + def test_page_10 + series_tests 10, + [:gap, 8, 9, "10", 11], + [1, 2, 3, :gap, 9, "10", 11], + [1, 2, 3, :gap, 8, 9, "10", 11] + end + + def test_page_11 + series_tests 11, + [:gap, 9, 10, "11"], + [1, 2, 3, :gap, 9, 10, "11"], + [1, 2, 3, :gap, 9, 10, "11"] + end + end + + # TODO: split these into #initialize or #series + describe "metrics" do + def setup + @vars = { items: 10, size: [3,2,2,3] } + @array = (1..103).to_a.extend(Pagy::Array::PageMethod) + end + + def test_count_0 + pagy = Pagy.new @vars.merge(count: 0) + assert_equal 1, pagy.pages + assert_equal 1, pagy.last + assert_equal 0, pagy.offset + assert_equal 0, pagy.from + assert_equal 0, pagy.to + assert_nil pagy.prev + assert_nil pagy.next + assert_equal ["1"], pagy.series + end + + def test_single_page + pagy = Pagy.new @vars.merge(count: 8) + assert_equal 1, pagy.pages + assert_equal 1, pagy.last + assert_equal 0, pagy.offset + assert_equal 1, pagy.from + assert_equal 8, pagy.to + assert_nil pagy.prev + assert_nil pagy.next + assert_equal ["1"], pagy.series + end + + def test_1_of_2_pages + pagy = Pagy.new @vars.merge(count: 15) + assert_equal 2, pagy.pages + assert_equal 2, pagy.last + assert_equal 0, pagy.offset + assert_equal 1, pagy.from + assert_equal 10, pagy.to + assert_nil pagy.prev + assert_equal 2, pagy.next + assert_equal ["1", 2], pagy.series + end + + def test_2_of_2_pages + pagy = Pagy.new @vars.merge(count: 15, page: 2) + assert_equal 2, pagy.pages + assert_equal 2, pagy.last + assert_equal 10, pagy.offset + assert_equal 11, pagy.from + assert_equal 15, pagy.to + assert_equal 1, pagy.prev + assert_nil pagy.next + assert_equal [1, "2"], pagy.series + end + + def test_page_1 + pagy, paged = @array.pagy(1, @vars) + assert_equal (1..10).to_a, paged + assert_instance_of Pagy, pagy + assert_equal 1, pagy.from + assert_equal 10, pagy.to + assert_nil pagy.prev + assert_equal 1, pagy.page + assert_equal 2, pagy.next + assert_equal ["1", 2, 3, :gap, 9, 10, 11], pagy.series + end + + def test_page_2 + pagy, paged = @array.pagy(2, @vars) + assert_equal (11..20).to_a, paged + assert_instance_of Pagy, pagy + assert_equal 11, pagy.from + assert_equal 20, pagy.to + assert_equal 1, pagy.prev + assert_equal 2, pagy.page + assert_equal 3, pagy.next + assert_equal [1, "2", 3, 4, :gap, 9, 10, 11], pagy.series + end + + def test_page_3 + pagy, paged = @array.pagy(3, @vars) + assert_equal (21..30).to_a, paged + assert_instance_of Pagy, pagy + assert_equal 21, pagy.from + assert_equal 30, pagy.to + assert_equal 2, pagy.prev + assert_equal 3, pagy.page + assert_equal 4, pagy.next + assert_equal [1, 2, "3", 4, 5, :gap, 9, 10, 11], pagy.series + end + + def test_page_4 + pagy, paged = @array.pagy(4, @vars) + assert_equal (31..40).to_a, paged + assert_instance_of Pagy, pagy + assert_equal 31, pagy.from + assert_equal 40, pagy.to + assert_equal 3, pagy.prev + assert_equal 4, pagy.page + assert_equal 5, pagy.next + assert_equal [1, 2, 3, "4", 5, 6, :gap, 9, 10, 11], pagy.series + end + + def test_page_5 + pagy, paged = @array.pagy(5, @vars) + assert_equal (41..50).to_a, paged + assert_instance_of Pagy, pagy + assert_equal 41, pagy.from + assert_equal 50, pagy.to + assert_equal 4, pagy.prev + assert_equal 5, pagy.page + assert_equal 6, pagy.next + assert_equal [1, 2, 3, 4, "5", 6, 7, 8, 9, 10, 11], pagy.series + end + + def test_page_6 + pagy, paged = @array.pagy(6, @vars) + assert_equal (51..60).to_a, paged + assert_equal 51, pagy.from + assert_equal 60, pagy.to + assert_instance_of Pagy, pagy + assert_equal 5, pagy.prev + assert_equal 6, pagy.page + assert_equal 7, pagy.next + assert_equal [1, 2, 3, 4, 5, "6", 7, 8, 9, 10, 11], pagy.series + end + + def test_page_7 + pagy, paged = @array.pagy(7, @vars) + assert_equal (61..70).to_a, paged + assert_instance_of Pagy, pagy + assert_equal 61, pagy.from + assert_equal 70, pagy.to + assert_equal 6, pagy.prev + assert_equal 7, pagy.page + assert_equal 8, pagy.next + assert_equal [1, 2, 3, 4, 5, 6, "7", 8, 9, 10, 11], pagy.series + end + + def test_page_8 + pagy, paged = @array.pagy(8, @vars) + assert_equal (71..80).to_a, paged + assert_instance_of Pagy, pagy + assert_equal 71, pagy.from + assert_equal 80, pagy.to + assert_equal 7, pagy.prev + assert_equal 8, pagy.page + assert_equal 9, pagy.next + assert_equal [1, 2, 3, :gap, 6, 7, "8", 9, 10, 11], pagy.series + end + + def test_page_9 + pagy, paged = @array.pagy(9, @vars) + assert_equal (81..90).to_a, paged + assert_instance_of Pagy, pagy + assert_equal 81, pagy.from + assert_equal 90, pagy.to + assert_equal 8, pagy.prev + assert_equal 9, pagy.page + assert_equal 10, pagy.next + assert_equal [1, 2, 3, :gap, 7, 8, "9", 10, 11], pagy.series + end + + def test_page_10 + pagy, paged = @array.pagy(10, @vars) + assert_equal (91..100).to_a, paged + assert_instance_of Pagy, pagy + assert_equal 91, pagy.from + assert_equal 100, pagy.to + assert_equal 9, pagy.prev + assert_equal 10, pagy.page + assert_equal 11, pagy.next + assert_equal [1, 2, 3, :gap, 8, 9, "10", 11], pagy.series + end + + def test_page_11 + pagy, paged = @array.pagy(11, @vars) + assert_equal (101..103).to_a, paged + assert_instance_of Pagy, pagy + assert_equal 101, pagy.from + assert_equal 103, pagy.to + assert_equal 10, pagy.prev + assert_equal 11, pagy.page + assert_nil pagy.next + assert_equal [1, 2, 3, :gap, 9, 10, "11"], pagy.series + end + + def test_other_output + pagy = Pagy.new @vars.merge(count: 103, page: 2) + assert_equal 103, pagy.count + assert_equal 10, pagy.items + assert_equal 10, pagy.offset + assert_equal 11 , pagy.pages + assert_equal 11, pagy.last + end + + def test_initial_offset_page_1 + pagy = Pagy.new(count: 87, page:1, outset:10, items: 10) + assert_equal 10, pagy.offset + assert_equal 10, pagy.items + assert_equal 1, pagy.from + assert_equal 10, pagy.to + assert_equal 9, pagy.pages + end + + def test_initial_offset_page_9 + pagy = Pagy.new(count: 87, page:9, outset:10, items: 10) + assert_equal 90, pagy.offset + assert_equal 7, pagy.items + assert_equal 81, pagy.from + assert_equal 87, pagy.to + assert_equal 9, pagy.pages + end + + def test_items_of_last_page_of_one + pagy = Pagy.new items: 10, count: 0 + assert_equal 0, pagy.items + pagy = Pagy.new items: 10, count: 4 + assert_equal 4, pagy.items + pagy = Pagy.new items: 10, count: 10 + assert_equal 10, pagy.items + end + + def test_items_of_last_page_of_many + pagy = Pagy.new items: 10, count: 14, page: 2 + assert_equal 4, pagy.items + pagy = Pagy.new items: 10, count: 20, page: 2 + assert_equal 10, pagy.items + end + end +end diff --git a/test/series_test.rb b/test/series_test.rb deleted file mode 100644 index 214090bef..000000000 --- a/test/series_test.rb +++ /dev/null @@ -1,106 +0,0 @@ -require "test_helper" - -class SeriesTest < Minitest::Test - - - def setup - - @vars0 = { count: 103, - items: 10, - size: [0,2,2,0] } - - @vars1 = { count: 103, - items: 10, - size: [3,0,0,3] } - - @vars2 = { count: 103, - items: 10, - size: [3,2,0,0] } - - end - - def series_tests(page, *expected) - expected.each_with_index do |e, i| - vars = instance_variable_get(:"@vars#{i}").merge(page: page) - assert_equal e, Pagy.new(vars).series - end - end - - def test_page_1 - series_tests 1, - ["1", 2, 3, :gap], - ["1", 2, 3, :gap, 9, 10, 11], - ["1", 2, 3, :gap] - end - - def test_page_2 - series_tests 2, - [1, "2", 3, 4, :gap], - [1, "2", 3, :gap, 9, 10, 11], - [1, "2", 3, :gap] - end - - def test_page_3 - series_tests 3, - [1, 2, "3", 4, 5, :gap], - [1, 2, "3", :gap, 9, 10, 11], - [1, 2, "3", :gap] - end - - def test_page_4 - series_tests 4, - [1, 2, 3, "4", 5, 6, :gap], - [1, 2, 3, "4", :gap, 9, 10, 11], - [1, 2, 3, "4", :gap] - end - - def test_page_5 - series_tests 5, - [:gap, 3, 4, "5", 6, 7, :gap], - [1, 2, 3, 4, "5", :gap, 9, 10, 11], - [1, 2, 3, 4, "5", :gap] - end - - def test_page_6 - series_tests 6, - [:gap, 4, 5, "6", 7, 8, :gap], - [1, 2, 3, :gap, "6", :gap, 9, 10, 11], - [1, 2, 3, 4, 5, "6", :gap] - end - - def test_page_7 - series_tests 7, - [:gap, 5, 6, "7", 8, 9, :gap], - [1, 2, 3, :gap, "7", 8, 9, 10, 11], - [1, 2, 3, 4, 5, 6, "7", :gap] - end - - def test_page_8 - series_tests 8, - [:gap, 6, 7, "8", 9, 10, 11], - [1, 2, 3, :gap, "8", 9, 10, 11], - [1, 2, 3, :gap, 6, 7, "8", :gap] - end - - def test_page_9 - series_tests 9, - [:gap, 7, 8, "9", 10, 11], - [1, 2, 3, :gap, "9", 10, 11], - [1, 2, 3, :gap, 7, 8, "9", :gap] - end - - def test_page_10 - series_tests 10, - [:gap, 8, 9, "10", 11], - [1, 2, 3, :gap, 9, "10", 11], - [1, 2, 3, :gap, 8, 9, "10", 11] - end - - def test_page_11 - series_tests 11, - [:gap, 9, 10, "11"], - [1, 2, 3, :gap, 9, 10, "11"], - [1, 2, 3, :gap, 9, 10, "11"] - end - -end diff --git a/test/test_helper.rb b/test/test_helper.rb index e170bf83b..f5dd8d23d 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -1,4 +1,4 @@ $LOAD_PATH.unshift File.expand_path("../../lib", __FILE__) require 'pagy' -require 'array.rb' +require_relative 'array' require "minitest/autorun"