Path: blob/dev/pkg/protocols/common/replacer/replacer_test.go
2072 views
package replacer12import (3"testing"45"github.com/stretchr/testify/require"6)78func TestReplacerReplace(t *testing.T) {9tests := []struct {10name string11template string12values map[string]interface{}13expected string14}{15{16name: "Invalid arguments",17template: "",18values: nil,19expected: "",20},21{22name: "Nested",23template: "{{base64_encode('{{test}}')}}",24values: map[string]interface{}{"test": "random"},25expected: "{{base64_encode('random')}}",26},27{28name: "Basic",29template: "{{test}} §hello§ {{data}}",30values: map[string]interface{}{"test": "random", "hello": "world"},31expected: "random world {{data}}",32},33{34name: "No template variables",35template: "Nothing to replace",36values: map[string]interface{}{"test": "random", "hello": "world"},37expected: "Nothing to replace",38},39{40name: "Nested variable",41template: "{{§var1§}} and §{{var2}}§",42values: map[string]interface{}{"var1": "variable 1", "var2": "variable 2"},43expected: "{{variable 1}} and §variable 2§",44},45{46name: "Space in variable name",47template: "{{var 1}} has a space",48values: map[string]interface{}{"var 1": "variable 1"},49expected: "variable 1 has a space",50},51{52name: "Escaped marker in template",53template: "{{\\§var 1\\§}}",54values: map[string]interface{}{"\\§var 1\\§": "variable 1"},55expected: "variable 1",56},57{58name: "Escaping no marker in template",59template: "{{\\§var 1\\§}}",60values: map[string]interface{}{"var 1": "variable 1"},61expected: "{{\\§var 1\\§}}",62},63{64name: "Empty variable name",65template: "{{}} §§ no vars here",66values: map[string]interface{}{"var 1": "variable 1"},67expected: "{{}} §§ no vars here",68},69{70name: "Multiple replacement",71template: "{{var1}} and §var1§ and another {{var2}}",72values: map[string]interface{}{"var1": "first variable", "var2": "second variable"},73expected: "first variable and first variable and another second variable",74},75}7677for _, test := range tests {78t.Run(test.name, func(t *testing.T) {79require.Equal(t, test.expected, Replace(test.template, test.values))80})81}82}8384func TestReplacerReplaceOne(t *testing.T) {85tests := []struct {86name string87template string88key string89value interface{}90expected string91}{92{93name: "Basic",94template: "once upon a time there was a {{var1}}",95key: "var1",96value: "variable 1",97expected: "once upon a time there was a variable 1",98},99{100name: "Basic Multiple Vars",101template: "once upon a time there was a {{var1}} and a §var2§",102key: "var2",103value: "variable 2",104expected: "once upon a time there was a {{var1}} and a variable 2",105},106{107name: "Missing key",108template: "once upon a time there was a {{var1}}",109key: "",110value: "variable 1",111expected: "once upon a time there was a {{var1}}",112},113{114name: "Replacement value empty",115template: "{{var1}}nothing{{var1}} to{{var1}} see",116key: "var1",117value: "",118expected: "nothing{{var1}} to{{var1}} see",119},120{121name: "Empty key and value different markers",122template: "{{}}both§§ the{{}} 1st and 2nd markers are replaced",123key: "",124value: "",125expected: "both the{{}} 1st and 2nd markers are replaced",126},127{128name: "Empty key and value same marker",129template: "{{}}only{{}} the first marker is replaced{{}}",130key: "",131value: "",132expected: "only{{}} the first marker is replaced{{}}",133},134}135for _, test := range tests {136t.Run(test.name, func(t *testing.T) {137require.Equal(t, test.expected, ReplaceOne(test.template, test.key, test.value))138})139}140}141142143