Files
rootandClaude Opus 5 8679200f41 Initial commit
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-15 13:57:10 +08:00

62 lines
1.3 KiB
Go

package rchgutil
import (
"encoding/json"
"testing"
"github.com/vmihailenco/msgpack/v5"
)
func TestDecodeCachedPayTypes(t *testing.T) {
want := []AllPayType{{
Money: "100",
Types: []mercPayTypeInfo{{Name: "支付宝", Type: "alipay"}},
}}
tests := []struct {
name string
data func(t *testing.T) []byte
}{
{
name: "json",
data: func(t *testing.T) []byte {
t.Helper()
data, err := json.Marshal(want)
if err != nil {
t.Fatal(err)
}
return data
},
},
{
name: "legacy msgpack",
data: func(t *testing.T) []byte {
t.Helper()
data, err := msgpack.Marshal(want)
if err != nil {
t.Fatal(err)
}
return data
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := decodeCachedPayTypes(string(tt.data(t)))
if err != nil {
t.Fatalf("decodeCachedPayTypes() error = %v", err)
}
if len(got) != 1 || got[0].Money != want[0].Money || len(got[0].Types) != 1 || got[0].Types[0] != want[0].Types[0] {
t.Fatalf("decodeCachedPayTypes() = %#v, want %#v", got, want)
}
})
}
}
func TestDecodeCachedPayTypesRejectsInvalidData(t *testing.T) {
if _, err := decodeCachedPayTypes("not-json-or-msgpack"); err == nil {
t.Fatal("decodeCachedPayTypes() error = nil, want decode error")
}
}