github.com/bytedance/sonic@v1.11.7-0.20240517092252-d2edb31b167b/issue_test/issue600_test.go (about)

     1  /**
     2   * Copyright 2024 ByteDance Inc.
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *     http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  package issue_test
    18  
    19  import (
    20  	"fmt"
    21  	"testing"
    22  
    23  	"github.com/bytedance/sonic/ast"
    24  	"github.com/stretchr/testify/require"
    25  )
    26  
    27  
    28  func TestIssue600(t *testing.T) {
    29      // object
    30      obj := ast.NewRaw("{\"x\":\"a\",\"y\":\"b\"}")
    31      if ok, err := obj.Unset("x"); !ok || err != nil {
    32          panic(fmt.Errorf("unset x fail, ok=%v, err=%v", ok, err))
    33      }
    34      if ok, err := obj.Unset("y"); !ok || err != nil {
    35          panic(fmt.Errorf("unset y fail, ok=%v, err=%v", ok, err))
    36      }
    37      result, err := obj.MarshalJSON()
    38      if err != nil {
    39          panic(fmt.Errorf("MarshalJSON fail: err=%v", err))
    40      }
    41      require.Equal(t, `{}`, string(result))
    42  
    43      obj = ast.NewRaw("{\"x\":\"a\",\"y\":\"b\"}")
    44      if ok, err := obj.Unset("y"); !ok || err != nil {
    45          panic(fmt.Errorf("unset x fail, ok=%v, err=%v", ok, err))
    46      }
    47      if ok, err := obj.Unset("x"); !ok || err != nil {
    48          panic(fmt.Errorf("unset y fail, ok=%v, err=%v", ok, err))
    49      }
    50      result, err = obj.MarshalJSON()
    51      if err != nil {
    52          panic(fmt.Errorf("MarshalJSON fail: err=%v", err))
    53      }
    54      require.Equal(t, `{}`, string(result))
    55  
    56      // array
    57      obj = ast.NewRaw("[1,2]")
    58      if ok, err := obj.UnsetByIndex(0); !ok || err != nil {
    59          panic(fmt.Errorf("unset x fail, ok=%v, err=%v", ok, err))
    60      }
    61      if ok, err := obj.UnsetByIndex(0); !ok || err != nil {
    62          panic(fmt.Errorf("unset y fail, ok=%v, err=%v", ok, err))
    63      }
    64      result, err = obj.MarshalJSON()
    65      if err != nil {
    66          panic(fmt.Errorf("MarshalJSON fail: err=%v", err))
    67      }
    68      require.Equal(t, `[]`, string(result))
    69  
    70      obj = ast.NewRaw("[1,2]")
    71      if ok, err := obj.UnsetByIndex(1); !ok || err != nil {
    72          panic(fmt.Errorf("unset x fail, ok=%v, err=%v", ok, err))
    73      }
    74      if ok, err := obj.UnsetByIndex(0); !ok || err != nil {
    75          panic(fmt.Errorf("unset y fail, ok=%v, err=%v", ok, err))
    76      }
    77      result, err = obj.MarshalJSON()
    78      if err != nil {
    79          panic(fmt.Errorf("MarshalJSON fail: err=%v", err))
    80      }
    81      require.Equal(t, `[]`, string(result))
    82  }