From f8e3fbbc2d1efdd8e998ff180d66d8f1f4997d79 Mon Sep 17 00:00:00 2001 From: Krzysztof Nozderko Date: Fri, 13 Oct 2023 11:05:09 +0200 Subject: [PATCH 1/8] SNOW-856228 better assertions in easy logging --- assert_test.go | 55 ++++++++++++++++++++++++++++++++++++ client_configuration_test.go | 38 ++++++------------------- 2 files changed, 64 insertions(+), 29 deletions(-) create mode 100644 assert_test.go diff --git a/assert_test.go b/assert_test.go new file mode 100644 index 000000000..6a227a7ed --- /dev/null +++ b/assert_test.go @@ -0,0 +1,55 @@ +package gosnowflake + +import ( + "fmt" + "strings" + "testing" +) + +func assertNil(t *testing.T, actual interface{}, descriptions ...string) { + if actual != nil { + desc := joinDescriptions(descriptions...) + errMsg := fmt.Sprintf("expected \"%s\" to be nil but was not%s", actual, desc) + t.Fatal(errMsg) + } +} + +func assertNotNil(t *testing.T, actual interface{}, descriptions ...string) { + if actual == nil { + desc := joinDescriptions(descriptions...) + errMsg := fmt.Sprintf("expected to be not nil but was nil%s", desc) + t.Fatal(errMsg) + } +} + +func assertEqual(t *testing.T, actual string, expected string, descriptions ...string) { + if expected != actual { + desc := joinDescriptions(descriptions...) + errMsg := fmt.Sprintf("expected \"%s\" to be equal to \"%s\" but was not%s", actual, expected, desc) + t.Fatal(errMsg) + } +} + +func assertContains(t *testing.T, actual string, expectedToContain string, descriptions ...string) { + if !strings.Contains(actual, expectedToContain) { + desc := joinDescriptions(descriptions...) + errMsg := fmt.Sprintf("expected \"%s\" to contain \"%s\" but did not%s", actual, expectedToContain, desc) + t.Fatal(errMsg) + } +} + +func assertHasPrefix(t *testing.T, actual string, expectedPrefix string, descriptions ...string) { + if !strings.HasPrefix(actual, expectedPrefix) { + desc := joinDescriptions(descriptions...) + errMsg := fmt.Sprintf("expected \"%s\" to start with \"%s\" but did not%s", actual, expectedPrefix, desc) + t.Fatal(errMsg) + } +} + +func joinDescriptions(descriptions ...string) string { + desc := "" + if descriptions != nil && len(descriptions) > 0 { + desc = " while checking: " + strings.Join(descriptions, " ") + } + return desc +} diff --git a/client_configuration_test.go b/client_configuration_test.go index b7ecdd2f0..fd0b763e7 100644 --- a/client_configuration_test.go +++ b/client_configuration_test.go @@ -6,7 +6,6 @@ import ( "fmt" "os" "path" - "strings" "testing" ) @@ -58,16 +57,9 @@ func TestParseConfiguration(t *testing.T) { fileName := createFile(t, tc.fileName, tc.fileContents, dir) config, err := parseClientConfiguration(fileName) - - if err != nil { - t.Fatalf("Error should be nil but was %s", err) - } - if config.Common.LogLevel != tc.expectedLogLevel { - t.Errorf("Log level should be %s but was %s", tc.expectedLogLevel, config.Common.LogLevel) - } - if config.Common.LogPath != tc.expectedLogPath { - t.Errorf("Log path should be %s but was %s", tc.expectedLogPath, config.Common.LogPath) - } + assertNil(t, err, "parse client configuration error") + assertEqual(t, config.Common.LogLevel, tc.expectedLogLevel, "log level") + assertEqual(t, config.Common.LogPath, tc.expectedLogPath, "log path") }) } } @@ -86,12 +78,8 @@ func TestParseAllLogLevels(t *testing.T) { config, err := parseClientConfiguration(fileName) - if err != nil { - t.Fatalf("Error should be nil but was: %s", err) - } - if config.Common.LogLevel != logLevel { - t.Errorf("Log level should be %s but was %s", logLevel, config.Common.LogLevel) - } + assertNil(t, err, "parse client config error") + assertEqual(t, config.Common.LogLevel, logLevel, "log level") }) } } @@ -150,17 +138,11 @@ func TestParseConfigurationFails(t *testing.T) { _, err := parseClientConfiguration(fileName) - if err == nil { - t.Fatal("Error should not be nil but was nil") - } + assertNotNil(t, err, "parse client configuration error") errMessage := fmt.Sprint(err) expectedPrefix := "parsing client config failed" - if !strings.HasPrefix(errMessage, expectedPrefix) { - t.Errorf("Error message: \"%s\" should start with prefix: \"%s\"", errMessage, expectedPrefix) - } - if !strings.Contains(errMessage, tc.expectedErrorMessageToContain) { - t.Errorf("Error message: \"%s\" should contain given phrase: \"%s\"", errMessage, tc.expectedErrorMessageToContain) - } + assertHasPrefix(t, errMessage, expectedPrefix, "error message") + assertContains(t, errMessage, tc.expectedErrorMessageToContain, "error message") }) } } @@ -168,8 +150,6 @@ func TestParseConfigurationFails(t *testing.T) { func createFile(t *testing.T, fileName string, fileContents string, directory string) string { fullFileName := path.Join(directory, fileName) err := os.WriteFile(fullFileName, []byte(fileContents), 0644) - if err != nil { - t.Fatal("Could not create file") - } + assertNotNil(t, err, "create file error") return fullFileName } From 36f075fac1b0627c331d12bd14c72262b8406586 Mon Sep 17 00:00:00 2001 From: Krzysztof Nozderko Date: Fri, 13 Oct 2023 17:30:56 +0200 Subject: [PATCH 2/8] fix linter comment --- assert_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/assert_test.go b/assert_test.go index 6a227a7ed..e8a0f01d8 100644 --- a/assert_test.go +++ b/assert_test.go @@ -48,7 +48,7 @@ func assertHasPrefix(t *testing.T, actual string, expectedPrefix string, descrip func joinDescriptions(descriptions ...string) string { desc := "" - if descriptions != nil && len(descriptions) > 0 { + if len(descriptions) > 0 { desc = " while checking: " + strings.Join(descriptions, " ") } return desc From 4266cedbfb41531d7ce87280299f63df58044209 Mon Sep 17 00:00:00 2001 From: Krzysztof Nozderko Date: Mon, 16 Oct 2023 09:59:05 +0200 Subject: [PATCH 3/8] fix assert --- client_configuration_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/client_configuration_test.go b/client_configuration_test.go index fd0b763e7..94e1ce9f6 100644 --- a/client_configuration_test.go +++ b/client_configuration_test.go @@ -150,6 +150,6 @@ func TestParseConfigurationFails(t *testing.T) { func createFile(t *testing.T, fileName string, fileContents string, directory string) string { fullFileName := path.Join(directory, fileName) err := os.WriteFile(fullFileName, []byte(fileContents), 0644) - assertNotNil(t, err, "create file error") + assertNil(t, err, "create file error") return fullFileName } From 5c97f5d7ac456eeee2eb446469049baf8a82c73b Mon Sep 17 00:00:00 2001 From: Krzysztof Nozderko Date: Mon, 16 Oct 2023 10:12:07 +0200 Subject: [PATCH 4/8] improve nil checking --- assert_test.go | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/assert_test.go b/assert_test.go index e8a0f01d8..9e1e08cbb 100644 --- a/assert_test.go +++ b/assert_test.go @@ -1,13 +1,16 @@ +// Copyright (c) 2023 Snowflake Computing Inc. All rights reserved. + package gosnowflake import ( "fmt" + "reflect" "strings" "testing" ) func assertNil(t *testing.T, actual interface{}, descriptions ...string) { - if actual != nil { + if !isNil(actual) { desc := joinDescriptions(descriptions...) errMsg := fmt.Sprintf("expected \"%s\" to be nil but was not%s", actual, desc) t.Fatal(errMsg) @@ -15,7 +18,7 @@ func assertNil(t *testing.T, actual interface{}, descriptions ...string) { } func assertNotNil(t *testing.T, actual interface{}, descriptions ...string) { - if actual == nil { + if isNil(actual) { desc := joinDescriptions(descriptions...) errMsg := fmt.Sprintf("expected to be not nil but was nil%s", desc) t.Fatal(errMsg) @@ -53,3 +56,11 @@ func joinDescriptions(descriptions ...string) string { } return desc } + +func isNil(value interface{}) bool { + if value == nil { + return true + } + val := reflect.ValueOf(value) + return val.Kind() == reflect.Pointer && val.IsNil() +} From 481be0054dc97dbbdfa45791f975093e328e4b02 Mon Sep 17 00:00:00 2001 From: Krzysztof Nozderko Date: Mon, 16 Oct 2023 11:21:58 +0200 Subject: [PATCH 5/8] changes after review --- assert_test.go | 24 ++++++++++-------------- client_configuration_test.go | 3 ++- 2 files changed, 12 insertions(+), 15 deletions(-) diff --git a/assert_test.go b/assert_test.go index 9e1e08cbb..1f44903bc 100644 --- a/assert_test.go +++ b/assert_test.go @@ -9,18 +9,18 @@ import ( "testing" ) -func assertNil(t *testing.T, actual interface{}, descriptions ...string) { +func assertNil(t *testing.T, actual any, descriptions ...string) { if !isNil(actual) { desc := joinDescriptions(descriptions...) - errMsg := fmt.Sprintf("expected \"%s\" to be nil but was not%s", actual, desc) + errMsg := fmt.Sprintf("expected \"%s\" to be nil but was not. %s", actual, desc) t.Fatal(errMsg) } } -func assertNotNil(t *testing.T, actual interface{}, descriptions ...string) { +func assertNotNil(t *testing.T, actual any, descriptions ...string) { if isNil(actual) { desc := joinDescriptions(descriptions...) - errMsg := fmt.Sprintf("expected to be not nil but was nil%s", desc) + errMsg := fmt.Sprintf("expected to be not nil but was not. %s", desc) t.Fatal(errMsg) } } @@ -28,15 +28,15 @@ func assertNotNil(t *testing.T, actual interface{}, descriptions ...string) { func assertEqual(t *testing.T, actual string, expected string, descriptions ...string) { if expected != actual { desc := joinDescriptions(descriptions...) - errMsg := fmt.Sprintf("expected \"%s\" to be equal to \"%s\" but was not%s", actual, expected, desc) + errMsg := fmt.Sprintf("expected \"%s\" to be equal to \"%s\" but was not. %s", actual, expected, desc) t.Fatal(errMsg) } } -func assertContains(t *testing.T, actual string, expectedToContain string, descriptions ...string) { +func assertStringContains(t *testing.T, actual string, expectedToContain string, descriptions ...string) { if !strings.Contains(actual, expectedToContain) { desc := joinDescriptions(descriptions...) - errMsg := fmt.Sprintf("expected \"%s\" to contain \"%s\" but did not%s", actual, expectedToContain, desc) + errMsg := fmt.Sprintf("expected \"%s\" to contain \"%s\" but did not. %s", actual, expectedToContain, desc) t.Fatal(errMsg) } } @@ -44,20 +44,16 @@ func assertContains(t *testing.T, actual string, expectedToContain string, descr func assertHasPrefix(t *testing.T, actual string, expectedPrefix string, descriptions ...string) { if !strings.HasPrefix(actual, expectedPrefix) { desc := joinDescriptions(descriptions...) - errMsg := fmt.Sprintf("expected \"%s\" to start with \"%s\" but did not%s", actual, expectedPrefix, desc) + errMsg := fmt.Sprintf("expected \"%s\" to start with \"%s\" but did not. %s", actual, expectedPrefix, desc) t.Fatal(errMsg) } } func joinDescriptions(descriptions ...string) string { - desc := "" - if len(descriptions) > 0 { - desc = " while checking: " + strings.Join(descriptions, " ") - } - return desc + return strings.Join(descriptions, " ") } -func isNil(value interface{}) bool { +func isNil(value any) bool { if value == nil { return true } diff --git a/client_configuration_test.go b/client_configuration_test.go index 94e1ce9f6..fe0e60881 100644 --- a/client_configuration_test.go +++ b/client_configuration_test.go @@ -57,6 +57,7 @@ func TestParseConfiguration(t *testing.T) { fileName := createFile(t, tc.fileName, tc.fileContents, dir) config, err := parseClientConfiguration(fileName) + assertNil(t, err, "parse client configuration error") assertEqual(t, config.Common.LogLevel, tc.expectedLogLevel, "log level") assertEqual(t, config.Common.LogPath, tc.expectedLogPath, "log path") @@ -142,7 +143,7 @@ func TestParseConfigurationFails(t *testing.T) { errMessage := fmt.Sprint(err) expectedPrefix := "parsing client config failed" assertHasPrefix(t, errMessage, expectedPrefix, "error message") - assertContains(t, errMessage, tc.expectedErrorMessageToContain, "error message") + assertStringContains(t, errMessage, tc.expectedErrorMessageToContain, "error message") }) } } From ce9de3f4be1a7e69915d93bda1e93ece62921457 Mon Sep 17 00:00:00 2001 From: Krzysztof Nozderko Date: Mon, 16 Oct 2023 14:08:05 +0200 Subject: [PATCH 6/8] fatal and error versions of asserts --- assert_test.go | 110 ++++++++++++++++++++++++++++------- client_configuration_test.go | 18 +++--- 2 files changed, 99 insertions(+), 29 deletions(-) diff --git a/assert_test.go b/assert_test.go index 1f44903bc..fd242222c 100644 --- a/assert_test.go +++ b/assert_test.go @@ -9,46 +9,116 @@ import ( "testing" ) -func assertNil(t *testing.T, actual any, descriptions ...string) { - if !isNil(actual) { - desc := joinDescriptions(descriptions...) - errMsg := fmt.Sprintf("expected \"%s\" to be nil but was not. %s", actual, desc) +func assertNilF(t *testing.T, actual any, descriptions ...string) { + errMsg := validateNil(actual, descriptions...) + if errMsg != "" { t.Fatal(errMsg) } } -func assertNotNil(t *testing.T, actual any, descriptions ...string) { - if isNil(actual) { - desc := joinDescriptions(descriptions...) - errMsg := fmt.Sprintf("expected to be not nil but was not. %s", desc) +func assertNilE(t *testing.T, actual any, descriptions ...string) { + errMsg := validateNil(actual, descriptions...) + if errMsg != "" { + t.Error(errMsg) + } +} + +func assertNotNilF(t *testing.T, actual any, descriptions ...string) { + errMsg := validateNotNil(actual, descriptions...) + if errMsg != "" { t.Fatal(errMsg) } } -func assertEqual(t *testing.T, actual string, expected string, descriptions ...string) { - if expected != actual { - desc := joinDescriptions(descriptions...) - errMsg := fmt.Sprintf("expected \"%s\" to be equal to \"%s\" but was not. %s", actual, expected, desc) +func assertNotNilE(t *testing.T, actual any, descriptions ...string) { + errMsg := validateNotNil(actual, descriptions...) + if errMsg != "" { + t.Error(errMsg) + } +} + +func assertEqualF(t *testing.T, actual string, expected string, descriptions ...string) { + errMsg := validateEqualStrings(actual, expected, descriptions...) + if errMsg != "" { t.Fatal(errMsg) } } -func assertStringContains(t *testing.T, actual string, expectedToContain string, descriptions ...string) { - if !strings.Contains(actual, expectedToContain) { - desc := joinDescriptions(descriptions...) - errMsg := fmt.Sprintf("expected \"%s\" to contain \"%s\" but did not. %s", actual, expectedToContain, desc) +func assertEqualE(t *testing.T, actual string, expected string, descriptions ...string) { + errMsg := validateEqualStrings(actual, expected, descriptions...) + if errMsg != "" { + t.Error(errMsg) + } +} + +func assertStringContainsF(t *testing.T, actual string, expectedToContain string, descriptions ...string) { + errMsg := validateStringContains(actual, expectedToContain, descriptions...) + if errMsg != "" { t.Fatal(errMsg) } } -func assertHasPrefix(t *testing.T, actual string, expectedPrefix string, descriptions ...string) { - if !strings.HasPrefix(actual, expectedPrefix) { - desc := joinDescriptions(descriptions...) - errMsg := fmt.Sprintf("expected \"%s\" to start with \"%s\" but did not. %s", actual, expectedPrefix, desc) +func assertStringContainsE(t *testing.T, actual string, expectedToContain string, descriptions ...string) { + errMsg := validateStringContains(actual, expectedToContain, descriptions...) + if errMsg != "" { + t.Error(errMsg) + } +} + +func assertHasPrefixF(t *testing.T, actual string, expectedPrefix string, descriptions ...string) { + errMsg := validateHasPrefix(actual, expectedPrefix, descriptions...) + if errMsg != "" { t.Fatal(errMsg) } } +func assertHasPrefixE(t *testing.T, actual string, expectedPrefix string, descriptions ...string) { + errMsg := validateHasPrefix(actual, expectedPrefix, descriptions...) + if errMsg != "" { + t.Error(errMsg) + } +} + +func validateNil(actual any, descriptions ...string) string { + if isNil(actual) { + return "" + } + desc := joinDescriptions(descriptions...) + return fmt.Sprintf("expected \"%s\" to be nil but was not. %s", actual, desc) +} + +func validateNotNil(actual any, descriptions ...string) string { + if !isNil(actual) { + return "" + } + desc := joinDescriptions(descriptions...) + return fmt.Sprintf("expected to be not nil but was not. %s", desc) +} + +func validateEqualStrings(actual any, expected string, descriptions ...string) string { + if expected == actual { + return "" + } + desc := joinDescriptions(descriptions...) + return fmt.Sprintf("expected \"%s\" to be equal to \"%s\" but was not. %s", actual, expected, desc) +} + +func validateStringContains(actual string, expectedToContain string, descriptions ...string) string { + if strings.Contains(actual, expectedToContain) { + return "" + } + desc := joinDescriptions(descriptions...) + return fmt.Sprintf("expected \"%s\" to contain \"%s\" but did not. %s", actual, expectedToContain, desc) +} + +func validateHasPrefix(actual string, expectedPrefix string, descriptions ...string) string { + if strings.HasPrefix(actual, expectedPrefix) { + return "" + } + desc := joinDescriptions(descriptions...) + return fmt.Sprintf("expected \"%s\" to start with \"%s\" but did not. %s", actual, expectedPrefix, desc) +} + func joinDescriptions(descriptions ...string) string { return strings.Join(descriptions, " ") } diff --git a/client_configuration_test.go b/client_configuration_test.go index fe0e60881..8da3be3ec 100644 --- a/client_configuration_test.go +++ b/client_configuration_test.go @@ -58,9 +58,9 @@ func TestParseConfiguration(t *testing.T) { config, err := parseClientConfiguration(fileName) - assertNil(t, err, "parse client configuration error") - assertEqual(t, config.Common.LogLevel, tc.expectedLogLevel, "log level") - assertEqual(t, config.Common.LogPath, tc.expectedLogPath, "log path") + assertNilF(t, err, "parse client configuration error") + assertEqualE(t, config.Common.LogLevel, tc.expectedLogLevel, "log level") + assertEqualE(t, config.Common.LogPath, tc.expectedLogPath, "log path") }) } } @@ -79,8 +79,8 @@ func TestParseAllLogLevels(t *testing.T) { config, err := parseClientConfiguration(fileName) - assertNil(t, err, "parse client config error") - assertEqual(t, config.Common.LogLevel, logLevel, "log level") + assertNilF(t, err, "parse client config error") + assertEqualE(t, config.Common.LogLevel, logLevel, "log level") }) } } @@ -139,11 +139,11 @@ func TestParseConfigurationFails(t *testing.T) { _, err := parseClientConfiguration(fileName) - assertNotNil(t, err, "parse client configuration error") + assertNotNilF(t, err, "parse client configuration error") errMessage := fmt.Sprint(err) expectedPrefix := "parsing client config failed" - assertHasPrefix(t, errMessage, expectedPrefix, "error message") - assertStringContains(t, errMessage, tc.expectedErrorMessageToContain, "error message") + assertHasPrefixE(t, errMessage, expectedPrefix, "error message") + assertStringContainsE(t, errMessage, tc.expectedErrorMessageToContain, "error message") }) } } @@ -151,6 +151,6 @@ func TestParseConfigurationFails(t *testing.T) { func createFile(t *testing.T, fileName string, fileContents string, directory string) string { fullFileName := path.Join(directory, fileName) err := os.WriteFile(fullFileName, []byte(fileContents), 0644) - assertNil(t, err, "create file error") + assertNilF(t, err, "create file error") return fullFileName } From bb17f39bd4b5a66cd4da27bc10a42a52656f9e10 Mon Sep 17 00:00:00 2001 From: Krzysztof Nozderko Date: Mon, 16 Oct 2023 14:16:46 +0200 Subject: [PATCH 7/8] remove unused methods --- assert_test.go | 35 ----------------------------------- 1 file changed, 35 deletions(-) diff --git a/assert_test.go b/assert_test.go index fd242222c..4fe5573b4 100644 --- a/assert_test.go +++ b/assert_test.go @@ -16,13 +16,6 @@ func assertNilF(t *testing.T, actual any, descriptions ...string) { } } -func assertNilE(t *testing.T, actual any, descriptions ...string) { - errMsg := validateNil(actual, descriptions...) - if errMsg != "" { - t.Error(errMsg) - } -} - func assertNotNilF(t *testing.T, actual any, descriptions ...string) { errMsg := validateNotNil(actual, descriptions...) if errMsg != "" { @@ -30,20 +23,6 @@ func assertNotNilF(t *testing.T, actual any, descriptions ...string) { } } -func assertNotNilE(t *testing.T, actual any, descriptions ...string) { - errMsg := validateNotNil(actual, descriptions...) - if errMsg != "" { - t.Error(errMsg) - } -} - -func assertEqualF(t *testing.T, actual string, expected string, descriptions ...string) { - errMsg := validateEqualStrings(actual, expected, descriptions...) - if errMsg != "" { - t.Fatal(errMsg) - } -} - func assertEqualE(t *testing.T, actual string, expected string, descriptions ...string) { errMsg := validateEqualStrings(actual, expected, descriptions...) if errMsg != "" { @@ -51,13 +30,6 @@ func assertEqualE(t *testing.T, actual string, expected string, descriptions ... } } -func assertStringContainsF(t *testing.T, actual string, expectedToContain string, descriptions ...string) { - errMsg := validateStringContains(actual, expectedToContain, descriptions...) - if errMsg != "" { - t.Fatal(errMsg) - } -} - func assertStringContainsE(t *testing.T, actual string, expectedToContain string, descriptions ...string) { errMsg := validateStringContains(actual, expectedToContain, descriptions...) if errMsg != "" { @@ -65,13 +37,6 @@ func assertStringContainsE(t *testing.T, actual string, expectedToContain string } } -func assertHasPrefixF(t *testing.T, actual string, expectedPrefix string, descriptions ...string) { - errMsg := validateHasPrefix(actual, expectedPrefix, descriptions...) - if errMsg != "" { - t.Fatal(errMsg) - } -} - func assertHasPrefixE(t *testing.T, actual string, expectedPrefix string, descriptions ...string) { errMsg := validateHasPrefix(actual, expectedPrefix, descriptions...) if errMsg != "" { From 4c6fd22e3a8c19fc55f862f92c22953c5b39716e Mon Sep 17 00:00:00 2001 From: Krzysztof Nozderko Date: Mon, 16 Oct 2023 14:38:12 +0200 Subject: [PATCH 8/8] tiny refactor --- assert_test.go | 33 +++++++++++++++------------------ 1 file changed, 15 insertions(+), 18 deletions(-) diff --git a/assert_test.go b/assert_test.go index 4fe5573b4..0394b4b08 100644 --- a/assert_test.go +++ b/assert_test.go @@ -10,35 +10,32 @@ import ( ) func assertNilF(t *testing.T, actual any, descriptions ...string) { - errMsg := validateNil(actual, descriptions...) - if errMsg != "" { - t.Fatal(errMsg) - } + fatalOnNonEmpty(t, validateNil(actual, descriptions...)) } func assertNotNilF(t *testing.T, actual any, descriptions ...string) { - errMsg := validateNotNil(actual, descriptions...) - if errMsg != "" { - t.Fatal(errMsg) - } + fatalOnNonEmpty(t, validateNotNil(actual, descriptions...)) } -func assertEqualE(t *testing.T, actual string, expected string, descriptions ...string) { - errMsg := validateEqualStrings(actual, expected, descriptions...) - if errMsg != "" { - t.Error(errMsg) - } +func assertEqualE(t *testing.T, actual any, expected any, descriptions ...string) { + errorOnNonEmpty(t, validateEqual(actual, expected, descriptions...)) } func assertStringContainsE(t *testing.T, actual string, expectedToContain string, descriptions ...string) { - errMsg := validateStringContains(actual, expectedToContain, descriptions...) + errorOnNonEmpty(t, validateStringContains(actual, expectedToContain, descriptions...)) +} + +func assertHasPrefixE(t *testing.T, actual string, expectedPrefix string, descriptions ...string) { + errorOnNonEmpty(t, validateHasPrefix(actual, expectedPrefix, descriptions...)) +} + +func fatalOnNonEmpty(t *testing.T, errMsg string) { if errMsg != "" { - t.Error(errMsg) + t.Fatal(errMsg) } } -func assertHasPrefixE(t *testing.T, actual string, expectedPrefix string, descriptions ...string) { - errMsg := validateHasPrefix(actual, expectedPrefix, descriptions...) +func errorOnNonEmpty(t *testing.T, errMsg string) { if errMsg != "" { t.Error(errMsg) } @@ -60,7 +57,7 @@ func validateNotNil(actual any, descriptions ...string) string { return fmt.Sprintf("expected to be not nil but was not. %s", desc) } -func validateEqualStrings(actual any, expected string, descriptions ...string) string { +func validateEqual(actual any, expected any, descriptions ...string) string { if expected == actual { return "" }