1 Star 0 Fork 51

黄苏伟/dongle

forked from dromara/dongle 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
morse_test.go 1.81 KB
一键复制 编辑 原始数据 按行查看 历史
gouguoyin 提交于 2022-12-09 12:03 . 对 import 的软件包进行分组
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, invalidMorseSrcError(), e.Error)
}
func TestMorse_Decoding_Error(t *testing.T) {
e := Decode.FromString("hello world").ByMorse()
assert.Equal(t, morseDecodingError(), e.Error)
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/huang-suwei/dongle.git
git@gitee.com:huang-suwei/dongle.git
huang-suwei
dongle
dongle
master

搜索帮助