github.com/pingcap/tidb/parser@v0.0.0-20231013125129-93a834a6bf8d/ast/base_test.go (about)

     1  // Copyright 2022 PingCAP, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  // Package ast is the abstract syntax tree parsed from a SQL statement by parser.
    15  // It can be analysed and transformed by optimizer.
    16  package ast
    17  
    18  import (
    19  	"testing"
    20  
    21  	"github.com/pingcap/tidb/parser/charset"
    22  	"github.com/stretchr/testify/require"
    23  )
    24  
    25  func TestNodeSetText(t *testing.T) {
    26  	n := &node{}
    27  	tests := []struct {
    28  		text           string
    29  		enc            charset.Encoding
    30  		expectUTF8Text string
    31  		expectText     string
    32  	}{
    33  		{"你好", nil, "你好", "你好"},
    34  		{"\xd2\xbb", charset.EncodingGBKImpl, "一", "\xd2\xbb"},
    35  		{"\xc1\xd0", charset.EncodingGBKImpl, "列", "\xc1\xd0"},
    36  	}
    37  	for _, tt := range tests {
    38  		n.SetText(tt.enc, tt.text)
    39  		require.Equal(t, tt.expectUTF8Text, n.Text())
    40  		require.Equal(t, tt.expectText, n.OriginalText())
    41  	}
    42  }