aboutsummaryrefslogtreecommitdiff
path: root/nesting_test.go
blob: 3fcc0599f30a30f5218c444b0301556fa0cdcf33 (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
package envcfg

import (
	"os"
	"testing"
)

type nestedStruct struct {
	SubStruct struct {
		Field string
	}
}

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

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

	s := nestedStruct{}

	ReadInto(&s)

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

type deeplyNestedStruct struct {
	SubStruct struct {
		SubSubStruct struct {
			Field string
		}
	}
}

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

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

	s := deeplyNestedStruct{}

	ReadInto(&s)

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