-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
encode_test.go
191 lines (173 loc) · 3.98 KB
/
encode_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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
// 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 (
"math"
"net/url"
"strconv"
"strings"
"testing"
"time"
"github.com/go-pogo/env/envtag"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestNewEncoder(t *testing.T) {
t.Run("nil writer", func(t *testing.T) {
assert.PanicsWithValue(t, panicNilWriter, func() { NewEncoder(nil) })
})
}
func TestEncoder_WithWriter(t *testing.T) {
t.Run("nil writer", func(t *testing.T) {
assert.PanicsWithValue(t, panicNilWriter, func() {
NewEncoder(&strings.Builder{}).WithWriter(nil)
})
})
}
func TestEncoder(t *testing.T) {
urlInput, err := url.Parse("https://example.com")
require.NoError(t, err)
tests := map[string]struct {
setup func(enc *Encoder)
input any
want []string
}{
"Map": {
input: Map{
`foo`: `bar`,
`qux`: ``,
},
want: []string{
`foo=bar`,
`qux=`,
},
},
"map": {
input: map[string]Value{
`foo`: `${bar}`,
`qux`: `"xoo"`,
},
want: []string{
`foo=${bar}`,
`qux='"xoo"'`,
},
},
"NamedValues": {
input: []NamedValue{
{Name: `foo`, Value: `12.3`},
{Name: `qux`, Value: `'xoo'`},
},
want: []string{
`foo=12.3`,
`qux="'xoo'"`,
},
},
"Tag": {
input: []envtag.Tag{
{Name: `FOO`, Default: `bar'n "boos"`},
},
want: []string{
`FOO="bar'n \"boos\""`,
},
},
// basic types as fields
"bool": {
setup: takeValues,
input: struct{ Any bool }{Any: true},
want: []string{"ANY=true"},
},
"int": {
setup: takeValues,
input: struct{ Any int }{Any: -123},
want: []string{"ANY=-123"},
},
"uint": {
setup: takeValues,
input: struct{ Any uint }{Any: 123},
want: []string{"ANY=123"},
},
"float": {
setup: takeValues,
input: struct{ Any float64 }{Any: math.Pi},
want: []string{"ANY=" + strconv.FormatFloat(math.Pi, 'g', -1, 64)},
},
"string": {
setup: takeValues,
input: struct{ Any string }{Any: "foo"},
want: []string{"ANY=foo"},
},
"url": {
setup: takeValues,
input: struct{ Any *url.URL }{Any: urlInput},
want: []string{"ANY=https://example.com"},
},
"duration": {
setup: takeValues,
input: struct{ Any time.Duration }{Any: time.Second + (time.Minute * 3)},
want: []string{"ANY=3m1s"},
},
}
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
var buf strings.Builder
enc := NewEncoder(&buf)
if tc.setup != nil {
tc.setup(enc)
}
assert.NoError(t, enc.Encode(tc.input))
assertSimilarOutput(t, buf.String(), tc.want)
})
}
t.Run("struct", func(t *testing.T) {
type subj struct {
Foo string `default:"bar"`
Flag bool `default:"true"`
unexported bool //nolint:golint,unused
}
t.Run("default", func(t *testing.T) {
var buf strings.Builder
enc := NewEncoder(&buf)
assert.NoError(t, enc.Encode(subj{Foo: "foobar"}))
assertSimilarOutput(t, buf.String(), []string{
`FOO=bar`,
`FLAG=true`,
})
})
t.Run("take values", func(t *testing.T) {
var buf strings.Builder
enc := NewEncoder(&buf)
enc.TakeValues = true
assert.NoError(t, enc.Encode(subj{Foo: "foobar"}))
assertSimilarOutput(t, buf.String(), []string{
`FOO=foobar`,
`FLAG=true`,
})
})
})
t.Run("nested struct", func(t *testing.T) {
type nested struct {
Foo string `env:"FOO" default:"bar"`
unexported bool `env:"NOPE"` //nolint:golint,unused
}
type subj struct {
Qux string
Nested nested
}
var buf strings.Builder
assert.NoError(t, NewEncoder(&buf).Encode(subj{Qux: "x00"}))
assertSimilarOutput(t, buf.String(), []string{
`QUX=`,
`FOO=bar`,
})
})
}
func assertSimilarOutput(t *testing.T, have string, want []string) {
assert.Len(t, have, 1+len(strings.Join(want, "\n")))
for _, line := range want {
assert.Contains(t, have, line)
}
}
func takeValues(enc *Encoder) {
enc.TakeValues = true
}