aboutsummaryrefslogtreecommitdiff
path: root/overwriting_test.go
blob: 1ed5daa460dba3dccd405e05d069463c70f54977 (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
package envcfg

import (
	"os"
	"testing"
)

func TestKeeping(t *testing.T) {
	const ORIG_VAL = "Remember: testing is the future!"

	os.Clearenv()

	s := struct{ Field string }{ORIG_VAL}

	ReadInto(&s)

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

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

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

	s := struct{ Field string }{ORIG_VAL}

	ReadInto(&s)

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

type superStruct struct {
	SubStruct nestedFields
}

type nestedFields struct {
	KeepMe      string
	OverwriteMe string
}

func TestMultiOverwriting(t *testing.T) {
	const ENV_KEY = "SUBSTRUCT_OVERWRITEME"
	const ENV_VAL = "Remember: testing is the future!"
	const ORIG_VAL = "Testing is pointless!"

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

	s := superStruct{nestedFields{ORIG_VAL, ORIG_VAL}}

	ReadInto(&s)

	if s.SubStruct.KeepMe != ORIG_VAL {
		t.Errorf("expected '%s', got '%s'", ORIG_VAL, s.SubStruct.KeepMe)
	}

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