github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/sql/sem/tree/collatedstring_test.go (about)

     1  // Copyright 2017 The Cockroach Authors.
     2  //
     3  // Use of this software is governed by the Business Source License
     4  // included in the file licenses/BSL.txt.
     5  //
     6  // As of the Change Date specified in that file, in accordance with
     7  // the Business Source License, use of this software will be governed
     8  // by the Apache License, Version 2.0, included in the file
     9  // licenses/APL.txt.
    10  
    11  package tree
    12  
    13  import (
    14  	"context"
    15  	"testing"
    16  
    17  	"github.com/cockroachdb/cockroach/pkg/settings/cluster"
    18  	"github.com/cockroachdb/cockroach/pkg/sql/types"
    19  	"github.com/cockroachdb/cockroach/pkg/util/leaktest"
    20  )
    21  
    22  func TestCastToCollatedString(t *testing.T) {
    23  	defer leaktest.AfterTest(t)()
    24  	cases := []struct {
    25  		typ      *types.T
    26  		contents string
    27  	}{
    28  		{types.MakeCollatedString(types.String, "de"), "test"},
    29  		{types.MakeCollatedString(types.String, "en"), "test"},
    30  		{types.MakeCollatedString(types.MakeString(5), "en"), "test"},
    31  		{types.MakeCollatedString(types.MakeString(4), "en"), "test"},
    32  		{types.MakeCollatedString(types.MakeString(3), "en"), "tes"},
    33  	}
    34  	ctx := context.Background()
    35  	for _, cas := range cases {
    36  		t.Run("", func(t *testing.T) {
    37  			expr := &CastExpr{Expr: NewDString("test"), Type: cas.typ, SyntaxMode: CastShort}
    38  			semaCtx := MakeSemaContext()
    39  			typedexpr, err := expr.TypeCheck(ctx, &semaCtx, types.Any)
    40  			if err != nil {
    41  				t.Fatal(err)
    42  			}
    43  			evalCtx := NewTestingEvalContext(cluster.MakeTestingClusterSettings())
    44  			defer evalCtx.Stop(context.Background())
    45  			val, err := typedexpr.Eval(evalCtx)
    46  			if err != nil {
    47  				t.Fatal(err)
    48  			}
    49  			switch v := val.(type) {
    50  			case *DCollatedString:
    51  				if v.Locale != cas.typ.Locale() {
    52  					t.Errorf("expected locale %q but got %q", cas.typ.Locale(), v.Locale)
    53  				}
    54  				if v.Contents != cas.contents {
    55  					t.Errorf("expected contents %q but got %q", cas.contents, v.Contents)
    56  				}
    57  			default:
    58  				t.Errorf("expected type *DCollatedString but got %T", v)
    59  			}
    60  		})
    61  	}
    62  }