From 8e99baedf2c3e433750c2f51681f3c9d15d412be Mon Sep 17 00:00:00 2001 From: Pacien TRAN-GIRARD Date: Sat, 14 Feb 2015 11:24:17 +0100 Subject: First version --- overwriting_test.go | 67 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 overwriting_test.go (limited to 'overwriting_test.go') diff --git a/overwriting_test.go b/overwriting_test.go new file mode 100644 index 0000000..1ed5daa --- /dev/null +++ b/overwriting_test.go @@ -0,0 +1,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) + } +} -- cgit v1.2.3