-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
replace_test.go
69 lines (64 loc) · 1.96 KB
/
replace_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
// 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 (
"testing"
"github.com/stretchr/testify/assert"
)
func TestReplaceAll(t *testing.T) {
tests := map[string]struct {
input Map
want Map
wantErr error
}{
"none": {
input: map[string]Value{"foo": "bar"},
want: map[string]Value{"foo": "bar"},
},
"basic": {
input: map[string]Value{"foo": `$bar`, "bar": "baz"},
want: map[string]Value{"foo": "baz", "bar": "baz"},
},
"double": {
input: map[string]Value{"foo": `$bar and $bar`, "bar": "baz"},
want: map[string]Value{"foo": "baz and baz", "bar": "baz"},
},
"missing": {
input: map[string]Value{"foo": `$missing var`, "bar": "baz"},
want: map[string]Value{"foo": "$missing var", "bar": "baz"},
},
"bash style": {
input: map[string]Value{"foo": `${bar}`, "bar": "baz"},
want: map[string]Value{"foo": "baz", "bar": "baz"},
},
"bash style with default": {
input: map[string]Value{"foo": `look at ${baz:-that}`, "bar": "baz"},
want: map[string]Value{"foo": "look at that", "bar": "baz"},
},
"dependencies": {
input: map[string]Value{"foo": `$bar`, "bar": `${qux:-baz}`},
want: map[string]Value{"foo": "baz", "bar": "baz"},
},
"multiple dependencies": {
input: map[string]Value{"foo": `some $bar ${qux} thing`, "bar": `$qux`, "qux": "xoo"},
want: map[string]Value{"foo": "some xoo xoo thing", "bar": "xoo", "qux": "xoo"},
},
"circular dependency": {
input: map[string]Value{"foo": `$bar`, "bar": `$foo`},
want: map[string]Value{"foo": `$bar`, "bar": `$foo`},
wantErr: ErrCircularDependency,
},
}
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
have, haveErr := ReplaceAll(tc.input)
assert.Equal(t, tc.want, have)
if tc.wantErr == nil {
assert.NoError(t, haveErr)
} else {
assert.ErrorIs(t, haveErr, tc.wantErr)
}
})
}
}