From 88088fd3b494fe6d16eb24e309727716a4ef60e3 Mon Sep 17 00:00:00 2001 From: trobro Date: Sun, 4 Sep 2022 17:49:30 +0200 Subject: [PATCH] Test type aliases (#48) --- encode_test.go | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/encode_test.go b/encode_test.go index 09df035..8b23b4a 100644 --- a/encode_test.go +++ b/encode_test.go @@ -396,6 +396,8 @@ func (s TestMarshalStruct) MarshalJSON() ([]byte, error) { }`), nil } +type TestMarshalAlias TestMarshalStruct + func TestEncodeMarshalJSON(t *testing.T) { input := TestMarshalStruct{} expected1 := `{ @@ -424,6 +426,23 @@ func TestEncodeMarshalJSON(t *testing.T) { t.Errorf("Expected:\n%s\nGot:\n%s\n\n", expected1, string(buf)) } + inputAlias := TestMarshalAlias(input) + buf, err = Marshal(inputAlias) + if err != nil { + t.Error(err) + } + if string(buf) == expected1 { + t.Error("Used interface on underlying type, even though the alias type doesn't implement it.") + } + + buf, err = Marshal(&inputAlias) + if err != nil { + t.Error(err) + } + if string(buf) == expected1 { + t.Error("Used interface on underlying type, even though the alias type doesn't implement it.") + } + myMap := map[string]interface{}{ "Zero": -0, "A": "FirstField", @@ -463,6 +482,8 @@ func TestEncodeMarshalJSON(t *testing.T) { } } +type myNet net.IP + func TestEncodeMarshalText(t *testing.T) { input := net.ParseIP("127.0.0.1") buf, err := Marshal(input) @@ -472,6 +493,7 @@ func TestEncodeMarshalText(t *testing.T) { if !reflect.DeepEqual(buf, []byte(`127.0.0.1`)) { t.Errorf("Expected '127.0.0.1', got '%s'", string(buf)) } + buf, err = Marshal(&input) if err != nil { t.Error(err) @@ -479,6 +501,23 @@ func TestEncodeMarshalText(t *testing.T) { if !reflect.DeepEqual(buf, []byte(`127.0.0.1`)) { t.Errorf("Expected '127.0.0.1', got '%s'", string(buf)) } + + myInput := myNet(input) + buf, err = Marshal(myInput) + if err != nil { + t.Error(err) + } + if reflect.DeepEqual(buf, []byte(`127.0.0.1`)) { + t.Error("Used interface on underlying type, even though the alias type doesn't implement it.") + } + + buf, err = Marshal(&myInput) + if err != nil { + t.Error(err) + } + if reflect.DeepEqual(buf, []byte(`127.0.0.1`)) { + t.Error("Used interface on underlying type, even though the alias type doesn't implement it.") + } } type marshallerStruct struct {