fixes issue #78, including comprehensive scenarios and tests
This commit is contained in:
parent
2f2e45bedf
commit
5ea06f3ad9
|
@ -6,35 +6,22 @@ Feature: Site pagination
|
|||
Scenario Outline: Paginate with N posts per page
|
||||
Given I have a configuration file with "paginate" set to "<num>"
|
||||
And I have a _layouts directory
|
||||
And I have an "index.html" file that contains "Basic Site"
|
||||
And I have an "index.html" file that contains "{{ paginator.posts.size }}"
|
||||
And I have a _posts directory
|
||||
And I have the following post:
|
||||
| title | date | layout | content |
|
||||
| Wargames | 3/27/2009 | default | The only winning move is not to play. |
|
||||
| Wargames2 | 4/27/2009 | default | The only winning move is not to play2. |
|
||||
| Wargames3 | 5/27/2009 | default | The only winning move is not to play2. |
|
||||
| Wargames3 | 5/27/2009 | default | The only winning move is not to play3. |
|
||||
| Wargames4 | 6/27/2009 | default | The only winning move is not to play4. |
|
||||
When I run jekyll
|
||||
Then the _site/page2 directory should exist
|
||||
And the _site/page2/index.html file should exist
|
||||
Then the _site/page<exist> directory should exist
|
||||
And the "_site/page<exist>/index.html" file should exist
|
||||
And I should see "<posts>" in "_site/page<exist>/index.html"
|
||||
And the "_site/page<not_exist>/index.html" file should not exist
|
||||
|
||||
Examples:
|
||||
| num |
|
||||
| 1 |
|
||||
| 2 |
|
||||
|
||||
Scenario: Correct liquid paginator replacements
|
||||
Given I have a configuration file with "paginate" set to "1"
|
||||
And I have a _layouts directory
|
||||
And I have an "index.html" file that contains "{{ paginator.page }}"
|
||||
And I have a _posts directory
|
||||
And I have the following post:
|
||||
| title | date | layout | content |
|
||||
| Wargames | 3/27/2009 | default | The only winning move is not to play. |
|
||||
| Wargames2 | 4/27/2009 | default | The only winning move is not to play2. |
|
||||
When I run jekyll
|
||||
Then the _site/index.html file should exist
|
||||
And I should see "1" in "_site/index.html"
|
||||
Then the _site/page2 directory should exist
|
||||
And the _site/page2/index.html file should exist
|
||||
And I should see "2" in "_site/page2/index.html"
|
||||
|
||||
| num | exist | posts | not_exist |
|
||||
| 1 | 4 | 1 | 5 |
|
||||
| 2 | 2 | 2 | 3 |
|
||||
| 3 | 2 | 1 | 3 |
|
||||
|
|
|
@ -115,14 +115,14 @@ Then /^the (.*) directory should exist$/ do |dir|
|
|||
assert File.directory?(dir)
|
||||
end
|
||||
|
||||
Then /^the (.*) file should exist$/ do |file|
|
||||
assert File.file?(file)
|
||||
end
|
||||
|
||||
Then /^I should see "(.*)" in "(.*)"$/ do |text, file|
|
||||
assert_match Regexp.new(text), File.open(file).readlines.join
|
||||
end
|
||||
|
||||
Then /^the "(.*)" file should exist$/ do |file|
|
||||
assert File.file?(file)
|
||||
end
|
||||
|
||||
Then /^the "(.*)" file should not exist$/ do |file|
|
||||
assert !File.exists?(file)
|
||||
end
|
||||
|
|
|
@ -4,7 +4,7 @@ module Jekyll
|
|||
|
||||
def self.calculate_pages(all_posts, per_page)
|
||||
num_pages = all_posts.size / per_page.to_i
|
||||
num_pages.abs + 1 if all_posts.size % per_page.to_i != 0
|
||||
num_pages = num_pages + 1 if all_posts.size % per_page.to_i != 0
|
||||
num_pages
|
||||
end
|
||||
|
||||
|
|
|
@ -252,7 +252,6 @@ module Jekyll
|
|||
def paginate_posts(file, dir)
|
||||
all_posts = self.posts.sort { |a,b| b <=> a }
|
||||
pages = Pager.calculate_pages(all_posts, self.config['paginate'].to_i)
|
||||
pages += 1
|
||||
(1..pages).each do |num_page|
|
||||
pager = Pager.new(self.config, num_page, all_posts, pages)
|
||||
page = Page.new(self, self.source, dir, file)
|
||||
|
|
|
@ -13,6 +13,10 @@ class TestGeneratedSite < Test::Unit::TestCase
|
|||
@index = File.read(dest_dir('index.html'))
|
||||
end
|
||||
|
||||
should "ensure post count is as expected" do
|
||||
assert_equal 17, @site.posts.size
|
||||
end
|
||||
|
||||
should "insert site.posts into the index" do
|
||||
assert @index.include?("#{@site.posts.size} Posts")
|
||||
end
|
||||
|
|
|
@ -1,7 +1,34 @@
|
|||
require File.dirname(__FILE__) + '/helper'
|
||||
|
||||
class TestPager < Test::Unit::TestCase
|
||||
context "pagination enabled" 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'))
|
||||
end
|
||||
|
||||
context "pagination disabled" do
|
||||
setup do
|
||||
stub(Jekyll).configuration do
|
||||
Jekyll::DEFAULTS.merge({
|
||||
'source' => source_dir,
|
||||
'destination' => dest_dir
|
||||
})
|
||||
end
|
||||
@config = Jekyll.configuration
|
||||
end
|
||||
|
||||
should "report that pagination is disabled" do
|
||||
assert !Pager.pagination_enabled?(@config, 'index.html')
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
context "pagination enabled for 2" do
|
||||
setup do
|
||||
stub(Jekyll).configuration do
|
||||
Jekyll::DEFAULTS.merge({
|
||||
|
@ -13,16 +40,22 @@ class TestPager < Test::Unit::TestCase
|
|||
|
||||
@config = Jekyll.configuration
|
||||
@site = Site.new(@config)
|
||||
@posts = @site.read_posts('')
|
||||
@site.process
|
||||
@posts = @site.posts
|
||||
end
|
||||
|
||||
should "calculate number of pages" do
|
||||
assert_equal(2, Pager.calculate_pages(@posts, @config['paginate']))
|
||||
should "report that pagination is enabled" do
|
||||
assert Pager.pagination_enabled?(@config, 'index.html')
|
||||
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(@config, 1, @posts)
|
||||
assert_equal(@config['paginate'].to_i, pager.posts.size)
|
||||
assert_equal(2, pager.posts.size)
|
||||
assert_equal(2, pager.total_pages)
|
||||
assert_nil(pager.previous_page)
|
||||
assert_equal(2, pager.next_page)
|
||||
|
@ -30,7 +63,7 @@ class TestPager < Test::Unit::TestCase
|
|||
|
||||
should "create second pager" do
|
||||
pager = Pager.new(@config, 2, @posts)
|
||||
assert_equal(@posts.size - @config['paginate'].to_i, pager.posts.size)
|
||||
assert_equal(2, pager.posts.size)
|
||||
assert_equal(2, pager.total_pages)
|
||||
assert_equal(1, pager.previous_page)
|
||||
assert_nil(pager.next_page)
|
||||
|
@ -40,8 +73,41 @@ class TestPager < Test::Unit::TestCase
|
|||
assert_raise(RuntimeError) { Pager.new(@config, 3, @posts) }
|
||||
end
|
||||
|
||||
should "report that pagination is enabled" do
|
||||
assert Pager.pagination_enabled?(@config, 'index.html')
|
||||
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(@config, 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)
|
||||
end
|
||||
|
||||
should "create second pager" do
|
||||
pager = Pager.new(@config, 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)
|
||||
end
|
||||
|
||||
should "create third pager" do
|
||||
pager = Pager.new(@config, 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)
|
||||
end
|
||||
|
||||
should "not create fourth pager" do
|
||||
assert_raise(RuntimeError) { Pager.new(@config, 4, @posts) }
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in New Issue