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 --- typeconv_test.go | 71 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 typeconv_test.go (limited to 'typeconv_test.go') diff --git a/typeconv_test.go b/typeconv_test.go new file mode 100644 index 0000000..c851b5b --- /dev/null +++ b/typeconv_test.go @@ -0,0 +1,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) + } +} -- cgit v1.2.3