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