加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
hex_test.go 1.48 KB
一键复制 编辑 原始数据 按行查看 历史
gouguoyin 提交于 2022-11-29 22:28 . 统一单元测试格式
package dongle
import (
"strconv"
"testing"
"github.com/stretchr/testify/assert"
)
var hexTest = []struct {
input string // 输入值
output string // 期望值
}{
{"", ""},
{"hello world", "68656c6c6f20776f726c64"},
}
func TestEncode_ByHex_ToString(t *testing.T) {
for index, test := range hexTest {
e := Encode.FromString(test.input).ByHex()
assert.Nil(t, e.Error)
assert.Equal(t, test.output, e.ToString(), "Current test index is "+strconv.Itoa(index))
}
}
func TestDecode_ByHex_ToString(t *testing.T) {
for index, test := range hexTest {
d := Decode.FromString(test.output).ByHex()
assert.Nil(t, d.Error)
assert.Equal(t, test.input, d.ToString(), "Current test index is "+strconv.Itoa(index))
}
}
func TestEncode_ByHex_ToBytes(t *testing.T) {
for index, test := range hexTest {
e := Encode.FromBytes([]byte(test.input)).ByHex()
assert.Nil(t, e.Error)
assert.Equal(t, []byte(test.output), e.ToBytes(), "Current test index is "+strconv.Itoa(index))
}
}
func TestDecode_ByHex_ToBytes(t *testing.T) {
for index, test := range hexTest {
d := Decode.FromBytes([]byte(test.output)).ByHex()
assert.Nil(t, d.Error)
assert.Equal(t, []byte(test.input), d.ToBytes(), "Current test index is "+strconv.Itoa(index))
}
}
func TestHex_Ciphertext_Error(t *testing.T) {
d1 := Decode.FromString("xxxxxx").ByHex()
assert.Equal(t, invalidCiphertextError("hex"), d1.Error)
d2 := Decode.FromBytes([]byte("xxxxxx")).ByHex()
assert.Equal(t, invalidCiphertextError("hex"), d2.Error)
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化