forked from snowflakedb/gosnowflake
-
Notifications
You must be signed in to change notification settings - Fork 0
/
assert_test.go
144 lines (119 loc) · 4.25 KB
/
assert_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
// Copyright (c) 2023 Snowflake Computing Inc. All rights reserved.
package gosnowflake
import (
"fmt"
"reflect"
"runtime"
"strings"
"testing"
)
func assertNilE(t *testing.T, actual any, descriptions ...string) {
errorOnNonEmpty(t, validateNil(actual, descriptions...))
}
func assertNilF(t *testing.T, actual any, descriptions ...string) {
fatalOnNonEmpty(t, validateNil(actual, descriptions...))
}
func assertNotNilE(t *testing.T, actual any, descriptions ...string) {
errorOnNonEmpty(t, validateNotNil(actual, descriptions...))
}
func assertNotNilF(t *testing.T, actual any, descriptions ...string) {
fatalOnNonEmpty(t, validateNotNil(actual, descriptions...))
}
func assertEqualE(t *testing.T, actual any, expected any, descriptions ...string) {
errorOnNonEmpty(t, validateEqual(actual, expected, descriptions...))
}
func assertEqualF(t *testing.T, actual any, expected any, descriptions ...string) {
fatalOnNonEmpty(t, validateEqual(actual, expected, descriptions...))
}
func assertTrueF(t *testing.T, actual bool, descriptions ...string) {
fatalOnNonEmpty(t, validateEqual(actual, true, descriptions...))
}
func assertFalseF(t *testing.T, actual bool, descriptions ...string) {
fatalOnNonEmpty(t, validateEqual(actual, false, descriptions...))
}
func assertStringContainsE(t *testing.T, actual string, expectedToContain string, descriptions ...string) {
errorOnNonEmpty(t, validateStringContains(actual, expectedToContain, descriptions...))
}
func assertHasPrefixE(t *testing.T, actual string, expectedPrefix string, descriptions ...string) {
errorOnNonEmpty(t, validateHasPrefix(actual, expectedPrefix, descriptions...))
}
func assertBetweenE(t *testing.T, value float64, min float64, max float64, descriptions ...string) {
errorOnNonEmpty(t, validateValueBetween(value, min, max, descriptions...))
}
func fatalOnNonEmpty(t *testing.T, errMsg string) {
if errMsg != "" {
t.Fatal(formatErrorMessage(errMsg))
}
}
func errorOnNonEmpty(t *testing.T, errMsg string) {
if errMsg != "" {
t.Error(formatErrorMessage(errMsg))
}
}
func formatErrorMessage(errMsg string) string {
return fmt.Sprintf("%s. Thrown from %s", errMsg, thrownFrom())
}
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 validateEqual(actual any, expected any, 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 validateValueBetween(value float64, min float64, max float64, descriptions ...string) string {
if value > min && value < max {
return ""
}
desc := joinDescriptions(descriptions...)
return fmt.Sprintf("expected \"%f\" should be between \"%f\" and \"%f\" but did not. %s", value, min, max, desc)
}
func joinDescriptions(descriptions ...string) string {
return strings.Join(descriptions, " ")
}
func isNil(value any) bool {
if value == nil {
return true
}
val := reflect.ValueOf(value)
return val.Kind() == reflect.Pointer && val.IsNil()
}
func thrownFrom() string {
buf := make([]byte, 1024)
size := runtime.Stack(buf, false)
stack := string(buf[0:size])
lines := strings.Split(stack, "\n\t")
for i, line := range lines {
if i > 0 && !strings.Contains(line, "assert_test.go") {
return line
}
}
return stack
}