-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmap_replacer_test.go
91 lines (73 loc) · 2.66 KB
/
map_replacer_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package replacer_test
import (
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/thecasualcoder/replacer"
)
func TestLoadMapReplacerFromFile(t *testing.T) {
t.Run("should load pattern from file content", func(t *testing.T) {
filecontents := `{
"matchWith": "values",
"patterns": {
"pattern-1": "value-1",
"pattern-2": "value-2"
}
}`
expected := map[string]string{"pattern-1": "value-1", "pattern-2": "value-2"}
reader := strings.NewReader(filecontents)
r, err := replacer.LoadMapReplacerFromFile(reader)
re, ok := r.(replacer.MapReplacer)
assert.NoError(t, err)
assert.NotNil(t, r)
assert.True(t, ok)
assert.NotNil(t, re)
assert.Equal(t, "values", re.MatchWith)
assert.Equal(t, expected, re.Patterns)
})
t.Run("should return error for invalid file content", func(t *testing.T) {
invalidFilecontent := `{
notajson
}`
invalidJSONReader := strings.NewReader(invalidFilecontent)
_, err := replacer.LoadMapReplacerFromFile(invalidJSONReader)
assert.Error(t, err, "Should have failed parsing JSON")
})
}
func TestNewMapReplacer(t *testing.T) {
t.Run("should return new MapReplacer", func(t *testing.T) {
patterns := map[string]string{"pattern": "value"}
r := replacer.NewMapReplacer("values", patterns)
re, ok := r.(replacer.MapReplacer)
assert.NotNil(t, r)
assert.True(t, ok)
assert.NotNil(t, re)
assert.Equal(t, "values", re.MatchWith)
assert.Equal(t, patterns, re.Patterns)
})
}
func TestMapReplace(t *testing.T) {
t.Run("should replace value if key pattern matches", func(t *testing.T) {
patterns := map[string]string{"pattern": "replaced value"}
r := replacer.NewMapReplacer("key", patterns)
result, changed := r.Replace(map[string]string{"This is pattern-1": "value-1", "something": "random"})
expected := map[string]string{"This is pattern-1": "replaced value", "something": "random"}
assert.True(t, changed)
assert.Equal(t, expected, result)
})
t.Run("should replace value if value pattern matches", func(t *testing.T) {
patterns := map[string]string{"pattern": "replaced value"}
r := replacer.NewMapReplacer("value", patterns)
result, changed := r.Replace(map[string]string{"key-1": "This is pattern-1", "key-2": "random"})
expected := map[string]string{"key-1": "This is replaced value-1", "key-2": "random"}
assert.True(t, changed)
assert.Equal(t, expected, result)
})
t.Run("should not replace value if source is not map[string]string", func(t *testing.T) {
patterns := map[string]string{"pattern": "replaced value"}
r := replacer.NewMapReplacer("key", patterns)
result, changed := r.Replace("source")
assert.False(t, changed)
assert.Equal(t, "source", result)
})
}