diff --git a/go/textutil/strings_test.go b/go/textutil/strings_test.go index 9ea175909fd..2b43166831c 100644 --- a/go/textutil/strings_test.go +++ b/go/textutil/strings_test.go @@ -20,6 +20,9 @@ import ( "testing" "github.com/stretchr/testify/assert" + + binlogdatapb "vitess.io/vitess/go/vt/proto/binlogdata" + topodatapb "vitess.io/vitess/go/vt/proto/topodata" ) func TestSplitDelimitedList(t *testing.T) { @@ -65,6 +68,12 @@ func TestSplitUnescape(t *testing.T) { assert.NoError(t, err) assert.Equal(t, expected, elems) } + { + s := "invalid%2" + elems, err := SplitUnescape(s, ",") + assert.Error(t, err) + assert.Equal(t, []string{}, elems) + } } func TestSingleWordCamel(t *testing.T) { @@ -108,3 +117,94 @@ func TestSingleWordCamel(t *testing.T) { }) } } + +func TestValueIsSimulatedNull(t *testing.T) { + tt := []struct { + name string + val interface{} + isNull bool + }{ + { + name: "case string false", + val: "test", + isNull: false, + }, + { + name: "case string true", + val: SimulatedNullString, + isNull: true, + }, + { + name: "case []string true", + val: []string{SimulatedNullString}, + isNull: true, + }, + { + name: "case []string false", + val: []string{SimulatedNullString, SimulatedNullString}, + isNull: false, + }, + { + name: "case binlogdatapb.OnDDLAction true", + val: binlogdatapb.OnDDLAction(SimulatedNullInt), + isNull: true, + }, + { + name: "case int true", + val: SimulatedNullInt, + isNull: true, + }, + { + name: "case int32 true", + val: int32(SimulatedNullInt), + isNull: true, + }, + { + name: "case int64 true", + val: int64(SimulatedNullInt), + isNull: true, + }, + { + name: "case []topodatapb.TabletType true", + val: []topodatapb.TabletType{topodatapb.TabletType(SimulatedNullInt)}, + isNull: true, + }, + { + name: "case binlogdatapb.VReplicationWorkflowState true", + val: binlogdatapb.VReplicationWorkflowState(SimulatedNullInt), + isNull: true, + }, + { + name: "case default", + val: float64(1), + isNull: false, + }, + } + + for _, tc := range tt { + t.Run(tc.name, func(t *testing.T) { + isNull := ValueIsSimulatedNull(tc.val) + assert.Equal(t, tc.isNull, isNull) + }) + } +} + +func TestTitle(t *testing.T) { + tt := []struct { + s string + expect string + }{ + {s: "hello world", expect: "Hello World"}, + {s: "snake_case", expect: "Snake_case"}, + {s: "TITLE CASE", expect: "TITLE CASE"}, + {s: "HelLo wOrLd", expect: "HelLo WOrLd"}, + {s: "", expect: ""}, + } + + for _, tc := range tt { + t.Run(tc.s, func(t *testing.T) { + title := Title(tc.s) + assert.Equal(t, tc.expect, title) + }) + } +} diff --git a/go/textutil/template_test.go b/go/textutil/template_test.go new file mode 100644 index 00000000000..077a6f6a72d --- /dev/null +++ b/go/textutil/template_test.go @@ -0,0 +1,51 @@ +/* +Copyright 2024 The Vitess Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package textutil + +import ( + "testing" + "text/template" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestExecuteTemplate(t *testing.T) { + tmplText := "Result: {{.Value}}" + inputData := struct{ Value string }{Value: "Test"} + tmpl, err := template.New("test").Parse(tmplText) + require.NoError(t, err) + + result, err := ExecuteTemplate(tmpl, inputData) + require.NoError(t, err) + + expectedResult := "Result: Test" + assert.Equal(t, expectedResult, result) + +} + +func TestExecuteTemplateWithError(t *testing.T) { + templText := "{{.UndefinedVariable}}" + invalidInput := struct{ Name string }{Name: "foo"} + + tmpl, err := template.New("test").Parse(templText) + require.NoError(t, err) + + result, err := ExecuteTemplate(tmpl, invalidInput) + assert.Error(t, err) + assert.Equal(t, "", result) +}