Add many more hashtag regexp specs for Taggable#tag_strings . Make them pass.
This commit is contained in:
parent
e123b72d35
commit
24702b9f6a
2 changed files with 45 additions and 5 deletions
|
|
@ -27,10 +27,11 @@ module Diaspora
|
|||
end
|
||||
|
||||
def tag_strings
|
||||
regex = /(?:^|\s)#(#{VALID_TAG_BODY})/
|
||||
matches = self.send(self.class.field_with_tags).scan(regex).map do |match|
|
||||
match.last
|
||||
end
|
||||
regex = /(?:^|\s)#([\w-]+|<3)/
|
||||
matches = self.
|
||||
send( self.class.field_with_tags ).
|
||||
scan(regex).
|
||||
map { |match| match[0] }
|
||||
unique_matches = matches.inject(Hash.new) do |h,element|
|
||||
h[element.downcase] = element unless h[element.downcase]
|
||||
h
|
||||
|
|
|
|||
|
|
@ -44,12 +44,51 @@ describe Diaspora::Taggable do
|
|||
describe '#tag_strings' do
|
||||
it 'returns a string for every #thing' do
|
||||
str = '#what #hey #that"smybike. #@hey ##boo # #THATWASMYBIKE #vöglein #hey#there #135440we #abc/23 ### #h!gh #ok? #see: #re:publica'
|
||||
arr = ['what', 'hey', 'that', 'THATWASMYBIKE', 'vöglein', '135440we', 'abc', 'h!gh', 'ok', 'see', 're:publica']
|
||||
arr = ['what', 'hey', 'that', 'THATWASMYBIKE', 'vöglein', '135440we', 'abc', 'h', 'ok', 'see', 're']
|
||||
|
||||
@object.send(@object.class.field_with_tags_setter, str)
|
||||
@object.tag_strings.should =~ arr
|
||||
end
|
||||
|
||||
it 'extracts tags despite surrounding text' do
|
||||
expected = {
|
||||
'#foobar' => 'foobar',
|
||||
'#foocar<br>' => 'foocar',
|
||||
'#fooo@oo' => 'fooo',
|
||||
'#num3ric hash tags' => 'num3ric',
|
||||
'#12345 tag' => '12345',
|
||||
'#12cde tag' => '12cde',
|
||||
'#abc45 tag' => 'abc45',
|
||||
'#<3' => '<3',
|
||||
'#<4' => nil,
|
||||
'test#foo test' => nil,
|
||||
'test.#joo bar' => nil,
|
||||
'test #foodar test' => 'foodar',
|
||||
'test #foofar<br> test ' => 'foofar',
|
||||
'test #gooo@oo test' => 'gooo',
|
||||
'test #<3 test' => '<3',
|
||||
'test #foo-test test' => 'foo-test',
|
||||
'test #hoo' => 'hoo',
|
||||
'test #two_word tags' => 'two_word',
|
||||
'test #three_word_tags' => 'three_word_tags',
|
||||
'#terminal_underscore_' => 'terminal_underscore_',
|
||||
'#terminalunderscore_' => 'terminalunderscore_',
|
||||
'#_initialunderscore' => '_initialunderscore',
|
||||
'#_initial_underscore' => '_initial_underscore',
|
||||
'#terminalhyphen-' => 'terminalhyphen-',
|
||||
'#terminal-hyphen-' => 'terminal-hyphen-',
|
||||
'#terminalhyphen- tag' => 'terminalhyphen-',
|
||||
'#-initialhyphen' => '-initialhyphen',
|
||||
'#-initialhyphen tag' => '-initialhyphen',
|
||||
'#-initial-hyphen' => '-initial-hyphen',
|
||||
}
|
||||
|
||||
expected.each do |text,hashtag|
|
||||
@object.send @object.class.field_with_tags_setter, text
|
||||
@object.tag_strings.should == [hashtag].compact
|
||||
end
|
||||
end
|
||||
|
||||
it 'returns no duplicates' do
|
||||
str = '#what #what #what #whaaaaaaaaaat'
|
||||
arr = ['what','whaaaaaaaaaat']
|
||||
|
|
|
|||
Loading…
Reference in a new issue