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

     1  package jzon
     2  
     3  import (
     4  	"io"
     5  	"testing"
     6  )
     7  
     8  func TestValDecoder_Native_Bool(t *testing.T) {
     9  	f := func(t *testing.T, data string, ex error, initValue, initBool bool) {
    10  		var p1 *bool
    11  		var p2 *bool
    12  		if initValue {
    13  			b1 := initBool
    14  			p1 = &b1
    15  			b2 := initBool
    16  			p2 = &b2
    17  		}
    18  		checkDecodeWithStandard(t, DefaultDecoderConfig, data, ex, p1, p2)
    19  	}
    20  	f2 := func(t *testing.T, data string, ex error) {
    21  		f(t, data, ex, true, true)
    22  	}
    23  	f3 := func(t *testing.T, data string, ex error) {
    24  		f(t, data, ex, true, false)
    25  	}
    26  	t.Run("nil pointer", func(t *testing.T) {
    27  		f(t, "null", ErrNilPointerReceiver, false, false)
    28  	})
    29  	t.Run("eof", func(t *testing.T) {
    30  		f2(t, "", io.EOF)
    31  	})
    32  	t.Run("invalid first byte", func(t *testing.T) {
    33  		f2(t, `+`, UnexpectedByteError{})
    34  	})
    35  	t.Run("invalid null", func(t *testing.T) {
    36  		f2(t, "nul", io.EOF)
    37  	})
    38  	t.Run("null", func(t *testing.T) {
    39  		f2(t, "null", nil)
    40  	})
    41  	t.Run("invalid true", func(t *testing.T) {
    42  		t.Run("init true", func(t *testing.T) {
    43  			f2(t, `tru`, io.EOF)
    44  		})
    45  		t.Run("init false", func(t *testing.T) {
    46  			f3(t, `tru`, io.EOF)
    47  		})
    48  	})
    49  	t.Run("valid true", func(t *testing.T) {
    50  		t.Run("init true", func(t *testing.T) {
    51  			f2(t, `true`, nil)
    52  		})
    53  		t.Run("init false", func(t *testing.T) {
    54  			f3(t, `true`, nil)
    55  		})
    56  	})
    57  	t.Run("invalid false", func(t *testing.T) {
    58  		t.Run("init true", func(t *testing.T) {
    59  			f2(t, `fals`, io.EOF)
    60  		})
    61  	})
    62  	t.Run("valid false", func(t *testing.T) {
    63  		t.Run("init true", func(t *testing.T) {
    64  			f2(t, `false`, nil)
    65  		})
    66  	})
    67  }
    68  
    69  func TestValDecoder_Native_String(t *testing.T) {
    70  	f := func(t *testing.T, data string, ex error, initValue string) {
    71  		var p1 *string
    72  		var p2 *string
    73  		if initValue != "" {
    74  			b1 := initValue
    75  			p1 = &b1
    76  			b2 := initValue
    77  			p2 = &b2
    78  		}
    79  		checkDecodeWithStandard(t, DefaultDecoderConfig, data, ex, p1, p2)
    80  	}
    81  	f2 := func(t *testing.T, data string, ex error) {
    82  		f(t, data, ex, "dummy")
    83  	}
    84  	t.Run("nil pointer", func(t *testing.T) {
    85  		f(t, "null", ErrNilPointerReceiver, "")
    86  	})
    87  	t.Run("eof", func(t *testing.T) {
    88  		f2(t, "", io.EOF)
    89  	})
    90  	t.Run("invalid first byte", func(t *testing.T) {
    91  		f2(t, `true`, UnexpectedByteError{})
    92  	})
    93  	t.Run("invalid null", func(t *testing.T) {
    94  		f2(t, "nul", io.EOF)
    95  	})
    96  	t.Run("null", func(t *testing.T) {
    97  		f2(t, "null", nil)
    98  	})
    99  	t.Run("invalid string", func(t *testing.T) {
   100  		f2(t, `"abc`, io.EOF)
   101  	})
   102  	t.Run("valid string", func(t *testing.T) {
   103  		f2(t, `"abc"`, nil)
   104  	})
   105  }