diff options
Diffstat (limited to 'tests')
-rw-r--r-- | tests/gobang.go | 245 |
1 files changed, 207 insertions, 38 deletions
diff --git a/tests/gobang.go b/tests/gobang.go index 9d42ca9..c862e82 100644 --- a/tests/gobang.go +++ b/tests/gobang.go @@ -507,27 +507,13 @@ func test_MakeGauge() { }) } -func test_TErrorIf() { - TestStart("TErrorIf()") - - Testing("noop on nil value", func() { - TErrorIf(nil) - }) -} - -func test_TErrorNil() { - TestStart("TErrorNil()") - - Testing("noop on thruthy value", func() { - TErrorNil(errors.New("some error")) - }) -} - func test_showColour() { TestStart("showColour()") const NAME = "NO_COLOUR" savedEnvvar := os.Getenv(NAME) + defer os.Setenv(NAME, savedEnvvar) + Testing("true when envvar is unset", func() { TErrorIf(os.Unsetenv(NAME)) @@ -544,7 +530,200 @@ func test_showColour() { TAssertEqual(showColour(), false) }) - TErrorIf(os.Setenv(NAME, savedEnvvar)) +} + +func test_TestStart() { + TestStart("TestStart()") + + Testing("simply writes the given name to `testOutput`", func() { + w := new(strings.Builder) + savedOutput := testOutput + testOutput = w + + TestStart("theFunctionName()") + TestStart("typeT.Method()") + TestStart("variable") + testOutput = savedOutput + + const expected = + "theFunctionName():\n" + + "typeT.Method():\n" + + "variable:\n" + + TAssertEqual(w.String(), expected) + }) +} + +func withSavedColour() func() { + println("antes") + const NAME = "NO_COLOUR" + savedEnvvar := os.Getenv(NAME) + return func() { + println("depois") + os.Setenv(NAME, savedEnvvar) + } +} + +func test_Testing() { + TestStart("Testing()") + + Testing("tries to write a successful report of the test", func() { + defer withSavedColour()() + + w := new(strings.Builder) + savedOutput := testOutput + testOutput = w + + + TErrorIf(os.Unsetenv("NO_COLOUR")) + Testing("colored description goes here", func() {}) + TErrorIf(os.Setenv("NO_COLOUR", "true")) + Testing("uncolored description goes here", func() {}) + testOutput = savedOutput + + const expected = + "\033[0;33mtesting\033[0m: " + + "colored description goes here... " + + "\033[0;32mOK\033[0m.\n" + + "testing: uncolored description goes here... OK.\n" + TAssertEqual(w.String(), expected) + }) +} + +func test_terr() { + TestStart("terr()") + + Testing("emits a failure string report", func() { + defer withSavedColour()() + + w1 := new(strings.Builder) + w2 := new(strings.Builder) + savedOutput := testOutput + + TErrorIf(os.Unsetenv("NO_COLOUR")) + testOutput = w1 + terr() + TErrorIf(os.Setenv("NO_COLOUR", "1")) + testOutput = w2 + terr() + testOutput = savedOutput + + const expected1 = "\033[0;31mERR\033[0m (" + const expected2 = "ERR (" + const suffix = ").\n" + TAssertEqual(strings.HasPrefix(w1.String(), expected1), true) + TAssertEqual(strings.HasPrefix(w2.String(), expected2), true) + TAssertEqual(strings.HasSuffix(w1.String(), suffix), true) + TAssertEqual(strings.HasSuffix(w2.String(), suffix), true) + }) +} + +func test_TAssertEqual() { + // FIXME +} + +func test_TAssertEqualS() { + // FIXME +} + +func test_TAssertEqualI() { + // FIXME +} + +func test_TErrorIf() { + TestStart("TErrorIf()") + + Testing("noop on nil value", func() { + exitFn := func(val int) { + panic(val) + } + + w := new(strings.Builder) + + savedExitFn := testExitFn + savedOutput := testOutput + testExitFn = exitFn + testOutput = w + + TErrorIf(nil) + testExitFn = savedExitFn + testOutput = savedOutput + + TAssertEqual(w.String(), "") + }) + + Testing("simple dynamic message plus exit code", func() { + myErr := errors.New("unexpected error") + myStr := fmt.Sprintf("%#v", myErr) + + var n int + exitFn := func(val int) { + n = val + } + + w := new(strings.Builder) + + savedExitFn := testExitFn + savedOutput := testOutput + testExitFn = exitFn + testOutput = w + + TErrorIf(myErr) + testExitFn = savedExitFn + testOutput = savedOutput + + given := strings.Split(w.String(), "\n")[1] + + TAssertEqual(n, 100) + TAssertEqual(given, "Unexpected error: " + myStr) + }) +} + +func test_TErrorNil() { + TestStart("TErrorNil()") + + Testing("noop on thruthy value", func() { + exitFn := func(val int) { + panic(val) + } + + w := new(strings.Builder) + + savedExitFn := testExitFn + savedOutput := testOutput + testExitFn = exitFn + testOutput = w + + TErrorNil(errors.New("some error")) + testExitFn = savedExitFn + testOutput = savedOutput + + TAssertEqual(w.String(), "") + }) + + + Testing("simple message with special exit code", func() { + var n int + exitFn := func(val int) { + n = val + } + + w := new(strings.Builder) + + savedExitFn := testExitFn + savedOutput := testOutput + testExitFn = exitFn + testOutput = w + + TErrorNil(nil) + testExitFn = savedExitFn + testOutput = savedOutput + + given := strings.Split(w.String(), "\n")[1] + + TAssertEqual(n, 100) + TAssertEqual(given, "Expected error, got nil") + }) } func test_SetLoggerOutput() { @@ -730,6 +909,10 @@ func test_setMetric() { const NAME = "NO_METRIC" savedEnvvar := os.Getenv(NAME) savedValue := emitMetric + defer func() { + TErrorIf(os.Setenv(NAME, savedEnvvar)) + emitMetric = savedValue + }() Testing("noop when envvar is unset", func() { TErrorIf(os.Unsetenv(NAME)) @@ -758,24 +941,6 @@ func test_setMetric() { TAssertEqual(emitMetric, false) }) - TErrorIf(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() { @@ -808,15 +973,19 @@ func MainTest() { test_Timed() test_MakeCounter() test_MakeGauge() + test_showColour() + test_TestStart() + test_Testing() + test_terr() + test_TAssertEqual() + test_TAssertEqualS() + test_TAssertEqualI() test_TErrorIf() test_TErrorNil() - test_showColour() test_SetLoggerOutput() test_levelFromString() test_setLogLevel() test_SetLevel() test_setMetric() - test_FatalIf() - test_Assert() test_setHostname() } |