|
| 1 | +// This file provides minimal assert/require helpers built on Go's standard library to |
| 2 | +// remove the dependency on github.com/stretchr/testify from objx tests. |
| 3 | +package objx_test |
| 4 | + |
| 5 | +import ( |
| 6 | + "fmt" |
| 7 | + "reflect" |
| 8 | + "testing" |
| 9 | +) |
| 10 | + |
| 11 | +type nonfatal struct{} |
| 12 | +type fatal struct{} |
| 13 | + |
| 14 | +var assert nonfatal |
| 15 | +var require fatal |
| 16 | + |
| 17 | +func (nonfatal) fail(t *testing.T, msg string, msgAndArgs ...interface{}) bool { |
| 18 | + t.Helper() |
| 19 | + if len(msgAndArgs) > 0 { |
| 20 | + msg = fmt.Sprintf(msg, msgAndArgs...) |
| 21 | + } |
| 22 | + t.Error(msg) |
| 23 | + return false |
| 24 | +} |
| 25 | + |
| 26 | +func (fatal) fail(t *testing.T, msg string, msgAndArgs ...interface{}) { |
| 27 | + t.Helper() |
| 28 | + if len(msgAndArgs) > 0 { |
| 29 | + msg = fmt.Sprintf(msg, msgAndArgs...) |
| 30 | + } |
| 31 | + t.Fatal(msg) |
| 32 | +} |
| 33 | + |
| 34 | +func (a nonfatal) Equal(t *testing.T, expected, actual interface{}, msgAndArgs ...interface{}) bool { |
| 35 | + t.Helper() |
| 36 | + if !reflect.DeepEqual(expected, actual) { |
| 37 | + return a.fail(t, "not equal:\nexpected: %#v\nactual: %#v", expected, actual) |
| 38 | + } |
| 39 | + return true |
| 40 | +} |
| 41 | +func (a fatal) Equal(t *testing.T, expected, actual interface{}, msgAndArgs ...interface{}) { |
| 42 | + t.Helper() |
| 43 | + if !reflect.DeepEqual(expected, actual) { |
| 44 | + a.fail(t, "not equal:\nexpected: %#v\nactual: %#v", expected, actual) |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +func (a nonfatal) NotEqual(t *testing.T, expected, actual interface{}, msgAndArgs ...interface{}) bool { |
| 49 | + t.Helper() |
| 50 | + if reflect.DeepEqual(expected, actual) { |
| 51 | + return a.fail(t, "should not be equal: %#v", actual) |
| 52 | + } |
| 53 | + return true |
| 54 | +} |
| 55 | + |
| 56 | +func (a fatal) NotEqual(t *testing.T, expected, actual interface{}, msgAndArgs ...interface{}) { |
| 57 | + t.Helper() |
| 58 | + if reflect.DeepEqual(expected, actual) { |
| 59 | + a.fail(t, "should not be equal: %#v", actual) |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +func isNil(i interface{}) bool { |
| 64 | + if i == nil { |
| 65 | + return true |
| 66 | + } |
| 67 | + v := reflect.ValueOf(i) |
| 68 | + switch v.Kind() { |
| 69 | + case reflect.Chan, reflect.Func, reflect.Interface, reflect.Map, reflect.Ptr, reflect.Slice: |
| 70 | + return v.IsNil() |
| 71 | + } |
| 72 | + return false |
| 73 | +} |
| 74 | + |
| 75 | +func (a nonfatal) Nil(t *testing.T, object interface{}, msgAndArgs ...interface{}) bool { |
| 76 | + t.Helper() |
| 77 | + if !isNil(object) { |
| 78 | + return a.fail(t, "expected nil, got: %#v", object) |
| 79 | + } |
| 80 | + return true |
| 81 | +} |
| 82 | + |
| 83 | +func (a fatal) Nil(t *testing.T, object interface{}, msgAndArgs ...interface{}) { |
| 84 | + t.Helper() |
| 85 | + if !isNil(object) { |
| 86 | + a.fail(t, "expected nil, got: %#v", object) |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +func (a nonfatal) NotNil(t *testing.T, object interface{}, msgAndArgs ...interface{}) bool { |
| 91 | + t.Helper() |
| 92 | + if isNil(object) { |
| 93 | + return a.fail(t, "expected not nil") |
| 94 | + } |
| 95 | + return true |
| 96 | +} |
| 97 | + |
| 98 | +func (a fatal) NotNil(t *testing.T, object interface{}, msgAndArgs ...interface{}) { |
| 99 | + t.Helper() |
| 100 | + if isNil(object) { |
| 101 | + a.fail(t, "expected not nil") |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +func (a nonfatal) True(t *testing.T, value bool, msgAndArgs ...interface{}) bool { |
| 106 | + t.Helper() |
| 107 | + if !value { |
| 108 | + return a.fail(t, "expected true, got false") |
| 109 | + } |
| 110 | + return true |
| 111 | +} |
| 112 | +func (a fatal) True(t *testing.T, value bool, msgAndArgs ...interface{}) { |
| 113 | + t.Helper() |
| 114 | + if !value { |
| 115 | + a.fail(t, "expected true, got false") |
| 116 | + } |
| 117 | +} |
| 118 | + |
| 119 | +func (a nonfatal) False(t *testing.T, value bool, msgAndArgs ...interface{}) bool { |
| 120 | + t.Helper() |
| 121 | + if value { |
| 122 | + return a.fail(t, "expected false, got true") |
| 123 | + } |
| 124 | + return true |
| 125 | +} |
| 126 | +func (a fatal) False(t *testing.T, value bool, msgAndArgs ...interface{}) { |
| 127 | + t.Helper() |
| 128 | + if value { |
| 129 | + a.fail(t, "expected false, got true") |
| 130 | + } |
| 131 | +} |
| 132 | + |
| 133 | +func (a nonfatal) NoError(t *testing.T, err error, msgAndArgs ...interface{}) bool { |
| 134 | + t.Helper() |
| 135 | + if err != nil { |
| 136 | + return a.fail(t, "expected no error, got: %v", err) |
| 137 | + } |
| 138 | + return true |
| 139 | +} |
| 140 | +func (a fatal) NoError(t *testing.T, err error, msgAndArgs ...interface{}) { |
| 141 | + t.Helper() |
| 142 | + if err != nil { |
| 143 | + a.fail(t, "expected no error, got: %v", err) |
| 144 | + } |
| 145 | +} |
| 146 | + |
| 147 | +func (a nonfatal) Error(t *testing.T, err error, msgAndArgs ...interface{}) bool { |
| 148 | + t.Helper() |
| 149 | + if err == nil { |
| 150 | + return a.fail(t, "expected error, got nil") |
| 151 | + } |
| 152 | + return true |
| 153 | +} |
| 154 | +func (a fatal) Error(t *testing.T, err error, msgAndArgs ...interface{}) { |
| 155 | + t.Helper() |
| 156 | + if err == nil { |
| 157 | + a.fail(t, "expected error, got nil") |
| 158 | + } |
| 159 | +} |
| 160 | + |
| 161 | +func (a nonfatal) Panics(t *testing.T, f func(), msgAndArgs ...interface{}) bool { |
| 162 | + t.Helper() |
| 163 | + defer func() { |
| 164 | + if r := recover(); r == nil { |
| 165 | + _ = a.fail(t, "expected panic, but function did not panic") |
| 166 | + } |
| 167 | + }() |
| 168 | + f() |
| 169 | + return true |
| 170 | +} |
| 171 | + |
| 172 | +func (a fatal) Panics(t *testing.T, f func(), msgAndArgs ...interface{}) { |
| 173 | + t.Helper() |
| 174 | + defer func() { |
| 175 | + if r := recover(); r == nil { |
| 176 | + a.fail(t, "expected panic, but function did not panic") |
| 177 | + } |
| 178 | + }() |
| 179 | + f() |
| 180 | +} |
| 181 | + |
| 182 | +func (a nonfatal) Empty(t *testing.T, object interface{}, msgAndArgs ...interface{}) bool { |
| 183 | + t.Helper() |
| 184 | + if !isEmpty(object) { |
| 185 | + return a.fail(t, "expected empty, got: %#v", object) |
| 186 | + } |
| 187 | + return true |
| 188 | +} |
| 189 | + |
| 190 | +func (a fatal) Empty(t *testing.T, object interface{}, msgAndArgs ...interface{}) { |
| 191 | + t.Helper() |
| 192 | + if !isEmpty(object) { |
| 193 | + a.fail(t, "expected empty, got: %#v", object) |
| 194 | + } |
| 195 | +} |
| 196 | + |
| 197 | +func isEmpty(object interface{}) bool { |
| 198 | + if object == nil { |
| 199 | + return true |
| 200 | + } |
| 201 | + v := reflect.ValueOf(object) |
| 202 | + switch v.Kind() { |
| 203 | + case reflect.Array, reflect.Slice, reflect.Map, reflect.Chan, reflect.String: |
| 204 | + return v.Len() == 0 |
| 205 | + case reflect.Ptr, reflect.Interface: |
| 206 | + if v.IsNil() { |
| 207 | + return true |
| 208 | + } |
| 209 | + return isEmpty(v.Elem().Interface()) |
| 210 | + } |
| 211 | + // numbers and structs are never considered empty here |
| 212 | + return false |
| 213 | +} |
| 214 | + |
| 215 | +func (a nonfatal) Len(t *testing.T, object interface{}, length int, msgAndArgs ...interface{}) bool { |
| 216 | + t.Helper() |
| 217 | + v := reflect.ValueOf(object) |
| 218 | + switch v.Kind() { |
| 219 | + case reflect.Array, reflect.Slice, reflect.Map, reflect.Chan, reflect.String: |
| 220 | + if v.Len() != length { |
| 221 | + return a.fail(t, "unexpected length, expected %d got %d", length, v.Len()) |
| 222 | + } |
| 223 | + return true |
| 224 | + default: |
| 225 | + return a.fail(t, "Len not supported for kind %s", v.Kind()) |
| 226 | + } |
| 227 | +} |
| 228 | + |
| 229 | +func (a fatal) Len(t *testing.T, object interface{}, length int, msgAndArgs ...interface{}) { |
| 230 | + t.Helper() |
| 231 | + v := reflect.ValueOf(object) |
| 232 | + switch v.Kind() { |
| 233 | + case reflect.Array, reflect.Slice, reflect.Map, reflect.Chan, reflect.String: |
| 234 | + if v.Len() != length { |
| 235 | + a.fail(t, "unexpected length, expected %d got %d", length, v.Len()) |
| 236 | + } |
| 237 | + default: |
| 238 | + a.fail(t, "Len not supported for kind %s", v.Kind()) |
| 239 | + } |
| 240 | +} |
0 commit comments