-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathenvfmt_test.go
119 lines (112 loc) · 2.84 KB
/
envfmt_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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package modmake
import (
"fmt"
"os"
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestF(t *testing.T) {
tests := map[string]struct {
in string
out string
data EnvMap
}{
"Case insensitive reference": {
in: "A string with a name of ${name}",
out: "A string with a name of Bob",
data: EnvMap{
"NAME": "Bob",
},
},
"Case insensitive data key": {
in: "A string with a name of ${NAME}",
out: "A string with a name of Bob",
data: EnvMap{
"name": "Bob",
},
},
"Nil data": {
in: "Nothing here ${here}",
out: "Nothing here ",
data: nil,
},
"Variable with no value": {
in: "Nothing here ${here}",
out: "Nothing here ",
data: EnvMap{
"here": "",
},
},
"Dollars aren't enough to trigger interpolation": {
in: "Here's a $VALUE",
out: "Here's a $VALUE",
data: EnvMap{
"VALUE": "Should not be substituted",
},
},
"Financial figures aren't interpolation": {
in: "Here's $10.00",
out: "Here's $10.00",
data: EnvMap{
"10.00": "11.00",
"10": "11",
},
},
"Braces aren't enough to trigger interpolation": {
in: "Here's a $ {VALUE}",
out: "Here's a $ {VALUE}",
data: EnvMap{
"VALUE": "Should not be substituted",
},
},
"Variable names can have space padding": {
in: "A string with a name of ${ \tname \t\n}",
out: "A string with a name of Bob",
data: EnvMap{
"NAME": "Bob",
},
},
"Variables can have default values": {
in: "A string with a name of ${ name : John }",
out: "A string with a name of John",
data: nil,
},
}
for name, tc := range tests {
tc := tc
t.Run(name, func(t *testing.T) {
result := F(tc.in, tc.data)
assert.Equal(t, tc.out, result)
result = string(FReader(strings.NewReader(tc.in), tc.data))
assert.Equal(t, tc.out, result)
})
}
}
func TestF_DynamicVariables(t *testing.T) {
const (
nonExistentKey = "SOME_KEY_THAT_SHOULD_NOT_BE_A_THING"
value = "some value"
)
oldEnv := Environment()
require.Equal(t, "", oldEnv[nonExistentKey])
assert.NoError(t, os.Setenv(nonExistentKey, value))
newEnv := Environment()
assert.Equal(t, "", oldEnv[nonExistentKey])
assert.NotEqual(t, oldEnv[nonExistentKey], newEnv[nonExistentKey])
assert.Equal(t, value, newEnv[nonExistentKey])
}
func ExampleF() {
fmt.Println(F("My string has a variable reference ${SOME_BUILD_NUM}?", EnvMap{
"SOME_BUILD_NUM": "1",
}))
fmt.Println(F("My string has a variable reference ${:$}{SOME_BUILD_NUM}?"))
fmt.Println(F("My string that references build ${SOME_BUILD_NUM}."))
fmt.Println(F("My string that references build ${SOME_BUILD_NUM:0}."))
// Output:
// My string has a variable reference 1?
// My string has a variable reference ${SOME_BUILD_NUM}?
// My string that references build .
// My string that references build 0.
}