-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmention_filter_test.go
212 lines (179 loc) · 7.24 KB
/
mention_filter_test.go
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
package pipeline
import (
"fmt"
"testing"
"github.com/stretchr/testify/assert"
)
func ExampleMentionFilter() {
text := `This is a @test_huacn-lee of some cool @中文名称 features that @mi_asd be
@use-ful but @don't. look at this email@address.com. @bla! I like #nylas but I don't
like to go to this apple.com?a#url. I also don't like the ### comment blocks.
But #msft is cool.`
pipe := NewPipeline([]Filter{
MentionFilter{
Format: func(name string) string {
return fmt.Sprintf(`<mention>%s</mention>`, name)
},
},
})
out, _ := pipe.Call(text)
fmt.Println(out)
// Output:
// This is a <mention>test_huacn-lee</mention> of some cool <mention>中文名称</mention> features that <mention>mi_asd</mention> be
// <mention>use-ful</mention> but <mention>don</mention>'t. look at this email@address.com. <mention>bla</mention>! I like #nylas but I don't
// like to go to this apple.com?a#url. I also don't like the ### comment blocks.
// But #msft is cool.
}
func ExampleMentionFilter_complex() {
text := `This is a @test_huacn-lee of some cool @中文名称 features that @mi_asd be
@use-ful but @don't. look at this email@address.com. @bla! I like #nylas but I don't
like to go to this apple.com?a#url. I also don't like the ### comment blocks.
But #msft is cool.`
pipe := NewPipeline([]Filter{
MentionFilter{
Format: func(name string) string {
return fmt.Sprintf(`<mention>@%s</mention>`, name)
},
},
MentionFilter{
Prefix: "#",
Format: func(name string) string {
return fmt.Sprintf(`<hashtag>#%s</hashtag>`, name)
},
},
})
out, _ := pipe.Call(text)
fmt.Println(out)
// Output:
// This is a <mention>@test_huacn-lee</mention> of some cool <mention>@中文名称</mention> features that <mention>@mi_asd</mention> be
// <mention>@use-ful</mention> but <mention>@don</mention>'t. look at this email@address.com. <mention>@bla</mention>! I like <hashtag>#nylas</hashtag> but I don't
// like to go to this apple.com?a#url. I also don't like the ### comment blocks.
// But <hashtag>#msft</hashtag> is cool.
}
func TestMentionFilterWithPlainText(t *testing.T) {
text := `This is a @test_huacn-lee of some cool @中文名称 features that @mi_asd be
@use-ful but @don't. look at this email@address.com. @bla! I like #nylas but I don't
like to go to this apple.com?a#url. I also don't like the ### comment blocks.
But #msft is cool.`
expected := `This is a <mention>test_huacn-lee</mention> of some cool <mention>中文名称</mention> features that <mention>mi_asd</mention> be
<mention>use-ful</mention> but <mention>don</mention>'t. look at this email@address.com. <mention>bla</mention>! I like #nylas but I don't
like to go to this apple.com?a#url. I also don't like the ### comment blocks.
But #msft is cool.`
pipe := NewPipeline([]Filter{
MentionFilter{
Format: func(name string) string {
return fmt.Sprintf(`<mention>%s</mention>`, name)
},
},
})
out, err := pipe.Call(text)
assert.NoError(t, err)
assert.Equal(t, expected, out)
}
func TestMentionFilterWithHTML(t *testing.T) {
text := `<p>This is a @test_huacn-lee of some cool @中文名称 features.</p>
<p>that <span>@mi_asd</span> <strong>@use-ful but @don't. look</strong> at this email@address.com. </p>
<p>@bla! I like #nylas but I don't like to go to this apple.com?a#url.</p>
<p>I also don't like the ### comment blocks. But #msft is cool.</p>`
expected := `<p>This is a <a href="https://twitter.com/test_huacn-lee">@test_huacn-lee</a> of some cool <a href="https://twitter.com/中文名称">@中文名称</a> features.</p>
<p>that <span><a href="https://twitter.com/mi_asd">@mi_asd</a></span> <strong><a href="https://twitter.com/use-ful">@use-ful</a> but <a href="https://twitter.com/don">@don</a>'t. look</strong> at this email@address.com. </p>
<p><a href="https://twitter.com/bla">@bla</a>! I like #nylas but I don't like to go to this apple.com?a#url.</p>
<p>I also don't like the ### comment blocks. But #msft is cool.</p>`
matchedNames := []string{}
mentionfilter := MentionFilter{
Format: func(name string) string {
return fmt.Sprintf(`<a href="https://twitter.com/%s">@%s</a>`, name, name)
},
NamesCallback: func(names []string) {
matchedNames = names
},
}
pipe := NewPipeline([]Filter{
mentionfilter,
})
out, err := pipe.Call(text)
assert.NoError(t, err)
assert.Equal(t, expected, out)
assert.Equal(t, mentionfilter.ExtractMentionNames(text), matchedNames)
}
func TestMentionFilterTwice(t *testing.T) {
cases := [][]string{
[]string{
`This is #html-pipeline example, created by @huacnlee at 2020.`,
`This is <hashtag>#html-pipeline</hashtag> example, created by <mention>@huacnlee</mention> at 2020.`,
},
[]string{
`<p>This is <em>#html-pipeline</em> example, created by <strong>@huacnlee</strong> at 2020.</p>`,
`<p>This is <em><hashtag>#html-pipeline</hashtag></em> example, created by <strong><mention>@huacnlee</mention></strong> at 2020.</p>`,
},
}
hashTags := []string{}
mentionNames := []string{}
pipe := NewPipeline([]Filter{
MentionFilter{
Prefix: "#",
Format: func(name string) string {
return "<hashtag>#" + name + "</hashtag>"
},
NamesCallback: func(names []string) {
hashTags = names
},
},
MentionFilter{
Format: func(name string) string {
return "<mention>@" + name + "</mention>"
},
NamesCallback: func(names []string) {
mentionNames = names
},
},
})
for _, item := range cases {
text := item[0]
expected := item[1]
out, err := pipe.Call(text)
assert.NoError(t, err)
assert.Equal(t, []string{"html-pipeline"}, hashTags)
assert.Equal(t, []string{"huacnlee"}, mentionNames)
assert.Equal(t, expected, out)
}
}
func ExampleMentionFilter_ExtractMentionNames() {
text := `@huacnlee This is a @test_huacn-lee of some cool @中文名称 features that @mi_asd be
@use-ful but @don't. look at this email@address.com. @bla! I like #nylas but I don't
like to go to this apple.com?a#url. I also don't like the ### comment blocks.
But #msft is cool.`
mentionFilter := MentionFilter{}
names := mentionFilter.ExtractMentionNames(text)
fmt.Println(names)
// Output: [huacnlee test_huacn-lee 中文名称 mi_asd use-ful don bla]
}
func TestExtractMentionNamesWithHashTag(t *testing.T) {
text := `#huacnlee This is a #test_huacn-lee of some cool #中文名称 features that #mi_asd be
#use-ful but #don't. look at this email#address.com. #bla! I like #nylas but I don't
like to go to this apple.com?a#url. I also don't like the comment blocks.
But #msft is cool.`
expectedNames := []string{"huacnlee", "test_huacn-lee", "中文名称", "mi_asd", "use-ful", "don", "bla",
"nylas", "msft"}
mentionFilter := MentionFilter{Prefix: "#"}
names := mentionFilter.ExtractMentionNames(text)
assert.Equal(t, expectedNames, names)
}
func BenchmarkMentionFilter(b *testing.B) {
raw := `#huacnlee This is a #test_huacn-lee of some cool #中文名称 features that #mi_asd be
#use-ful but #don't. look at this email#address.com. #bla! I like #nylas but I don't
like to go to this apple.com?a#url. I also don't like the comment blocks.
But #msft is cool.`
pipe := NewPipeline([]Filter{
MentionFilter{
Prefix: "#",
Format: func(name string) string {
return "<hashtag>#" + name + "</hashtag>"
},
},
})
for i := 0; i < b.N; i++ {
// 32978 ns/op
pipe.Call(raw)
}
}