Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue 2460: extended unit tests for replacer #2691

Merged
merged 1 commit into from
Oct 10, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
129 changes: 126 additions & 3 deletions v2/pkg/protocols/common/replacer/replacer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,133 @@ package replacer
import (
"testing"

"github.com/stretchr/testify/require"
"github.com/stretchr/testify/assert"
)

func TestReplacerReplace(t *testing.T) {
replaced := Replace("{{test}} §hello§ {{data}}", map[string]interface{}{"test": "random", "hello": "world"})
require.Equal(t, "random world {{data}}", replaced, "could not get correct replaced data")
tests := []struct {
name string
template string
values map[string]interface{}
expected string
}{
{
name: "Invalid arguments",
template: "",
values: nil,
expected: "",
},
{
name: "Basic",
template: "{{test}} §hello§ {{data}}",
values: map[string]interface{}{"test": "random", "hello": "world"},
expected: "random world {{data}}",
},
{
name: "No template variables",
template: "Nothing to replace",
values: map[string]interface{}{"test": "random", "hello": "world"},
expected: "Nothing to replace",
},
{
name: "Nested variable",
template: "{{§var1§}} and §{{var2}}§",
values: map[string]interface{}{"var1": "variable 1", "var2": "variable 2"},
expected: "{{variable 1}} and §variable 2§",
},
{
name: "Space in variable name",
template: "{{var 1}} has a space",
values: map[string]interface{}{"var 1": "variable 1"},
expected: "variable 1 has a space",
},
{
name: "Escaped marker in template",
template: "{{\\§var 1\\§}}",
values: map[string]interface{}{"\\§var 1\\§": "variable 1"},
expected: "variable 1",
},
{
name: "Escaping no marker in template",
template: "{{\\§var 1\\§}}",
values: map[string]interface{}{"var 1": "variable 1"},
expected: "{{\\§var 1\\§}}",
},
{
name: "Empty variable name",
template: "{{}} §§ no vars here",
values: map[string]interface{}{"var 1": "variable 1"},
expected: "{{}} §§ no vars here",
},
{
name: "Multiple replacement",
template: "{{var1}} and §var1§ and another {{var2}}",
values: map[string]interface{}{"var1": "first variable", "var2": "second variable"},
expected: "first variable and first variable and another second variable",
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
assert.Equal(t, test.expected, Replace(test.template, test.values))
})
}
}

func TestReplacerReplaceOne(t *testing.T) {
tests := []struct {
name string
template string
key string
value interface{}
expected string
}{
{
name: "Basic",
template: "once upon a time there was a {{var1}}",
key: "var1",
value: "variable 1",
expected: "once upon a time there was a variable 1",
},
{
name: "Basic Multiple Vars",
template: "once upon a time there was a {{var1}} and a §var2§",
key: "var2",
value: "variable 2",
expected: "once upon a time there was a {{var1}} and a variable 2",
},
{
name: "Missing key",
template: "once upon a time there was a {{var1}}",
key: "",
value: "variable 1",
expected: "once upon a time there was a {{var1}}",
},
{
name: "Replacement value empty",
template: "{{var1}}nothing{{var1}} to{{var1}} see",
key: "var1",
value: "",
expected: "nothing{{var1}} to{{var1}} see",
},
{
name: "Empty key and value different markers",
template: "{{}}both§§ the{{}} 1st and 2nd markers are replaced",
key: "",
value: "",
expected: "both the{{}} 1st and 2nd markers are replaced",
},
{
name: "Empty key and value same marker",
template: "{{}}only{{}} the first marker is replaced{{}}",
key: "",
value: "",
expected: "only{{}} the first marker is replaced{{}}",
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
assert.Equal(t, test.expected, ReplaceOne(test.template, test.key, test.value))
})
}
}