-
Notifications
You must be signed in to change notification settings - Fork 22
/
emoji_test.go
193 lines (159 loc) · 4.03 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
package emoji_test
import (
"bytes"
"fmt"
"testing"
emoji "github.com/Andrew-M-C/go.emoji"
"github.com/smartystreets/goconvey/convey"
)
var (
cv = convey.Convey
so = convey.So
eq = convey.ShouldEqual
le = convey.ShouldBeLessThan
notPanic = convey.ShouldNotPanic
)
func TestEmoji(t *testing.T) {
cv("visualize unicode", t, func() { testUnicode((t)) })
cv("ReplaceAllEmojiFunc()", t, func() { testReplaceAllEmojiFunc(t) })
cv("IterateChars()", t, func() { testIterateChars(t) })
cv("#3", t, func() { testIssue3(t) })
cv("#4", t, func() { testIssue4(t) })
}
// reference: https://www.jianshu.com/p/9682f8ce1260
func testUnicode(t *testing.T) {
printf := t.Logf
buff := bytes.Buffer{}
// ==== Emoji_presentation_sequence ====
// thunder default mode
buff.WriteRune(0x26A1)
printf("%s", buff.String())
buff.Reset()
// thunder text mode
buff.WriteRune(0x26A1)
buff.WriteRune(0xFE0E)
printf("%s", buff.String())
buff.Reset()
// thunder emoji mode
buff.WriteRune(0x26A1)
buff.WriteRune(0xFE0F)
printf("%s", buff.String())
buff.Reset()
// ==== Modfier_Base_Sequence ====
buff.WriteRune(0x270D)
buff.WriteRune(0xFE0F)
printf("%s", buff.String())
buff.Reset()
buff.WriteRune(0x270D)
buff.WriteRune(0x1F3FF)
printf("%s", buff.String())
buff.Reset()
// ==== Flag Sequence ====
// reference: https://en.wikipedia.org/wiki/Regional_Indicator_Symbol
// from 0x1F1E6 to 0x1F1FF
buff.WriteRune(0x1F1E8)
buff.WriteRune(0x1F1F3)
// buff.WriteRune(0x1F1FA)
// buff.WriteRune(0x1F1F8)
printf("%s", buff.String())
buff.Reset()
// ==== KeyCap Sequence ====
// 012345678*#
buff.WriteRune('#')
buff.WriteRune(0xFE0F)
buff.WriteRune(0x20E3)
printf("%s", buff.String())
buff.Reset()
// ==== ZWJ sequence ====
buff.WriteRune(0x1F468)
buff.WriteRune(0x200D)
buff.WriteRune(0x1F469)
buff.WriteRune(0x200D)
buff.WriteRune(0x1F467)
buff.WriteRune(0x200D)
buff.WriteRune(0x1F466)
buff.WriteRune(0x1F46a)
printf("%s", buff.String())
buff.Reset()
// ==== Tag Sequence ====
buff.WriteRune(0x1F3F4)
buff.WriteRune(0xE0067)
buff.WriteRune(0xE0062)
buff.WriteRune(0xE0065)
buff.WriteRune(0xE006E)
buff.WriteRune(0xE0067)
buff.WriteRune(0xE007F)
printf("%s", buff.String())
buff.Reset()
}
func testReplaceAllEmojiFunc(t *testing.T) {
printf := t.Logf
s := "👩👩👦🇨🇳"
i := 0
final := emoji.ReplaceAllEmojiFunc(s, func(emoji string) string {
i++
printf("%02d - %s - len %d", i, emoji, len(emoji))
return fmt.Sprintf("%d-", i)
})
printf("final: <%s>", final)
}
func testIterateChars(t *testing.T) {
cv("common case", func() {
const s = "China🇨🇳US🇺🇸"
i := 0
expectRune := []string{"C", "h", "i", "n", "a", "🇨🇳", "U", "S", "🇺🇸"}
expectBool := []bool{false, false, false, false, false, true, false, false, true}
for it := emoji.IterateChars(s); it.Next(); i++ {
t.Log(i, "-", it.Current(), "-", it.CurrentIsEmoji())
so(it.Current(), eq, expectRune[i])
so(it.CurrentIsEmoji(), eq, expectBool[i])
so(i, le, len(expectRune))
}
so(i, eq, len(expectRune))
})
cv("empty string", func() {
so(func() {
for it := emoji.IterateChars(""); it.Next(); {
panic("should NOT iterate!")
}
}, notPanic)
})
cv("string without emoji", func() {
const s = "Golang"
i := 0
for it := emoji.IterateChars(s); it.Next(); i++ {
so(it.Current(), eq, string(s[i]))
}
so(i, eq, len(s))
})
}
func testIssue3(t *testing.T) {
str := string([]rune{0x2764, '|', 0x2764, 0xFE0F})
cnt := 0
final := emoji.ReplaceAllEmojiFunc(str, func(emoji string) string {
cnt++
return "heart"
})
so(cnt, eq, 2)
so(final, eq, "heart|heart")
t.Log(str)
}
func testIssue4(t *testing.T) {
cv("original issue", func() {
// s := "China_1m"
s := "1"
i := 0
for it := emoji.IterateChars(s); it.Next(); i++ {
t.Log(i, "-", it.Current(), "-", it.CurrentIsEmoji())
so(it.CurrentIsEmoji(), eq, false)
so(i, le, len(s))
}
})
cv("extra study", func() {
s := "1234567890"
res := emoji.ReplaceAllEmojiFunc(s, func(emoji string) string {
return "*"
})
so(res, eq, s)
})
}