|
| 1 | +package settingo |
| 2 | + |
| 3 | +import ( |
| 4 | + "os" |
| 5 | + "reflect" |
| 6 | + "testing" |
| 7 | +) |
| 8 | + |
| 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 | +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"` |
| 58 | +} |
| 59 | + |
| 60 | +func Test_struct_types_os_env(t *testing.T) { |
| 61 | + |
| 62 | + expected := "other value" |
| 63 | + os.Setenv("FOOBAR", expected) |
| 64 | + defaultStr := "default value" |
| 65 | + |
| 66 | + expectedInt := 44 |
| 67 | + os.Setenv("FOOBARINT", "44") |
| 68 | + defaultInt := 42 |
| 69 | + |
| 70 | + os.Setenv("FOOBARBOOL", "y") |
| 71 | + expectedBool := true |
| 72 | + defaultBool := false |
| 73 | + |
| 74 | + os.Setenv("FOOBARMAP", "foo:bar;foo1:bar1,bar2") |
| 75 | + expectedMap := make(map[string][]string) |
| 76 | + defaultMap := make(map[string][]string) |
| 77 | + |
| 78 | + expectedMap["foo"] = []string{"bar"} |
| 79 | + expectedMap["foo1"] = []string{"bar1", "bar2"} |
| 80 | + |
| 81 | + config := &ExampleConfig{ |
| 82 | + Foobar: defaultStr, |
| 83 | + FoobarInt: defaultInt, |
| 84 | + FoobarBool: defaultBool, |
| 85 | + FoobarMap: defaultMap, |
| 86 | + } |
| 87 | + |
| 88 | + SETTINGS.ParseTo(config) |
| 89 | + |
| 90 | + if config.Foobar != expected { |
| 91 | + t.Error(config.Foobar, " != ", expected) |
| 92 | + } |
| 93 | + |
| 94 | + if config.FoobarInt != expectedInt { |
| 95 | + t.Error(config.FoobarInt, " != ", expectedInt) |
| 96 | + } |
| 97 | + |
| 98 | + if config.FoobarBool != expectedBool { |
| 99 | + t.Error(config.FoobarBool, " != ", expectedBool) |
| 100 | + } |
| 101 | + |
| 102 | + if !reflect.DeepEqual(config.FoobarMap, expectedMap) { |
| 103 | + t.Error(config.FoobarMap, " != ", expectedMap) |
| 104 | + } |
| 105 | + |
| 106 | + // Cleanup |
| 107 | + os.Unsetenv("FOOBAR") |
| 108 | + os.Unsetenv("FOOBAR_INT") |
| 109 | + os.Unsetenv("FOOBAR_BOOL") |
| 110 | + os.Unsetenv("FOOBAR_MAP") |
| 111 | +} |
0 commit comments