-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathparse_test.go
61 lines (55 loc) · 1.17 KB
/
parse_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
package goconf
import (
"os"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
type Conf struct {
Name string `env:"MY_NAME"`
Age int `env:"MY_AGE"`
Team string `env:"MY_TEAM"`
}
func TestParseEnv(t *testing.T) {
updateEnv()
tests := []struct {
name string
input Conf
expectedOutput Conf
expectedErr string
}{
{
name: "Successfully parsed the configs",
input: Conf{},
expectedOutput: Conf{
Name: "coderx",
Age: 99,
Team: "backend",
},
expectedErr: "",
},
{
name: "config parse failure scenario",
input: Conf{},
expectedOutput: Conf{},
expectedErr: "env: expected a pointer to a Struct",
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
if test.expectedErr == "" {
err := ParseEnv(&test.input)
assert.NoError(t, err)
assert.Equal(t, test.expectedOutput, test.input)
} else {
err := ParseEnv(test.input)
require.ErrorContains(t, err, test.expectedErr)
}
})
}
}
func updateEnv() {
_ = os.Setenv("MY_NAME", "coderx")
_ = os.Setenv("MY_AGE", "99")
_ = os.Setenv("MY_TEAM", "backend")
}