-
Notifications
You must be signed in to change notification settings - Fork 0
/
string_test.go
116 lines (91 loc) · 2.7 KB
/
string_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
package dynflags_test
import (
"testing"
"github.com/containeroo/dynflags"
"github.com/stretchr/testify/assert"
)
func TestStringValue(t *testing.T) {
t.Parallel()
t.Run("Parse valid string", func(t *testing.T) {
t.Parallel()
stringValue := dynflags.StringValue{}
parsed, err := stringValue.Parse("example")
assert.NoError(t, err)
assert.Equal(t, "example", parsed)
})
t.Run("Set valid string", func(t *testing.T) {
t.Parallel()
bound := "initial"
stringValue := dynflags.StringValue{Bound: &bound}
err := stringValue.Set("updated")
assert.NoError(t, err)
assert.Equal(t, "updated", bound)
})
t.Run("Set invalid type", func(t *testing.T) {
t.Parallel()
bound := "initial"
stringValue := dynflags.StringValue{Bound: &bound}
err := stringValue.Set(123) // Invalid type
assert.Error(t, err)
assert.EqualError(t, err, "invalid value type: expected string")
})
}
func TestGroupConfigString(t *testing.T) {
t.Parallel()
t.Run("Define string flag", func(t *testing.T) {
t.Parallel()
group := &dynflags.ConfigGroup{Flags: make(map[string]*dynflags.Flag)}
defaultValue := "default"
group.String("stringFlag", defaultValue, "A string flag")
assert.Contains(t, group.Flags, "stringFlag")
assert.Equal(t, "A string flag", group.Flags["stringFlag"].Usage)
assert.Equal(t, defaultValue, group.Flags["stringFlag"].Default)
})
}
func TestParsedGroupGetString(t *testing.T) {
t.Parallel()
t.Run("Get existing string flag", func(t *testing.T) {
t.Parallel()
group := &dynflags.ParsedGroup{
Values: map[string]interface{}{
"stringFlag": "value",
},
}
str, err := group.GetString("stringFlag")
assert.NoError(t, err)
assert.Equal(t, "value", str)
})
t.Run("Get non-existent string flag", func(t *testing.T) {
t.Parallel()
group := &dynflags.ParsedGroup{
Values: map[string]interface{}{},
}
str, err := group.GetString("stringFlag")
assert.Error(t, err)
assert.Equal(t, "", str)
assert.EqualError(t, err, "flag 'stringFlag' not found in group ''")
})
t.Run("Get string flag with invalid type", func(t *testing.T) {
t.Parallel()
group := &dynflags.ParsedGroup{
Values: map[string]interface{}{
"stringFlag": 123, // Invalid type
},
}
str, err := group.GetString("stringFlag")
assert.Error(t, err)
assert.Equal(t, "", str)
assert.EqualError(t, err, "flag 'stringFlag' is not a string")
})
}
func TestStringGetBound(t *testing.T) {
t.Run("StringValue - GetBound", func(t *testing.T) {
var str *string
value := "test"
str = &value
stringValue := dynflags.StringValue{Bound: str}
assert.Equal(t, "test", stringValue.GetBound())
stringValue = dynflags.StringValue{Bound: nil}
assert.Nil(t, stringValue.GetBound())
})
}