Skip to content

Commit

Permalink
fix: add more tests (#2473)
Browse files Browse the repository at this point in the history
* chore: add string to map in httpx parse method

* feat: add httpx parse stringToMap method test

* fix: add more test
  • Loading branch information
pipi-lv committed Sep 30, 2022
1 parent ada2941 commit aa2be01
Showing 1 changed file with 106 additions and 0 deletions.
106 changes: 106 additions & 0 deletions core/mapping/unmarshaler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package mapping

import (
"encoding/json"
"fmt"
"strconv"
"strings"
"testing"
Expand Down Expand Up @@ -467,6 +468,85 @@ func TestUnmarshalIntSliceFromString(t *testing.T) {
ast.Equal(2, v.Values[1])
}

func TestUnmarshalIntMapFromString(t *testing.T) {
var v struct {
Sort map[string]int `key:"sort"`
}
m := map[string]interface{}{
"sort": `{"value":12345,"zeroVal":0,"nullVal":null}`,
}

ast := assert.New(t)
ast.Nil(UnmarshalKey(m, &v))
ast.Equal(3, len(v.Sort))
ast.Equal(12345, v.Sort["value"])
ast.Equal(0, v.Sort["zeroVal"])
ast.Equal(0, v.Sort["nullVal"])
}

func TestUnmarshalBoolMapFromString(t *testing.T) {
var v struct {
Sort map[string]bool `key:"sort"`
}
m := map[string]interface{}{
"sort": `{"value":true,"zeroVal":false,"nullVal":null}`,
}

ast := assert.New(t)
ast.Nil(UnmarshalKey(m, &v))
ast.Equal(3, len(v.Sort))
ast.Equal(true, v.Sort["value"])
ast.Equal(false, v.Sort["zeroVal"])
ast.Equal(false, v.Sort["nullVal"])
}

type CustomStringer string

type UnsupportedStringer string

func (c CustomStringer) String() string {
return fmt.Sprintf("{%s}", string(c))
}

func TestUnmarshalStringMapFromStringer(t *testing.T) {
var v struct {
Sort map[string]string `key:"sort"`
}
m := map[string]interface{}{
"sort": CustomStringer(`"value":"ascend","emptyStr":""`),
}

ast := assert.New(t)
ast.Nil(UnmarshalKey(m, &v))
ast.Equal(2, len(v.Sort))
ast.Equal("ascend", v.Sort["value"])
ast.Equal("", v.Sort["emptyStr"])
}

func TestUnmarshalStringMapFromUnsupportedType(t *testing.T) {
var v struct {
Sort map[string]string `key:"sort"`
}
m := map[string]interface{}{
"sort": UnsupportedStringer(`{"value":"ascend","emptyStr":""}`),
}

ast := assert.New(t)
ast.NotNil(UnmarshalKey(m, &v))
}

func TestUnmarshalStringMapFromNotSettableValue(t *testing.T) {
var v struct {
sort map[string]string `key:"sort"`
}
m := map[string]interface{}{
"sort": `{"value":"ascend","emptyStr":""}`,
}

ast := assert.New(t)
ast.NotNil(UnmarshalKey(m, &v))
}

func TestUnmarshalStringMapFromString(t *testing.T) {
var v struct {
Sort map[string]string `key:"sort"`
Expand All @@ -482,6 +562,32 @@ func TestUnmarshalStringMapFromString(t *testing.T) {
ast.Equal("", v.Sort["emptyStr"])
}

func TestUnmarshalStructMapFromString(t *testing.T) {
var v struct {
Filter map[string]struct {
Field1 bool `json:"field1"`
Field2 int64 `json:"field2,string"`
Field3 string `json:"field3"`
Field4 *string `json:"field4"`
Field5 []string `json:"field5"`
} `key:"filter"`
}
m := map[string]interface{}{
"filter": `{"obj":{"field1":true,"field2":"1573570455447539712","field3":"this is a string",
"field4":"this is a string pointer","field5":["str1","str2"]}}`,
}

ast := assert.New(t)
ast.Nil(UnmarshalKey(m, &v))
ast.Equal(1, len(v.Filter))
ast.NotNil(v.Filter["obj"])
ast.Equal(true, v.Filter["obj"].Field1)
ast.Equal(int64(1573570455447539712), v.Filter["obj"].Field2)
ast.Equal("this is a string", v.Filter["obj"].Field3)
ast.Equal("this is a string pointer", *v.Filter["obj"].Field4)
ast.ElementsMatch([]string{"str1", "str2"}, v.Filter["obj"].Field5)
}

func TestUnmarshalStringSliceMapFromString(t *testing.T) {
var v struct {
Filter map[string][]string `key:"filter"`
Expand Down

0 comments on commit aa2be01

Please sign in to comment.