package go_environment import ( "os" "testing" ) const ( envKey = "ENV_STRING_TEST_VAR" testValue = "dummy" testJSON = `"` + envKey + `"` ) func TestEnvString(t *testing.T) { os.Setenv(envKey, "") test := EnvironmentString{ Variable: envKey, } if test.Key() != envKey { t.Errorf("unexpected environment variable key") } if test.Valid() { t.Errorf("unexpectedly valid environment variable") } if test.String() != "" { t.Errorf("unexpectedly valid environment variable") } os.Setenv(envKey, testValue) if !test.Valid() { t.Errorf("unexpectedly invalid environment variable") } if test.String() != testValue { t.Errorf("unexpectedly valid environment variable") } os.Unsetenv(envKey) } func TestEnvStringMarshalling(t *testing.T) { test := EnvironmentString{} if test.UnmarshalJSON([]byte(testJSON)) != nil { t.Errorf("failed to unmarshal") } if test.Key() != envKey { t.Errorf("unexpected environment variable key") } if binary, err := test.MarshalJSON(); err != nil { t.Errorf("failed to marshal") } else if string(binary) != testJSON { t.Errorf("unexpected JSON") } } func TestValid(t *testing.T) { os.Setenv(envKey, testValue) test := EnvironmentString{ Variable: "", } if test.Valid() { t.Errorf("TestValid: test case 1: unexpectedly valid environment variable") } test.Variable = envKey if !test.Valid() { t.Errorf("TestValid: test case 2: unexpectedly invalid environment variable") } os.Unsetenv(envKey) if test.Valid() { t.Errorf("TestValid: test case 3: unexpectedly valid environment variable") } }