forked from hashicorp/levant
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkvflag_test.go
95 lines (90 loc) · 1.84 KB
/
kvflag_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
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package helper
import (
"reflect"
"testing"
"github.com/hashicorp/go-multierror"
"github.com/stretchr/testify/require"
)
func TestHelper_Set(t *testing.T) {
cases := []struct {
Label string
Inputs []string
Output map[string]interface{}
Error bool
}{
{
"simple value",
[]string{"key=value"},
map[string]interface{}{"key": "value"},
false,
},
{
"nested replaces simple",
[]string{"key=1", "key.nested=2"},
nil,
true,
},
{
"simple replaces nested",
[]string{"key.nested=2", "key=1"},
map[string]interface{}{"key": "1"},
false,
},
{
"nested siblings",
[]string{"nested.a=1", "nested.b=2"},
map[string]interface{}{"nested": map[string]interface{}{"a": "1", "b": "2"}},
false,
},
{
"nested singleton",
[]string{"nested.key=value"},
map[string]interface{}{"nested": map[string]interface{}{"key": "value"}},
false,
},
{
"nested with parent",
[]string{"root=a", "nested.key=value"},
map[string]interface{}{"root": "a", "nested": map[string]interface{}{"key": "value"}},
false,
},
{
"empty value",
[]string{"key="},
map[string]interface{}{"key": ""},
false,
},
{
"value contains equal sign",
[]string{"key=foo=bar"},
map[string]interface{}{"key": "foo=bar"},
false,
},
{
"missing equal sign",
[]string{"key"},
nil,
true,
},
}
for _, tc := range cases {
t.Run(tc.Label, func(t *testing.T) {
f := new(Flag)
mErr := multierror.Error{}
for _, input := range tc.Inputs {
err := f.Set(input)
if err != nil {
mErr.Errors = append(mErr.Errors, err)
}
}
if tc.Error {
require.Error(t, mErr.ErrorOrNil())
} else {
actual := map[string]interface{}(*f)
require.True(t, reflect.DeepEqual(actual, tc.Output))
}
})
}
}