1
1
package settingo
2
2
3
3
import (
4
+ "net/url"
4
5
"os"
5
6
"reflect"
6
7
"testing"
7
8
)
8
9
9
- type TestConfig struct {
10
- Foobar string `settingo:"help text for foobar"`
11
- FoobarInt int `settingo:"help text for FoobarInt"`
12
- FoobarBool bool `settingo:"help text for FoobarBool"`
13
- FoobarMap map [string ][]string `settingo:"help text FoobarMap"`
14
- }
15
-
16
- func Test_struct_types_default (t * testing.T ) {
17
-
18
- expected := "default_value_for_foobar"
19
- expectedInt := 42
20
- expectedBool := true
21
- expectedMap := make (map [string ][]string )
22
-
23
- expectedMap ["foo" ] = []string {"bar" }
24
- expectedMap ["foo1" ] = []string {"bar1" , "bar2" }
25
-
26
- config := & TestConfig {
27
- Foobar : expected ,
28
- FoobarInt : expectedInt ,
29
- FoobarBool : expectedBool ,
30
- FoobarMap : expectedMap ,
31
- }
32
-
33
- SETTINGS .LoadStruct (config )
34
-
35
- if config .Foobar != expected {
36
- t .Error (config .Foobar , " != " , expected )
37
- }
38
-
39
- if config .FoobarInt != expectedInt {
40
- t .Error (config .FoobarInt , " != " , expectedInt )
41
- }
42
-
43
- if config .FoobarBool != expectedBool {
44
- t .Error (config .FoobarBool , " != " , expectedBool )
45
- }
46
-
47
- if ! reflect .DeepEqual (config .FoobarMap , expectedMap ) {
48
- t .Error (config .FoobarMap , " != " , expectedMap )
49
- }
50
-
51
- }
52
-
53
10
type ExampleConfig struct {
54
- Foobar string `settingo:"help text for foobar"`
55
- FoobarInt int `settingo:"help text for FoobarInt"`
56
- FoobarBool bool `settingo:"help text for FoobarBool"`
57
- FoobarMap map [string ][]string `settingo:"help text FoobarMap"`
11
+ Foobar string `settingo:"help text for foobar"`
12
+ FoobarInt int `settingo:"help text for FoobarInt"`
13
+ FoobarBool bool `settingo:"help text for FoobarBool"`
14
+ FoobarMap map [string ][]string `settingo:"help text FoobarMap"`
15
+ FoobarSlice []string `settingo:"help text for FoobarSlice"`
16
+ FooParse string `settingo:"help text for FooParse"`
17
+ FooParseInt int `settingo:"help text for FooParseInt"`
58
18
}
59
19
60
20
func Test_struct_types_os_env (t * testing.T ) {
61
-
62
21
expected := "other value"
63
22
os .Setenv ("FOOBAR" , expected )
64
23
defaultStr := "default value"
@@ -78,14 +37,59 @@ func Test_struct_types_os_env(t *testing.T) {
78
37
expectedMap ["foo" ] = []string {"bar" }
79
38
expectedMap ["foo1" ] = []string {"bar1" , "bar2" }
80
39
40
+ os .Setenv ("FOOBARSLICE" , "item1,item2,item3" )
41
+ expectedSlice := []string {"item1" , "item2" , "item3" }
42
+ defaultSlice := []string {}
43
+
44
+ os .
Setenv (
"FOOPARSE" ,
"postgres://user:[email protected] :5432/mydb" )
45
+ expectedFooParse := "database.example.com"
46
+ defaultFooParse := "foobar"
47
+
48
+ SetParsed ("FOOPARSE" , defaultFooParse , "database hostname" , func (input string ) string {
49
+ u , err := url .Parse (input )
50
+ if err != nil {
51
+ return input
52
+ }
53
+ return u .Hostname ()
54
+ })
55
+
56
+ Set ("FOOBAR" , defaultStr , "help text for foobar" )
57
+ SetString ("FOOBAR" , defaultStr , "help text for foobar" )
58
+ SetInt ("FOOBARINT" , defaultInt , "help text for FoobarInt" )
59
+ SetBool ("FOOBARBOOL" , defaultBool , "help text for FoobarBool" )
60
+ SetMap ("FOOBARMAP" , defaultMap , "help text FoobarMap" )
61
+ SetSlice ("FOOBARSLICE" , defaultSlice , "help text for FoobarSlice" , "," )
62
+
81
63
config := & ExampleConfig {
82
- Foobar : defaultStr ,
83
- FoobarInt : defaultInt ,
84
- FoobarBool : defaultBool ,
85
- FoobarMap : defaultMap ,
64
+ Foobar : defaultStr ,
65
+ FoobarInt : defaultInt ,
66
+ FoobarBool : defaultBool ,
67
+ FoobarMap : defaultMap ,
68
+ FoobarSlice : defaultSlice ,
69
+ FooParse : defaultFooParse ,
86
70
}
87
71
88
- SETTINGS .ParseTo (config )
72
+ ParseTo (config )
73
+
74
+ if Get ("FOOBAR" ) != expected {
75
+ t .Error (Get ("FOOBAR" ), " != " , expected )
76
+ }
77
+
78
+ if GetInt ("FOOBARINT" ) != expectedInt {
79
+ t .Error (GetInt ("FOOBARINT" ), " != " , expectedInt )
80
+ }
81
+
82
+ if GetBool ("FOOBARBOOL" ) != expectedBool {
83
+ t .Error (GetBool ("FOOBARBOOL" ), " != " , expectedBool )
84
+ }
85
+
86
+ if ! reflect .DeepEqual (GetMap ("FOOBARMAP" ), expectedMap ) {
87
+ t .Error (GetMap ("FOOBARMAP" ), " != " , expectedMap )
88
+ }
89
+
90
+ if ! reflect .DeepEqual (GetSlice ("FOOBARSLICE" ), expectedSlice ) {
91
+ t .Error (GetSlice ("FOOBARSLICE" ), " != " , expectedSlice )
92
+ }
89
93
90
94
if config .Foobar != expected {
91
95
t .Error (config .Foobar , " != " , expected )
@@ -103,9 +107,20 @@ func Test_struct_types_os_env(t *testing.T) {
103
107
t .Error (config .FoobarMap , " != " , expectedMap )
104
108
}
105
109
110
+ if ! reflect .DeepEqual (config .FoobarSlice , expectedSlice ) {
111
+ t .Error (config .FoobarSlice , " != " , expectedSlice )
112
+ }
113
+
114
+ if config .FooParse != expectedFooParse {
115
+ t .Error (config .FooParse , " != " , expectedFooParse )
116
+ }
117
+
106
118
// Cleanup
107
119
os .Unsetenv ("FOOBAR" )
108
- os .Unsetenv ("FOOBAR_INT" )
109
- os .Unsetenv ("FOOBAR_BOOL" )
110
- os .Unsetenv ("FOOBAR_MAP" )
120
+ os .Unsetenv ("FOOBARINT" )
121
+ os .Unsetenv ("FOOBARBOOL" )
122
+ os .Unsetenv ("FOOBARMAP" )
123
+ os .Unsetenv ("FOOBARSLICE" )
124
+ os .Unsetenv ("FOOPARSE" )
125
+ os .Unsetenv ("FOOPARSEINT" )
111
126
}
0 commit comments