From fee5d9384862af086613c4f09e0fd96f7bd347b8 Mon Sep 17 00:00:00 2001 From: Gareth Rees Date: Tue, 3 Jun 2014 11:19:21 +0100 Subject: Add cache-busting to request response notification Users get a new response email, click the link, but get a cached page. This is a quick fix to ensure they always see the response when clicking the link. --- spec/helpers/link_to_helper_spec.rb | 68 +++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) (limited to 'spec/helpers') diff --git a/spec/helpers/link_to_helper_spec.rb b/spec/helpers/link_to_helper_spec.rb index 4a01ec683..08362da6e 100644 --- a/spec/helpers/link_to_helper_spec.rb +++ b/spec/helpers/link_to_helper_spec.rb @@ -20,6 +20,74 @@ describe LinkToHelper do end + describe 'when linking to new incoming messages' do + + before do + @info_request = mock_model(InfoRequest, :id => 123, :url_title => 'test_title') + @incoming_message = mock_model(IncomingMessage, :id => 32, :info_request => @info_request) + end + + context 'for external links' do + + it 'generates the url to the info request of the message' do + incoming_message_url(@incoming_message).should include('http://test.host/request/test_title') + end + + it 'includes an anchor to the new message' do + incoming_message_url(@incoming_message).should include('#incoming-32') + end + + it 'includes a cache busting parameter' do + incoming_message_url(@incoming_message).should include('nocache=incoming-32') + end + + end + + context 'for internal links' do + + it 'generates the incoming_message_url with the path only' do + expected = '/request/test_title?nocache=incoming-32#incoming-32' + incoming_message_path(@incoming_message).should == expected + end + + end + + end + + describe 'when linking to new outgoing messages' do + + before do + @info_request = mock_model(InfoRequest, :id => 123, :url_title => 'test_title') + @outgoing_message = mock_model(OutgoingMessage, :id => 32, :info_request => @info_request) + end + + context 'for external links' do + + it 'generates the url to the info request of the message' do + outgoing_message_url(@outgoing_message).should include('http://test.host/request/test_title') + end + + it 'includes an anchor to the new message' do + outgoing_message_url(@outgoing_message).should include('#outgoing-32') + end + + it 'includes a cache busting parameter' do + outgoing_message_url(@outgoing_message).should include('nocache=outgoing-32') + end + + end + + context 'for internal links' do + + it 'generates the outgoing_message_url with the path only' do + expected = '/request/test_title?nocache=outgoing-32#outgoing-32' + outgoing_message_path(@outgoing_message).should == expected + end + + end + + end + describe 'when displaying a user link for a request' do context "for external requests" do -- cgit v1.2.3 From 6bd0bfe7599aee4e55bdd63196d1e2c5cc80129c Mon Sep 17 00:00:00 2001 From: Gareth Rees Date: Tue, 3 Jun 2014 12:34:00 +0100 Subject: Remove duplication from new correspondence urls --- spec/helpers/link_to_helper_spec.rb | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) (limited to 'spec/helpers') diff --git a/spec/helpers/link_to_helper_spec.rb b/spec/helpers/link_to_helper_spec.rb index 08362da6e..f7be9eab0 100644 --- a/spec/helpers/link_to_helper_spec.rb +++ b/spec/helpers/link_to_helper_spec.rb @@ -37,8 +37,12 @@ describe LinkToHelper do incoming_message_url(@incoming_message).should include('#incoming-32') end - it 'includes a cache busting parameter' do - incoming_message_url(@incoming_message).should include('nocache=incoming-32') + it 'includes does not cache by default' do + incoming_message_url(@incoming_message).should_not include('nocache=incoming-32') + end + + it 'includes a cache busting parameter if set' do + incoming_message_url(@incoming_message, :cachebust => true).should include('nocache=incoming-32') end end @@ -46,7 +50,7 @@ describe LinkToHelper do context 'for internal links' do it 'generates the incoming_message_url with the path only' do - expected = '/request/test_title?nocache=incoming-32#incoming-32' + expected = '/request/test_title#incoming-32' incoming_message_path(@incoming_message).should == expected end @@ -71,8 +75,12 @@ describe LinkToHelper do outgoing_message_url(@outgoing_message).should include('#outgoing-32') end - it 'includes a cache busting parameter' do - outgoing_message_url(@outgoing_message).should include('nocache=outgoing-32') + it 'includes does not cache by default' do + outgoing_message_url(@outgoing_message).should_not include('nocache=outgoing-32') + end + + it 'includes a cache busting parameter if set' do + outgoing_message_url(@outgoing_message, :cachebust => true).should include('nocache=outgoing-32') end end @@ -80,7 +88,7 @@ describe LinkToHelper do context 'for internal links' do it 'generates the outgoing_message_url with the path only' do - expected = '/request/test_title?nocache=outgoing-32#outgoing-32' + expected = '/request/test_title#outgoing-32' outgoing_message_path(@outgoing_message).should == expected end -- cgit v1.2.3 From 77ce9b795c62d8241b22878cf60ca688fa4b44a1 Mon Sep 17 00:00:00 2001 From: Gareth Rees Date: Tue, 3 Jun 2014 11:35:55 +0100 Subject: Move date helpers to DateTimeHelper --- spec/helpers/date_time_helper_spec.rb | 54 +++++++++++++++++++++++++++++++++++ spec/helpers/link_to_helper_spec.rb | 47 ------------------------------ 2 files changed, 54 insertions(+), 47 deletions(-) create mode 100644 spec/helpers/date_time_helper_spec.rb (limited to 'spec/helpers') diff --git a/spec/helpers/date_time_helper_spec.rb b/spec/helpers/date_time_helper_spec.rb new file mode 100644 index 000000000..aa047052c --- /dev/null +++ b/spec/helpers/date_time_helper_spec.rb @@ -0,0 +1,54 @@ +require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') + +describe DateTimeHelper do + + include DateTimeHelper + + describe 'simple_date' do + + it 'formats a date in html by default' do + time = Time.utc(2012, 11, 07, 21, 30, 26) + self.should_receive(:simple_date_html).with(time) + simple_date(time) + end + + it 'formats a date in the specified format' do + time = Time.utc(2012, 11, 07, 21, 30, 26) + self.should_receive(:simple_date_text).with(time) + simple_date(time, :format => :text) + end + + it 'raises an argument error if given an unrecognized format' do + time = Time.utc(2012, 11, 07, 21, 30, 26) + expect { simple_date(time, :format => :unknown) }.to raise_error(ArgumentError) + end + + end + + describe 'simple_date_html' do + + it 'formats a date in a time tag' do + Time.use_zone('London') do + time = Time.utc(2012, 11, 07, 21, 30, 26) + expected = "" + simple_date_html(time).should == expected + end + end + + end + + describe 'simple_date_text' do + + it 'should respect time zones' do + Time.use_zone('Australia/Sydney') do + simple_date_text(Time.utc(2012, 11, 07, 21, 30, 26)).should == 'November 08, 2012' + end + end + + it 'should handle Date objects' do + simple_date_text(Date.new(2012, 11, 21)).should == 'November 21, 2012' + end + + end + +end diff --git a/spec/helpers/link_to_helper_spec.rb b/spec/helpers/link_to_helper_spec.rb index f7be9eab0..b11c35056 100644 --- a/spec/helpers/link_to_helper_spec.rb +++ b/spec/helpers/link_to_helper_spec.rb @@ -145,51 +145,4 @@ describe LinkToHelper do end - describe 'simple_date' do - - it 'formats a date in html by default' do - time = Time.utc(2012, 11, 07, 21, 30, 26) - self.should_receive(:simple_date_html).with(time) - simple_date(time) - end - - it 'formats a date in the specified format' do - time = Time.utc(2012, 11, 07, 21, 30, 26) - self.should_receive(:simple_date_text).with(time) - simple_date(time, :format => :text) - end - - it 'raises an argument error if given an unrecognized format' do - time = Time.utc(2012, 11, 07, 21, 30, 26) - expect { simple_date(time, :format => :unknown) }.to raise_error(ArgumentError) - end - - end - - describe 'simple_date_html' do - - it 'formats a date in a time tag' do - Time.use_zone('London') do - time = Time.utc(2012, 11, 07, 21, 30, 26) - expected = "" - simple_date_html(time).should == expected - end - end - - end - - describe 'simple_date_text' do - - it 'should respect time zones' do - Time.use_zone('Australia/Sydney') do - simple_date_text(Time.utc(2012, 11, 07, 21, 30, 26)).should == 'November 08, 2012' - end - end - - it 'should handle Date objects' do - simple_date_text(Date.new(2012, 11, 21)).should == 'November 21, 2012' - end - - end - end -- cgit v1.2.3 From dbeecf59d62bf878fc6f4fe8a976f66f19cde415 Mon Sep 17 00:00:00 2001 From: Gareth Rees Date: Tue, 3 Jun 2014 11:45:18 +0100 Subject: Add specs for DateTimeHelper#year_from_date --- spec/helpers/date_time_helper_spec.rb | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) (limited to 'spec/helpers') diff --git a/spec/helpers/date_time_helper_spec.rb b/spec/helpers/date_time_helper_spec.rb index aa047052c..67d2efdd3 100644 --- a/spec/helpers/date_time_helper_spec.rb +++ b/spec/helpers/date_time_helper_spec.rb @@ -51,4 +51,20 @@ describe DateTimeHelper do end + describe :year_from_date do + + it 'returns the year component of a date' do + year_from_date(Date.new(2012, 11, 21)).should == '2012' + end + + it 'returns the year component of a datetime' do + year_from_date(DateTime.new(2012, 11, 21)).should == '2012' + end + + it 'returns the year component of a time' do + year_from_date(Time.now).should == Date.today.year.to_s + end + + end + end -- cgit v1.2.3 From ce262c7c4291533468f57fb209131828d7e08462 Mon Sep 17 00:00:00 2001 From: Gareth Rees Date: Tue, 3 Jun 2014 11:56:56 +0100 Subject: Add specs for DateTimeHelper#simple_time --- spec/helpers/date_time_helper_spec.rb | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) (limited to 'spec/helpers') diff --git a/spec/helpers/date_time_helper_spec.rb b/spec/helpers/date_time_helper_spec.rb index 67d2efdd3..6b55a11d1 100644 --- a/spec/helpers/date_time_helper_spec.rb +++ b/spec/helpers/date_time_helper_spec.rb @@ -51,6 +51,24 @@ describe DateTimeHelper do end + describe :simple_time do + + it 'returns 00:00:00 for a date' do + simple_time(Date.new(2012, 11, 21)).should == '00:00:00' + end + + it 'returns the time component of a datetime' do + date = DateTime.new(2012, 11, 21, 10, 34, 56) + simple_time(date).should == '10:34:56' + end + + it 'returns the time component of a time' do + time = Time.utc(2000, 'jan', 1, 20, 15, 1) + simple_time(time).should == '20:15:01' + end + + end + describe :year_from_date do it 'returns the year component of a date' do -- cgit v1.2.3 From d5b1587b378c73354b1677fb17e96f39c76f2ec3 Mon Sep 17 00:00:00 2001 From: Gareth Rees Date: Tue, 3 Jun 2014 11:59:54 +0100 Subject: Minor tidying of DateTimeHelper and specs --- spec/helpers/date_time_helper_spec.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'spec/helpers') diff --git a/spec/helpers/date_time_helper_spec.rb b/spec/helpers/date_time_helper_spec.rb index 6b55a11d1..870deb2d0 100644 --- a/spec/helpers/date_time_helper_spec.rb +++ b/spec/helpers/date_time_helper_spec.rb @@ -4,7 +4,7 @@ describe DateTimeHelper do include DateTimeHelper - describe 'simple_date' do + describe :simple_date do it 'formats a date in html by default' do time = Time.utc(2012, 11, 07, 21, 30, 26) @@ -25,19 +25,19 @@ describe DateTimeHelper do end - describe 'simple_date_html' do + describe :simple_date_html do it 'formats a date in a time tag' do Time.use_zone('London') do time = Time.utc(2012, 11, 07, 21, 30, 26) - expected = "" + expected = %Q() simple_date_html(time).should == expected end end end - describe 'simple_date_text' do + describe :simple_date_text do it 'should respect time zones' do Time.use_zone('Australia/Sydney') do -- cgit v1.2.3 From 07556cd8d6e48f5ca54eb2e8b09fba179f7579a3 Mon Sep 17 00:00:00 2001 From: Gareth Rees Date: Wed, 4 Jun 2014 15:45:52 +0100 Subject: Remove DateTimeHelper#year_from_date DateTime, Date and Time all have #year methods --- spec/helpers/date_time_helper_spec.rb | 17 ----------------- 1 file changed, 17 deletions(-) (limited to 'spec/helpers') diff --git a/spec/helpers/date_time_helper_spec.rb b/spec/helpers/date_time_helper_spec.rb index 870deb2d0..c4fdee1d1 100644 --- a/spec/helpers/date_time_helper_spec.rb +++ b/spec/helpers/date_time_helper_spec.rb @@ -68,21 +68,4 @@ describe DateTimeHelper do end end - - describe :year_from_date do - - it 'returns the year component of a date' do - year_from_date(Date.new(2012, 11, 21)).should == '2012' - end - - it 'returns the year component of a datetime' do - year_from_date(DateTime.new(2012, 11, 21)).should == '2012' - end - - it 'returns the year component of a time' do - year_from_date(Time.now).should == Date.today.year.to_s - end - - end - end -- cgit v1.2.3 From 7bf6cbe307d3fd99680c084025240ed504e37297 Mon Sep 17 00:00:00 2001 From: Gareth Rees Date: Mon, 16 Jun 2014 10:36:36 +0100 Subject: Fix typo Fixes typo in 6bd0bfe --- spec/helpers/link_to_helper_spec.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'spec/helpers') diff --git a/spec/helpers/link_to_helper_spec.rb b/spec/helpers/link_to_helper_spec.rb index b11c35056..261e1ef3e 100644 --- a/spec/helpers/link_to_helper_spec.rb +++ b/spec/helpers/link_to_helper_spec.rb @@ -37,7 +37,7 @@ describe LinkToHelper do incoming_message_url(@incoming_message).should include('#incoming-32') end - it 'includes does not cache by default' do + it 'does not cache by default' do incoming_message_url(@incoming_message).should_not include('nocache=incoming-32') end @@ -75,7 +75,7 @@ describe LinkToHelper do outgoing_message_url(@outgoing_message).should include('#outgoing-32') end - it 'includes does not cache by default' do + it 'does not cache by default' do outgoing_message_url(@outgoing_message).should_not include('nocache=outgoing-32') end -- cgit v1.2.3 From e5a73815f580d296572e11b71b5f3ed320bbe912 Mon Sep 17 00:00:00 2001 From: Gareth Rees Date: Fri, 30 May 2014 12:42:48 +0100 Subject: Add helper to highlight and excerpt by regex Backport of https://github.com/rails/rails/pull/11793/ Contains integration tests to check that it works as expected with ActsAsXapian. --- spec/helpers/highlight_helper_spec.rb | 189 ++++++++++++++++++++++++++++++++++ 1 file changed, 189 insertions(+) create mode 100644 spec/helpers/highlight_helper_spec.rb (limited to 'spec/helpers') diff --git a/spec/helpers/highlight_helper_spec.rb b/spec/helpers/highlight_helper_spec.rb new file mode 100644 index 000000000..bd0c62226 --- /dev/null +++ b/spec/helpers/highlight_helper_spec.rb @@ -0,0 +1,189 @@ +require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') + +describe HighlightHelper do + + include HighlightHelper + + + describe :highlight_matches do + + it 'highlights' do + assert_equal( + "This is a beautiful morning", + highlight_matches("This is a beautiful morning", "beautiful") + ) + + assert_equal( + "This is a beautiful morning, but also a beautiful day", + highlight_matches("This is a beautiful morning, but also a beautiful day", "beautiful") + ) + + assert_equal( + "This is a beautiful morning, but also a beautiful day", + highlight_matches("This is a beautiful morning, but also a beautiful day", "beautiful", :highlighter => '\1') + ) + + assert_equal( + "This text is not changed because we supplied an empty phrase", + highlight_matches("This text is not changed because we supplied an empty phrase", nil) + ) + + assert_equal ' ', highlight_matches(' ', 'blank text is returned verbatim') + end + + it 'sanitizes input' do + assert_equal( + "This is a beautiful morning", + highlight_matches("This is a beautiful morning", "beautiful") + ) + end + + it 'doesnt sanitize when the sanitize option is false' do + assert_equal( + "This is a beautiful morning", + highlight_matches("This is a beautiful morning", "beautiful", :sanitize => false) + ) + end + + it 'highlights using regexp' do + assert_equal( + "This is a beautiful! morning", + highlight_matches("This is a beautiful! morning", "beautiful!") + ) + + assert_equal( + "This is a beautiful! morning", + highlight_matches("This is a beautiful! morning", "beautiful! morning") + ) + + assert_equal( + "This is a beautiful? morning", + highlight_matches("This is a beautiful? morning", "beautiful? morning") + ) + end + + it 'accepts regex' do + assert_equal("This day was challenging for judge Allen and his colleagues.", + highlight_matches("This day was challenging for judge Allen and his colleagues.", /\ballen\b/i)) + end + + it 'highlights multiple phrases in one pass' do + assert_equal %(wow em), highlight_matches('wow em', %w(wow em), :highlighter => '\1') + end + + it 'highlights with html' do + assert_equal( + "

This is a beautiful morning, but also a beautiful day

", + highlight_matches("

This is a beautiful morning, but also a beautiful day

", "beautiful") + ) + assert_equal( + "

This is a beautiful morning, but also a beautiful day

", + highlight_matches("

This is a beautiful morning, but also a beautiful day

", "beautiful") + ) + assert_equal( + "

This is a beautiful morning, but also a beautiful day

", + highlight_matches("

This is a beautiful morning, but also a beautiful day

", "beautiful") + ) + assert_equal( + "

This is a beautiful morning, but also a beautiful day

", + highlight_matches("

This is a beautiful morning, but also a beautiful day

", "beautiful") + ) + assert_equal( + "

This is a beautiful morning, but also a beautiful day

", + highlight_matches("

This is a beautiful morning, but also a beautiful day

", "beautiful") + ) + assert_equal( + "
abc div
", + highlight_matches("
abc div
", "div", :highlighter => '\1') + ) + end + + it 'doesnt modify the options hash' do + options = { :highlighter => '\1', :sanitize => false } + passed_options = options.dup + highlight_matches("
abc div
", "div", passed_options) + assert_equal options, passed_options + end + + end + + describe :excerpt do + + it 'excerpts' do + assert_equal("...is a beautiful morn...", excerpt("This is a beautiful morning", "beautiful", :radius => 5)) + assert_equal("This is a...", excerpt("This is a beautiful morning", "this", :radius => 5)) + assert_equal("...iful morning", excerpt("This is a beautiful morning", "morning", :radius => 5)) + assert_equal("...udge Allen and...", excerpt("This day was challenging for judge Allen and his colleagues.", /\ballen\b/i, :radius => 5)) + assert_equal("...judge Allen and...", excerpt("This day was challenging for judge Allen and his colleagues.", /\ballen\b/i, :radius => 1, :separator => ' ')) + assert_nil excerpt("This is a beautiful morning", "day") + end + + it 'is not html safe' do + assert !excerpt('This is a beautiful! morning', 'beautiful', :radius => 5).html_safe? + end + + it 'excerpts borderline cases' do + assert_equal("", excerpt("", "", :radius => 0)) + assert_equal("a", excerpt("a", "a", :radius => 0)) + assert_equal("...b...", excerpt("abc", "b", :radius => 0)) + assert_equal("abc", excerpt("abc", "b", :radius => 1)) + assert_equal("abc...", excerpt("abcd", "b", :radius => 1)) + assert_equal("...abc", excerpt("zabc", "b", :radius => 1)) + assert_equal("...abc...", excerpt("zabcd", "b", :radius => 1)) + assert_equal("zabcd", excerpt("zabcd", "b", :radius => 2)) + + # excerpt strips the resulting string before ap-/prepending excerpt_string. + # whether this behavior is meaningful when excerpt_string is not to be + # appended is questionable. + assert_equal("zabcd", excerpt(" zabcd ", "b", :radius => 4)) + assert_equal("...abc...", excerpt("z abc d", "b", :radius => 1)) + end + + it 'excerpts with regex' do + assert_equal('...is a beautiful! mor...', excerpt('This is a beautiful! morning', 'beautiful', :radius => 5)) + assert_equal('...is a beautiful? mor...', excerpt('This is a beautiful? morning', 'beautiful', :radius => 5)) + end + + it 'excerpts with omission' do + assert_equal("[...]is a beautiful morn[...]", excerpt("This is a beautiful morning", "beautiful", :omission => "[...]",:radius => 5)) + assert_equal( + "This is the ultimate supercalifragilisticexpialidoceous very looooooooooooooooooong looooooooooooong beautiful morning with amazing sunshine and awesome tempera[...]", + excerpt("This is the ultimate supercalifragilisticexpialidoceous very looooooooooooooooooong looooooooooooong beautiful morning with amazing sunshine and awesome temperatures. So what are you gonna do about it?", "very", + :omission => "[...]") + ) + end + + it 'excerpts with utf8' do + if RUBY_VERSION.to_f >= 1.9 + assert_equal("...\357\254\203ciency could not be...".force_encoding(Encoding::UTF_8), excerpt("That's why e\357\254\203ciency could not be helped".force_encoding(Encoding::UTF_8), 'could', :radius => 8)) + else + assert_equal("...\357\254\203ciency could not be...", excerpt("That's why e\357\254\203ciency could not be helped", 'could', :radius => 8)) + end + end + + it 'doesnt modify the options hash' do + options = { :omission => "[...]",:radius => 5 } + passed_options = options.dup + excerpt("This is a beautiful morning", "beautiful", passed_options) + assert_equal options, passed_options + end + + it 'excerpts with separator' do + options = { :separator => ' ', :radius => 1 } + assert_equal('...a very beautiful...', excerpt('This is a very beautiful morning', 'very', options)) + assert_equal('This is...', excerpt('This is a very beautiful morning', 'this', options)) + assert_equal('...beautiful morning', excerpt('This is a very beautiful morning', 'morning', options)) + + options = { :separator => "\n", :radius => 0 } + assert_equal("...very long...", excerpt("my very\nvery\nvery long\nstring", 'long', options)) + + options = { :separator => "\n", :radius => 1 } + assert_equal("...very\nvery long\nstring", excerpt("my very\nvery\nvery long\nstring", 'long', options)) + + assert_equal excerpt('This is a beautiful morning', 'a'), + excerpt('This is a beautiful morning', 'a', :separator => nil) + end + + end + +end -- cgit v1.2.3 From 4d9c89d0e416825eb52a720d74230f547454ba31 Mon Sep 17 00:00:00 2001 From: Gareth Rees Date: Fri, 30 May 2014 15:48:10 +0100 Subject: Use regex based highlighting --- spec/helpers/highlight_helper_spec.rb | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) (limited to 'spec/helpers') diff --git a/spec/helpers/highlight_helper_spec.rb b/spec/helpers/highlight_helper_spec.rb index bd0c62226..675100f00 100644 --- a/spec/helpers/highlight_helper_spec.rb +++ b/spec/helpers/highlight_helper_spec.rb @@ -105,6 +105,13 @@ describe HighlightHelper do assert_equal options, passed_options end + it 'highlights with a block' do + assert_equal( + "one two three", + highlight_matches("one two three", ["one", "two", "three"]) { |word| "#{word}" } + ) + end + end describe :excerpt do @@ -113,8 +120,6 @@ describe HighlightHelper do assert_equal("...is a beautiful morn...", excerpt("This is a beautiful morning", "beautiful", :radius => 5)) assert_equal("This is a...", excerpt("This is a beautiful morning", "this", :radius => 5)) assert_equal("...iful morning", excerpt("This is a beautiful morning", "morning", :radius => 5)) - assert_equal("...udge Allen and...", excerpt("This day was challenging for judge Allen and his colleagues.", /\ballen\b/i, :radius => 5)) - assert_equal("...judge Allen and...", excerpt("This day was challenging for judge Allen and his colleagues.", /\ballen\b/i, :radius => 1, :separator => ' ')) assert_nil excerpt("This is a beautiful morning", "day") end @@ -142,6 +147,11 @@ describe HighlightHelper do it 'excerpts with regex' do assert_equal('...is a beautiful! mor...', excerpt('This is a beautiful! morning', 'beautiful', :radius => 5)) assert_equal('...is a beautiful? mor...', excerpt('This is a beautiful? morning', 'beautiful', :radius => 5)) + assert_equal('...is a beautiful? mor...', excerpt('This is a beautiful? morning', /\bbeau\w*\b/i, :radius => 5)) + assert_equal('...is a beautiful? mor...', excerpt('This is a beautiful? morning', /\b(beau\w*)\b/i, :radius => 5)) + assert_equal("...udge Allen and...", excerpt("This day was challenging for judge Allen and his colleagues.", /\ballen\b/i, :radius => 5)) + assert_equal("...judge Allen and...", excerpt("This day was challenging for judge Allen and his colleagues.", /\ballen\b/i, :radius => 1, :separator => ' ')) + assert_equal("...was challenging for...", excerpt("This day was challenging for judge Allen and his colleagues.", /\b(\w*allen\w*)\b/i, :radius => 5)) end it 'excerpts with omission' do -- cgit v1.2.3 From e490c4a7ec7157e794d849c962371e298d8342d9 Mon Sep 17 00:00:00 2001 From: Gareth Rees Date: Tue, 10 Jun 2014 09:35:32 +0100 Subject: Add spec for highlight_and_excerpt --- spec/helpers/highlight_helper_spec.rb | 50 ++++++++++++++++++++++++++++++++++- 1 file changed, 49 insertions(+), 1 deletion(-) (limited to 'spec/helpers') 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 Humpadinking' + 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 Humpadinking' + 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 Humpadinking +decided to visit Humpadink so that he could be with the Humpadinks +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 " \ + "Humpadinking decided to vis..." + + matches = [/\b(humpadink\w*)\b/iu] + highlight_and_excerpt(text, matches, 15).should == expected + end + + end + + end describe :highlight_matches do -- cgit v1.2.3