github.com/zerosnake0/jzon@v0.0.9-0.20230801092939-1b135cb83f7f/val_decoder_native_map_test.go (about)

     1  package jzon
     2  
     3  import (
     4  	"io"
     5  	"runtime/debug"
     6  	"testing"
     7  
     8  	"github.com/stretchr/testify/require"
     9  )
    10  
    11  func TestValDecoder_Native_Map(t *testing.T) {
    12  	f := func(t *testing.T, data string, ex error, p1, p2 interface{}) {
    13  		checkDecodeWithStandard(t, DefaultDecoderConfig, data, ex, p1, p2)
    14  	}
    15  	t.Run("nil pointer", func(t *testing.T) {
    16  		f(t, "null", ErrNilPointerReceiver, nil, nil)
    17  	})
    18  	t.Run("eof", func(t *testing.T) {
    19  		m1 := map[string]int{"a": 1}
    20  		m2 := map[string]int{"a": 1}
    21  		f(t, "", io.EOF, &m1, &m2)
    22  	})
    23  	t.Run("invalid null", func(t *testing.T) {
    24  		m1 := map[string]int{"a": 1}
    25  		m2 := map[string]int{"a": 1}
    26  		f(t, "nul", io.EOF, &m1, &m2)
    27  	})
    28  	t.Run("null", func(t *testing.T) {
    29  		m1 := map[string]int{"a": 1}
    30  		m2 := map[string]int{"a": 1}
    31  		f(t, "null", nil, &m1, &m2)
    32  	})
    33  	t.Run("invalid first byte", func(t *testing.T) {
    34  		m1 := map[string]int{"a": 1}
    35  		m2 := map[string]int{"a": 1}
    36  		f(t, " } ", UnexpectedByteError{}, &m1, &m2)
    37  	})
    38  	t.Run("eof after bracket", func(t *testing.T) {
    39  		m1 := map[string]int{"a": 1}
    40  		m2 := map[string]int{"a": 1}
    41  		f(t, " { ", io.EOF, &m1, &m2)
    42  	})
    43  	t.Run("empty", func(t *testing.T) {
    44  		m1 := map[string]int{"a": 1}
    45  		m2 := map[string]int{"a": 1}
    46  		f(t, " { } ", nil, &m1, &m2)
    47  	})
    48  	t.Run("empty on nil", func(t *testing.T) {
    49  		var m1 map[string]int
    50  		var m2 map[string]int
    51  		f(t, " { } ", nil, &m1, &m2)
    52  	})
    53  	t.Run("value on nil", func(t *testing.T) {
    54  		var m1 map[string]int
    55  		var m2 map[string]int
    56  		f(t, ` { "a" : 1 } `, nil, &m1, &m2)
    57  	})
    58  	t.Run("bad key", func(t *testing.T) {
    59  		m1 := map[string]int{"a": 1}
    60  		m2 := map[string]int{"a": 1}
    61  		f(t, ` { "a`, io.EOF, &m1, &m2)
    62  	})
    63  	t.Run("eof after key", func(t *testing.T) {
    64  		m1 := map[string]int{"a": 1}
    65  		m2 := map[string]int{"a": 1}
    66  		f(t, ` { "a" `, io.EOF, &m1, &m2)
    67  	})
    68  	t.Run("invalid colon", func(t *testing.T) {
    69  		m1 := map[string]int{"a": 1}
    70  		m2 := map[string]int{"a": 1}
    71  		f(t, ` { "a" } `, UnexpectedByteError{}, &m1, &m2)
    72  	})
    73  	t.Run("bad value", func(t *testing.T) {
    74  		m1 := map[string]int{"a": 1}
    75  		m2 := map[string]int{"a": 1}
    76  		f(t, ` { "b" : "c" } `, InvalidDigitError{}, &m1, &m2)
    77  	})
    78  	t.Run("eof after value", func(t *testing.T) {
    79  		m1 := map[string]int{"a": 1}
    80  		m2 := map[string]int{"a": 1}
    81  		f(t, ` { "b" : 2 `, io.EOF, &m1, &m2)
    82  	})
    83  	t.Run("bad comma", func(t *testing.T) {
    84  		m1 := map[string]int{"a": 1}
    85  		m2 := map[string]int{"a": 1}
    86  		f(t, ` { "b" : 2 { `, UnexpectedByteError{}, &m1, &m2)
    87  	})
    88  	t.Run("more items", func(t *testing.T) {
    89  		m1 := map[string]int{"a": 1}
    90  		m2 := map[string]int{"a": 1}
    91  		f(t, ` { "b" : 2 , "c" : 3 } `, nil, &m1, &m2)
    92  	})
    93  	debug.FreeOSMemory()
    94  }
    95  
    96  type testMapIntKey int
    97  
    98  func (k *testMapIntKey) UnmarshalText(data []byte) error {
    99  	*k = testMapIntKey(len(data))
   100  	return nil
   101  }
   102  
   103  type testMapStringKey string
   104  
   105  func (k *testMapStringKey) UnmarshalText(data []byte) error {
   106  	*k = testMapStringKey("`" + string(data) + "`")
   107  	return nil
   108  }
   109  
   110  func TestValDecoder_Native_Map_KeyDecoder_TextUnmarshaler(t *testing.T) {
   111  	f := func(t *testing.T, data string, ex error, p1, p2 interface{}) {
   112  		checkDecodeWithStandard(t, DefaultDecoderConfig, data, ex, p1, p2)
   113  	}
   114  	t.Run("not supported", func(t *testing.T) {
   115  		type key testTextUnmarshaler
   116  		m1 := map[key]int{{
   117  			data: "a",
   118  		}: 1}
   119  		m2 := map[key]int{{
   120  			data: "a",
   121  		}: 1}
   122  		f(t, ` { "b" : 2 } `, TypeNotSupportedError(""), &m1, &m2)
   123  	})
   124  	t.Run("string", func(t *testing.T) {
   125  		type key = testTextUnmarshaler
   126  		m1 := map[key]int{{
   127  			data: "a",
   128  		}: 1}
   129  		m2 := map[key]int{{
   130  			data: "a",
   131  		}: 1}
   132  		f(t, ` { "b" : 2 } `, nil, &m1, &m2)
   133  	})
   134  	t.Run("int key", func(t *testing.T) {
   135  		m1 := map[testMapIntKey]testMapIntKey{testMapIntKey(1): 2}
   136  		m2 := map[testMapIntKey]testMapIntKey{testMapIntKey(1): 2}
   137  		f(t, ` { "3" : "4" } `, nil, &m1, &m2)
   138  	})
   139  	t.Run("string key", func(t *testing.T) {
   140  		// the UnmarshalText of the key is ignored
   141  		m1 := map[testMapStringKey]testMapStringKey{testMapStringKey("1"): "2"}
   142  		m2 := map[testMapStringKey]testMapStringKey{testMapStringKey("1"): "2"}
   143  		v := "go1.13.15"
   144  		if goVersion.LessEqual(v) {
   145  			err := Unmarshal([]byte(` { "3" : "4" } `), &m2)
   146  			require.NoError(t, err)
   147  			require.Equal(t, testMapStringKey("`4`"), m2["`3`"])
   148  		} else {
   149  			f(t, ` { "3" : "4" } `, nil, &m1, &m2)
   150  		}
   151  	})
   152  }
   153  
   154  func TestValDecoder_Native_Map_KeyDecoder_String(t *testing.T) {
   155  	f := func(t *testing.T, data string, ex error, p1, p2 interface{}) {
   156  		checkDecodeWithStandard(t, DefaultDecoderConfig, data, ex, p1, p2)
   157  	}
   158  	t.Run("string", func(t *testing.T) {
   159  		type key string
   160  		m1 := map[key]int{"a": 1}
   161  		m2 := map[key]int{"a": 1}
   162  		f(t, ` { "b" : 2 } `, nil, &m1, &m2)
   163  	})
   164  	t.Run("string 2", func(t *testing.T) {
   165  		type key string
   166  		m1 := map[key]int{"a": 1}
   167  		m2 := map[key]int{"a": 1}
   168  		f(t, ` { "\"" : 2 } `, nil, &m1, &m2)
   169  	})
   170  }
   171  
   172  func TestValDecoder_Native_Map_KeyDecoder_Int(t *testing.T) {
   173  	f := func(t *testing.T, data string, ex error, p1, p2 interface{}) {
   174  		checkDecodeWithStandard(t, DefaultDecoderConfig, data, ex, p1, p2)
   175  	}
   176  	t.Run("int8", func(t *testing.T) {
   177  		type key int8
   178  		t.Run("invalid", func(t *testing.T) {
   179  			m1 := map[key]int{1: 2}
   180  			m2 := map[key]int{1: 2}
   181  			f(t, ` { "b" : 3 } `, InvalidDigitError{}, &m1, &m2)
   182  		})
   183  		t.Run("no leading quote", func(t *testing.T) {
   184  			m1 := map[key]int{1: 2}
   185  			m2 := map[key]int{1: 2}
   186  			f(t, ` { 2`, UnexpectedByteError{}, &m1, &m2)
   187  		})
   188  		t.Run("no trimming quote", func(t *testing.T) {
   189  			m1 := map[key]int{1: 2}
   190  			m2 := map[key]int{1: 2}
   191  			f(t, ` { "2`, io.EOF, &m1, &m2)
   192  		})
   193  		t.Run("overflow", func(t *testing.T) {
   194  			m1 := map[key]int{1: 2}
   195  			m2 := map[key]int{1: 2}
   196  			f(t, ` { "128" : 3 } `, IntOverflowError{}, &m1, &m2)
   197  		})
   198  		t.Run("leading space", func(t *testing.T) {
   199  			m1 := map[key]int{1: 2}
   200  			m2 := map[key]int{1: 2}
   201  			f(t, ` { " 2" : 3 } `, InvalidDigitError{}, &m1, &m2)
   202  		})
   203  		t.Run("trailing space", func(t *testing.T) {
   204  			m1 := map[key]int{1: 2}
   205  			m2 := map[key]int{1: 2}
   206  			f(t, ` { "2 " : 3 } `, UnexpectedByteError{}, &m1, &m2)
   207  		})
   208  		t.Run("valid", func(t *testing.T) {
   209  			m1 := map[key]int{1: 2}
   210  			m2 := map[key]int{1: 2}
   211  			f(t, ` { "2" : 3 } `, nil, &m1, &m2)
   212  		})
   213  	})
   214  	t.Run("int16", func(t *testing.T) {
   215  		type key int16
   216  		t.Run("invalid", func(t *testing.T) {
   217  			m1 := map[key]int{1: 2}
   218  			m2 := map[key]int{1: 2}
   219  			f(t, ` { "b" : 3 } `, InvalidDigitError{}, &m1, &m2)
   220  		})
   221  		t.Run("no leading quote", func(t *testing.T) {
   222  			m1 := map[key]int{1: 2}
   223  			m2 := map[key]int{1: 2}
   224  			f(t, ` { 2`, UnexpectedByteError{}, &m1, &m2)
   225  		})
   226  		t.Run("no trimming quote", func(t *testing.T) {
   227  			m1 := map[key]int{1: 2}
   228  			m2 := map[key]int{1: 2}
   229  			f(t, ` { "2`, io.EOF, &m1, &m2)
   230  		})
   231  		t.Run("overflow", func(t *testing.T) {
   232  			m1 := map[key]int{1: 2}
   233  			m2 := map[key]int{1: 2}
   234  			f(t, ` { "32768" : 3 } `, IntOverflowError{}, &m1, &m2)
   235  		})
   236  		t.Run("leading space", func(t *testing.T) {
   237  			m1 := map[key]int{1: 2}
   238  			m2 := map[key]int{1: 2}
   239  			f(t, ` { " 2" : 3 } `, InvalidDigitError{}, &m1, &m2)
   240  		})
   241  		t.Run("trailing space", func(t *testing.T) {
   242  			m1 := map[key]int{1: 2}
   243  			m2 := map[key]int{1: 2}
   244  			f(t, ` { "2 " : 3 } `, UnexpectedByteError{}, &m1, &m2)
   245  		})
   246  		t.Run("valid", func(t *testing.T) {
   247  			m1 := map[key]int{1: 2}
   248  			m2 := map[key]int{1: 2}
   249  			f(t, ` { "2" : 3 } `, nil, &m1, &m2)
   250  		})
   251  	})
   252  	t.Run("int32", func(t *testing.T) {
   253  		type key int32
   254  		t.Run("invalid", func(t *testing.T) {
   255  			m1 := map[key]int{1: 2}
   256  			m2 := map[key]int{1: 2}
   257  			f(t, ` { "b" : 3 } `, InvalidDigitError{}, &m1, &m2)
   258  		})
   259  		t.Run("no leading quote", func(t *testing.T) {
   260  			m1 := map[key]int{1: 2}
   261  			m2 := map[key]int{1: 2}
   262  			f(t, ` { 2`, UnexpectedByteError{}, &m1, &m2)
   263  		})
   264  		t.Run("no trimming quote", func(t *testing.T) {
   265  			m1 := map[key]int{1: 2}
   266  			m2 := map[key]int{1: 2}
   267  			f(t, ` { "2`, io.EOF, &m1, &m2)
   268  		})
   269  		t.Run("overflow", func(t *testing.T) {
   270  			m1 := map[key]int{1: 2}
   271  			m2 := map[key]int{1: 2}
   272  			f(t, ` { "2147483649" : 3 } `, IntOverflowError{}, &m1, &m2)
   273  		})
   274  		t.Run("leading space", func(t *testing.T) {
   275  			m1 := map[key]int{1: 2}
   276  			m2 := map[key]int{1: 2}
   277  			f(t, ` { " 2" : 3 } `, InvalidDigitError{}, &m1, &m2)
   278  		})
   279  		t.Run("trailing space", func(t *testing.T) {
   280  			m1 := map[key]int{1: 2}
   281  			m2 := map[key]int{1: 2}
   282  			f(t, ` { "2 " : 3 } `, UnexpectedByteError{}, &m1, &m2)
   283  		})
   284  		t.Run("valid", func(t *testing.T) {
   285  			m1 := map[key]int{1: 2}
   286  			m2 := map[key]int{1: 2}
   287  			f(t, ` { "2" : 3 } `, nil, &m1, &m2)
   288  		})
   289  	})
   290  	t.Run("int64", func(t *testing.T) {
   291  		type key int64
   292  		t.Run("invalid", func(t *testing.T) {
   293  			m1 := map[key]int{1: 2}
   294  			m2 := map[key]int{1: 2}
   295  			f(t, ` { "b" : 3 } `, InvalidDigitError{}, &m1, &m2)
   296  		})
   297  		t.Run("no leading quote", func(t *testing.T) {
   298  			m1 := map[key]int{1: 2}
   299  			m2 := map[key]int{1: 2}
   300  			f(t, ` { 2`, UnexpectedByteError{}, &m1, &m2)
   301  		})
   302  		t.Run("no trimming quote", func(t *testing.T) {
   303  			m1 := map[key]int{1: 2}
   304  			m2 := map[key]int{1: 2}
   305  			f(t, ` { "2`, io.EOF, &m1, &m2)
   306  		})
   307  		t.Run("overflow", func(t *testing.T) {
   308  			m1 := map[key]int{1: 2}
   309  			m2 := map[key]int{1: 2}
   310  			f(t, ` { "9223372036854775808" : 3 } `, IntOverflowError{}, &m1, &m2)
   311  		})
   312  		t.Run("leading space", func(t *testing.T) {
   313  			m1 := map[key]int{1: 2}
   314  			m2 := map[key]int{1: 2}
   315  			f(t, ` { " 2" : 3 } `, InvalidDigitError{}, &m1, &m2)
   316  		})
   317  		t.Run("trailing space", func(t *testing.T) {
   318  			m1 := map[key]int{1: 2}
   319  			m2 := map[key]int{1: 2}
   320  			f(t, ` { "2 " : 3 } `, UnexpectedByteError{}, &m1, &m2)
   321  		})
   322  		t.Run("valid", func(t *testing.T) {
   323  			m1 := map[key]int{1: 2}
   324  			m2 := map[key]int{1: 2}
   325  			f(t, ` { "2" : 3 } `, nil, &m1, &m2)
   326  		})
   327  	})
   328  }
   329  
   330  func TestValDecoder_Native_Map_KeyDecoder_Uint(t *testing.T) {
   331  	f := func(t *testing.T, data string, ex error, p1, p2 interface{}) {
   332  		checkDecodeWithStandard(t, DefaultDecoderConfig, data, ex, p1, p2)
   333  	}
   334  	t.Run("uint8", func(t *testing.T) {
   335  		type key uint8
   336  		t.Run("invalid", func(t *testing.T) {
   337  			m1 := map[key]uint{1: 2}
   338  			m2 := map[key]uint{1: 2}
   339  			f(t, ` { "b" : 3 } `, InvalidDigitError{}, &m1, &m2)
   340  		})
   341  		t.Run("no leading quote", func(t *testing.T) {
   342  			m1 := map[key]uint{1: 2}
   343  			m2 := map[key]uint{1: 2}
   344  			f(t, ` { 2`, UnexpectedByteError{}, &m1, &m2)
   345  		})
   346  		t.Run("no trimming quote", func(t *testing.T) {
   347  			m1 := map[key]uint{1: 2}
   348  			m2 := map[key]uint{1: 2}
   349  			f(t, ` { "2`, io.EOF, &m1, &m2)
   350  		})
   351  		t.Run("overflow", func(t *testing.T) {
   352  			m1 := map[key]uint{1: 2}
   353  			m2 := map[key]uint{1: 2}
   354  			f(t, ` { "256" : 3 } `, IntOverflowError{}, &m1, &m2)
   355  		})
   356  		t.Run("leading space", func(t *testing.T) {
   357  			m1 := map[key]uint{1: 2}
   358  			m2 := map[key]uint{1: 2}
   359  			f(t, ` { " 2" : 3 } `, InvalidDigitError{}, &m1, &m2)
   360  		})
   361  		t.Run("trailing space", func(t *testing.T) {
   362  			m1 := map[key]uint{1: 2}
   363  			m2 := map[key]uint{1: 2}
   364  			f(t, ` { "2 " : 3 } `, UnexpectedByteError{}, &m1, &m2)
   365  		})
   366  		t.Run("valid", func(t *testing.T) {
   367  			m1 := map[key]uint{1: 2}
   368  			m2 := map[key]uint{1: 2}
   369  			f(t, ` { "2" : 3 } `, nil, &m1, &m2)
   370  		})
   371  	})
   372  	t.Run("uint16", func(t *testing.T) {
   373  		type key uint16
   374  		t.Run("invalid", func(t *testing.T) {
   375  			m1 := map[key]uint{1: 2}
   376  			m2 := map[key]uint{1: 2}
   377  			f(t, ` { "b" : 3 } `, InvalidDigitError{}, &m1, &m2)
   378  		})
   379  		t.Run("no leading quote", func(t *testing.T) {
   380  			m1 := map[key]uint{1: 2}
   381  			m2 := map[key]uint{1: 2}
   382  			f(t, ` { 2`, UnexpectedByteError{}, &m1, &m2)
   383  		})
   384  		t.Run("no trimming quote", func(t *testing.T) {
   385  			m1 := map[key]uint{1: 2}
   386  			m2 := map[key]uint{1: 2}
   387  			f(t, ` { "2`, io.EOF, &m1, &m2)
   388  		})
   389  		t.Run("overflow", func(t *testing.T) {
   390  			m1 := map[key]uint{1: 2}
   391  			m2 := map[key]uint{1: 2}
   392  			f(t, ` { "65536" : 3 } `, IntOverflowError{}, &m1, &m2)
   393  		})
   394  		t.Run("leading space", func(t *testing.T) {
   395  			m1 := map[key]uint{1: 2}
   396  			m2 := map[key]uint{1: 2}
   397  			f(t, ` { " 2" : 3 } `, InvalidDigitError{}, &m1, &m2)
   398  		})
   399  		t.Run("trailing space", func(t *testing.T) {
   400  			m1 := map[key]uint{1: 2}
   401  			m2 := map[key]uint{1: 2}
   402  			f(t, ` { "2 " : 3 } `, UnexpectedByteError{}, &m1, &m2)
   403  		})
   404  		t.Run("valid", func(t *testing.T) {
   405  			m1 := map[key]uint{1: 2}
   406  			m2 := map[key]uint{1: 2}
   407  			f(t, ` { "2" : 3 } `, nil, &m1, &m2)
   408  		})
   409  	})
   410  	t.Run("uint32", func(t *testing.T) {
   411  		type key uint32
   412  		t.Run("invalid", func(t *testing.T) {
   413  			m1 := map[key]uint{1: 2}
   414  			m2 := map[key]uint{1: 2}
   415  			f(t, ` { "b" : 3 } `, InvalidDigitError{}, &m1, &m2)
   416  		})
   417  		t.Run("no leading quote", func(t *testing.T) {
   418  			m1 := map[key]uint{1: 2}
   419  			m2 := map[key]uint{1: 2}
   420  			f(t, ` { 2`, UnexpectedByteError{}, &m1, &m2)
   421  		})
   422  		t.Run("no trimming quote", func(t *testing.T) {
   423  			m1 := map[key]uint{1: 2}
   424  			m2 := map[key]uint{1: 2}
   425  			f(t, ` { "2`, io.EOF, &m1, &m2)
   426  		})
   427  		t.Run("overflow", func(t *testing.T) {
   428  			m1 := map[key]uint{1: 2}
   429  			m2 := map[key]uint{1: 2}
   430  			f(t, ` { "4294967296" : 3 } `, IntOverflowError{}, &m1, &m2)
   431  		})
   432  		t.Run("leading space", func(t *testing.T) {
   433  			m1 := map[key]uint{1: 2}
   434  			m2 := map[key]uint{1: 2}
   435  			f(t, ` { " 2" : 3 } `, InvalidDigitError{}, &m1, &m2)
   436  		})
   437  		t.Run("trailing space", func(t *testing.T) {
   438  			m1 := map[key]uint{1: 2}
   439  			m2 := map[key]uint{1: 2}
   440  			f(t, ` { "2 " : 3 } `, UnexpectedByteError{}, &m1, &m2)
   441  		})
   442  		t.Run("valid", func(t *testing.T) {
   443  			m1 := map[key]uint{1: 2}
   444  			m2 := map[key]uint{1: 2}
   445  			f(t, ` { "2" : 3 } `, nil, &m1, &m2)
   446  		})
   447  	})
   448  	t.Run("uint64", func(t *testing.T) {
   449  		type key uint64
   450  		t.Run("invalid", func(t *testing.T) {
   451  			m1 := map[key]uint{1: 2}
   452  			m2 := map[key]uint{1: 2}
   453  			f(t, ` { "b" : 3 } `, InvalidDigitError{}, &m1, &m2)
   454  		})
   455  		t.Run("no leading quote", func(t *testing.T) {
   456  			m1 := map[key]uint{1: 2}
   457  			m2 := map[key]uint{1: 2}
   458  			f(t, ` { 2`, UnexpectedByteError{}, &m1, &m2)
   459  		})
   460  		t.Run("no trimming quote", func(t *testing.T) {
   461  			m1 := map[key]uint{1: 2}
   462  			m2 := map[key]uint{1: 2}
   463  			f(t, ` { "2`, io.EOF, &m1, &m2)
   464  		})
   465  		t.Run("overflow", func(t *testing.T) {
   466  			m1 := map[key]uint{1: 2}
   467  			m2 := map[key]uint{1: 2}
   468  			f(t, ` { "18446744073709551616" : 3 } `, IntOverflowError{}, &m1, &m2)
   469  		})
   470  		t.Run("leading space", func(t *testing.T) {
   471  			m1 := map[key]uint{1: 2}
   472  			m2 := map[key]uint{1: 2}
   473  			f(t, ` { " 2" : 3 } `, InvalidDigitError{}, &m1, &m2)
   474  		})
   475  		t.Run("trailing space", func(t *testing.T) {
   476  			m1 := map[key]uint{1: 2}
   477  			m2 := map[key]uint{1: 2}
   478  			f(t, ` { "2 " : 3 } `, UnexpectedByteError{}, &m1, &m2)
   479  		})
   480  		t.Run("valid", func(t *testing.T) {
   481  			m1 := map[key]uint{1: 2}
   482  			m2 := map[key]uint{1: 2}
   483  			f(t, ` { "2" : 3 } `, nil, &m1, &m2)
   484  		})
   485  	})
   486  }