Skip to content

Commit

Permalink
Add unit tests for replaceAll (#5481)
Browse files Browse the repository at this point in the history
  • Loading branch information
vepatel committed May 1, 2024
1 parent 7f19bed commit 3ce86ae
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 0 deletions.
37 changes: 37 additions & 0 deletions internal/configs/version1/template_helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,34 @@ func TestTrimWhiteSpaceFromInputString(t *testing.T) {
}
}

func TestReplaceAll(t *testing.T) {
t.Parallel()

tmpl := newReplaceAll(t)
testCases := []struct {
InputString string
OldSubstring string
NewSubstring string
expected string
}{
{InputString: "foobarfoo", OldSubstring: "bar", NewSubstring: "foo", expected: "foofoofoo"},
{InputString: "footest", OldSubstring: "test", NewSubstring: "bar", expected: "foobar"},
{InputString: "barfoo", OldSubstring: "bar", NewSubstring: "test", expected: "testfoo"},
{InputString: "foofoofoo", OldSubstring: "foo", NewSubstring: "bar", expected: "barbarbar"},
}

for _, tc := range testCases {
var buf bytes.Buffer
err := tmpl.Execute(&buf, tc)
if err != nil {
t.Fatalf("Failed to execute the template %v", err)
}
if buf.String() != tc.expected {
t.Errorf("Template generated wrong config, got %v but expected %v.", buf.String(), tc.expected)
}
}
}

func TestContainsSubstring(t *testing.T) {
t.Parallel()

Expand Down Expand Up @@ -477,6 +505,15 @@ func newToUpperTemplate(t *testing.T) *template.Template {
return tmpl
}

func newReplaceAll(t *testing.T) *template.Template {
t.Helper()
tmpl, err := template.New("testTemplate").Funcs(helperFunctions).Parse(`{{replaceAll .InputString .OldSubstring .NewSubstring}}`)
if err != nil {
t.Fatalf("Failed to parse template: %v", err)
}
return tmpl
}

func TestGenerateProxySetHeadersForValidHeadersInMaster(t *testing.T) {
t.Parallel()

Expand Down
37 changes: 37 additions & 0 deletions internal/configs/version2/template_helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,34 @@ func newContainsTemplate(t *testing.T) *template.Template {
return tmpl
}

func TestReplaceAll(t *testing.T) {
t.Parallel()

tmpl := newReplaceAll(t)
testCases := []struct {
InputString string
OldSubstring string
NewSubstring string
expected string
}{
{InputString: "foobarfoo", OldSubstring: "bar", NewSubstring: "foo", expected: "foofoofoo"},
{InputString: "footest", OldSubstring: "test", NewSubstring: "bar", expected: "foobar"},
{InputString: "barfoo", OldSubstring: "bar", NewSubstring: "test", expected: "testfoo"},
{InputString: "foofoofoo", OldSubstring: "foo", NewSubstring: "bar", expected: "barbarbar"},
}

for _, tc := range testCases {
var buf bytes.Buffer
err := tmpl.Execute(&buf, tc)
if err != nil {
t.Fatalf("Failed to execute the template %v", err)
}
if buf.String() != tc.expected {
t.Errorf("Template generated wrong config, got %v but expected %v.", buf.String(), tc.expected)
}
}
}

func newHasPrefixTemplate(t *testing.T) *template.Template {
t.Helper()
tmpl, err := template.New("testTemplate").Funcs(helperFunctions).Parse(`{{hasPrefix .InputString .Prefix}}`)
Expand Down Expand Up @@ -358,3 +386,12 @@ func newMakeSecretPathTemplate(t *testing.T) *template.Template {
}
return tmpl
}

func newReplaceAll(t *testing.T) *template.Template {
t.Helper()
tmpl, err := template.New("testTemplate").Funcs(helperFunctions).Parse(`{{replaceAll .InputString .OldSubstring .NewSubstring}}`)
if err != nil {
t.Fatalf("Failed to parse template: %v", err)
}
return tmpl
}

0 comments on commit 3ce86ae

Please sign in to comment.