diff options
-rw-r--r-- | src/gobang.go | 43 | ||||
-rw-r--r-- | tests/gobang.go | 158 |
2 files changed, 173 insertions, 28 deletions
diff --git a/src/gobang.go b/src/gobang.go index ff8f68b..fff0503 100644 --- a/src/gobang.go +++ b/src/gobang.go @@ -740,38 +740,25 @@ func setLoggerOutput(w io.Writer) { )) } -func levelFromString(name string) (bool, logLevel) { - label := strings.ToUpper(name) - - if label == "NONE" { - return true, LevelNone - } - - if label == "ERROR" { - return true, LevelError - } - - if label == "WARNING" { - return true, LevelWarning - } - - if label == "INFO" { - return true, LevelInfo - } - - if label == "DEBUG" { - return true, LevelDebug +func levelFromString(name string, fallback logLevel) logLevel { + switch strings.ToUpper(name) { + case "NONE": + return LevelNone + case "ERROR": + return LevelError + case "WARNING": + return LevelWarning + case "INFO": + return LevelInfo + case "DEBUG": + return LevelDebug + default: + return fallback } - - return false, level } func setLogLevel() { - ok, envLevel := levelFromString(os.Getenv("LOG_LEVEL")) - - if ok { - level = envLevel - } + level = levelFromString(os.Getenv("LOG_LEVEL"), level) } func setMetric() { diff --git a/tests/gobang.go b/tests/gobang.go index f071f53..a03e8bc 100644 --- a/tests/gobang.go +++ b/tests/gobang.go @@ -489,6 +489,157 @@ func test_ParseUUID() { }) } +func test_showColour() { + TestStart("showColour()") + + const NAME = "NO_COLOUR" + savedEnvvar := os.Getenv(NAME) + + Testing("true when envvar is unset", func() { + ErrorIf(os.Unsetenv(NAME)) + AssertEqual(showColour(), true) + }) + + Testing("true when envvar is empty", func() { + ErrorIf(os.Setenv(NAME, "")) + AssertEqual(showColour(), true) + }) + + Testing("false otherwise", func() { + ErrorIf(os.Setenv(NAME, "1")) + AssertEqual(showColour(), false) + }) + + ErrorIf(os.Setenv(NAME, savedEnvvar)) +} + +func test_levelFromString() { + TestStart("levelFromString()") + + values := []string { "NONE", "ERROR", "WARNING", "INFO", "DEBUG" } + Testing("ok for expected values", func() { + for _, s := range values { + var fallback logLevel = 123 + theLevel := levelFromString(s, fallback) + notFallback := + theLevel >= LevelNone && + theLevel <= LevelDebug && + theLevel != fallback + AssertEqual(notFallback, true) + } + }) + + Testing("fallback for unexpected values", func() { + var fallback logLevel = 123 + theLevel := levelFromString(string(Random(10)), fallback) + AssertEqual(theLevel, fallback) + }) +} + +func test_setLogLevel() { + TestStart("setLogLevel()") + + const NAME = "LOG_LEVEL" + savedEnvvar := os.Getenv(NAME) + savedValue := level + var fallbackValue logLevel = 123 + + Testing("noop when envvar is unset", func() { + ErrorIf(os.Unsetenv(NAME)) + level = fallbackValue + + setLogLevel() + + AssertEqual(level, fallbackValue) + }) + + Testing("noop when envvar is empty", func() { + ErrorIf(os.Setenv(NAME, "")) + level = fallbackValue + + setLogLevel() + + AssertEqual(level, fallbackValue) + }) + + Testing("update variable otherwise", func() { + ErrorIf(os.Setenv(NAME, "DEBUG")) + level = fallbackValue + + setLogLevel() + + AssertEqual(level, LevelDebug) + }) + + ErrorIf(os.Setenv(NAME, savedEnvvar)) + level = savedValue +} + +func test_setMetric() { + TestStart("setMetric()") + + const NAME = "NO_METRIC" + savedEnvvar := os.Getenv(NAME) + savedValue := emitMetric + + Testing("noop when envvar is unset", func() { + ErrorIf(os.Unsetenv(NAME)) + emitMetric = true + + setMetric() + + AssertEqual(emitMetric, true) + }) + + Testing("noop when envvar is empty", func() { + ErrorIf(os.Setenv(NAME, "")) + emitMetric = true + + setMetric() + + AssertEqual(emitMetric, true) + }) + + Testing("make variable false otherwise", func() { + ErrorIf(os.Setenv(NAME, "1")) + emitMetric = true + + setMetric() + + AssertEqual(emitMetric, false) + }) + + ErrorIf(os.Setenv(NAME, savedEnvvar)) + emitMetric = savedValue +} + +func test_FatalIf() { + TestStart("FatalIf()") + + Testing("noop on nil value", func() { + FatalIf(nil) + }) +} + +func test_Assert() { + TestStart("Assert()") + + Testing("noop on true value", func() { + Assert(true, "") + }) +} + +func test_setHostname() { + TestStart("setHostname()") + + Testing("it assigns the value to the global variable", func() { + name, _ := os.Hostname() + hostname = "" + setHostname() + AssertEqual(hostname, name) + }) +} + func TestSetLoggerOutput() { return type entry struct { @@ -533,6 +684,13 @@ func t4() { test_NewUUID() test_UUIDString() test_ParseUUID() + test_showColour() + test_levelFromString() + test_setLogLevel() + test_setMetric() + test_FatalIf() + test_Assert() + test_setHostname() } func MainTest() { |