aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGareth Rees <gareth@mysociety.org>2014-06-10 09:35:32 +0100
committerGareth Rees <gareth@mysociety.org>2014-06-25 10:40:37 +0100
commite490c4a7ec7157e794d849c962371e298d8342d9 (patch)
tree0cf3509b2b9ed6c69ce41b046633ab5c49a9fb81
parent4d9c89d0e416825eb52a720d74230f547454ba31 (diff)
Add spec for highlight_and_excerpt
-rw-r--r--app/helpers/highlight_helper.rb1
-rw-r--r--spec/helpers/highlight_helper_spec.rb50
2 files changed, 50 insertions, 1 deletions
diff --git a/app/helpers/highlight_helper.rb b/app/helpers/highlight_helper.rb
index 06ade48ee..a98f6f320 100644
--- a/app/helpers/highlight_helper.rb
+++ b/app/helpers/highlight_helper.rb
@@ -1,4 +1,5 @@
module HighlightHelper
+ include ERB::Util
# Implementation of rails' highlight that allows regex to be passed to
# the phrases parameter.
diff --git a/spec/helpers/highlight_helper_spec.rb b/spec/helpers/highlight_helper_spec.rb
index 675100f00..e1be7e153 100644
--- a/spec/helpers/highlight_helper_spec.rb
+++ b/spec/helpers/highlight_helper_spec.rb
@@ -1,9 +1,57 @@
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
describe HighlightHelper do
-
include HighlightHelper
+ describe :highlight_and_excerpt do
+
+ it 'excerpts text and highlights phrases' do
+ text = "Quentin Nobble-Boston, Permanent Under-Secretary, Department for Humpadinking"
+ phrases = ['humpadinking']
+ expected = '...Department for <span class="highlight">Humpadinking</span>'
+ highlight_and_excerpt(text, phrases, 15).should == expected
+ end
+
+ it 'excerpts text and highlights matches' do
+ text = "Quentin Nobble-Boston, Permanent Under-Secretary, Department for Humpadinking"
+ matches = [/\bhumpadink\w*\b/iu]
+ expected = '...Department for <span class="highlight">Humpadinking</span>'
+ highlight_and_excerpt(text, matches, 15).should == expected
+ end
+
+ context 'multiple matches' do
+
+ it 'highlights multiple matches' do
+ text = <<-EOF
+Quentin Nobble-Boston, Permanent Under-Secretary, Department for Humpadinking
+decided to visit Humpadink so that he could be with the Humpadinks
+EOF
+
+ expected = <<-EOF
+Quentin Nobble-Boston, Permanent Under-Secretary, Department for <span class="highlight">Humpadinking</span>
+decided to visit <span class="highlight">Humpadink</span> so that he could be with the <span class="highlight">Humpadinks</span>
+EOF
+ text.chomp!
+ expected.chomp!
+ matches = [/\b(humpadink\w*)\b/iu]
+ highlight_and_excerpt(text, matches, 1000).should == expected
+ end
+
+ it 'bases the split on the first match' do
+ text = "Quentin Nobble-Boston, Permanent Under-Secretary," \
+ "Department for Humpadinking decided to visit Humpadink" \
+ "so that he could be with the Humpadinks"
+
+ expected = "...Department for <span class=\"highlight\">" \
+ "Humpadinking</span> decided to vis..."
+
+ matches = [/\b(humpadink\w*)\b/iu]
+ highlight_and_excerpt(text, matches, 15).should == expected
+ end
+
+ end
+
+ end
describe :highlight_matches do