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

     1  package jzon
     2  
     3  import (
     4  	"encoding/json"
     5  	"io"
     6  	"testing"
     7  )
     8  
     9  func TestValDecoder_JsonNumber(t *testing.T) {
    10  	f := func(t *testing.T, data string, ex error, initValue json.Number) {
    11  		var p1 *json.Number
    12  		var p2 *json.Number
    13  		if initValue != "" {
    14  			b1 := initValue
    15  			p1 = &b1
    16  			b2 := initValue
    17  			p2 = &b2
    18  		}
    19  		checkDecodeWithStandard(t, DefaultDecoderConfig, data, ex, p1, p2)
    20  	}
    21  	f2 := func(t *testing.T, data string, ex error) {
    22  		f(t, data, ex, "1.23")
    23  	}
    24  	t.Run("nil pointer", func(t *testing.T) {
    25  		f(t, "null", ErrNilPointerReceiver, "")
    26  	})
    27  	t.Run("eof", func(t *testing.T) {
    28  		f2(t, "", io.EOF)
    29  	})
    30  	t.Run("invalid first byte", func(t *testing.T) {
    31  		f2(t, `+`, UnexpectedByteError{})
    32  	})
    33  	t.Run("invalid null", func(t *testing.T) {
    34  		f2(t, "nul", io.EOF)
    35  	})
    36  	t.Run("null", func(t *testing.T) {
    37  		f2(t, "null", nil)
    38  	})
    39  	t.Run("invalid string", func(t *testing.T) {
    40  		f2(t, `"-123.2e+1`, io.EOF)
    41  	})
    42  	t.Run("leading space", func(t *testing.T) {
    43  		v := "go1.13.15"
    44  		if goVersion.LessEqual(v) {
    45  			var n json.Number
    46  			err := Unmarshal([]byte(`" 1"`), &n)
    47  			checkError(t, InvalidDigitError{}, err)
    48  		} else {
    49  			f2(t, `" 1"`, InvalidDigitError{})
    50  		}
    51  	})
    52  	t.Run("trailing space", func(t *testing.T) {
    53  		v := "go1.13.15"
    54  		if goVersion.LessEqual(v) {
    55  			var n json.Number
    56  			err := Unmarshal([]byte(`"1 "`), &n)
    57  			checkError(t, UnexpectedByteError{}, err)
    58  		} else {
    59  			f2(t, `"1 "`, UnexpectedByteError{})
    60  		}
    61  	})
    62  	t.Run("string", func(t *testing.T) {
    63  		v := "go1.13.15"
    64  		if goVersion.LessEqual(v) {
    65  			var n json.Number
    66  			err := Unmarshal([]byte(`"abc"`), &n)
    67  			checkError(t, InvalidDigitError{}, err)
    68  		} else {
    69  			f2(t, `"abc"`, InvalidDigitError{})
    70  		}
    71  	})
    72  	t.Run("invalid number", func(t *testing.T) {
    73  		f2(t, `-0.e`, InvalidDigitError{})
    74  	})
    75  	t.Run("valid number", func(t *testing.T) {
    76  		f2(t, `123.456e+0789`, nil)
    77  	})
    78  }