From df377bcc6ff540a91ed3bf8c6e840a76f0cd14a8 Mon Sep 17 00:00:00 2001 From: EuAndreh Date: Tue, 6 Aug 2024 21:31:18 -0300 Subject: tests/gobang.go: Add tests to more functions --- tests/gobang.go | 158 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 158 insertions(+) (limited to 'tests/gobang.go') 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() { -- cgit v1.2.3