Sha256: b71a06be1b91e3ae26b95b53492a553b7223cc95ad5026144cff8fb3a8a4f055

Contents?: true

Size: 1.99 KB

Versions: 4

Compression:

Stored size: 1.99 KB

Contents

require File.join(File.dirname(__FILE__), 'spec_helper')

describe 'search with highlighting results', :type => :search do
  before :each do
    @posts = Array.new(2) { Post.new }
    stub_results_with_highlighting(
      @posts[0],
      { 'title_text' => ['one @@@hl@@@two@@@endhl@@@ three'] },
      @posts[1],
      { 'title_text' => ['three four @@@hl@@@five@@@endhl@@@'],
        'body_text' => ['@@@hl@@@five@@@ six seven', '@@@hl@@@eight@@@endhl@@@ nine @@@hl@@@ten@@@endhl@@@'] }
    )
    @search = session.search(Post)
  end

  it 'returns all highlights' do
    @search.hits.last.should have(3).highlights
  end

  it 'returns all highlights for a specified field' do
    @search.hits.last.should have(2).highlights(:body)
  end

  it 'returns an empty array if a given field does not have a highlight' do
    @search.hits.first.highlights(:body).should == []
  end

  it 'should format hits with <em> by default' do
    highlight = @search.hits.first.highlights(:title).first.formatted
    highlight.should == 'one <em>two</em> three'
  end

  it 'should format hits with provided block' do
    highlight = @search.hits.first.highlights(:title).first.format do |word|
      "<i>#{word}</i>"
    end
    highlight.should == 'one <i>two</i> three'
  end

  it 'should handle multiple highlighted words' do
    highlight = @search.hits.last.highlights(:body).last.format do |word|
      "<b>#{word}</b>"
    end
    highlight.should == '<b>eight</b> nine <b>ten</b>'
  end

  private

  def stub_results_with_highlighting(*instances_and_highlights)
    docs, highlights = [], []
    instances_and_highlights.each_slice(2) do |doc, highlight|
      docs << doc
      highlights << highlight
    end
    response = stub_full_results(*docs.map { |doc| { 'instance' => doc }})
    highlighting = response['highlighting'] = {}
    highlights.each_with_index do |highlight, i|
      if highlight
        instance = docs[i]
        highlighting["#{instance.class.name} #{instance.id}"] = highlight
      end
    end
    response
  end
end

Version data entries

4 entries across 4 versions & 2 rubygems

Version Path
sunspot-0.10.8 spec/api/search/highlighting_spec.rb
nxa-sunspot-0.10.7 spec/api/search/highlighting_spec.rb
sunspot-0.10.7 spec/api/search/highlighting_spec.rb
sunspot-0.10.6 spec/api/search/highlighting_spec.rb