Skip to content

Commit

Permalink
code-restyling: test
Browse files Browse the repository at this point in the history
  • Loading branch information
ddnexus committed Apr 10, 2021
1 parent cb67353 commit aea25a1
Show file tree
Hide file tree
Showing 35 changed files with 130 additions and 147 deletions.
1 change: 0 additions & 1 deletion test/locales/utils/p11n_test.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# encoding: utf-8
# frozen_string_literal: true

# Tests adapted from https://github.com/svenfuchs/rails-i18n
Expand Down
5 changes: 3 additions & 2 deletions test/mock_helpers/arel.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module Arel
def self.star(*)
self
Expand All @@ -13,8 +15,7 @@ def self.over(*)

module Nodes
class Grouping
def initialize(array)
end
def initialize(_); end
end
end
end
8 changes: 5 additions & 3 deletions test/mock_helpers/collection.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
# frozen_string_literal: true

class MockCollection < Array

def initialize(arr=Array(1..1000))
super
@collection = self.clone
@collection = clone
end

def offset(value)
@collection = self[value..-1]
@collection = self[value..]
self
end

Expand All @@ -33,7 +35,7 @@ def group_values
class Grouped < MockCollection

def count(*)
Hash[@collection.map { |value| [value, value + 1] }]
@collection.map { |value| [value, value + 1] }.to_h
end

def unscope(*)
Expand Down
1 change: 0 additions & 1 deletion test/mock_helpers/controller.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# encoding: utf-8
# frozen_string_literal: true

require_relative 'collection'
Expand Down
4 changes: 3 additions & 1 deletion test/mock_helpers/elasticsearch_rails.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
# frozen_string_literal: true

require 'pagy/extras/elasticsearch_rails'

module MockElasticsearchRails

RESULTS = { 'a' => ('a-1'..'a-1000').to_a,
'b' => ('b-1'..'b-1000').to_a }
'b' => ('b-1'..'b-1000').to_a }.freeze

class Search

Expand Down
12 changes: 7 additions & 5 deletions test/mock_helpers/searchkick.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
# frozen_string_literal: true

require 'pagy/extras/searchkick'

module MockSearchkick

RESULTS = { 'a' => ('a-1'..'a-1000').to_a,
'b' => ('b-1'..'b-1000').to_a }
'b' => ('b-1'..'b-1000').to_a }.freeze

class Results

Expand All @@ -15,13 +17,13 @@ def initialize(query, options={}, &block)
from = @options[:per_page] * (@options[:page] - 1)
results = @entries[from, @options[:per_page]]
addition = yield if block
@results = results && results.map{|r| "#{addition}#{r}"}
@results = results&.map{|r| "#{addition}#{r}"}
end

def results
@results.map{|r| "R-#{r}"}
end
alias_method :records, :results # enables loops in items_test
alias records results # enables loops in items_test

def total_count
@entries.size
Expand All @@ -31,8 +33,8 @@ def total_count

class Model

def self.search(*args, &block)
Results.new(*args, &block)
def self.search(...)
Results.new(...)
end

extend Pagy::Searchkick
Expand Down
1 change: 0 additions & 1 deletion test/mock_helpers/view.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# encoding: utf-8
# frozen_string_literal: true

class MockView
Expand Down
7 changes: 3 additions & 4 deletions test/pagy/backend_test.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# encoding: utf-8
# frozen_string_literal: true

require_relative '../test_helper'
Expand All @@ -7,7 +6,7 @@

let(:controller) { MockController.new }

describe "#pagy" do
describe '#pagy' do

before do
@collection = MockCollection.new
Expand Down Expand Up @@ -36,7 +35,7 @@

end

describe "#pagy_get_vars" do
describe '#pagy_get_vars' do

before do
@collection = MockCollection.new
Expand Down Expand Up @@ -89,7 +88,7 @@

end

describe "#pagy_get_items" do
describe '#pagy_get_items' do

it 'gets items' do
collection = MockCollection.new
Expand Down
15 changes: 7 additions & 8 deletions test/pagy/countless_test.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# encoding: utf-8
# frozen_string_literal: true

require_relative '../test_helper'
Expand All @@ -8,7 +7,7 @@

let(:controller) { MockController.new } # page = 3, items = 20

describe "#finalize" do
describe '#finalize' do

before do
@empty_collection = MockCollection.new([])
Expand All @@ -22,7 +21,7 @@
end

it 'initializes empty collection' do
pagy, _ = controller.send(:pagy_countless, @empty_collection, page: 1)
pagy, = controller.send(:pagy_countless, @empty_collection, page: 1)
_(pagy.items).must_equal 20
_(pagy.pages).must_equal 1
_(pagy.last).must_equal 1
Expand All @@ -33,7 +32,7 @@
end

it 'initializes first page' do
pagy, _ = controller.send(:pagy_countless, @collection, page: 1)
pagy, = controller.send(:pagy_countless, @collection, page: 1)
_(pagy).must_be_instance_of Pagy::Countless
_(pagy.items).must_equal 20
_(pagy.last).must_equal 2
Expand All @@ -45,7 +44,7 @@
end

it 'initializes single full page' do
pagy, _ = controller.send(:pagy_countless, MockCollection.new(Array(1..20)), page: 1)
pagy, = controller.send(:pagy_countless, MockCollection.new(Array(1..20)), page: 1)
_(pagy.items).must_equal 20
_(pagy.pages).must_equal 1
_(pagy.from).must_equal 1
Expand All @@ -55,7 +54,7 @@
end

it 'initialize single partial page' do
pagy, _ = controller.send(:pagy_countless, MockCollection.new(Array(1..4)), page: 1)
pagy, = controller.send(:pagy_countless, MockCollection.new(Array(1..4)), page: 1)
_(pagy.items).must_equal 4
_(pagy.pages).must_equal 1
_(pagy.from).must_equal 1
Expand All @@ -66,7 +65,7 @@
end

it 'initializes last partial page' do
pagy, _ = controller.send(:pagy_countless, @collection, page: last_page)
pagy, = controller.send(:pagy_countless, @collection, page: last_page)
_(pagy.items).must_equal 19
_(pagy.pages).must_equal last_page
_(pagy.from).must_equal 41
Expand All @@ -76,7 +75,7 @@
end

it 'handles the :cycle variable' do
pagy, _ = controller.send(:pagy_countless, @collection, page: last_page, cycle: true)
pagy, = controller.send(:pagy_countless, @collection, page: last_page, cycle: true)
_(pagy.items).must_equal 19
_(pagy.pages).must_equal last_page
_(pagy.from).must_equal 41
Expand Down
3 changes: 1 addition & 2 deletions test/pagy/exceptions_test.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
# encoding: utf-8
# frozen_string_literal: true

require_relative '../test_helper'


describe Pagy::VariableError do

describe "#variable and #value" do
describe '#variable and #value' do

it 'raises for non overflow pages' do
begin
Expand Down
5 changes: 2 additions & 3 deletions test/pagy/extras/arel_test.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# encoding: utf-8
# frozen_string_literal: true

require_relative '../../test_helper'
Expand All @@ -9,7 +8,7 @@

let(:controller) { MockController.new }

describe "#pagy_arel" do
describe '#pagy_arel' do

before do
@collection = MockCollection.new
Expand Down Expand Up @@ -38,7 +37,7 @@

end

describe "#pagy_arel_get_vars" do
describe '#pagy_arel_get_vars' do

before do
@collection = MockCollection.new
Expand Down
5 changes: 2 additions & 3 deletions test/pagy/extras/array_test.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# encoding: utf-8
# frozen_string_literal: true

require_relative '../../test_helper'
Expand All @@ -8,7 +7,7 @@

let(:controller) { MockController.new }

describe "#pagy_array" do
describe '#pagy_array' do

before do
@collection = (1..1000).to_a
Expand Down Expand Up @@ -37,7 +36,7 @@

end

describe "#pagy_array_get_vars" do
describe '#pagy_array_get_vars' do

before do
@collection = (1..1000).to_a
Expand Down
7 changes: 3 additions & 4 deletions test/pagy/extras/bootstrap_test.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# encoding: utf-8
# frozen_string_literal: true

require_relative '../../test_helper'
Expand All @@ -9,7 +8,7 @@
let(:view) { MockView.new }
let(:pagy_test_id) { 'test-id' }

describe "#pagy_bootstrap_nav" do
describe '#pagy_bootstrap_nav' do

it 'renders first page' do
pagy = Pagy.new(count: 1000, page: 1)
Expand All @@ -31,7 +30,7 @@

end

describe "#pagy_bootstrap_nav_js" do
describe '#pagy_bootstrap_nav_js' do

it 'renders first page' do
pagy = Pagy.new(count: 1000, page: 1)
Expand Down Expand Up @@ -59,7 +58,7 @@

end

describe "#pagy_bootstrap_combo_nav_js" do
describe '#pagy_bootstrap_combo_nav_js' do

it 'renders first page' do
pagy = Pagy.new(count: 103, page: 1)
Expand Down
5 changes: 2 additions & 3 deletions test/pagy/extras/bulma_test.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# encoding: utf-8
# frozen_string_literal: true

require_relative '../../test_helper'
Expand Down Expand Up @@ -28,7 +27,7 @@

end

describe "#pagy_bulma_nav_js" do
describe '#pagy_bulma_nav_js' do

it 'renders first page' do
pagy = Pagy.new(count: 1000, page: 1)
Expand All @@ -52,7 +51,7 @@

end

describe "#pagy_bulma_combo_nav_js" do
describe '#pagy_bulma_combo_nav_js' do

it 'renders first page' do
pagy = Pagy.new(count: 103, page: 1)
Expand Down
15 changes: 7 additions & 8 deletions test/pagy/extras/countless_test.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# encoding: utf-8
# frozen_string_literal: true

require_relative '../../test_helper'
Expand All @@ -19,45 +18,45 @@
Pagy::VARS[:page_param] = @default_page_param
end

describe "#pagy_countless" do
describe '#pagy_countless' do

it 'shows current and next for first page' do
pagy, _ = controller.send(:pagy_countless, @collection, { size: [1, 4, 4, 1], page: 1 })
pagy, = controller.send(:pagy_countless, @collection, { size: [1, 4, 4, 1], page: 1 })
_(pagy.series).must_equal ['1', 2]
_(pagy.prev).must_be_nil
_(pagy.next).must_equal 2
end

it 'shows start-pages, :gap, before-pages, current and next for intermediate page' do
pagy, _ = controller.send(:pagy_countless, @collection, {page: 25})
pagy, = controller.send(:pagy_countless, @collection, {page: 25})
_(pagy.series).must_equal [1, :gap, 21, 22, 23, 24, '25', 26]
_(pagy.prev).must_equal 24
_(pagy.next).must_equal 26
end

it 'shows start-pages, :gap, before-pages, current and next for last page' do
pagy, _ = controller.send(:pagy_countless, @collection, {page: last_page})
pagy, = controller.send(:pagy_countless, @collection, {page: last_page})
_(pagy.series).must_equal [1, :gap, 46, 47, 48, 49, '50']
_(pagy.prev).must_equal 49
_(pagy.next).must_be_nil
end

it 'returns empty series for empty :size variable for first page' do
pagy, _ = controller.send(:pagy_countless, @collection, {size: [], page: 1})
pagy, = controller.send(:pagy_countless, @collection, {size: [], page: 1})
_(pagy.series).must_equal []
_(pagy.prev).must_be_nil
_(pagy.next).must_equal 2
end

it 'returns empty series for empty :size variable for intermediate page' do
pagy, _ = controller.send(:pagy_countless, @collection, {size: [], page: 25})
pagy, = controller.send(:pagy_countless, @collection, {size: [], page: 25})
_(pagy.series).must_equal []
_(pagy.prev).must_equal 24
_(pagy.next).must_equal 26
end

it 'returns empty series for empty :size variable for last page' do
pagy, _ = controller.send(:pagy_countless, @collection, {size: [], page: last_page})
pagy, = controller.send(:pagy_countless, @collection, {size: [], page: last_page})
_(pagy.series).must_equal []
_(pagy.prev).must_equal 49
_(pagy.next).must_be_nil
Expand Down
Loading

0 comments on commit aea25a1

Please sign in to comment.