1 Star 0 Fork 0

golang-package/dongle

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
morse_test.go 1.81 KB
一键复制 编辑 原始数据 按行查看 历史
gouguoyin 提交于 2024-11-27 16:45 . 统一错误处理
package dongle
import (
"fmt"
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
var morseTests = []struct {
input string // 输入值
separator string // 分隔符
output string // 期望值
}{
{"", "", ""},
{"1", "/", ".----"},
{"F", "/", "..-."},
{"dongle", "|", "-..|---|-.|--.|.-..|."},
{"SOS", "/", ".../---/..."},
}
func TestMorse_Encode_String(t *testing.T) {
for index, test := range morseTests {
e := Encode.FromString(test.input).ByMorse(test.separator)
t.Run(fmt.Sprintf("test_%d", index), func(t *testing.T) {
assert.Nil(t, e.Error)
assert.Equal(t, test.output, e.ToString())
})
}
}
func TestMorse_Decode_String(t *testing.T) {
for index, test := range morseTests {
d := Decode.FromString(test.output).ByMorse(test.separator)
t.Run(fmt.Sprintf("test_%d", index), func(t *testing.T) {
assert.Nil(t, d.Error)
assert.Equal(t, strings.ToLower(test.input), d.ToString())
})
}
}
func TestMorse_Encode_Bytes(t *testing.T) {
for index, test := range morseTests {
e := Encode.FromBytes([]byte(test.input)).ByMorse(test.separator)
t.Run(fmt.Sprintf("test_%d", index), func(t *testing.T) {
assert.Nil(t, e.Error)
assert.Equal(t, []byte(test.output), e.ToBytes())
})
}
}
func TestMorse_Decode_Bytes(t *testing.T) {
for index, test := range morseTests {
d := Decode.FromBytes([]byte(test.output)).ByMorse(test.separator)
t.Run(fmt.Sprintf("test_%d", index), func(t *testing.T) {
assert.Nil(t, d.Error)
assert.Equal(t, []byte(strings.ToLower(test.input)), d.ToBytes())
})
}
}
func TestMorse_Src_Error(t *testing.T) {
e := Encode.FromString("hello world").ByMorse()
assert.Equal(t, morseError.SrcError(), e.Error)
}
func TestMorse_Decoding_Error(t *testing.T) {
e := Decode.FromString("hello world").ByMorse()
assert.Equal(t, morseError.DecodeError(), e.Error)
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/golang-package/dongle.git
git@gitee.com:golang-package/dongle.git
golang-package
dongle
dongle
master

搜索帮助

0d507c66 1850385 C8b1a773 1850385