aboutsummaryrefslogtreecommitdiff
path: root/typeconv_test.go
blob: c851b5bd788e5b61ec3cce868fe86690769fefa1 (plain)
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
package envcfg

import (
	"fmt"
	"os"
	"testing"
)

func TestStringMapping(t *testing.T) {
	const ENV_KEY = "FIELD"
	const ENV_VAL = "Remember: testing is the future!"

	os.Clearenv()
	os.Setenv(ENV_KEY, ENV_VAL)

	s := struct{ Field string }{}

	ReadInto(&s)

	if s.Field != ENV_VAL {
		t.Errorf("expected '%s', got '%s'", ENV_VAL, s.Field)
	}
}

func TestBoolMapping(t *testing.T) {
	const ENV_KEY = "FIELD"
	const ENV_VAL = true

	os.Clearenv()
	os.Setenv(ENV_KEY, fmt.Sprintf("%t", ENV_VAL))

	s := struct{ Field bool }{}

	ReadInto(&s)

	if s.Field != ENV_VAL {
		t.Errorf("expected '%t', got '%t'", ENV_VAL, s.Field)
	}
}

func TestIntMapping(t *testing.T) {
	const ENV_KEY = "FIELD"
	const ENV_VAL = 42

	os.Clearenv()
	os.Setenv(ENV_KEY, fmt.Sprintf("%d", ENV_VAL))

	s := struct{ Field int }{}

	ReadInto(&s)

	if s.Field != ENV_VAL {
		t.Errorf("expected '%d', got '%d'", ENV_VAL, s.Field)
	}
}

func TestFloatMapping(t *testing.T) {
	const ENV_KEY = "FIELD"
	const ENV_VAL = 13.37

	os.Clearenv()
	os.Setenv(ENV_KEY, fmt.Sprintf("%f", ENV_VAL))

	s := struct{ Field float32 }{}

	ReadInto(&s)

	if s.Field != ENV_VAL {
		t.Errorf("expected '%f', got '%f'", ENV_VAL, s.Field)
	}
}