From 5a6fb2fbf3f4a6c20d8104314461bd4539926a64 Mon Sep 17 00:00:00 2001 From: "liuqiang.06" Date: Fri, 22 Nov 2024 16:41:52 +0800 Subject: [PATCH] fix: add more tests --- decoder/decoder_native_test.go | 47 ++++++++++++++++++++++++++++++++++ decoder/decoder_test.go | 9 ------- 2 files changed, 47 insertions(+), 9 deletions(-) diff --git a/decoder/decoder_native_test.go b/decoder/decoder_native_test.go index ebf287ed2..45d326788 100644 --- a/decoder/decoder_native_test.go +++ b/decoder/decoder_native_test.go @@ -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 diff --git a/decoder/decoder_test.go b/decoder/decoder_test.go index c58ed97a5..6da755857 100644 --- a/decoder/decoder_test.go +++ b/decoder/decoder_test.go @@ -26,7 +26,6 @@ import ( "time" "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/require" ) func TestMain(m *testing.M) { @@ -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)