github.com/dolthub/go-mysql-server@v0.18.0/sql/expression/function/trim_ltrim_rtrim_test.go (about)

     1  // Copyright 2020-2021 Dolthub, 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  // 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 function
    16  
    17  import (
    18  	"testing"
    19  
    20  	"github.com/stretchr/testify/require"
    21  
    22  	"github.com/dolthub/go-mysql-server/sql"
    23  	"github.com/dolthub/go-mysql-server/sql/expression"
    24  	"github.com/dolthub/go-mysql-server/sql/types"
    25  )
    26  
    27  func TestTrim(t *testing.T) {
    28  	f := NewTrim(expression.NewGetField(0, types.LongText, "", false), expression.NewGetField(1, types.LongText, "", false), "b")
    29  	testCases := []struct {
    30  		name     string
    31  		row      sql.Row
    32  		expected interface{}
    33  		err      bool
    34  	}{
    35  		{"null input", sql.NewRow(nil, " ", "b"), nil, false},
    36  		{"trimmed string", sql.NewRow("foo", " ", "b"), "foo", false},
    37  		{"spaces in both sides", sql.NewRow("  foo    ", " ", "b"), "foo", false},
    38  		{"spaces in left side", sql.NewRow("  foo", " ", "b"), "foo", false},
    39  		{"spaces in right side", sql.NewRow("foo    ", " ", "b"), "foo", false},
    40  		{"two words with spaces", sql.NewRow(" foo   bar ", " ", "b"), "foo   bar", false},
    41  		{"different kinds of spaces", sql.NewRow("\r\tfoo   bar \v", " ", "b"), "\r\tfoo   bar \v", false},
    42  	}
    43  	for _, tt := range testCases {
    44  		t.Run(tt.name, func(t *testing.T) {
    45  			t.Helper()
    46  			require := require.New(t)
    47  			ctx := sql.NewEmptyContext()
    48  
    49  			v, err := f.Eval(ctx, tt.row)
    50  			if tt.err {
    51  				require.Error(err)
    52  			} else {
    53  				require.NoError(err)
    54  				require.Equal(tt.expected, v)
    55  			}
    56  		})
    57  	}
    58  }
    59  
    60  func TestLTrim(t *testing.T) {
    61  	f := NewLeftTrim(expression.NewGetField(0, types.LongText, "", false))
    62  	testCases := []struct {
    63  		name     string
    64  		row      sql.Row
    65  		expected interface{}
    66  		err      bool
    67  	}{
    68  		{"null input", sql.NewRow(nil), nil, false},
    69  		{"trimmed string", sql.NewRow("foo"), "foo", false},
    70  		{"spaces in both sides", sql.NewRow("  foo    "), "foo    ", false},
    71  		{"spaces in left side", sql.NewRow("  foo"), "foo", false},
    72  		{"spaces in right side", sql.NewRow("foo    "), "foo    ", false},
    73  		{"two words with spaces", sql.NewRow(" foo   bar "), "foo   bar ", false},
    74  		{"different kinds of spaces", sql.NewRow("\r\tfoo   bar \v"), "\r\tfoo   bar \v", false},
    75  	}
    76  	for _, tt := range testCases {
    77  		t.Run(tt.name, func(t *testing.T) {
    78  			t.Helper()
    79  			require := require.New(t)
    80  			ctx := sql.NewEmptyContext()
    81  
    82  			v, err := f.Eval(ctx, tt.row)
    83  			if tt.err {
    84  				require.Error(err)
    85  			} else {
    86  				require.NoError(err)
    87  				require.Equal(tt.expected, v)
    88  			}
    89  		})
    90  	}
    91  }
    92  
    93  func TestRTrim(t *testing.T) {
    94  	f := NewRightTrim(expression.NewGetField(0, types.LongText, "", false))
    95  	testCases := []struct {
    96  		name     string
    97  		row      sql.Row
    98  		expected interface{}
    99  		err      bool
   100  	}{
   101  		{"null input", sql.NewRow(nil), nil, false},
   102  		{"trimmed string", sql.NewRow("foo"), "foo", false},
   103  		{"spaces in both sides", sql.NewRow("  foo    "), "  foo", false},
   104  		{"spaces in left side", sql.NewRow("  foo"), "  foo", false},
   105  		{"spaces in right side", sql.NewRow("foo    "), "foo", false},
   106  		{"two words with spaces", sql.NewRow(" foo   bar "), " foo   bar", false},
   107  		{"different kinds of spaces", sql.NewRow("\r\tfoo   bar \v"), "\r\tfoo   bar \v", false},
   108  	}
   109  	for _, tt := range testCases {
   110  		t.Run(tt.name, func(t *testing.T) {
   111  			t.Helper()
   112  			require := require.New(t)
   113  			ctx := sql.NewEmptyContext()
   114  
   115  			v, err := f.Eval(ctx, tt.row)
   116  			if tt.err {
   117  				require.Error(err)
   118  			} else {
   119  				require.NoError(err)
   120  				require.Equal(tt.expected, v)
   121  			}
   122  		})
   123  	}
   124  }