You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
learn_mqtt_go/common/common_test.go

40 lines
766 B
Go

package common
import (
"os"
"testing"
)
var testEnvKey string = "TEST_MQTT_KEY"
func assertError(t testing.TB, got, want string) {
t.Helper()
if got != want {
t.Errorf("Error: got '%s', want '%s'", got, want)
}
}
func TestGetenvDefault(t *testing.T) {
t.Run("key not set", func(t *testing.T) {
originalEnv := os.Getenv(testEnvKey)
os.Unsetenv(testEnvKey)
defer os.Setenv(testEnvKey, originalEnv)
got := GetenvDefault(testEnvKey, "found")
want := "found"
assertError(t, got, want)
})
t.Run("key set", func(t *testing.T) {
originalEnv := os.Getenv(testEnvKey)
os.Setenv(testEnvKey, "batman")
defer os.Setenv(testEnvKey, originalEnv)
got := GetenvDefault(testEnvKey, "found")
want := "batman"
assertError(t, got, want)
})
}