1
0
mirror of https://github.com/fumiama/emozi.git synced 2026-06-05 08:40:34 +08:00
Files
emozi/coder_test.go
2024-02-15 17:14:56 +09:00

101 lines
2.4 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package emozi
import (
"testing"
"time"
)
func TestEncode(t *testing.T) {
c, err := NewCoder(time.Minute)
if err != nil {
t.Fatal(err)
}
es, lst, err := c.Encode(false, "你好,世界!看看多音字:行。")
if err != nil {
t.Fatal(err)
}
t.Log(es.String(), lst)
if es.String() != "🥛👔🐴👤🌹🐱🐴👩,💦🌞😨🌍➕✌😨👨‍🌾!😭🔐🍉👁️😭🔐🍉👁️🔪🌀🍉🪩🐑🎵🍉🎵👈🌞😨🚼:[👇🦅🧗⛕|🌹👍🧗⛕]。" {
t.Fatal("got", es.String())
}
if len(lst) != 1 && lst[0] != 2 {
t.Fail()
}
es, lst, err = c.Encode(false, "你好,世界!指定多音字:银行行。", 1, 0)
if err != nil {
t.Fatal(err)
}
t.Log(es.String(), lst)
if es.String() != "🥛👔🐴👤🌹🐱🐴👩,💦🌞😨🌍➕✌😨👨‍🌾!🐽🌞🐴✋🔪🦅😨🏠🔪🌀🍉🪩🐑🎵🍉🎵👈🌞😨🚼:🐑🎵🧗💰🌹👍🧗⛕👇🦅🧗⛕。" {
t.Fatal("got", es.String())
}
if len(lst) != 2 && lst[0] != 2 && lst[1] != 2 {
t.Fail()
}
es, lst, err = c.Encode(false, "的")
if err != nil {
t.Fatal(err)
}
t.Log(es.String(), lst)
if es.String() != "的🈳🈳🈳" {
t.Fatal("got", es.String())
}
}
func TestDecode(t *testing.T) {
c, err := NewCoder(time.Minute)
if err != nil {
t.Fatal(err)
}
s := "你好,世界!看看多音字:行。"
es, lst, err := c.Encode(false, s)
if err != nil {
t.Fatal(err)
}
t.Log(es.String(), lst)
ds, err := c.Decode(es, false)
if err != nil {
t.Fatal(err)
}
t.Log(ds)
if ds != "[你|儗]好,世[界|畍]!看看多音字:[行|行]。" {
t.Fatal("got", ds)
}
es, lst, err = c.Encode(false, "你好,世界!指定多音字:银行行。", 1, 0)
if err != nil {
t.Fatal(err)
}
t.Log(es.String(), lst)
ds, err = c.Decode(es, false)
if err != nil {
t.Fatal(err)
}
t.Log(ds)
if ds != "[你|儗]好,世[界|畍][指|抧|扺]定多音字:[銀|银]行行。" {
t.Fatal("got", ds)
}
es, lst, err = c.Encode(false, "好啊")
if err != nil {
t.Fatal(err)
}
t.Log(es.String(), lst)
ds, err = c.Decode(es, false)
if err != nil {
t.Fatal(err)
}
t.Log(ds)
if ds != "好啊" {
t.Fatal("got", ds)
}
es = EmoziString("🌹😺‎🐴‫👩") // nolint: go-staticcheck
t.Log(es.String())
ds, err = c.Decode(es, false)
if err != nil {
t.Fatal(err)
}
t.Log(ds)
if ds != "好" {
t.Fatal("got", ds)
}
}