Skip to content

Commit

Permalink
refactoring of test structure
Browse files Browse the repository at this point in the history
  • Loading branch information
ddnexus committed Jun 17, 2018
1 parent 1cc335b commit e2efb89
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 53 deletions.
52 changes: 15 additions & 37 deletions test/pagy/backend_test.rb
Original file line number Diff line number Diff line change
@@ -1,41 +1,9 @@
require_relative '../test_helper'
require 'rack'

SingleCov.covered!

describe Pagy::Backend do

class TestController
include Pagy::Backend

def params(vars={})
{a: 'a', page: 3}.merge(vars)
end

end

class TestCollection < Array

def initialize(*args)
super
@collection = self.clone
end

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

def limit(value)
@collection[0, value]
end

def count(*_)
size
end

end


let(:backend) { TestController.new }

Expand Down Expand Up @@ -71,21 +39,31 @@ def test_pagy_method_with_vars

end

describe "#pagy_get_variables" do
describe "#pagy_get_vars" do
before do
@collection = TestCollection.new((1..1000).to_a)
end

def test_pagy_get_variables_with_default
def test_pagy_get_vars_with_default
vars = {}
merged = backend.send :pagy_get_vars, @collection, vars
assert_equal({:count=>1000, :page=>3}, merged)
assert_includes(merged.keys, :count)
assert_includes(merged.keys, :page)
assert_equal(1000, merged[:count])
assert_equal(3, merged[:page])
end

def test_pagy_get_variables_with_vars
def test_pagy_get_vars_with_vars
vars = {page: 2, items: 10, link_extra: 'X'}
merged = backend.send :pagy_get_vars, @collection, vars
assert_equal({:count=>1000, :page=>2, :items=>10, :link_extra=>"X"}, merged)
assert_includes(merged.keys, :count)
assert_includes(merged.keys, :page)
assert_includes(merged.keys, :items)
assert_includes(merged.keys, :link_extra)
assert_equal(1000, merged[:count])
assert_equal(2, merged[:page])
assert_equal(10, merged[:items])
assert_equal('X', merged[:link_extra])
end

end
Expand Down
15 changes: 0 additions & 15 deletions test/pagy/frontend_test.rb
Original file line number Diff line number Diff line change
@@ -1,24 +1,9 @@
require_relative '../test_helper'
require 'rack'

SingleCov.covered!

describe Pagy::Frontend do

class TestView
include Pagy::Frontend

def request
Rack::Request.new('SCRIPT_NAME' => '/foo')
end
end

class TestViewOverride < TestView
def pagy_get_params(params)
params.except(:a).merge!(k: 'k')
end
end

let(:frontend) { TestView.new }

describe "#pagy_nav" do
Expand Down
5 changes: 4 additions & 1 deletion test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,8 @@

$LOAD_PATH.unshift File.expand_path("../../lib", __FILE__)
require 'pagy'
require_relative 'array'
require 'rack'
require_relative 'test_helper/array'
require_relative 'test_helper/frontend'
require_relative 'test_helper/backend'
require "minitest/autorun"
File renamed without changes.
32 changes: 32 additions & 0 deletions test/test_helper/backend.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
class TestController
include Pagy::Backend

attr_reader :params

def initialize(params={a: 'a', page: 3})
@params = params
end

end

class TestCollection < Array

def initialize(*args)
super
@collection = self.clone
end

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

def limit(value)
@collection[0, value]
end

def count(*_)
size
end

end
14 changes: 14 additions & 0 deletions test/test_helper/frontend.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class TestView
include Pagy::Frontend

def request
Rack::Request.new('SCRIPT_NAME' => '/foo')
end
end

class TestViewOverride < TestView
def pagy_get_params(params)
params.except(:a).merge!(k: 'k')
end
end

0 comments on commit e2efb89

Please sign in to comment.