Skip to content

Commit

Permalink
Test type aliases (#48)
Browse files Browse the repository at this point in the history
  • Loading branch information
trobro authored Sep 4, 2022
1 parent b1f6751 commit 88088fd
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions encode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,8 @@ func (s TestMarshalStruct) MarshalJSON() ([]byte, error) {
}`), nil
}

type TestMarshalAlias TestMarshalStruct

func TestEncodeMarshalJSON(t *testing.T) {
input := TestMarshalStruct{}
expected1 := `{
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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)
Expand All @@ -472,13 +493,31 @@ 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)
}
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 {
Expand Down

0 comments on commit 88088fd

Please sign in to comment.