Skip to content

Commit

Permalink
[*] fix: #4
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrew-M-C committed Oct 29, 2024
1 parent 7a68bbf commit 1519f0e
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 31 deletions.
40 changes: 33 additions & 7 deletions emoji_test.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
package emoji
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
)
Expand All @@ -21,6 +23,7 @@ func TestEmoji(t *testing.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
Expand Down Expand Up @@ -106,7 +109,7 @@ func testReplaceAllEmojiFunc(t *testing.T) {
s := "👩‍👩‍👦🇨🇳"
i := 0

final := ReplaceAllEmojiFunc(s, func(emoji string) string {
final := emoji.ReplaceAllEmojiFunc(s, func(emoji string) string {
i++
printf("%02d - %s - len %d", i, emoji, len(emoji))
return fmt.Sprintf("%d-", i)
Expand All @@ -123,17 +126,19 @@ func testIterateChars(t *testing.T) {
expectRune := []string{"C", "h", "i", "n", "a", "🇨🇳", "U", "S", "🇺🇸"}
expectBool := []bool{false, false, false, false, false, true, false, false, true}

for it := IterateChars(s); it.Next(); i++ {
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 := IterateChars(""); it.Next(); {
for it := emoji.IterateChars(""); it.Next(); {
panic("should NOT iterate!")
}
}, notPanic)
Expand All @@ -143,7 +148,7 @@ func testIterateChars(t *testing.T) {
const s = "Golang"

i := 0
for it := IterateChars(s); it.Next(); i++ {
for it := emoji.IterateChars(s); it.Next(); i++ {
so(it.Current(), eq, string(s[i]))
}

Expand All @@ -155,13 +160,34 @@ func testIssue3(t *testing.T) {
str := string([]rune{0x2764, '|', 0x2764, 0xFE0F})
cnt := 0

final := ReplaceAllEmojiFunc(str, func(emoji string) string {
final := emoji.ReplaceAllEmojiFunc(str, func(emoji string) string {
cnt++
return "heart"
})

so(cnt, eq, 2)
so(final, eq, "heart|heart")

t.Logf(str)
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)
})
}
24 changes: 0 additions & 24 deletions internal/official/emoji-sequences.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions internal/tool/const/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/Andrew-M-C/go.emoji/internal/tool/const

go 1.23.1
6 changes: 6 additions & 0 deletions internal/tool/const/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,12 @@ func printEmojiDataParam() {
}()

for _, rec := range records {
// Fix #4: previous version also treat some ascii as emoji, which
// should be ignored
if len(rec.runes) == 1 && rec.runes[0] < 0x7F {
continue
}

runesStr := []string{}
for _, r := range rec.runes {
runesStr = append(runesStr, fmt.Sprintf("0x%04x", r))
Expand Down

0 comments on commit 1519f0e

Please sign in to comment.