-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse_test.go
100 lines (80 loc) · 1.94 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
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
package env
import (
"errors"
"os"
"strconv"
"testing"
"github.com/blugnu/test"
)
func TestParse(t *testing.T) {
// ARRANGE
defer test.Using(&osLookupEnv, func(string) (string, bool) {
return "123", true
})()
// ACT
value, err := Parse("VAR", strconv.Atoi)
// ASSERT
test.That(t, value).Equals(123)
test.That(t, err).IsNil()
}
func TestParse_WhenVariableNotSet(t *testing.T) {
// ARRANGE
defer test.Using(&osLookupEnv, func(string) (string, bool) {
return "", false
})()
// ACT
value, err := Parse("NOT_SET", func(s string) (string, error) { return s, nil })
// ASSERT
test.That(t, value).Equals("")
test.Error(t, err).Is(ErrNotSet)
}
func TestParse_WhenConversionFails(t *testing.T) {
// ARRANGE
converr := errors.New("conversion error")
defer test.Using(&osLookupEnv, func(string) (string, bool) {
return "not-a-number", true
})()
// ACT
value, err := Parse("NOT_A_NUMBER", func(s string) (int, error) { return 0, converr })
// ASSERT
test.That(t, value).Equals(0)
test.Error(t, err).Is(InvalidValueError{Value: "not-a-number", Err: converr})
}
func TestOverride(t *testing.T) {
// ARRANGE
var value = 42
defer State().Reset()
os.Clearenv()
os.Setenv("VAR", "123")
// ACT
result, err := Override(&value, "VAR", strconv.Atoi)
// ASSERT
test.That(t, err).IsNil()
test.IsTrue(t, result)
test.That(t, value).Equals(123)
}
func TestOverride_WhenValueIsNotChanged(t *testing.T) {
// ARRANGE
var value = 123
defer State().Reset()
os.Clearenv()
os.Setenv("VAR", "123")
// ACT
result, err := Override(&value, "VAR", strconv.Atoi)
// ASSERT
test.That(t, err).IsNil()
test.IsFalse(t, result)
test.That(t, value).Equals(123)
}
func TestOverride_WhenVariableIsNotSet(t *testing.T) {
// ARRANGE
var value = 42
defer State().Reset()
os.Clearenv()
// ACT
result, err := Override(&value, "VAR", strconv.Atoi)
// ASSERT
test.Error(t, err).Is(ErrNotSet)
test.IsFalse(t, result)
test.That(t, value).Equals(42)
}