55 lines
1.4 KiB
Go
55 lines
1.4 KiB
Go
package common_test
|
|
|
|
import (
|
|
"encoding/json"
|
|
"testing"
|
|
|
|
"fiskerinc.com/modules/common"
|
|
"fiskerinc.com/modules/testhelper"
|
|
)
|
|
|
|
type testHexBinary struct {
|
|
Data *common.BinaryHex `json:"data"`
|
|
}
|
|
|
|
func TestBinaryHexMarshalJSON(t *testing.T) {
|
|
expected := `"0000ffff"`
|
|
value := common.BinaryHex{00, 00, 0xff, 0xff}
|
|
|
|
json, err := value.MarshalJSON()
|
|
|
|
testhelper.NoError(t, "MarshalJSON error", err)
|
|
testhelper.Equal(t, "MarshalJSON json", expected, string(json))
|
|
}
|
|
|
|
func TestBinaryHexUnmarshalJSON(t *testing.T) {
|
|
expected := []byte{00, 00, 0xff, 0xff}
|
|
value := common.BinaryHex{}
|
|
|
|
err := value.UnmarshalJSON([]byte(`"0000ffff"`))
|
|
|
|
testhelper.NoError(t, "UnmarshalJSON error", err)
|
|
testhelper.EqualByteArray(t, "UnmarshalJSON len", expected, value)
|
|
}
|
|
|
|
func TestBinaryHexStructMarshalJSON(t *testing.T) {
|
|
expected := `{"data":"0000ffff"}`
|
|
value := testHexBinary{Data: &common.BinaryHex{00, 00, 0xff, 0xff}}
|
|
|
|
json, err := json.Marshal(value)
|
|
|
|
testhelper.NoError(t, "MarshalJSON error", err)
|
|
testhelper.Equal(t, "MarshalJSON json", expected, string(json))
|
|
}
|
|
|
|
func TestBinaryHexStructUnmarshalJSON(t *testing.T) {
|
|
obj := testHexBinary{}
|
|
data := `{"data":"0e00ffff"}`
|
|
expected := []byte{0x0e, 00, 0xff, 0xff}
|
|
|
|
err := json.Unmarshal([]byte(data), &obj)
|
|
|
|
testhelper.NoError(t, "UnmarshalJSON error", err)
|
|
testhelper.EqualByteArray(t, "UnmarshalJSON json", *obj.Data, expected)
|
|
}
|