-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lookup_test.go
103 lines (93 loc) · 2.16 KB
/
lookup_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
// Copyright (c) 2023, Roel Schut. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package env
import (
"os"
"reflect"
"testing"
"github.com/stretchr/testify/assert"
)
func TestLookupFrom(t *testing.T) {
map1 := Map{"foo": "bar", "qux": "xoo"}
map2 := Map{"bruce": "batman", "clark": "superman"}
var se2 error
se1, ok := os.LookupEnv("GOROOT")
if !ok {
se2 = ErrNotFound
}
tests := map[string]struct {
src []Lookupper
key string
wantVal Value
wantErr error
}{
"empty map": {
src: []Lookupper{},
key: "foo",
wantErr: ErrNotFound,
},
"one map": {
src: []Lookupper{map1},
key: "foo",
wantVal: "bar",
},
"one map, invalid key": {
src: []Lookupper{map1},
key: "bar",
wantErr: ErrNotFound,
},
"two maps": {
src: []Lookupper{map1, map2},
key: "clark",
wantVal: "superman",
},
"two maps, invalid key": {
src: []Lookupper{map1, map2},
key: "peter",
wantErr: ErrNotFound,
},
"system env": {
src: []Lookupper{map1, map2, System()},
key: "GOROOT",
wantVal: Value(se1),
wantErr: se2,
},
}
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
haveVal, haveErr := Lookup(tc.key, tc.src...)
assert.Exactly(t, tc.wantVal, haveVal)
if tc.wantErr == nil {
assert.Nil(t, haveErr)
} else {
assert.True(t, IsNotFound(haveErr))
}
})
}
}
func TestChain(t *testing.T) {
t.Run("single", func(t *testing.T) {
want := System()
have := Chain(want)
assert.Exactly(t, reflect.ValueOf(want).Pointer(), reflect.ValueOf(have).Pointer())
})
t.Run("chain", func(t *testing.T) {
chain1 := Chain(System(), System())
assert.Len(t, chain1, 2)
chain2 := Chain(chain1, System())
assert.Len(t, chain2, 3)
})
t.Run("nil", func(t *testing.T) {
assert.Equal(t, chainLookupper{}, Chain(nil))
})
t.Run("nils", func(t *testing.T) {
assert.Equal(t, chainLookupper{}, Chain(nil, nil))
})
}
func TestChain_Lookup(t *testing.T) {
t.Run("nil", func(t *testing.T) {
_, err := Chain(nil).Lookup("doesnt matter")
assert.ErrorIs(t, err, ErrNotFound)
})
}