gojay

high performance JSON encoder/decoder with stream API for Golang
git clone git://git.lair.cx/gojay
Log | Files | Refs | README | LICENSE

encode_string_test.go (6765B)


      1 package gojay
      2 
      3 import (
      4 	"strings"
      5 	"testing"
      6 
      7 	"github.com/stretchr/testify/assert"
      8 )
      9 
     10 func TestEncoderStringEncodeAPI(t *testing.T) {
     11 	t.Run("basic", func(t *testing.T) {
     12 		builder := &strings.Builder{}
     13 		enc := NewEncoder(builder)
     14 		err := enc.EncodeString("hello world")
     15 		assert.Nil(t, err, "Error should be nil")
     16 		assert.Equal(
     17 			t,
     18 			`"hello world"`,
     19 			builder.String(),
     20 			"Result of marshalling is different as the one expected")
     21 	})
     22 	t.Run("utf8", func(t *testing.T) {
     23 		builder := &strings.Builder{}
     24 		enc := NewEncoder(builder)
     25 		err := enc.EncodeString("漢字𩸽")
     26 		assert.Nil(t, err, "Error should be nil")
     27 		assert.Equal(
     28 			t,
     29 			`"漢字𩸽"`,
     30 			builder.String(),
     31 			"Result of marshalling is different as the one expected")
     32 	})
     33 	t.Run("utf8-multibyte", func(t *testing.T) {
     34 		str := "テュールスト マーティン ヤコブ 😁"
     35 		builder := &strings.Builder{}
     36 		enc := NewEncoder(builder)
     37 		err := enc.EncodeString(str)
     38 		assert.Nil(t, err, "Error should be nil")
     39 		assert.Equal(
     40 			t,
     41 			`"テュールスト マーティン ヤコブ 😁"`,
     42 			builder.String(),
     43 			"Result of marshalling is different as the one expected")
     44 	})
     45 	t.Run("escaped-sequence1", func(t *testing.T) {
     46 		str := `テュールスト マ\ーテ
     47 ィン ヤコブ 😁`
     48 		builder := &strings.Builder{}
     49 		enc := NewEncoder(builder)
     50 		err := enc.EncodeString(str)
     51 		assert.Nil(t, err, "Error should be nil")
     52 		assert.Equal(
     53 			t,
     54 			`"テュールスト マ\\ーテ\nィン ヤコブ 😁"`,
     55 			builder.String(),
     56 			"Result of marshalling is different as the one expected")
     57 	})
     58 	t.Run("escaped-sequence2", func(t *testing.T) {
     59 		str := `テュールスト マ\ーテ
     60 ィン ヤコブ 😁	`
     61 		builder := &strings.Builder{}
     62 		enc := NewEncoder(builder)
     63 		err := enc.EncodeString(str)
     64 		assert.Nil(t, err, "Error should be nil")
     65 		assert.Equal(
     66 			t,
     67 			`"テュールスト マ\\ーテ\nィン ヤコブ 😁\t"`,
     68 			builder.String(),
     69 			"Result of marshalling is different as the one expected")
     70 	})
     71 	t.Run("escaped-sequence3", func(t *testing.T) {
     72 		str := "hello \r world 𝄞"
     73 		builder := &strings.Builder{}
     74 		enc := NewEncoder(builder)
     75 		err := enc.EncodeString(str)
     76 		assert.Nil(t, err, "Error should be nil")
     77 		assert.Equal(
     78 			t,
     79 			`"hello \r world 𝄞"`,
     80 			builder.String(),
     81 			"Result of marshalling is different as the one expected")
     82 	})
     83 	t.Run("escaped-sequence3", func(t *testing.T) {
     84 		str := "hello \b world 𝄞"
     85 		builder := &strings.Builder{}
     86 		enc := NewEncoder(builder)
     87 		err := enc.EncodeString(str)
     88 		assert.Nil(t, err, "Error should be nil")
     89 		assert.Equal(
     90 			t,
     91 			`"hello \b world 𝄞"`,
     92 			builder.String(),
     93 			"Result of marshalling is different as the one expected")
     94 	})
     95 	t.Run("escaped-control-char", func(t *testing.T) {
     96 		str := "\u001b"
     97 		builder := &strings.Builder{}
     98 		enc := NewEncoder(builder)
     99 		err := enc.EncodeString(str)
    100 		assert.Nil(t, err, "Error should be nil")
    101 		assert.Equal(
    102 			t,
    103 			`"\u001b"`,
    104 			builder.String(),
    105 			"Result of marshalling is different as the one expected")
    106 	})
    107 	t.Run("escaped-sequence3", func(t *testing.T) {
    108 		str := "hello \f world 𝄞"
    109 		builder := &strings.Builder{}
    110 		enc := NewEncoder(builder)
    111 		err := enc.EncodeString(str)
    112 		assert.Nil(t, err, "Error should be nil")
    113 		assert.Equal(
    114 			t,
    115 			"\"hello \\f world 𝄞\"",
    116 			builder.String(),
    117 			"Result of marshalling is different as the one expected")
    118 	})
    119 }
    120 
    121 func TestEncoderStringEncodeAPIErrors(t *testing.T) {
    122 	t.Run("pool-error", func(t *testing.T) {
    123 		v := ""
    124 		enc := BorrowEncoder(nil)
    125 		enc.isPooled = 1
    126 		defer func() {
    127 			err := recover()
    128 			assert.NotNil(t, err, "err shouldnt be nil")
    129 			assert.IsType(t, InvalidUsagePooledEncoderError(""), err, "err should be of type InvalidUsagePooledEncoderError")
    130 			assert.Equal(t, "Invalid usage of pooled encoder", err.(InvalidUsagePooledEncoderError).Error(), "err should be of type InvalidUsagePooledDecoderError")
    131 		}()
    132 		_ = enc.EncodeString(v)
    133 		assert.True(t, false, "should not be called as it should have panicked")
    134 	})
    135 	t.Run("write-error", func(t *testing.T) {
    136 		v := "test"
    137 		w := TestWriterError("")
    138 		enc := BorrowEncoder(w)
    139 		defer enc.Release()
    140 		err := enc.EncodeString(v)
    141 		assert.NotNil(t, err, "err should not be nil")
    142 	})
    143 }
    144 
    145 func TestEncoderStringMarshalAPI(t *testing.T) {
    146 	t.Run("basic", func(t *testing.T) {
    147 		r, err := Marshal("string")
    148 		assert.Nil(t, err, "Error should be nil")
    149 		assert.Equal(
    150 			t,
    151 			`"string"`,
    152 			string(r),
    153 			"Result of marshalling is different as the one expected")
    154 	})
    155 	t.Run("utf8", func(t *testing.T) {
    156 		r, err := Marshal("漢字")
    157 		assert.Nil(t, err, "Error should be nil")
    158 		assert.Equal(
    159 			t,
    160 			`"漢字"`,
    161 			string(r),
    162 			"Result of marshalling is different as the one expected")
    163 	})
    164 }
    165 
    166 func TestEncoderStringNullEmpty(t *testing.T) {
    167 	var testCases = []struct {
    168 		name         string
    169 		baseJSON     string
    170 		expectedJSON string
    171 	}{
    172 		{
    173 			name:         "basic 1st elem",
    174 			baseJSON:     "[",
    175 			expectedJSON: `[null,"true"`,
    176 		},
    177 		{
    178 			name:         "basic 2nd elem",
    179 			baseJSON:     `["test"`,
    180 			expectedJSON: `["test",null,"true"`,
    181 		},
    182 		{
    183 			name:         "basic 2nd elem",
    184 			baseJSON:     `["test"`,
    185 			expectedJSON: `["test",null,"true"`,
    186 		},
    187 	}
    188 	for _, testCase := range testCases {
    189 		t.Run("true", func(t *testing.T) {
    190 			var b strings.Builder
    191 			var enc = NewEncoder(&b)
    192 			enc.writeString(testCase.baseJSON)
    193 			enc.StringNullEmpty("")
    194 			enc.AddStringNullEmpty("true")
    195 			enc.Write()
    196 			assert.Equal(t, testCase.expectedJSON, b.String())
    197 		})
    198 	}
    199 }
    200 
    201 func TestEncoderStringNullEmpty2(t *testing.T) {
    202 	var testCases = []struct {
    203 		name         string
    204 		baseJSON     string
    205 		expectedJSON string
    206 	}{
    207 		{
    208 			name:         "basic 1st elem",
    209 			baseJSON:     "[",
    210 			expectedJSON: `["test"`,
    211 		},
    212 	}
    213 	for _, testCase := range testCases {
    214 		t.Run("true", func(t *testing.T) {
    215 			var b strings.Builder
    216 			var enc = NewEncoder(&b)
    217 			enc.writeString(testCase.baseJSON)
    218 			enc.StringNullEmpty("test")
    219 			enc.Write()
    220 			assert.Equal(t, testCase.expectedJSON, b.String())
    221 		})
    222 	}
    223 }
    224 
    225 func TestEncoderStringNullKeyEmpty(t *testing.T) {
    226 	var testCases = []struct {
    227 		name         string
    228 		baseJSON     string
    229 		expectedJSON string
    230 	}{
    231 		{
    232 			name:         "basic 1st elem",
    233 			baseJSON:     "{",
    234 			expectedJSON: `{"foo":null,"bar":"true"`,
    235 		},
    236 		{
    237 			name:         "basic 2nd elem",
    238 			baseJSON:     `{"test":"test"`,
    239 			expectedJSON: `{"test":"test","foo":null,"bar":"true"`,
    240 		},
    241 	}
    242 	for _, testCase := range testCases {
    243 		t.Run("true", func(t *testing.T) {
    244 			var b strings.Builder
    245 			var enc = NewEncoder(&b)
    246 			enc.writeString(testCase.baseJSON)
    247 			enc.StringKeyNullEmpty("foo", "")
    248 			enc.AddStringKeyNullEmpty("bar", "true")
    249 			enc.Write()
    250 			assert.Equal(t, testCase.expectedJSON, b.String())
    251 		})
    252 	}
    253 }