github.com/goshafaq/sonic@v0.0.0-20231026082336-871835fb94c6/encoder/encoder_amd64_test.go (about)

     1  //go:build amd64 && go1.16 && !go1.22
     2  // +build amd64,go1.16,!go1.22
     3  
     4  /*
     5   * Copyright 2021 ByteDance Inc.
     6   *
     7   * Licensed under the Apache License, Version 2.0 (the "License");
     8   * you may not use this file except in compliance with the License.
     9   * You may obtain a copy of the License at
    10   *
    11   *     http://www.apache.org/licenses/LICENSE-2.0
    12   *
    13   * Unless required by applicable law or agreed to in writing, software
    14   * distributed under the License is distributed on an "AS IS" BASIS,
    15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    16   * See the License for the specific language governing permissions and
    17   * limitations under the License.
    18   */
    19  
    20  package encoder
    21  
    22  import (
    23  	"encoding/json"
    24  	"testing"
    25  
    26  	"github.com/stretchr/testify/require"
    27  )
    28  
    29  func TestOptionSliceOrMapNoNull(t *testing.T) {
    30  	obj := sample{}
    31  	out, err := Encode(obj, NoNullSliceOrMap)
    32  	if err != nil {
    33  		t.Fatal(err)
    34  	}
    35  	require.Equal(t, `{"M":{},"S":[],"A":[],"MP":null,"SP":null,"AP":null}`, string(out))
    36  
    37  	obj2 := sample{}
    38  	out, err = Encode(obj2, 0)
    39  	if err != nil {
    40  		t.Fatal(err)
    41  	}
    42  	require.Equal(t, `{"M":null,"S":null,"A":[],"MP":null,"SP":null,"AP":null}`, string(out))
    43  }
    44  
    45  func TestEncoder_Marshaler(t *testing.T) {
    46  	v := MarshalerStruct{V: MarshalerImpl{X: 12345}}
    47  	ret, err := Encode(&v, 0)
    48  	require.NoError(t, err)
    49  	require.Equal(t, `{"V":12345    }`, string(ret))
    50  	ret, err = Encode(v, 0)
    51  	require.NoError(t, err)
    52  	require.Equal(t, `{"V":{"X":12345}}`, string(ret))
    53  
    54  	ret2, err2 := Encode(&v, 0)
    55  	require.NoError(t, err2)
    56  	require.Equal(t, `{"V":12345    }`, string(ret2))
    57  	ret3, err3 := Encode(v, CompactMarshaler)
    58  	require.NoError(t, err3)
    59  	require.Equal(t, `{"V":{"X":12345}}`, string(ret3))
    60  }
    61  
    62  func TestMarshalerError(t *testing.T) {
    63  	v := MarshalerErrorStruct{}
    64  	ret, err := Encode(&v, 0)
    65  	require.EqualError(t, err, `invalid Marshaler output json syntax at 5: "[\"\"] {"`)
    66  	require.Equal(t, []byte(nil), ret)
    67  }
    68  
    69  func TestEncoder_RawMessage(t *testing.T) {
    70  	rms := RawMessageStruct{
    71  		X: json.RawMessage("123456    "),
    72  	}
    73  	ret, err := Encode(&rms, 0)
    74  	require.NoError(t, err)
    75  	require.Equal(t, `{"X":123456    }`, string(ret))
    76  
    77  	ret, err = Encode(&rms, CompactMarshaler)
    78  	require.NoError(t, err)
    79  	require.Equal(t, `{"X":123456}`, string(ret))
    80  }
    81  
    82  func TestEncoder_TextMarshaler(t *testing.T) {
    83  	v := TextMarshalerStruct{V: TextMarshalerImpl{X: (`{"a"}`)}}
    84  	ret, err := Encode(&v, 0)
    85  	require.NoError(t, err)
    86  	require.Equal(t, `{"V":"{\"a\"}"}`, string(ret))
    87  	ret, err = Encode(v, 0)
    88  	require.NoError(t, err)
    89  	require.Equal(t, `{"V":{"X":"{\"a\"}"}}`, string(ret))
    90  
    91  	ret2, err2 := Encode(&v, NoQuoteTextMarshaler)
    92  	require.NoError(t, err2)
    93  	require.Equal(t, `{"V":{"a"}}`, string(ret2))
    94  	ret3, err3 := Encode(v, NoQuoteTextMarshaler)
    95  	require.NoError(t, err3)
    96  	require.Equal(t, `{"V":{"X":"{\"a\"}"}}`, string(ret3))
    97  }
    98  
    99  func TestEncoder_Marshal_EscapeHTML(t *testing.T) {
   100  	v := map[string]TextMarshalerImpl{"&&": {"<>"}}
   101  	ret, err := Encode(v, EscapeHTML)
   102  	require.NoError(t, err)
   103  	require.Equal(t, `{"\u0026\u0026":{"X":"\u003c\u003e"}}`, string(ret))
   104  	ret, err = Encode(v, 0)
   105  	require.NoError(t, err)
   106  	require.Equal(t, `{"&&":{"X":"<>"}}`, string(ret))
   107  
   108  	// “ is \xe2\x80\x9c, and ” is \xe2\x80\x9d,
   109  	// similar as HTML escaped chars \u2028(\xe2\x80\xa8) and \u2029(\xe2\x80\xa9)
   110  	m := map[string]string{"test": "“123”"}
   111  	ret, err = Encode(m, EscapeHTML)
   112  	require.Equal(t, string(ret), `{"test":"“123”"}`)
   113  	require.NoError(t, err)
   114  
   115  	m = map[string]string{"K": "\u2028\u2028\xe2"}
   116  	ret, err = Encode(m, EscapeHTML)
   117  	require.Equal(t, string(ret), "{\"K\":\"\\u2028\\u2028\xe2\"}")
   118  	require.NoError(t, err)
   119  }