From 3ce86ae4401ec05235e51a0155a91cfe1c57bc7f Mon Sep 17 00:00:00 2001 From: Venktesh Shivam Patel Date: Wed, 1 May 2024 13:12:33 +0100 Subject: [PATCH] Add unit tests for replaceAll (#5481) --- .../configs/version1/template_helper_test.go | 37 +++++++++++++++++++ .../configs/version2/template_helper_test.go | 37 +++++++++++++++++++ 2 files changed, 74 insertions(+) diff --git a/internal/configs/version1/template_helper_test.go b/internal/configs/version1/template_helper_test.go index ff1572d569..684f69860e 100644 --- a/internal/configs/version1/template_helper_test.go +++ b/internal/configs/version1/template_helper_test.go @@ -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() @@ -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() diff --git a/internal/configs/version2/template_helper_test.go b/internal/configs/version2/template_helper_test.go index 87890f702c..8b0e9b99d9 100644 --- a/internal/configs/version2/template_helper_test.go +++ b/internal/configs/version2/template_helper_test.go @@ -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}}`) @@ -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 +}