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