This repository has been archived by the owner on Jun 12, 2023. It is now read-only.
forked from wcharczuk/go-chart
-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.go
202 lines (179 loc) · 4.95 KB
/
helpers.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
package testutil
import (
"math"
"reflect"
"strings"
"testing"
)
// AssertNil asserts an argument is nil.
func AssertNil(t *testing.T, actual interface{}) {
if !isNil(actual) {
t.Errorf("assertion failed; expected actual to be nil")
t.FailNow()
}
}
// AssertNotNil asserts an argument is not nil.
func AssertNotNil(t *testing.T, actual interface{}, message ...interface{}) {
if isNil(actual) {
t.Error("assertion failed; expected actual to not be nil")
if len(message) > 0 {
t.Error(message...)
}
t.FailNow()
}
}
// AssertEqual asserts two arguments are equal.
func AssertEqual(t *testing.T, expected, actual interface{}, message ...interface{}) {
if !equal(expected, actual) {
t.Errorf("assertion failed; expected %v to equal %v", actual, expected)
if len(message) > 0 {
t.Error(message...)
}
t.FailNow()
}
}
// AssertNotEqual asserts two arguments are not equal.
func AssertNotEqual(t *testing.T, expected, actual interface{}, message ...interface{}) {
if equal(expected, actual) {
t.Errorf("assertion failed; expected %v to not equal %v", actual, expected)
if len(message) > 0 {
t.Error(message...)
}
t.FailNow()
}
}
// AssertZero asserts an argument is zero.
func AssertZero(t *testing.T, actual interface{}, message ...interface{}) {
AssertEqual(t, 0, actual)
}
// AssertNotZero asserts an argument is not zero.
func AssertNotZero(t *testing.T, actual interface{}, message ...interface{}) {
AssertNotEqual(t, 0, actual)
}
// AssertTrue asserts an argument is true.
func AssertTrue(t *testing.T, arg bool, message ...interface{}) {
if !arg {
t.Errorf("assertion failed; expected actual to be true")
if len(message) > 0 {
t.Error(message...)
}
t.FailNow()
}
}
// AssertFalse asserts an argument is false.
func AssertFalse(t *testing.T, arg bool, message ...interface{}) {
if arg {
t.Errorf("assertion failed; expected actual to be false")
if len(message) > 0 {
t.Error(message...)
}
t.FailNow()
}
}
// AssertInDelta asserts a two arguments are within a delta of eachother.
//
// This delta will be determined absolute, and the delta should always be positive.
func AssertInDelta(t *testing.T, from, to, delta float64, message ...interface{}) {
if diff := math.Abs(from - to); diff > delta {
t.Errorf("assertion failed; expected absolute difference of %f and %f to be %f", from, to, delta)
if len(message) > 0 {
t.Error(message...)
}
t.FailNow()
}
}
// AssertEmpty asserts an argument is empty.
func AssertEmpty(t *testing.T, arg interface{}, message ...interface{}) {
if getLength(arg) != 0 {
t.Errorf("assertion failed; expected actual to be empty")
if len(message) > 0 {
t.Error(message...)
}
t.FailNow()
}
}
// AssertNotEmpty asserts an argument is not empty.
func AssertNotEmpty(t *testing.T, arg interface{}, message ...interface{}) {
if getLength(arg) == 0 {
t.Errorf("assertion failed; expected actual to not be empty")
if len(message) > 0 {
t.Error(message...)
}
t.FailNow()
}
}
// AssertLen asserts an argument has a given length.
func AssertLen(t *testing.T, arg interface{}, length int, message ...interface{}) {
if getLength(arg) != length {
t.Errorf("assertion failed; expected actual to have length %d", length)
if len(message) > 0 {
t.Error(message...)
}
t.FailNow()
}
}
// AssertContains asserts an argument contains a given substring.
func AssertContains(t *testing.T, s, substr string, message ...interface{}) {
if !strings.Contains(s, substr) {
t.Errorf("assertion failed; expected actual to contain %q", substr)
if len(message) > 0 {
t.Error(message...)
}
t.FailNow()
}
}
// AssertNotContains asserts an argument does not contain a given substring.
func AssertNotContains(t *testing.T, s, substr string, message ...interface{}) {
if strings.Contains(s, substr) {
t.Errorf("assertion failed; expected actual to not contain %q", substr)
if len(message) > 0 {
t.Error(message...)
}
t.FailNow()
}
}
func equal(expected, actual interface{}) bool {
if expected == nil && actual == nil {
return true
}
if (expected == nil && actual != nil) || (expected != nil && actual == nil) {
return false
}
actualType := reflect.TypeOf(actual)
if actualType == nil {
return false
}
expectedValue := reflect.ValueOf(expected)
if expectedValue.IsValid() && expectedValue.Type().ConvertibleTo(actualType) {
return reflect.DeepEqual(expectedValue.Convert(actualType).Interface(), actual)
}
return reflect.DeepEqual(expected, actual)
}
func isNil(object interface{}) bool {
if object == nil {
return true
}
value := reflect.ValueOf(object)
kind := value.Kind()
if kind >= reflect.Chan && kind <= reflect.Slice && value.IsNil() {
return true
}
return false
}
func getLength(object interface{}) int {
if object == nil {
return 0
} else if object == "" {
return 0
}
objValue := reflect.ValueOf(object)
switch objValue.Kind() {
case reflect.Map:
fallthrough
case reflect.Slice, reflect.Chan, reflect.String:
{
return objValue.Len()
}
}
return 0
}