github.com/matrixorigin/matrixone@v0.7.0/pkg/sql/plan/function/builtin/multi/trim_test.go (about)

     1  // Copyright 2022 Matrix Origin
     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  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package multi
    16  
    17  import (
    18  	"fmt"
    19  	"github.com/matrixorigin/matrixone/pkg/container/types"
    20  	"github.com/matrixorigin/matrixone/pkg/testutil"
    21  	"github.com/stretchr/testify/require"
    22  	"testing"
    23  )
    24  
    25  func TestTrim(t *testing.T) {
    26  	kases := []struct {
    27  		mode     string
    28  		input    string
    29  		trimWord string
    30  		output   string
    31  		info     string
    32  	}{
    33  		{
    34  			mode:     "both",
    35  			input:    "   hello world   ",
    36  			trimWord: " ",
    37  			output:   "hello world",
    38  		},
    39  		{
    40  			mode:     "leading",
    41  			input:    "   hello world   ",
    42  			trimWord: " ",
    43  			output:   "hello world   ",
    44  		},
    45  		{
    46  			mode:     "trailing",
    47  			input:    "   hello world   ",
    48  			trimWord: " ",
    49  			output:   "   hello world",
    50  		},
    51  		{
    52  			mode:     "both",
    53  			input:    "   hello world   ",
    54  			trimWord: "h",
    55  			output:   "   hello world   ",
    56  		},
    57  		{
    58  			mode:     "trailing",
    59  			input:    "   hello world",
    60  			trimWord: "d",
    61  			output:   "   hello worl",
    62  		},
    63  		{
    64  			mode:     "leading",
    65  			input:    "hello world   ",
    66  			trimWord: "h",
    67  			output:   "ello world   ",
    68  		},
    69  		{
    70  			mode:     "both",
    71  			input:    "嗷嗷0k七七",
    72  			trimWord: "七",
    73  			output:   "嗷嗷0k",
    74  		},
    75  		{
    76  			mode:     "leading",
    77  			input:    "嗷嗷0k七七",
    78  			trimWord: "七",
    79  			output:   "嗷嗷0k七七",
    80  		},
    81  		{
    82  			mode:     "trailing",
    83  			input:    "嗷嗷0k七七",
    84  			trimWord: "七",
    85  			output:   "嗷嗷0k",
    86  		},
    87  		{
    88  			mode:     "both",
    89  			input:    "嗷嗷0k七七",
    90  			trimWord: "k七七",
    91  			output:   "嗷嗷0",
    92  		},
    93  		{
    94  			mode:     "leading",
    95  			input:    "嗷嗷0k七七",
    96  			trimWord: "",
    97  			output:   "嗷嗷0k七七",
    98  		},
    99  		{
   100  			mode:     "trailing",
   101  			input:    "",
   102  			trimWord: "嗷嗷0k七七",
   103  			output:   "",
   104  		},
   105  	}
   106  	proc := testutil.NewProc()
   107  	for idx, kase := range kases {
   108  		inputs := []testutil.FunctionTestInput{
   109  			testutil.NewFunctionTestInput(types.T_varchar.ToType(), []string{kase.mode}, nil),
   110  			testutil.NewFunctionTestInput(types.T_varchar.ToType(), []string{kase.trimWord}, nil),
   111  			testutil.NewFunctionTestInput(types.T_varchar.ToType(), []string{kase.input}, nil),
   112  		}
   113  		expect := testutil.NewFunctionTestResult(types.T_varchar.ToType(), false, []string{kase.output}, nil)
   114  		kaseNow := testutil.NewFunctionTestCase(proc,
   115  			inputs, expect, Trim)
   116  		s, info := kaseNow.Run()
   117  		require.True(t, s, fmt.Sprintf("case %d, err info is '%s'", idx, info))
   118  	}
   119  }