diff options
-rw-r--r-- | src/gobang.go | 70 | ||||
-rw-r--r-- | tests/gobang.go | 76 |
2 files changed, 78 insertions, 68 deletions
diff --git a/src/gobang.go b/src/gobang.go index 0e5bbd9..2efaca4 100644 --- a/src/gobang.go +++ b/src/gobang.go @@ -288,20 +288,6 @@ func MakeGauge(label string, staticArgs ...any) Gauge { } } -func ErrorIf(err error) { - if err != nil { - fmt.Fprintf(os.Stderr, "Unexpected error: %#v\n", err) - os.Exit(1) - } -} - -func ErrorNil(err error) { - if err == nil { - fmt.Fprintf(os.Stderr, "Expected error, got nil\n") - os.Exit(1) - } -} - func showColour() bool { return os.Getenv("NO_COLOUR") == "" } @@ -326,31 +312,55 @@ func Testing(message string, body func()) { } } +func terr() { + if showColour() { + fmt.Fprintf(os.Stderr, "\033[0;31mERR\033[0m.\n") + } else { + fmt.Fprintf(os.Stderr, "ERR.\n") + } +} + func TAssertEqual(given any, expected any) { if !reflect.DeepEqual(given, expected) { - if showColour() { - fmt.Fprintf(os.Stderr, "\033[0;31mERR\033[0m.\n") - } else { - fmt.Fprintf(os.Stderr, "ERR.\n") - } + 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(1) + os.Exit(100) } } -func TAssertEqualI(i int, given any, expected any) { - if !reflect.DeepEqual(given, expected) { - if showColour() { - fmt.Fprintf(os.Stderr, "\033[0;31mERR\033[0m.\n") - } else { - fmt.Fprintf(os.Stderr, "ERR.\n") +func TAssertEqualI(givenarr []any, expectedarr []any) { + for i, _ := range givenarr { + given := givenarr[i] + expected := expectedarr[i] + if !reflect.DeepEqual(given, expected) { + terr() + fmt.Fprintf( + os.Stderr, + "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(os.Stderr, "given != expected (i = %d)\n", i) - fmt.Fprintf(os.Stderr, "given: %#v\n", given) - fmt.Fprintf(os.Stderr, "expected: %#v\n", expected) - os.Exit(1) + } +} + +func TErrorIf(err error) { + if err != nil { + terr() + fmt.Fprintf(os.Stderr, "Unexpected error: %#v\n", err) + os.Exit(100) + } +} + +func TErrorNil(err error) { + if err == nil { + terr() + fmt.Fprintf(os.Stderr, "Expected error, got nil\n") + os.Exit(100) } } diff --git a/tests/gobang.go b/tests/gobang.go index 9073b66..c7d1549 100644 --- a/tests/gobang.go +++ b/tests/gobang.go @@ -162,7 +162,7 @@ func test_logArgs() { TAssertEqual(args[0], "id") _, err := guuid.FromString(args[1]) - ErrorIf(err) + TErrorIf(err) expected := []string { "kind", "log", "type", "the-type" } TAssertEqual(args[2:], expected) @@ -198,10 +198,10 @@ func testLog( { var data map[string]interface{} err := json.Unmarshal([]byte(s.String()), &data) - ErrorIf(err) + TErrorIf(err) _, err = time.Parse(time.RFC3339, data["time"].(string)) - ErrorIf(err) + TErrorIf(err) TAssertEqual(data["level"].(string), levelName) TAssertEqual(data["kind" ].(string), "log") @@ -307,14 +307,14 @@ func test_metric() { var data map[string]interface{} err := json.Unmarshal([]byte(s.String()), &data) - ErrorIf(err) + TErrorIf(err) timeStr := data["time"].(string) _, err = time.Parse(time.RFC3339, timeStr) - ErrorIf(err) + TErrorIf(err) _, err = guuid.FromString(data["id"].(string)) - ErrorIf(err) + TErrorIf(err) TAssertEqual(data["level"].(string), "INFO") TAssertEqual(data["msg" ].(string), "_") @@ -343,7 +343,7 @@ func test_Timed() { var data map[string]interface{} err := json.Unmarshal([]byte(s.String()), &data) - ErrorIf(err) + TErrorIf(err) TAssertEqual(data["type"].(string), "timer") TAssertEqual(data["label"].(string), "timer-label") @@ -378,7 +378,7 @@ func test_MakeCounter() { var data map[string]interface{} err := json.Unmarshal([]byte(s.String()), &data) - ErrorIf(err) + TErrorIf(err) TAssertEqual(data["type" ].(string), "counter") TAssertEqual(data["label"].(string), "emit-tcp-error") @@ -402,7 +402,7 @@ func test_MakeCounter() { var data map[string]interface{} err := json.Unmarshal([]byte(s.String()), &data) - ErrorIf(err) + TErrorIf(err) TAssertEqual(data["type"].(string), "counter") TAssertEqual(data["label"].(string), "label-1") @@ -431,7 +431,7 @@ func test_MakeGauge() { { var data map[string]interface{} err := json.Unmarshal([]byte(s.String()), &data) - ErrorIf(err) + TErrorIf(err) TAssertEqual(data["level"].(string), "ERROR") TAssertEqual(data["msg" ].(string), "Gauge went negative") @@ -465,7 +465,7 @@ func test_MakeGauge() { var data map[string]interface{} err := json.Unmarshal([]byte(strs[len(strs) - 2]), &data) - ErrorIf(err) + TErrorIf(err) TAssertEqual(data["type" ].(string), "gauge") TAssertEqual(data["label"].(string), "active-tcp-connections") @@ -477,19 +477,19 @@ func test_MakeGauge() { }) } -func test_ErrorIf() { - TestStart("ErrorIf()") +func test_TErrorIf() { + TestStart("TErrorIf()") Testing("noop on nil value", func() { - ErrorIf(nil) + TErrorIf(nil) }) } -func test_ErrorNil() { - TestStart("ErrorNil()") +func test_TErrorNil() { + TestStart("TErrorNil()") Testing("noop on thruthy value", func() { - ErrorNil(errors.New("some error")) + TErrorNil(errors.New("some error")) }) } @@ -500,21 +500,21 @@ func test_showColour() { savedEnvvar := os.Getenv(NAME) Testing("true when envvar is unset", func() { - ErrorIf(os.Unsetenv(NAME)) + TErrorIf(os.Unsetenv(NAME)) TAssertEqual(showColour(), true) }) Testing("true when envvar is empty", func() { - ErrorIf(os.Setenv(NAME, "")) + TErrorIf(os.Setenv(NAME, "")) TAssertEqual(showColour(), true) }) Testing("false otherwise", func() { - ErrorIf(os.Setenv(NAME, "1")) + TErrorIf(os.Setenv(NAME, "1")) TAssertEqual(showColour(), false) }) - ErrorIf(os.Setenv(NAME, savedEnvvar)) + TErrorIf(os.Setenv(NAME, savedEnvvar)) } func test_SetLoggerOutput() { @@ -532,7 +532,7 @@ func test_SetLoggerOutput() { var data map[string]interface{} err := json.Unmarshal([]byte(s.String()), &data) - ErrorIf(err) + TErrorIf(err) src := data["src"].(map[string]interface{}) file := src["file"].(string) @@ -552,7 +552,7 @@ func test_SetLoggerOutput() { var data map[string]interface{} err := json.Unmarshal([]byte(s.String()), &data) - ErrorIf(err) + TErrorIf(err) info := data["info"].(map[string]interface{}) @@ -560,7 +560,7 @@ func test_SetLoggerOutput() { TAssertEqual(int(info["ppid"].(float64)), os.Getppid()) _, err = guuid.FromString(info["puuid"].(string)) - ErrorIf(err) + TErrorIf(err) }) Testing("we can add groups to the default logger", func() { @@ -570,7 +570,7 @@ func test_SetLoggerOutput() { var data map[string]interface{} err := json.Unmarshal([]byte(s.String()), &data) - ErrorIf(err) + TErrorIf(err) TAssertEqual(data["one"].(map[string]interface{})["key"].(string), "value") }) @@ -591,7 +591,7 @@ func test_SetLoggerOutput() { var puuidFromString = func(str string) string { var data map[string]interface{} err := json.Unmarshal([]byte(str), &data) - ErrorIf(err) + TErrorIf(err) return data["info"].(map[string]interface{})["puuid"].(string) } @@ -601,9 +601,9 @@ func test_SetLoggerOutput() { TAssertEqual(puuid1, puuid2) uuid1, err := guuid.FromString(puuid1) - ErrorIf(err) + TErrorIf(err) uuid2, err := guuid.FromString(puuid2) - ErrorIf(err) + TErrorIf(err) TAssertEqual(uuid1, uuid2) }) @@ -642,7 +642,7 @@ func test_setLogLevel() { var fallbackValue LogLevel = 123 Testing("noop when envvar is unset", func() { - ErrorIf(os.Unsetenv(NAME)) + TErrorIf(os.Unsetenv(NAME)) level = fallbackValue setLogLevel() @@ -651,7 +651,7 @@ func test_setLogLevel() { }) Testing("noop when envvar is empty", func() { - ErrorIf(os.Setenv(NAME, "")) + TErrorIf(os.Setenv(NAME, "")) level = fallbackValue setLogLevel() @@ -660,7 +660,7 @@ func test_setLogLevel() { }) Testing("update variable otherwise", func() { - ErrorIf(os.Setenv(NAME, "DEBUG")) + TErrorIf(os.Setenv(NAME, "DEBUG")) level = fallbackValue setLogLevel() @@ -668,7 +668,7 @@ func test_setLogLevel() { TAssertEqual(level, LevelDebug) }) - ErrorIf(os.Setenv(NAME, savedEnvvar)) + TErrorIf(os.Setenv(NAME, savedEnvvar)) level = savedValue } @@ -702,7 +702,7 @@ func test_setMetric() { savedValue := emitMetric Testing("noop when envvar is unset", func() { - ErrorIf(os.Unsetenv(NAME)) + TErrorIf(os.Unsetenv(NAME)) emitMetric = true setMetric() @@ -711,7 +711,7 @@ func test_setMetric() { }) Testing("noop when envvar is empty", func() { - ErrorIf(os.Setenv(NAME, "")) + TErrorIf(os.Setenv(NAME, "")) emitMetric = true setMetric() @@ -720,7 +720,7 @@ func test_setMetric() { }) Testing("make variable false otherwise", func() { - ErrorIf(os.Setenv(NAME, "1")) + TErrorIf(os.Setenv(NAME, "1")) emitMetric = true setMetric() @@ -728,7 +728,7 @@ func test_setMetric() { TAssertEqual(emitMetric, false) }) - ErrorIf(os.Setenv(NAME, savedEnvvar)) + TErrorIf(os.Setenv(NAME, savedEnvvar)) emitMetric = savedValue } @@ -776,8 +776,8 @@ func MainTest() { test_Timed() test_MakeCounter() test_MakeGauge() - test_ErrorIf() - test_ErrorNil() + test_TErrorIf() + test_TErrorNil() test_showColour() test_SetLoggerOutput() test_levelFromString() |