diff options
-rw-r--r-- | src/gobang.go | 67 | ||||
-rw-r--r-- | tests/gobang.go | 245 |
2 files changed, 251 insertions, 61 deletions
diff --git a/src/gobang.go b/src/gobang.go index a35a57e..98d6e84 100644 --- a/src/gobang.go +++ b/src/gobang.go @@ -54,6 +54,9 @@ var ( emitMetric = true hostname string + testOutput io.Writer = os.Stderr + testExitFn = os.Exit + SourceInfoSkip = 3 ErrBadSQLTablePrefix = errors.New("Invalid table prefix") @@ -316,40 +319,58 @@ func showColour() bool { } func TestStart(name string) { - fmt.Fprintf(os.Stderr, "%s:\n", name) + fmt.Fprintf(testOutput, "%s:\n", name) } func Testing(message string, body func()) { if showColour() { fmt.Fprintf( - os.Stderr, + testOutput, "\033[0;33mtesting\033[0m: %s... ", message, ) body() - fmt.Fprint(os.Stderr, "\033[0;32mOK\033[0m.\n") + fmt.Fprint(testOutput, "\033[0;32mOK\033[0m.\n") } else { - fmt.Fprintf(os.Stderr, "testing: %s...", message) + fmt.Fprintf(testOutput, "testing: %s...", message) body() - fmt.Fprint(os.Stderr, " OK.\n") + fmt.Fprint(testOutput, " OK.\n") } } func terr() { if showColour() { - fmt.Fprintf(os.Stderr, "\033[0;31mERR\033[0m.\n") + fmt.Fprintf(testOutput, "\033[0;31mERR\033[0m") } else { - fmt.Fprintf(os.Stderr, "ERR.\n") + fmt.Fprintf(testOutput, "ERR") + } + + _, file, line, ok := runtime.Caller(2) + if ok { + fmt.Fprintf(testOutput, " (%s:%d)", file, line) } + + fmt.Fprintf(testOutput, ".\n") } func TAssertEqual(given any, expected any) { if !reflect.DeepEqual(given, expected) { terr() - fmt.Fprintf(os.Stderr, "given != expected\n") - fmt.Fprintf(os.Stderr, "given: %#v\n", given) - fmt.Fprintf(os.Stderr, "expected: %#v\n", expected) - os.Exit(100) + fmt.Fprintf(testOutput, "given != expected\n") + fmt.Fprintf(testOutput, "given: %#v\n", given) + fmt.Fprintf(testOutput, "expected: %#v\n", expected) + testExitFn(100) + } +} + +func TAssertEqualS(given any, expected any, message string) { + if !reflect.DeepEqual(given, expected) { + terr() + fmt.Fprintf(testOutput, "message: %s\n", message) + fmt.Fprintf(testOutput, "given != expected\n") + fmt.Fprintf(testOutput, "given: %#v\n", given) + fmt.Fprintf(testOutput, "expected: %#v\n", expected) + testExitFn(100) } } @@ -360,23 +381,23 @@ func TAssertEqualI[T any](givenarr []T, expectedarr []T) { if givenlen < expectedlen { terr() fmt.Fprintf( - os.Stderr, + testOutput, "expected has %d more elements:\n%#v\n", expectedlen - givenlen, expectedarr[givenlen:], ) - os.Exit(100) + testExitFn(100) } if givenlen > expectedlen { terr() fmt.Fprintf( - os.Stderr, + testOutput, "given has %d more elements:\n%#v\n", givenlen - expectedlen, givenarr[expectedlen:], ) - os.Exit(100) + testExitFn(100) } for i, _ := range givenarr { @@ -385,13 +406,13 @@ func TAssertEqualI[T any](givenarr []T, expectedarr []T) { if !reflect.DeepEqual(given, expected) { terr() fmt.Fprintf( - os.Stderr, + testOutput, "given != expected (i = %d)\n", i, ) - fmt.Fprintf(os.Stderr, "given: %#v\n", given) - fmt.Fprintf(os.Stderr, "expected: %#v\n", expected) - os.Exit(100) + fmt.Fprintf(testOutput, "given: %#v\n", given) + fmt.Fprintf(testOutput, "expected: %#v\n", expected) + testExitFn(100) } } } @@ -399,16 +420,16 @@ func TAssertEqualI[T any](givenarr []T, expectedarr []T) { func TErrorIf(err error) { if err != nil { terr() - fmt.Fprintf(os.Stderr, "Unexpected error: %#v\n", err) - os.Exit(100) + fmt.Fprintf(testOutput, "Unexpected error: %#v\n", err) + testExitFn(100) } } func TErrorNil(err error) { if err == nil { terr() - fmt.Fprintf(os.Stderr, "Expected error, got nil\n") - os.Exit(100) + fmt.Fprintf(testOutput, "Expected error, got nil\n") + testExitFn(100) } } 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() } |