Skip to content

Commit c219d9d

Browse files
SNOW-856228 better assertions in easy logging (#933)
* SNOW-856228 better assertions in easy logging
1 parent 9736386 commit c219d9d

File tree

2 files changed

+103
-28
lines changed

2 files changed

+103
-28
lines changed

assert_test.go

+94
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
// Copyright (c) 2023 Snowflake Computing Inc. All rights reserved.
2+
3+
package gosnowflake
4+
5+
import (
6+
"fmt"
7+
"reflect"
8+
"strings"
9+
"testing"
10+
)
11+
12+
func assertNilF(t *testing.T, actual any, descriptions ...string) {
13+
fatalOnNonEmpty(t, validateNil(actual, descriptions...))
14+
}
15+
16+
func assertNotNilF(t *testing.T, actual any, descriptions ...string) {
17+
fatalOnNonEmpty(t, validateNotNil(actual, descriptions...))
18+
}
19+
20+
func assertEqualE(t *testing.T, actual any, expected any, descriptions ...string) {
21+
errorOnNonEmpty(t, validateEqual(actual, expected, descriptions...))
22+
}
23+
24+
func assertStringContainsE(t *testing.T, actual string, expectedToContain string, descriptions ...string) {
25+
errorOnNonEmpty(t, validateStringContains(actual, expectedToContain, descriptions...))
26+
}
27+
28+
func assertHasPrefixE(t *testing.T, actual string, expectedPrefix string, descriptions ...string) {
29+
errorOnNonEmpty(t, validateHasPrefix(actual, expectedPrefix, descriptions...))
30+
}
31+
32+
func fatalOnNonEmpty(t *testing.T, errMsg string) {
33+
if errMsg != "" {
34+
t.Fatal(errMsg)
35+
}
36+
}
37+
38+
func errorOnNonEmpty(t *testing.T, errMsg string) {
39+
if errMsg != "" {
40+
t.Error(errMsg)
41+
}
42+
}
43+
44+
func validateNil(actual any, descriptions ...string) string {
45+
if isNil(actual) {
46+
return ""
47+
}
48+
desc := joinDescriptions(descriptions...)
49+
return fmt.Sprintf("expected \"%s\" to be nil but was not. %s", actual, desc)
50+
}
51+
52+
func validateNotNil(actual any, descriptions ...string) string {
53+
if !isNil(actual) {
54+
return ""
55+
}
56+
desc := joinDescriptions(descriptions...)
57+
return fmt.Sprintf("expected to be not nil but was not. %s", desc)
58+
}
59+
60+
func validateEqual(actual any, expected any, descriptions ...string) string {
61+
if expected == actual {
62+
return ""
63+
}
64+
desc := joinDescriptions(descriptions...)
65+
return fmt.Sprintf("expected \"%s\" to be equal to \"%s\" but was not. %s", actual, expected, desc)
66+
}
67+
68+
func validateStringContains(actual string, expectedToContain string, descriptions ...string) string {
69+
if strings.Contains(actual, expectedToContain) {
70+
return ""
71+
}
72+
desc := joinDescriptions(descriptions...)
73+
return fmt.Sprintf("expected \"%s\" to contain \"%s\" but did not. %s", actual, expectedToContain, desc)
74+
}
75+
76+
func validateHasPrefix(actual string, expectedPrefix string, descriptions ...string) string {
77+
if strings.HasPrefix(actual, expectedPrefix) {
78+
return ""
79+
}
80+
desc := joinDescriptions(descriptions...)
81+
return fmt.Sprintf("expected \"%s\" to start with \"%s\" but did not. %s", actual, expectedPrefix, desc)
82+
}
83+
84+
func joinDescriptions(descriptions ...string) string {
85+
return strings.Join(descriptions, " ")
86+
}
87+
88+
func isNil(value any) bool {
89+
if value == nil {
90+
return true
91+
}
92+
val := reflect.ValueOf(value)
93+
return val.Kind() == reflect.Pointer && val.IsNil()
94+
}

client_configuration_test.go

+9-28
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"fmt"
77
"os"
88
"path"
9-
"strings"
109
"testing"
1110
)
1211

@@ -59,15 +58,9 @@ func TestParseConfiguration(t *testing.T) {
5958

6059
config, err := parseClientConfiguration(fileName)
6160

62-
if err != nil {
63-
t.Fatalf("Error should be nil but was %s", err)
64-
}
65-
if config.Common.LogLevel != tc.expectedLogLevel {
66-
t.Errorf("Log level should be %s but was %s", tc.expectedLogLevel, config.Common.LogLevel)
67-
}
68-
if config.Common.LogPath != tc.expectedLogPath {
69-
t.Errorf("Log path should be %s but was %s", tc.expectedLogPath, config.Common.LogPath)
70-
}
61+
assertNilF(t, err, "parse client configuration error")
62+
assertEqualE(t, config.Common.LogLevel, tc.expectedLogLevel, "log level")
63+
assertEqualE(t, config.Common.LogPath, tc.expectedLogPath, "log path")
7164
})
7265
}
7366
}
@@ -86,12 +79,8 @@ func TestParseAllLogLevels(t *testing.T) {
8679

8780
config, err := parseClientConfiguration(fileName)
8881

89-
if err != nil {
90-
t.Fatalf("Error should be nil but was: %s", err)
91-
}
92-
if config.Common.LogLevel != logLevel {
93-
t.Errorf("Log level should be %s but was %s", logLevel, config.Common.LogLevel)
94-
}
82+
assertNilF(t, err, "parse client config error")
83+
assertEqualE(t, config.Common.LogLevel, logLevel, "log level")
9584
})
9685
}
9786
}
@@ -150,26 +139,18 @@ func TestParseConfigurationFails(t *testing.T) {
150139

151140
_, err := parseClientConfiguration(fileName)
152141

153-
if err == nil {
154-
t.Fatal("Error should not be nil but was nil")
155-
}
142+
assertNotNilF(t, err, "parse client configuration error")
156143
errMessage := fmt.Sprint(err)
157144
expectedPrefix := "parsing client config failed"
158-
if !strings.HasPrefix(errMessage, expectedPrefix) {
159-
t.Errorf("Error message: \"%s\" should start with prefix: \"%s\"", errMessage, expectedPrefix)
160-
}
161-
if !strings.Contains(errMessage, tc.expectedErrorMessageToContain) {
162-
t.Errorf("Error message: \"%s\" should contain given phrase: \"%s\"", errMessage, tc.expectedErrorMessageToContain)
163-
}
145+
assertHasPrefixE(t, errMessage, expectedPrefix, "error message")
146+
assertStringContainsE(t, errMessage, tc.expectedErrorMessageToContain, "error message")
164147
})
165148
}
166149
}
167150

168151
func createFile(t *testing.T, fileName string, fileContents string, directory string) string {
169152
fullFileName := path.Join(directory, fileName)
170153
err := os.WriteFile(fullFileName, []byte(fileContents), 0644)
171-
if err != nil {
172-
t.Fatal("Could not create file")
173-
}
154+
assertNilF(t, err, "create file error")
174155
return fullFileName
175156
}

0 commit comments

Comments
 (0)