github.com/goshafaq/sonic@v0.0.0-20231026082336-871835fb94c6/internal/decoder/errors_test.go (about)

     1  /*
     2   * Copyright 2021 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 decoder
    18  
    19  import (
    20  	"testing"
    21  
    22  	"github.com/goshafaq/sonic/internal/native/types"
    23  	"github.com/stretchr/testify/assert"
    24  )
    25  
    26  func make_err(src string, pos int) SyntaxError {
    27  	return SyntaxError{
    28  		Src:  src,
    29  		Pos:  pos,
    30  		Code: types.ERR_INVALID_CHAR,
    31  	}
    32  }
    33  
    34  func TestErrors_Normal(t *testing.T) {
    35  	println(make_err("this is a very long message with 'hello, world' embedded in the string", 33).Description())
    36  }
    37  
    38  func TestErrors_LeftEdge(t *testing.T) {
    39  	println(make_err("this is a very long message with 'hello, world' embedded in the string", 6).Description())
    40  }
    41  
    42  func TestErrors_RightEdge(t *testing.T) {
    43  	println(make_err("this is a very long message with 'hello, world' embedded in the string", 65).Description())
    44  }
    45  
    46  func TestErrors_AfterRightEdge(t *testing.T) {
    47  	println(make_err("this is a very long message with 'hello, world' embedded in the string", 70).Description())
    48  }
    49  
    50  func TestErrors_ShortDescription(t *testing.T) {
    51  	e := make_err("hello, world", 5)
    52  	println(e.Description())
    53  	assert.Equal(t, "Syntax error at index 5: invalid char\n\n\thello, world\n\t.....^......\n", e.Description())
    54  	assert.Equal(t, `"Syntax error at index 5: invalid char\n\n\thello, world\n\t.....^......\n"`, e.Error())
    55  }
    56  
    57  func TestErrors_EmptyDescription(t *testing.T) {
    58  	println(make_err("", 0).Description())
    59  }