Skip to content

Commit

Permalink
Add tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
parkr committed May 26, 2014
1 parent 69a4e0c commit 884f3cd
Show file tree
Hide file tree
Showing 11 changed files with 126 additions and 76 deletions.
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,5 @@ lib/bundler/man
pkg
rdoc
spec/reports
test/tmp
test/version_tmp
spec/dest
tmp
164 changes: 96 additions & 68 deletions spec/pager_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,102 +2,130 @@

describe(Jekyll::Paginate::Pager) do

should "calculate number of pages" do
assert_equal(0, Pager.calculate_pages([], '2'))
assert_equal(1, Pager.calculate_pages([1], '2'))
assert_equal(1, Pager.calculate_pages([1,2], '2'))
assert_equal(2, Pager.calculate_pages([1,2,3], '2'))
assert_equal(2, Pager.calculate_pages([1,2,3,4], '2'))
assert_equal(3, Pager.calculate_pages([1,2,3,4,5], '2'))
it "calculate number of pages" do
expect(described_class.calculate_pages([], '2')).to eql(0)
expect(described_class.calculate_pages([1], '2')).to eql(1)
expect(described_class.calculate_pages([1,2], '2')).to eql(1)
expect(described_class.calculate_pages([1,2,3], '2')).to eql(2)
expect(described_class.calculate_pages([1,2,3,4], '2')).to eql(2)
expect(described_class.calculate_pages([1,2,3,4,5], '2')).to eql(3)
end

should "determine the pagination path" do
assert_equal("/index.html", Pager.paginate_path(build_site, 1))
assert_equal("/page2", Pager.paginate_path(build_site, 2))
assert_equal("/index.html", Pager.paginate_path(build_site({'paginate_path' => '/blog/page-:num'}), 1))
assert_equal("/blog/page-2", Pager.paginate_path(build_site({'paginate_path' => '/blog/page-:num'}), 2))
assert_equal("/index.html", Pager.paginate_path(build_site({'paginate_path' => '/blog/page/:num'}), 1))
assert_equal("/blog/page/2", Pager.paginate_path(build_site({'paginate_path' => '/blog/page/:num'}), 2))
assert_equal("/contacts/index.html", Pager.paginate_path(build_site({'paginate_path' => '/contacts/page:num'}), 1))
assert_equal("/contacts/page2", Pager.paginate_path(build_site({'paginate_path' => '/contacts/page:num'}), 2))
assert_equal("/contacts/index.html", Pager.paginate_path(build_site({'paginate_path' => '/contacts/page/:num'}), 1))
assert_equal("/contacts/page/2", Pager.paginate_path(build_site({'paginate_path' => '/contacts/page/:num'}), 2))
context "with the default paginate_path" do
let(:site) { build_site }

it "determines the correct pagination path for each page" do
expect(described_class.paginate_path(site, 1)).to eql("/index.html")
expect(described_class.paginate_path(site, 2)).to eql("/page2")
end
end

context "with paginate_path set to a subdirectory with no index.html" do
let(:site) { build_site({'paginate_path' => '/blog/page-:num'}) }

it "determines the correct pagination path for each page" do
expect(described_class.paginate_path(site, 1)).to eql("/index.html")
expect(described_class.paginate_path(site, 2)).to eql("/blog/page-2")
end
end

context "with paginate_path set to a subdirectory with no index.html with num pages being in subdirectories" do
let(:site) { build_site({'paginate_path' => '/blog/page/:num'}) }

it "determines the correct pagination path for each page" do
expect(described_class.paginate_path(site, 1)).to eql("/index.html")
expect(described_class.paginate_path(site, 2)).to eql("/blog/page/2")
end
end

context "with paginate_path set to a subdirectory wherein an index.html exists" do
let(:site) { build_site({'paginate_path' => '/contacts/page:num'}) }

it "determines the correct pagination path for each page" do
expect(described_class.paginate_path(site, 1)).to eql("/contacts/index.html")
expect(described_class.paginate_path(site, 2)).to eql("/contacts/page2")
end
end

context "with paginate_path set to a subdir wherein an index.html exists with pages in subdirs" do
let(:site) { build_site({'paginate_path' => '/contacts/page/:num'}) }

it "determines the correct pagination path for each page" do
expect(described_class.paginate_path(site, 1)).to eql("/contacts/index.html")
expect(described_class.paginate_path(site, 2)).to eql("/contacts/page/2")
end
end

context "pagination disabled" do
should "report that pagination is disabled" do
assert !Pager.pagination_enabled?(build_site('paginate' => nil))
let(:site) { build_site('paginate' => nil) }

it "report that pagination is disabled" do
expect(described_class.pagination_enabled?(site)).to be_false
end
end

context "pagination enabled for 2" do
setup do
@site = build_site('paginate' => 2)
@posts = @site.posts
end
let(:site) { build_site('paginate' => 2) }
let(:posts) { site.posts }

should "report that pagination is enabled" do
assert Pager.pagination_enabled?(@site)
it "report that pagination is enabled" do
expect(described_class.pagination_enabled?(site)).to be_true
end

context "with 4 posts" do
setup do
@posts = @site.posts[1..4] # limit to 4
end

should "create first pager" do
pager = Pager.new(@site, 1, @posts)
assert_equal(2, pager.posts.size)
assert_equal(2, pager.total_pages)
assert_nil(pager.previous_page)
assert_equal(2, pager.next_page)
let(:posts) { site.posts[1..4] }

it "create first pager" do
pager = described_class.new(site, 1, posts)
expect(pager.posts.size).to eql(2)
expect(pager.total_pages).to eql(2)
expect(pager.previous_page).to be_nil
expect(pager.next_page).to eql(2)
end

should "create second pager" do
pager = Pager.new(@site, 2, @posts)
assert_equal(2, pager.posts.size)
assert_equal(2, pager.total_pages)
assert_equal(1, pager.previous_page)
assert_nil(pager.next_page)
it "create second pager" do
pager = described_class.new(site, 2, posts)
expect(pager.posts.size).to eql(2)
expect(pager.total_pages).to eql(2)
expect(pager.previous_page).to eql(1)
expect(pager.next_page).to be_nil
end

should "not create third pager" do
assert_raise(RuntimeError) { Pager.new(@site, 3, @posts) }
it "not create third pager" do
expect { described_class.new(site, 3, posts) }.to raise_error
end

end

context "with 5 posts" do
setup do
@posts = @site.posts[1..5] # limit to 5
end

should "create first pager" do
pager = Pager.new(@site, 1, @posts)
assert_equal(2, pager.posts.size)
assert_equal(3, pager.total_pages)
assert_nil(pager.previous_page)
assert_equal(2, pager.next_page)
let(:posts) { site.posts[1..5] }

it "create first pager" do
pager = described_class.new(site, 1, posts)
expect(pager.posts.size).to eql(2)
expect(pager.total_pages).to eql(3)
expect(pager.previous_page).to be_nil
expect(pager.next_page).to eql(2)
end

should "create second pager" do
pager = Pager.new(@site, 2, @posts)
assert_equal(2, pager.posts.size)
assert_equal(3, pager.total_pages)
assert_equal(1, pager.previous_page)
assert_equal(3, pager.next_page)
it "create second pager" do
pager = described_class.new(site, 2, posts)
expect(pager.posts.size).to eql(2)
expect(pager.total_pages).to eql(3)
expect(pager.previous_page).to eql(1)
expect(pager.next_page).to eql(3)
end

should "create third pager" do
pager = Pager.new(@site, 3, @posts)
assert_equal(1, pager.posts.size)
assert_equal(3, pager.total_pages)
assert_equal(2, pager.previous_page)
assert_nil(pager.next_page)
it "create third pager" do
pager = described_class.new(site, 3, posts)
expect(pager.posts.size).to eql(1)
expect(pager.total_pages).to eql(3)
expect(pager.previous_page).to eql(2)
expect(pager.next_page).to be_nil
end

should "not create fourth pager" do
assert_raise(RuntimeError) { Pager.new(@site, 4, @posts) }
it "not create fourth pager" do
expect { described_class.new(site, 4, posts) }.to raise_error(RuntimeError)
end

end
Expand Down
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
2 changes: 2 additions & 0 deletions spec/source/contacts/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
---
2 changes: 2 additions & 0 deletions spec/source/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
---
31 changes: 25 additions & 6 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,17 +1,36 @@
require File.expand_path("../lib/jekyll/paginate", File.dirname(__FILE__))
require 'jekyll'
require File.expand_path("../lib/jekyll-paginate", File.dirname(__FILE__))

RSpec.configure do |config|
config.treat_symbols_as_metadata_keys_with_true_values = true
config.run_all_when_everything_filtered = true
config.filter_run :focus
config.order = 'random'

def test_dir(*subdirs)
File.join(File.dirname(__FILE__), *subdirs)
end

def dest_dir(*subdirs)
test_dir('dest', *subdirs)
end

def source_dir(*subdirs)
test_dir('source', *subdirs)
end

def build_configs(overrides, base_hash = Jekyll::Configuration::DEFAULTS)
Jekyll::Utils.deep_merge_hashes(base_hash, overrides)
end

def site_configuration(overrides = {})
build_configs({
"source" => source_dir,
"destination" => dest_dir
}, build_configs(overrides))
end

def build_site(config = {})
base = build_configs({
'source' => source_dir,
'destination' => dest_dir,
'paginate' => 1
})
site = Jekyll::Site.new(site_configuration(
{"paginate" => 1}.merge(config)
))
Expand Down

0 comments on commit 884f3cd

Please sign in to comment.