-
Notifications
You must be signed in to change notification settings - Fork 89
/
Copy pathemoji_test.go
272 lines (233 loc) · 5.7 KB
/
emoji_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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
package emoji
import (
"bytes"
"fmt"
"math"
"strings"
"sync"
"testing"
)
const (
beerKey = ":beer:"
beerText = " ビール!!!"
flag = ":flag-us:"
plusOne = ":+1:"
)
var testFText = "test " + Emojize(beerKey) + beerText
var testText = Emojize(beerKey) + beerText
func TestFlag(t *testing.T) {
f := Emojize(flag)
expected := "\U0001f1fA\U0001f1f8"
if f != expected {
t.Error("emojize ", f, "!=", expected)
}
}
func TestPlusOne(t *testing.T) {
f := Emojize(plusOne)
expected := "\U0001f44d "
if f != expected {
t.Error("emojize ", f, "!=", expected)
}
}
func TestMultiColons(t *testing.T) {
var buf bytes.Buffer
_, err := Fprint(&buf, "A :smile: and another: :smile:")
if err != nil {
t.Error("Fprint ", err)
}
testCase := "A " + Emojize(":smile:") + " and another: " + Emojize(":smile:")
if buf.String() != testCase {
t.Error("Fprint ", buf.String(), "!=", testCase)
}
}
func TestContinuityColons(t *testing.T) {
var buf bytes.Buffer
_, err := Fprint(&buf, "::smile:")
if err != nil {
t.Error("Fprint ", err)
}
testCase := ":" + Emojize(":smile:")
if buf.String() != testCase {
t.Error("Fprint ", buf.String(), "!=", testCase)
}
}
func TestCodeMap(t *testing.T) {
m := CodeMap()
if &emojiCodeMap == &m {
t.Error("emojiCodeMap != EmojiCodeMap")
}
}
func TestRevCodeMap(t *testing.T) {
m := RevCodeMap()
if &emojiRevCodeMap == &m {
t.Error("emojiRevCodeMap != EmojiRevCodeMap")
}
}
func TestHasAlias(t *testing.T) {
hasAlias := HasAlias(":+1:")
if !hasAlias {
t.Error(":+1: doesn't have an alias")
}
hasAlias = HasAlias(":no-good:")
if hasAlias {
t.Error(":no-good: has an alias")
}
}
func TestNoramlizeShortCode(t *testing.T) {
test := ":thumbs_up:"
expected := ":+1:"
normalized := NormalizeShortCode(test)
if normalized != expected {
t.Errorf("Normalized %q != %q", test, expected)
}
test = ":no-good:"
normalized = NormalizeShortCode(test)
if normalized != test {
t.Errorf("Normalized %q != %q", test, normalized)
}
}
func TestPrint(t *testing.T) {
_, err := Print(beerKey, beerText)
if err != nil {
t.Error("Print ", err)
}
}
func TestPrintln(t *testing.T) {
_, err := Println(beerKey, beerText)
if err != nil {
t.Error("Println ", err)
}
}
func TestPrintf(t *testing.T) {
_, err := Printf("%s "+beerKey+beerText, "test")
if err != nil {
t.Error("Printf ", err)
}
}
func TestFprint(t *testing.T) {
var buf bytes.Buffer
_, err := Fprint(&buf, beerKey+beerText)
if err != nil {
t.Error("Fprint ", err)
}
if buf.String() != testText {
t.Error("Fprint ", buf.String(), testText)
}
}
func TestFprintln(t *testing.T) {
var buf bytes.Buffer
_, err := Fprintln(&buf, beerKey+beerText)
if err != nil {
t.Error("Fprintln ", err)
}
if buf.String() != (testText + "\n") {
t.Error("Fprintln ", buf.String(), (testText + "\n"))
}
}
func TestFprintf(t *testing.T) {
var buf bytes.Buffer
_, err := Fprintf(&buf, "%s "+beerKey+beerText, "test")
if err != nil {
t.Error("Fprintf ", err)
}
if buf.String() != testFText {
t.Error("Fprintf ", buf.String(), testFText)
}
}
func TestSprint(t *testing.T) {
convertBeer := Sprint(beerKey, beerText)
if convertBeer != testText {
t.Error("Sprint ", convertBeer, testText)
}
}
func TestSprintf(t *testing.T) {
convertBeer := Sprintf("%s "+beerKey+beerText, "test")
if convertBeer != testFText {
t.Error("Sprintf ", convertBeer, testFText)
}
}
func TestErrorf(t *testing.T) {
error := Errorf("%s "+beerKey+beerText, "test")
if error.Error() != testFText {
t.Error("Errorf ", error, testFText)
}
}
func TestSprintMulti(t *testing.T) {
convertBeer := Sprint(beerKey, beerText, beerKey, beerText)
if convertBeer != (testText + testText) {
t.Error("Sprint ", convertBeer, testText)
}
fmt.Println(convertBeer)
}
// Copyright 2016 The Hugo Authors. All rights reserved.
// source: https://github.com/spf13/hugo/blob/master/helpers/emoji_test.go
func BenchmarkFprint(b *testing.B) {
f := func(in []byte) []byte {
buff := getBuffer()
defer putBuffer(buff)
if _, err := Fprint(buff, string(in)); err != nil {
return nil
}
bc := make([]byte, buff.Len())
copy(bc, buff.Bytes())
return bc
}
doBenchmarkEmoji(b, f)
}
func BenchmarkSprint(b *testing.B) {
f := func(in []byte) []byte {
return []byte(Sprint(string(in)))
}
doBenchmarkEmoji(b, f)
}
func doBenchmarkEmoji(b *testing.B, f func(in []byte) []byte) {
type input struct {
in []byte
expect []byte
}
data := []struct {
input string
expect string
}{
{"A :smile: a day", Sprint("A :smile: a day")},
{"A :smile: and a :beer: day keeps the doctor away", Sprint("A :smile: and a :beer: day keeps the doctor away")},
{"A :smile: a day and 10 " + strings.Repeat(":beer: ", 10), Sprint("A :smile: a day and 10 " + strings.Repeat(":beer: ", 10))},
{"No smiles today.", "No smiles today."},
{"No smiles for you or " + strings.Repeat("you ", 1000), "No smiles for you or " + strings.Repeat("you ", 1000)},
}
var in = make([]input, b.N*len(data))
var cnt = 0
for i := 0; i < b.N; i++ {
for _, this := range data {
in[cnt] = input{[]byte(this.input), []byte(this.expect)}
cnt++
}
}
b.ResetTimer()
cnt = 0
for i := 0; i < b.N; i++ {
for j := range data {
currIn := in[cnt]
cnt++
result := f(currIn.in)
// The Emoji implementations gives slightly different output.
diffLen := len(result) - len(currIn.expect)
diffLen = int(math.Abs(float64(diffLen)))
if diffLen > 30 {
b.Fatalf("[%d] emoji std, got \n%q but expected \n%q", j, result, currIn.expect)
}
}
}
}
var bufferPool = &sync.Pool{
New: func() interface{} {
return &bytes.Buffer{}
},
}
func getBuffer() (buf *bytes.Buffer) {
return bufferPool.Get().(*bytes.Buffer)
}
func putBuffer(buf *bytes.Buffer) {
buf.Reset()
bufferPool.Put(buf)
}