/
excerpt_test.go
180 lines (166 loc) · 4.71 KB
/
excerpt_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
package stringfy_test
import (
"testing"
"github.com/hgsigner/stringfy"
)
var text string = `Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section 1.10.32.
The standard chunk of Lorem Ipsum used since the 1500s is reproduced below for those interested. Sections 1.10.32 and 1.10.33 from "de Finibus Bonorum et Malorum" by Cicero are also reproduced in their exact original form, accompanied by English versions from the 1914 translation by H. Rackham.`
func Test_Excerpt(t *testing.T) {
tests := []struct {
text string
phrase string
radious int
addRadious bool
omission string
addOmission bool
separator string
addSeparator bool
out string
errorOut string
}{
{
text: text,
phrase: "bhtk",
addRadious: true,
radious: 5,
out: "",
errorOut: "phrase (bhtk) not found",
},
{
text: text,
phrase: "to",
addRadious: true,
radious: 5,
out: "...rary to popu...",
errorOut: "",
},
{
text: text,
phrase: "tra",
addRadious: true,
radious: 5,
out: "Contrary to...",
errorOut: "",
},
{
text: text,
phrase: "Rackham",
addRadious: true,
radious: 5,
out: "...y H. Rackham.",
errorOut: "",
},
{
text: text,
phrase: "sit",
addRadious: true,
radious: 5,
addOmission: true,
omission: "(...)",
out: "(...)olor sit amet(...)",
errorOut: "",
},
{
text: text,
phrase: "popular",
addRadious: true,
radious: 1,
addSeparator: true,
separator: " ",
out: "...to popular belief,...",
errorOut: "",
},
{
text: text,
phrase: "to",
addRadious: true,
radious: 2,
addSeparator: true,
separator: " ",
out: "Contrary to popular belief,...",
errorOut: "",
},
{
text: text,
phrase: "bhtk",
addRadious: true,
radious: 1,
addSeparator: true,
separator: " ",
out: "",
errorOut: "phrase (bhtk) not found",
},
{
text: text,
phrase: "to",
addRadious: true,
radious: 5,
addSeparator: true,
separator: "-",
out: "...rary to popu...",
errorOut: "",
},
{
text: text,
phrase: "to popular",
addRadious: true,
radious: 5,
addSeparator: true,
separator: " ",
out: "",
errorOut: "when composing the excerpt, your phrase should not contain more than one word",
},
{
text: text,
phrase: "to popular",
addRadious: true,
radious: 5,
addSeparator: true,
separator: "-",
out: "",
errorOut: "when composing the excerpt, your phrase should not contain more than one word",
},
}
for _, test := range tests {
exc := stringfy.NewExcerpt()
if test.addRadious {
exc.Options(stringfy.AddRadius(test.radious))
}
if test.addOmission {
exc.Options(stringfy.AddOmission(test.omission))
}
if test.addSeparator {
exc.Options(stringfy.AddSeparator(test.separator))
}
excf, err := exc.Perform(test.text, test.phrase)
if err != nil {
if err.Error() != test.errorOut {
t.Errorf("\nError Expected: %s\nError Got: %s", test.errorOut, err.Error())
}
}
if excf != test.out {
t.Errorf("\nExpected: %s\nGot: %s", test.out, excf)
}
}
}
func BenchmarkExcerptPerform_Radious(b *testing.B) {
exc := stringfy.NewExcerpt()
exc.Options(stringfy.AddRadius(5))
for i := 0; i < b.N; i++ {
exc.Perform(text, "to")
}
}
func BenchmarkExcerptPerform_RadiosSeparator(b *testing.B) {
exc := stringfy.NewExcerpt()
exc.Options(stringfy.AddRadius(1), stringfy.AddSeparator(" "))
for i := 0; i < b.N; i++ {
exc.Perform(text, "popular")
}
}
func BenchmarkExcerptPerform_RadiosOmission(b *testing.B) {
exc := stringfy.NewExcerpt()
exc.Options(stringfy.AddRadius(5), stringfy.AddOmission("(...)"))
for i := 0; i < b.N; i++ {
exc.Perform(text, "sit")
}
}