encode_bool_test.go (3348B)
1 package gojay 2 3 import ( 4 "strings" 5 "testing" 6 7 "github.com/stretchr/testify/assert" 8 ) 9 10 func TestEncoderBoolMarshalAPI(t *testing.T) { 11 t.Run("true", func(t *testing.T) { 12 b, err := Marshal(true) 13 assert.Nil(t, err, "err must be nil") 14 assert.Equal(t, "true", string(b), "string(b) must be equal to 'true'") 15 }) 16 t.Run("false", func(t *testing.T) { 17 b, err := Marshal(false) 18 assert.Nil(t, err, "err must be nil") 19 assert.Equal(t, "false", string(b), "string(b) must be equal to 'false'") 20 }) 21 } 22 23 func TestEncoderBoolEncodeAPI(t *testing.T) { 24 t.Run("true", func(t *testing.T) { 25 builder := &strings.Builder{} 26 enc := BorrowEncoder(builder) 27 defer enc.Release() 28 err := enc.EncodeBool(true) 29 assert.Nil(t, err, "err must be nil") 30 assert.Equal(t, "true", builder.String(), "string(b) must be equal to 'true'") 31 }) 32 t.Run("false", func(t *testing.T) { 33 builder := &strings.Builder{} 34 enc := BorrowEncoder(builder) 35 defer enc.Release() 36 err := enc.EncodeBool(false) 37 assert.Nil(t, err, "err must be nil") 38 assert.Equal(t, "false", builder.String(), "string(b) must be equal to 'false'") 39 }) 40 } 41 42 func TestEncoderBoolErrors(t *testing.T) { 43 t.Run("pool-error", func(t *testing.T) { 44 builder := &strings.Builder{} 45 enc := BorrowEncoder(builder) 46 enc.isPooled = 1 47 defer func() { 48 err := recover() 49 assert.NotNil(t, err, "err shouldnt be nil") 50 assert.IsType(t, InvalidUsagePooledEncoderError(""), err, "err should be of type InvalidUsagePooledEncoderError") 51 assert.Equal(t, "Invalid usage of pooled encoder", err.(InvalidUsagePooledEncoderError).Error(), "err should be of type InvalidUsagePooledEncoderError") 52 }() 53 _ = enc.EncodeBool(false) 54 assert.True(t, false, "should not be called as it should have panicked") 55 }) 56 t.Run("encode-api-write-error", func(t *testing.T) { 57 v := true 58 w := TestWriterError("") 59 enc := BorrowEncoder(w) 60 defer enc.Release() 61 err := enc.EncodeBool(v) 62 assert.NotNil(t, err, "err should not be nil") 63 }) 64 } 65 66 func TestEncoderBoolNullEmpty(t *testing.T) { 67 var testCases = []struct { 68 name string 69 baseJSON string 70 expectedJSON string 71 }{ 72 { 73 name: "basic 1st elem", 74 baseJSON: "[", 75 expectedJSON: "[null,true", 76 }, 77 { 78 name: "basic 2nd elem", 79 baseJSON: `["test"`, 80 expectedJSON: `["test",null,true`, 81 }, 82 } 83 for _, testCase := range testCases { 84 t.Run("true", func(t *testing.T) { 85 var b strings.Builder 86 var enc = NewEncoder(&b) 87 enc.writeString(testCase.baseJSON) 88 enc.BoolNullEmpty(false) 89 enc.AddBoolNullEmpty(true) 90 enc.Write() 91 assert.Equal(t, testCase.expectedJSON, b.String()) 92 }) 93 } 94 } 95 96 func TestEncoderBoolNullKeyEmpty(t *testing.T) { 97 var testCases = []struct { 98 name string 99 baseJSON string 100 expectedJSON string 101 }{ 102 { 103 name: "basic 1st elem", 104 baseJSON: "{", 105 expectedJSON: `{"foo":null,"bar":true`, 106 }, 107 { 108 name: "basic 2nd elem", 109 baseJSON: `{"test":"test"`, 110 expectedJSON: `{"test":"test","foo":null,"bar":true`, 111 }, 112 } 113 for _, testCase := range testCases { 114 t.Run("true", func(t *testing.T) { 115 var b strings.Builder 116 var enc = NewEncoder(&b) 117 enc.writeString(testCase.baseJSON) 118 enc.BoolKeyNullEmpty("foo", false) 119 enc.AddBoolKeyNullEmpty("bar", true) 120 enc.Write() 121 assert.Equal(t, testCase.expectedJSON, b.String()) 122 }) 123 } 124 }