Files
jekyll/test/test_related_posts.rb
Justin Weiss fa435fae94 Use Jekyll::Posts for both LSI indexing and lookup.
When looking for related posts, Jekyll was indexing `Jekyll::Post`
objects, but finding related posts based on `Jekyll::Post#content`. This
caused two problems:

1. Ruby 2.2 will warn on == if <=> throws an exception (and future Ruby
versions will surface that exception). Because `String`s can't be
compared with `Jekyll::Post`s, this warning was appearing all the time
while searching for related posts.

2. LSI won't return a post itself when searching for related posts. But
LSI could never tell that we were searching on a post, since Jekyll
passed post content, not a post object. With this fix, we can remove the
`- [post]` from `Jekyll::RelatedPosts#find_related`.

This is a more accurate fix for #3484.
2015-03-29 23:43:55 -07:00

53 lines
1.7 KiB
Ruby

require 'helper'
class TestRelatedPosts < JekyllUnitTest
context "building related posts without lsi" do
setup do
@site = fixture_site
end
should "use the most recent posts for related posts" do
@site.reset
@site.read
last_post = @site.posts.last
related_posts = Jekyll::RelatedPosts.new(last_post).build
last_10_recent_posts = (@site.posts.reverse - [last_post]).first(10)
assert_equal last_10_recent_posts, related_posts
end
end
context "building related posts with lsi" do
setup do
allow_any_instance_of(Jekyll::RelatedPosts).to receive(:display)
@site = fixture_site({"lsi" => true})
@site.reset
@site.read
require 'classifier-reborn'
Jekyll::RelatedPosts.lsi = nil
end
should "index Jekyll::Post objects" do
@site.posts = @site.posts.first(1)
expect_any_instance_of(::ClassifierReborn::LSI).to receive(:add_item).with(kind_of(Jekyll::Post))
Jekyll::RelatedPosts.new(@site.posts.last).build_index
end
should "find related Jekyll::Post objects, given a Jekyll::Post object" do
post = @site.posts.last
allow_any_instance_of(::ClassifierReborn::LSI).to receive(:build_index)
expect_any_instance_of(::ClassifierReborn::LSI).to receive(:find_related).with(post, 11).and_return(@site.posts[-1..-9])
Jekyll::RelatedPosts.new(post).build
end
should "use lsi for the related posts" do
allow_any_instance_of(::ClassifierReborn::LSI).to receive(:find_related).and_return(@site.posts[-1..-9])
allow_any_instance_of(::ClassifierReborn::LSI).to receive(:build_index)
assert_equal @site.posts[-1..-9], Jekyll::RelatedPosts.new(@site.posts.last).build
end
end
end