forked from evcc-io/evcc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathformat_test.go
72 lines (62 loc) · 1.14 KB
/
format_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
package util
import (
"math"
"testing"
)
func TestTruish(t *testing.T) {
cases := []struct {
k string
v bool
}{
{"", false},
{"false", false},
{"0", false},
{"off", false},
{"true", true},
{"1", true},
{"on", true},
}
for _, c := range cases {
b := Truish(c.k)
if b != c.v {
t.Errorf("expected %v got %v", c.v, b)
}
}
}
func TestReplace(t *testing.T) {
cases := []struct {
k string
v interface{}
fmt, expected string
}{
// regex tests
{"foo", true, "${foo}", "true"},
{"foo", "1", "abc${foo}${foo}", "abc11"},
{"foo", math.Pi, "${foo:%.2f}", "3.14"},
}
for _, c := range cases {
s, err := ReplaceFormatted(c.fmt, map[string]interface{}{
c.k: c.v,
})
if s != c.expected || err != nil {
t.Error(s, err)
}
}
}
func TestReplaceMulti(t *testing.T) {
s, err := ReplaceFormatted("${foo}-${bar}", map[string]interface{}{
"foo": "bar",
"bar": "baz",
})
if s != "bar-baz" || err != nil {
t.Error(s, err)
}
}
func TestReplaceNoMatch(t *testing.T) {
s, err := ReplaceFormatted("${foo}", map[string]interface{}{
"bar": "baz",
})
if err == nil {
t.Error(s, err)
}
}