Skip to content

Commit

Permalink
fix: add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
liuq19 committed Nov 22, 2024
1 parent 285504e commit 5a6fb2f
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 9 deletions.
47 changes: 47 additions & 0 deletions decoder/decoder_native_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,53 @@ func TestDecoder_OptionCaseSensitive(t *testing.T) {
}


func TestDecoder_MapWithIndirectElement(t *testing.T) {
var v map[string]struct { A [129]byte }
_, err := decode(`{"":{"A":[1,2,3,4,5]}}`, &v, false)
require.NoError(t, err)
assert.Equal(t, [129]byte{1, 2, 3, 4, 5}, v[""].A)
}

func TestDecoder_OptionCaseSensitiveForManyKeys(t *testing.T) {
var js = `{"a":1,"b":2,"C":3,"DD":4,"eE":5,"fF":6,"G":7,"H":8,"I":9,"J":10,"K":11,"L":12,"M":13}`
type TS struct{
A int
B int
C int `json:"c"`
Dd int `json:"dd"`
Ee int `json:"ee"`
Ff int `json:"Ff"`
G int `json:"g"`
H int `json:"h"`
I int `json:"i"`
J int `json:"j"`
K int `json:"k"`
L int `json:"l"`
M int `json:"m"`
}

{
var obj = TS{}
err := json.Unmarshal([]byte(js), &obj)
require.NoError(t, err)

var obj2 = TS{}
d := NewDecoder(js)
// d.SetOptions(OptionCaseSensitive)
err2 := d.Decode(&obj2)
require.NoError(t, err2)
require.Equal(t, obj, obj2)
}

var obj = TS{}
d := NewDecoder(js)
d.SetOptions(OptionCaseSensitive)
err := d.Decode(&obj)
require.NoError(t, err)
require.Equal(t, TS{}, obj)
}


func BenchmarkSkipValidate(b *testing.B) {
type skiptype struct {
A int `json:"a"` // mismatched
Expand Down
9 changes: 0 additions & 9 deletions decoder/decoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestMain(m *testing.M) {
Expand Down Expand Up @@ -287,14 +286,6 @@ func TestDecoder_Binding(t *testing.T) {
assert.Equal(t, _BindingValue, v, 0)
}


func TestDecoder_MapWithIndirectElement(t *testing.T) {
var v map[string]struct { A [129]byte }
_, err := decode(`{"":{"A":[1,2,3,4,5]}}`, &v, false)
require.NoError(t, err)
assert.Equal(t, [129]byte{1, 2, 3, 4, 5}, v[""].A)
}

func BenchmarkDecoder_Generic_Sonic(b *testing.B) {
var w interface{}
_, _ = decode(TwitterJson, &w, true)
Expand Down

0 comments on commit 5a6fb2f

Please sign in to comment.