summaryrefslogtreecommitdiff
path: root/replacer_test.go
diff options
context:
space:
mode:
authorMatthew Holt <mholt@users.noreply.github.com>2020-03-30 11:49:53 -0600
committerMatthew Holt <mholt@users.noreply.github.com>2020-03-30 11:49:53 -0600
commit105acfa08664c97460a6fe3fb49635618be5bcb2 (patch)
tree3c8ea6bc0fa1d335787a5b4852bd03ae50d4ef81 /replacer_test.go
parentdeba26d225c5b321a944439eb6b108117ac3d569 (diff)
Keep type information with placeholders until replacements happen
Diffstat (limited to 'replacer_test.go')
-rw-r--r--replacer_test.go87
1 files changed, 50 insertions, 37 deletions
diff --git a/replacer_test.go b/replacer_test.go
index a48917a..d6ac033 100644
--- a/replacer_test.go
+++ b/replacer_test.go
@@ -173,41 +173,12 @@ func TestReplacer(t *testing.T) {
}
}
-func BenchmarkReplacer(b *testing.B) {
- type testCase struct {
- name, input, empty string
- }
-
- rep := testReplacer()
-
- for _, bm := range []testCase{
- {
- name: "no placeholder",
- input: `simple string`,
- },
- {
- name: "placeholder",
- input: `{"json": "object"}`,
- },
- {
- name: "escaped placeholder",
- input: `\{"json": \{"nested": "{bar}"\}\}`,
- },
- } {
- b.Run(bm.name, func(b *testing.B) {
- for i := 0; i < b.N; i++ {
- rep.ReplaceAll(bm.input, bm.empty)
- }
- })
- }
-}
-
func TestReplacerSet(t *testing.T) {
rep := testReplacer()
for _, tc := range []struct {
variable string
- value string
+ value interface{}
}{
{
variable: "test1",
@@ -218,6 +189,10 @@ func TestReplacerSet(t *testing.T) {
value: "123",
},
{
+ variable: "numbers",
+ value: 123.456,
+ },
+ {
variable: "äöü",
value: "öö_äü",
},
@@ -252,7 +227,7 @@ func TestReplacerSet(t *testing.T) {
// test if all keys are still there (by length)
length := len(rep.static)
- if len(rep.static) != 7 {
+ if len(rep.static) != 8 {
t.Errorf("Expected length '%v' got '%v'", 7, length)
}
}
@@ -261,7 +236,7 @@ func TestReplacerReplaceKnown(t *testing.T) {
rep := Replacer{
providers: []ReplacerFunc{
// split our possible vars to two functions (to test if both functions are called)
- func(key string) (val string, ok bool) {
+ func(key string) (val interface{}, ok bool) {
switch key {
case "test1":
return "val1", true
@@ -275,7 +250,7 @@ func TestReplacerReplaceKnown(t *testing.T) {
return "NOOO", false
}
},
- func(key string) (val string, ok bool) {
+ func(key string) (val interface{}, ok bool) {
switch key {
case "1":
return "test-123", true
@@ -331,7 +306,7 @@ func TestReplacerReplaceKnown(t *testing.T) {
func TestReplacerDelete(t *testing.T) {
rep := Replacer{
- static: map[string]string{
+ static: map[string]interface{}{
"key1": "val1",
"key2": "val2",
"key3": "val3",
@@ -366,10 +341,10 @@ func TestReplacerMap(t *testing.T) {
rep := testReplacer()
for i, tc := range []ReplacerFunc{
- func(key string) (val string, ok bool) {
+ func(key string) (val interface{}, ok bool) {
return "", false
},
- func(key string) (val string, ok bool) {
+ func(key string) (val interface{}, ok bool) {
return "", false
},
} {
@@ -434,12 +409,50 @@ func TestReplacerNew(t *testing.T) {
}
}
}
+}
+func BenchmarkReplacer(b *testing.B) {
+ type testCase struct {
+ name, input, empty string
+ }
+
+ rep := testReplacer()
+ rep.Set("str", "a string")
+ rep.Set("int", 123.456)
+
+ for _, bm := range []testCase{
+ {
+ name: "no placeholder",
+ input: `simple string`,
+ },
+ {
+ name: "string replacement",
+ input: `str={str}`,
+ },
+ {
+ name: "int replacement",
+ input: `int={int}`,
+ },
+ {
+ name: "placeholder",
+ input: `{"json": "object"}`,
+ },
+ {
+ name: "escaped placeholder",
+ input: `\{"json": \{"nested": "{bar}"\}\}`,
+ },
+ } {
+ b.Run(bm.name, func(b *testing.B) {
+ for i := 0; i < b.N; i++ {
+ rep.ReplaceAll(bm.input, bm.empty)
+ }
+ })
+ }
}
func testReplacer() Replacer {
return Replacer{
providers: make([]ReplacerFunc, 0),
- static: make(map[string]string),
+ static: make(map[string]interface{}),
}
}