github.com/zerosnake0/jzon@v0.0.9-0.20230801092939-1b135cb83f7f/val_encoder_json_number_test.go (about) 1 package jzon 2 3 import ( 4 "encoding/json" 5 "testing" 6 ) 7 8 func TestValEncoder_JsonNumber(t *testing.T) { 9 t.Run("non pointer", func(t *testing.T) { 10 f := func(t *testing.T, n json.Number) { 11 checkEncodeValueWithStandard(t, n, nil) 12 } 13 t.Run("empty", func(t *testing.T) { 14 f(t, "") 15 }) 16 t.Run("non empty", func(t *testing.T) { 17 f(t, "-1.2e-3") 18 }) 19 t.Run("invalid", func(t *testing.T) { 20 // TODO: 21 }) 22 }) 23 t.Run("pointer", func(t *testing.T) { 24 f := func(t *testing.T, ptr *json.Number, err error) { 25 checkEncodeValueWithStandard(t, ptr, err) 26 } 27 t.Run("nil", func(t *testing.T) { 28 f(t, nil, nil) 29 }) 30 t.Run("non nil", func(t *testing.T) { 31 var n json.Number = "1.23" 32 f(t, &n, nil) 33 }) 34 }) 35 t.Run("pointer of pointer", func(t *testing.T) { 36 f := func(t *testing.T, ptr **json.Number, err error) { 37 checkEncodeValueWithStandard(t, ptr, err) 38 } 39 t.Run("nil", func(t *testing.T) { 40 f(t, nil, nil) 41 }) 42 t.Run("pointer of nil", func(t *testing.T) { 43 ptr := (*json.Number)(nil) 44 f(t, &ptr, nil) 45 }) 46 t.Run("pointer of non nil", func(t *testing.T) { 47 var n json.Number = "1.23" 48 ptr := &n 49 f(t, &ptr, nil) 50 }) 51 }) 52 } 53 54 func TestValEncoder_JsonNumber_OmitEmpty(t *testing.T) { 55 type st struct { 56 A json.Number `json:",omitempty"` 57 } 58 t.Run("empty", func(t *testing.T) { 59 checkEncodeValueWithStandard(t, st{}, nil) 60 }) 61 t.Run("zero", func(t *testing.T) { 62 checkEncodeValueWithStandard(t, st{ 63 A: "0", 64 }, nil) 65 }) 66 t.Run("zero float", func(t *testing.T) { 67 checkEncodeValueWithStandard(t, st{ 68 A: "0.0", 69 }, nil) 70 }) 71 }