utils: Add tests for CB58.UnmarshalJSON

This commit is contained in:
Alex Willmer 2020-04-01 18:48:59 +01:00
parent e8c6188b27
commit d90baa383c
1 changed files with 53 additions and 0 deletions

View File

@ -26,6 +26,59 @@ func TestCB58Single(t *testing.T) {
}
}
func TestCB58UnmarshalJSON(t *testing.T) {
expected := CB58{[]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 255}}
cb58 := CB58{}
err := cb58.UnmarshalJSON([]byte("\"1NVSVezva3bAtJesnUj\""))
if err != nil {
t.Fatalf("CB58.UnmarshalJSON unexpected error unmarshalling: %s", err)
} else if !bytes.Equal(cb58.Bytes, expected.Bytes) {
t.Fatalf("CB58.UnmarshalJSON got 0x%x, expected 0x%x", cb58, expected)
}
}
func TestCB58UnmarshalJSONNull(t *testing.T) {
cb58 := CB58{}
err := cb58.UnmarshalJSON([]byte("null"))
if err != nil {
t.Fatalf("CB58.UnmarshalJSON unexpected error unmarshalling null: %s", err)
}
}
func TestCB58UnmarshalJSONError(t *testing.T) {
tests := []struct {
in string
expected error
}{
{"", errMissingQuotes},
{"\"foo", errMissingQuotes},
{"foo", errMissingQuotes},
{"foo\"", errMissingQuotes},
{"\"foo\"", errMissingChecksum},
{"\"foobar\"", errBadChecksum},
}
cb58 := CB58{}
for _, tt := range tests {
t.Run(tt.in, func(t *testing.T) {
err := cb58.UnmarshalJSON([]byte(tt.in))
if err != tt.expected {
t.Errorf("got error %q, expected error %q", err, tt.expected)
}
})
}
}
func TestCB58MarshalJSONError(t *testing.T) {
cb58 := CB58{[]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 255}}
expected := []byte("\"1NVSVezva3bAtJesnUj\"")
result, err := cb58.MarshalJSON()
if err != nil {
t.Fatalf("CB58.MarshalJSON unexpected error: %s", err)
} else if !bytes.Equal(result, expected) {
t.Fatalf("CB58.MarshalJSON got %q, expected %q", result, expected)
}
}
func TestCB58ParseBytes(t *testing.T) {
ui := "1NVSVezva3bAtJesnUj"
cb58 := CB58{}