/
source_test.go
97 lines (77 loc) · 1.8 KB
/
source_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
92
93
94
95
96
97
package config
import (
"errors"
"fmt"
"reflect"
"testing"
"github.com/stretchr/testify/assert"
"github.com/krostar/config/internal/trivialerr"
)
type stubSourceThatUseReflection map[string]string
func (s stubSourceThatUseReflection) Name() string { return "stub reflect" }
func (s stubSourceThatUseReflection) SetValueFromConfigTreePath(v *reflect.Value, name string) (bool, error) {
var (
str string
found bool
)
for key, resp := range s {
if name == key {
str = resp
found = true
break
}
}
if !found {
return false, nil
}
newV, err := InitializeNewValueOfTypeWithString(v.Type(), str)
if err != nil {
return false, fmt.Errorf("unable to initialize new value from %q: %w", str, err)
}
return SetNewValue(v, newV)
}
type stubSourceThatUnmarshal int
func (s stubSourceThatUnmarshal) Name() string { return "stub unmarshal" }
func (s stubSourceThatUnmarshal) Unmarshal(interface{}) error {
var err error
switch {
case s < 0:
err = trivialerr.New("not found")
case s > 0:
err = errors.New("not found")
}
return err
}
type dumbSource struct{}
func (dumbSource) Name() string { return "dumb" }
func Test_appendConfigTreePath(t *testing.T) {
var tests = map[string]struct {
parentName string
childName string
expectedName string
}{
"normal case": {
parentName: "parent",
childName: "child",
expectedName: "parent.child",
},
"no parent": {
parentName: "",
childName: "child",
expectedName: "child",
},
"uppercase": {
parentName: "ToTo",
childName: "tItI",
expectedName: "toto.titi",
},
}
for name, test := range tests {
var test = test
t.Run(name, func(t *testing.T) {
t.Parallel()
name := appendConfigTreePath(test.parentName, test.childName)
assert.Equal(t, test.expectedName, name)
})
}
}