Way more test coverage for Taggable.format_tags .

This commit is contained in:
Pistos 2011-11-01 01:20:59 -04:00
parent 73c79bdc0d
commit b3853f48ed
2 changed files with 66 additions and 7 deletions

View file

@ -4,8 +4,6 @@
module Diaspora
module Taggable
VALID_TAG_BODY = /[^_,\s#*\[\]()\@\/"'\.%]+\b/
def self.included(model)
model.class_eval do
cattr_accessor :field_with_tags
@ -43,10 +41,16 @@ module Diaspora
return text if opts[:plain_text]
text = ERB::Util.h(text) unless opts[:no_escape]
regex = /(^|\s|>)#(#{VALID_TAG_BODY})/
regex = /(^|\s|>)#([\w-]+|<3)/
text.to_str.gsub(regex) { |matched_string|
%{#{$1}<a href="/tags/#{$2}" class="tag">##{$2}</a>}
pre, url_bit, clickable = $1, $2, "##{$2}"
if $2 == '&lt;3'
# Special case for love, because the world needs more love.
url_bit = '<3'
end
%{#{pre}<a href="/tags/#{url_bit}" class="tag">#{clickable}</a>}
}.html_safe
end
end

View file

@ -12,6 +12,10 @@ describe Diaspora::Taggable do
def controller
end
def tag_link(s)
link_to "##{s}", "/tags/#{s}", :class => 'tag'
end
describe '.format_tags' do
before do
@str = '#what #hey #vöglein'
@ -21,13 +25,59 @@ describe Diaspora::Taggable do
end
it 'links the tag to /p' do
link = link_to('#vöglein', '/tags/vöglein', :class => 'tag')
Diaspora::Taggable.format_tags(@str).should include(link)
Diaspora::Taggable.format_tags(@str).should include( tag_link('vöglein') )
end
it 'responds to plain_text' do
Diaspora::Taggable.format_tags(@str, :plain_text => true).should == @str
end
it "doesn't mangle text when tags are involved" do
expected = {
nil => '',
'' => '',
'abc' => 'abc',
'a #b c' => "a #{tag_link('b')} c",
'#' => '#',
'##' => '##',
'###' => '###',
'#a' => tag_link('a'),
'#foobar' => tag_link('foobar'),
'#foocar<br>' => "#{tag_link('foocar')}&lt;br&gt;",
'#fooo@oo' => "#{tag_link('fooo')}@oo",
'#num3ric hash tags' => "#{tag_link('num3ric')} hash tags",
'#12345 tag' => "#{tag_link('12345')} tag",
'#12cde tag' => "#{tag_link('12cde')} tag",
'#abc45 tag' => "#{tag_link('abc45')} tag",
'#<3' => %{<a href="/tags/<3" class="tag">#&lt;3</a>},
'i #<3' => %{i <a href="/tags/<3" class="tag">#&lt;3</a>},
'i #<3 you' => %{i <a href="/tags/<3" class="tag">#&lt;3</a> you},
'#<4' => '#&lt;4',
'test#foo test' => 'test#foo test',
'test.#joo bar' => 'test.#joo bar',
'test #foodar test' => "test #{tag_link('foodar')} test",
'test #foofar<br> test' => "test #{tag_link('foofar')}&lt;br&gt; test",
'test #gooo@oo test' => "test #{tag_link('gooo')}@oo test",
'test #foo-test test' => "test #{tag_link('foo-test')} test",
'test #hoo' => "test #{tag_link('hoo')}",
'test #two_word tags' => "test #{tag_link('two_word')} tags",
'test #three_word_tags' => "test #{tag_link('three_word_tags')}",
'#terminal_underscore_' => tag_link('terminal_underscore_'),
'#terminalunderscore_' => tag_link('terminalunderscore_'),
'#_initialunderscore' => tag_link('_initialunderscore'),
'#_initial_underscore' => tag_link('_initial_underscore'),
'#terminalhyphen-' => tag_link('terminalhyphen-'),
'#terminal-hyphen-' => tag_link('terminal-hyphen-'),
'#terminalhyphen- tag' => "#{tag_link('terminalhyphen-')} tag",
'#-initialhyphen' => tag_link('-initialhyphen'),
'#-initialhyphen tag' => "#{tag_link('-initialhyphen')} tag",
'#-initial-hyphen' => tag_link('-initial-hyphen'),
}
expected.each do |input,output|
Diaspora::Taggable.format_tags(input).should == output
end
end
end
describe '#build_tags' do
@ -52,6 +102,11 @@ describe Diaspora::Taggable do
it 'extracts tags despite surrounding text' do
expected = {
'' => nil,
'#' => nil,
'##' => nil,
'###' => nil,
'#a' => 'a',
'#foobar' => 'foobar',
'#foocar<br>' => 'foocar',
'#fooo@oo' => 'fooo',
@ -64,7 +119,7 @@ describe Diaspora::Taggable do
'test#foo test' => nil,
'test.#joo bar' => nil,
'test #foodar test' => 'foodar',
'test #foofar<br> test ' => 'foofar',
'test #foofar<br> test' => 'foofar',
'test #gooo@oo test' => 'gooo',
'test #<3 test' => '<3',
'test #foo-test test' => 'foo-test',