diff options
Diffstat (limited to 'overwriting_test.go')
-rw-r--r-- | overwriting_test.go | 67 |
1 files changed, 67 insertions, 0 deletions
diff --git a/overwriting_test.go b/overwriting_test.go new file mode 100644 index 0000000..1ed5daa --- /dev/null +++ b/overwriting_test.go | |||
@@ -0,0 +1,67 @@ | |||
1 | package envcfg | ||
2 | |||
3 | import ( | ||
4 | "os" | ||
5 | "testing" | ||
6 | ) | ||
7 | |||
8 | func TestKeeping(t *testing.T) { | ||
9 | const ORIG_VAL = "Remember: testing is the future!" | ||
10 | |||
11 | os.Clearenv() | ||
12 | |||
13 | s := struct{ Field string }{ORIG_VAL} | ||
14 | |||
15 | ReadInto(&s) | ||
16 | |||
17 | if s.Field != ORIG_VAL { | ||
18 | t.Errorf("expected '%s', got '%s'", ORIG_VAL, s.Field) | ||
19 | } | ||
20 | } | ||
21 | |||
22 | func TestOverwriting(t *testing.T) { | ||
23 | const ENV_KEY = "FIELD" | ||
24 | const ENV_VAL = "Remember: testing is the future!" | ||
25 | const ORIG_VAL = "Testing is pointless!" | ||
26 | |||
27 | os.Clearenv() | ||
28 | os.Setenv(ENV_KEY, ENV_VAL) | ||
29 | |||
30 | s := struct{ Field string }{ORIG_VAL} | ||
31 | |||
32 | ReadInto(&s) | ||
33 | |||
34 | if s.Field != ENV_VAL { | ||
35 | t.Errorf("expected '%s', got '%s'", ENV_VAL, s.Field) | ||
36 | } | ||
37 | } | ||
38 | |||
39 | type superStruct struct { | ||
40 | SubStruct nestedFields | ||
41 | } | ||
42 | |||
43 | type nestedFields struct { | ||
44 | KeepMe string | ||
45 | OverwriteMe string | ||
46 | } | ||
47 | |||
48 | func TestMultiOverwriting(t *testing.T) { | ||
49 | const ENV_KEY = "SUBSTRUCT_OVERWRITEME" | ||
50 | const ENV_VAL = "Remember: testing is the future!" | ||
51 | const ORIG_VAL = "Testing is pointless!" | ||
52 | |||
53 | os.Clearenv() | ||
54 | os.Setenv(ENV_KEY, ENV_VAL) | ||
55 | |||
56 | s := superStruct{nestedFields{ORIG_VAL, ORIG_VAL}} | ||
57 | |||
58 | ReadInto(&s) | ||
59 | |||
60 | if s.SubStruct.KeepMe != ORIG_VAL { | ||
61 | t.Errorf("expected '%s', got '%s'", ORIG_VAL, s.SubStruct.KeepMe) | ||
62 | } | ||
63 | |||
64 | if s.SubStruct.OverwriteMe != ENV_VAL { | ||
65 | t.Errorf("expected '%s', got '%s'", ENV_VAL, s.SubStruct.OverwriteMe) | ||
66 | } | ||
67 | } | ||