mention_helper_test.rb
3.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
require_relative "../test_helper"
class MentionHelperTest < ActionView::TestCase
include MentionHelper
#helper ApplicationHelper
def setup
@person = create_user('user-test').person
@friends = [
create_user('friend-1').person, create_user('friend-2').person
]
@person.add_friend @friends[0]
@person.add_friend @friends[1]
@community = fast_create(Community, :name=>'community-test')
@members = [
create_user('member-1').person, create_user('member-2').person
]
@community.add_member @members[0]
@community.add_member @members[1]
@person.save!
@community.add_member @person
@community.save!
@sample_message = "@#{@friends[0].identifier} and @#{@members[0].identifier} " +
"send an email to fulano@gmail.com " +
"and @#{@person.identifier}.And @#{@person.identifier} do nothing"
end
should 'mention regex match mentions at thet very start of a text' do
assert_match mention_search_regex, '@mention-in-the-start and etc'
end
should 'mention regex match mentions throughout the text' do
assert_match mention_search_regex, 'there is a mention in @middle of the text'
end
should 'mention regex not match email' do
assert_no_match mention_search_regex, 'The email fulano@gmail.com is not a mention'
end
should 'verify if there is a mention on a given text' do
assert_equal true, has_mentions?(@sample_message)
assert_equal false, has_mentions?("fulano@gmail.com is not a mention")
end
should 'get the identifiers from the mentions on a text' do
mentions = get_mentions @sample_message
assert_equal 3, mentions.count
assert_equal false, mentions.include?("gmail.com")
assert_equal true, mentions.include?(@person.identifier)
assert_equal true, mentions.include?(@friends[0].identifier)
assert_equal true, mentions.include?(@members[0].identifier)
end
should 'get invalid mentions from a person' do
invalid = get_invalid_mentions @sample_message, @person, @community
assert_equal true, invalid.empty?
@sample_message += " and @ciclano"
invalid = get_invalid_mentions @sample_message, @person, @community
assert_equal false, invalid.empty?
assert_equal true, invalid.include?("ciclano")
end
should 'remove a given set of invalid mentions from a text' do
invalid_mentions = [@friends[0].identifier, @members[0].identifier]
text = remove_invalid_mentions @sample_message, invalid_mentions
mentions = get_mentions text
assert_equal 1, mentions.count
assert_equal @person.identifier, mentions[0]
assert_equal false, mentions.include?(@friends[0].identifier)
assert_equal false, mentions.include?(@members[0].identifier)
end
should 'tell if there is valid mentions by removing the invalid ones' do
article = Article.new :name => 'test article', :profile => @community
article.save!
comment = Comment.new :body => @sample_message, :author => @person, :source => article
comment.save!
assert_equal true, has_valid_mentions?(comment, :body, @person, @community)
comment.body += " and @ciclano" # will have its mention removed
assert_equal true, has_valid_mentions?(comment, :body, @person, @community) # only the "@" will be removed from ciclano
assert_equal false, comment.body.include?("@ciclano")
assert_equal true, comment.body.include?("ciclano")
comment.body = "A total @invalid mentioned text"
assert_equal false, has_valid_mentions?(comment, :body, @person, @community)
assert_equal false, comment.body.include?("@invalid")
assert_equal true, comment.body.include?("invalid")
end
end