aboutsummaryrefslogtreecommitdiff
path: root/tagging_test.go
blob: 2fe2e33c9f1f868edc8e2b7a74b91e794537a362 (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
package envcfg

import (
	"os"
	"testing"
)

type taggedStruct struct {
	Field string `env:"CUSTOM_POTATOE"`
}

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

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

	s := taggedStruct{}

	ReadInto(&s)

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

type taggedSubStruct struct {
	SubStruct struct {
		Field string
	} `env:"POTATOE"`
}

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

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

	s := taggedSubStruct{}

	ReadInto(&s)

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

type absTaggedField struct {
	SubStruct struct {
		Field string `env:"POTATOE" absenv:"true"`
	}
}

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

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

	s := absTaggedField{}

	ReadInto(&s)

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