Skip to content

Commit

Permalink
refactoring and additions of tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ddnexus committed Apr 23, 2021
1 parent 1538f24 commit d2891fb
Show file tree
Hide file tree
Showing 35 changed files with 562 additions and 944 deletions.
1 change: 0 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ gem 'rake-manifest'
group :test do
gem 'codecov', require: false
gem 'minitest'
gem 'minitest-reporters'
gem 'rubocop', '~> 1.12'
gem 'rubocop-minitest'
gem 'rubocop-packaging'
Expand Down
10 changes: 1 addition & 9 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
GEM
remote: https://rubygems.org/
specs:
ansi (1.5.0)
ast (2.4.2)
benchmark-ips (2.8.4)
builder (3.2.4)
codecov (0.5.1)
simplecov (>= 0.15, < 0.22)
concurrent-ruby (1.1.8)
Expand All @@ -17,11 +15,6 @@ GEM
memoist (0.11.0)
memory_profiler (1.0.0)
minitest (5.14.4)
minitest-reporters (1.4.3)
ansi
builder
minitest (>= 5.0)
ruby-progressbar
multi_json (1.15.0)
mustermann (1.1.1)
ruby2_keywords (~> 0.0.1)
Expand Down Expand Up @@ -93,7 +86,6 @@ DEPENDENCIES
kalibera
memory_profiler
minitest
minitest-reporters
oj
puma
rack
Expand All @@ -109,4 +101,4 @@ DEPENDENCIES
sinatra-contrib

BUNDLED WITH
2.2.3
2.2.15
4 changes: 2 additions & 2 deletions tasks/test.rake
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ test_tasks = {}
i18n
overflow
support
oj_shared
shared
shared_oj
shared_json
trim
items_trim
items_countless
Expand Down
File renamed without changes.
File renamed without changes.
25 changes: 2 additions & 23 deletions test/locales/utils/p11n_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,49 +4,40 @@

require_relative '../../test_helper'

describe 'p11n' do

_, p11n = eval(Pagy.root.join('locales', 'utils', 'p11n.rb').read) #rubocop:disable Security/Eval
describe 'locales/utils/p11n' do
let(:p11n) { eval(Pagy.root.join('locales', 'utils', 'p11n.rb').read)[1].freeze } #rubocop:disable Security/Eval

describe :one_other do

it "detects that 1 belongs to 'one'" do
_(p11n[:one_other].call(1)).must_equal 'one'
end

[0, 0.3, 1.2, 2, 3, 5, 10, 11, 21, 23, 31, 50, 81, 99, 100].each do |count|
it "detects that #{count} belongs to 'other'" do
_(p11n[:one_other].call(count)).must_equal 'other'
end
end

end

describe :one_two_other do
it "detects that 1 belongs to 'one'" do
_(p11n[:one_two_other].call(1)).must_equal 'one'
end

it "detects that 2 belongs to 'two'" do
_(p11n[:one_two_other].call(2)).must_equal 'two'
end

[0, 0.3, 1.2, 3, 5, 10, 11, 21, 23, 31, 50, 81, 99, 100].each do |count|
it "detects that #{count} belongs to 'other'" do
_(p11n[:one_two_other].call(count)).must_equal 'other'
end
end

end

describe :one_upto_two_other do

[0, 0.5, 1, 1.2, 1.8].each do |count|
it "detects that #{count} belongs to 'one'" do
_(p11n[:one_upto_two_other].call(count)).must_equal 'one'
end
end

[2, 2.1, 5, 11, 21, 22, 37, 40, 900.5].each do |count|
it "detects that #{count} belongs to 'other'" do
_(p11n[:one_upto_two_other].call(count)).must_equal 'other'
Expand All @@ -55,66 +46,54 @@
end

describe :other do

[0, 1, 1.2, 2, 5, 11, 21, 22, 27, 99, 1000].each do |count|
it "detects that #{count} belongs to 'other'" do
_(p11n[:other].call(count)).must_equal 'other'
end
end

end

describe :east_slavic do

[1, 21, 51, 71, 101, 1031].each do |count|
it "detects that #{count} belongs to 'one'" do
_(p11n[:east_slavic].call(count)).must_equal 'one'
end
end

[2, 3, 4, 22, 23, 24, 92, 93, 94].each do |count|
it "detects that #{count} belongs to 'few'" do
_(p11n[:east_slavic].call(count)).must_equal 'few'
end
end

[0, 5, 8, 10, 11, 18, 20, 25, 27, 30, 35, 38, 40].each do |count|
it "detects that #{count} belongs to 'many'" do
_(p11n[:east_slavic].call(count)).must_equal 'many'
end
end

[1.2, 3.7, 11.5, 20.8, 1004.3].each do |count|
it "detects that #{count} in ru is 'other'" do
_(p11n[:east_slavic].call(count)).must_equal 'other'
end
end

end

describe :polish do

it "detects that 1 belongs to 'one'" do
_(p11n[:polish].call(1)).must_equal 'one'
end

[2, 3, 4, 22, 23, 24, 92, 93, 94].each do |count|
it "detects that #{count} belongs to 'few'" do
_(p11n[:polish].call(count)).must_equal 'few'
end
end

[0, 5, 8, 10, 11, 18, 20, 21, 25, 27, 30, 31, 35, 38, 40, 41, 114].each do |count|
it "detects that #{count} belongs to 'many'" do
_(p11n[:polish].call(count)).must_equal 'many'
end
end

[1.2, 3.7, 11.5, 20.8, 1004.3].each do |count|
it "detects that #{count} belongs to 'other'" do
_(p11n[:polish].call(count)).must_equal 'other'
end
end
end

end
17 changes: 1 addition & 16 deletions test/pagy/backend_test.rb
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
# frozen_string_literal: true

require_relative '../test_helper'

describe Pagy::Backend do

describe 'pagy/backend' do
let(:controller) { MockController.new }

describe '#pagy' do

before do
@collection = MockCollection.new
end

it 'paginates with defaults' do
pagy, records = controller.send(:pagy, @collection)
_(pagy).must_be_instance_of Pagy
Expand All @@ -21,7 +17,6 @@
_(records.count).must_equal Pagy::VARS[:items]
_(records).must_equal [41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60]
end

it 'paginates with vars' do
pagy, records = controller.send(:pagy, @collection, page: 2, items: 10, link_extra: 'X')
_(pagy).must_be_instance_of Pagy
Expand All @@ -32,15 +27,12 @@
_(records.count).must_equal 10
_(records).must_equal [11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
end

end

describe '#pagy_get_vars' do

before do
@collection = MockCollection.new
end

it 'gets defaults' do
vars = {}
merged = controller.send :pagy_get_vars, @collection, vars
Expand All @@ -49,7 +41,6 @@
_(merged[:count]).must_equal 1000
_(merged[:page]).must_equal 3
end

it 'gets vars' do
vars = {page: 2, items: 10, link_extra: 'X'}
merged = controller.send :pagy_get_vars, @collection, vars
Expand All @@ -62,7 +53,6 @@
_(merged[:items]).must_equal 10
_(merged[:link_extra]).must_equal 'X'
end

it 'works with grouped collections' do
@collection = MockCollection::Grouped.new((1..1000).to_a)
vars = {page: 2, items: 10, link_extra: 'X'}
Expand All @@ -76,7 +66,6 @@
_(merged[:items]).must_equal 10
_(merged[:link_extra]).must_equal 'X'
end

it 'overrides count and page' do
vars = {count: 10, page: 32}
merged = controller.send :pagy_get_vars, @collection, vars
Expand All @@ -85,18 +74,14 @@
_(merged.keys).must_include :page
_(merged[:page]).must_equal 32
end

end

describe '#pagy_get_items' do

it 'gets items' do
collection = MockCollection.new
pagy = Pagy.new count: 1000
items = controller.send :pagy_get_items, collection, pagy
_(items).must_equal [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
end

end

end
15 changes: 1 addition & 14 deletions test/pagy/countless_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,18 @@
require_relative '../test_helper'
require 'pagy/extras/countless'

describe Pagy::Countless do

describe 'pagy/countless' do
let(:controller) { MockController.new } # page = 3, items = 20

describe '#finalize' do

before do
@empty_collection = MockCollection.new([])
@collection = MockCollection.new(Array(1..59))
end

let(:last_page) { 3 }

it 'raises exception with no retrieved items' do
_(proc { Pagy::Countless.new(page: 2).finalize(0) }).must_raise Pagy::OverflowError
end

it 'initializes empty collection' do
pagy, = controller.send(:pagy_countless, @empty_collection, page: 1)
_(pagy.items).must_equal 20
Expand All @@ -30,7 +25,6 @@
_(pagy.prev).must_be_nil
_(pagy.next).must_be_nil
end

it 'initializes first page' do
pagy, = controller.send(:pagy_countless, @collection, page: 1)
_(pagy).must_be_instance_of Pagy::Countless
Expand All @@ -42,7 +36,6 @@
_(pagy.prev).must_be_nil
_(pagy.next).must_equal 2
end

it 'initializes single full page' do
pagy, = controller.send(:pagy_countless, MockCollection.new(Array(1..20)), page: 1)
_(pagy.items).must_equal 20
Expand All @@ -52,7 +45,6 @@
_(pagy.prev).must_be_nil
_(pagy.next).must_be_nil
end

it 'initialize single partial page' do
pagy, = controller.send(:pagy_countless, MockCollection.new(Array(1..4)), page: 1)
_(pagy.items).must_equal 4
Expand All @@ -61,9 +53,7 @@
_(pagy.to).must_equal 4
_(pagy.prev).must_be_nil
_(pagy.next).must_be_nil

end

it 'initializes last partial page' do
pagy, = controller.send(:pagy_countless, @collection, page: last_page)
_(pagy.items).must_equal 19
Expand All @@ -73,7 +63,6 @@
_(pagy.prev).must_equal(last_page - 1)
_(pagy.next).must_be_nil
end

it 'handles the :cycle variable' do
pagy, = controller.send(:pagy_countless, @collection, page: last_page, cycle: true)
_(pagy.items).must_equal 19
Expand All @@ -83,7 +72,5 @@
_(pagy.prev).must_equal(last_page - 1)
_(pagy.next).must_equal 1
end

end

end
7 changes: 1 addition & 6 deletions test/pagy/exceptions_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@

require_relative '../test_helper'

describe Pagy::VariableError do
describe 'pagy/exceptions' do

describe '#variable and #value' do

it 'raises for non overflow pages' do
begin
Pagy.new(count: 1, page: 0)
Expand Down Expand Up @@ -35,21 +34,17 @@
_(e.variable).must_equal :count
_(e.value).must_equal(-10)
end

it 'rescues as ArgumentError' do
Pagy.new(count: 1, page: -10)
rescue ArgumentError => e
_(e.variable).must_equal :page
_(e.value).must_equal(-10)
end

it 'does not raise for :count and :offset set to arbitrary strings (converted to 0)' do
pagy = Pagy.new(count: 'string', outset: 'string')
_(pagy).must_be_instance_of Pagy
_(pagy.count).must_equal 0
_(pagy.instance_variable_get(:@outset)).must_equal 0
end

end

end
Loading

0 comments on commit d2891fb

Please sign in to comment.