| 1 |
package qt_test |
| 2 |
|
| 3 |
import ( |
| 4 |
"errors" |
| 5 |
"fmt" |
| 6 |
"io" |
| 7 |
"math" |
| 8 |
"net" |
| 9 |
"os" |
| 10 |
"testing" |
| 11 |
|
| 12 |
"github.com/go-quicktest/qt" |
| 13 |
"github.com/google/go-cmp/cmp" |
| 14 |
"github.com/google/go-cmp/cmp/cmpopts" |
| 15 |
) |
| 16 |
|
| 17 |
func ExampleComment() { |
| 18 |
runExampleTest(func(t testing.TB) { |
| 19 |
a := 42 |
| 20 |
qt.Assert(t, qt.Equals(a, 42), qt.Commentf("no answer to life, the universe, and everything")) |
| 21 |
}) |
| 22 |
// Output: PASS |
| 23 |
} |
| 24 |
|
| 25 |
func ExampleEquals() { |
| 26 |
runExampleTest(func(t testing.TB) { |
| 27 |
answer := int64(42) |
| 28 |
qt.Assert(t, qt.Equals(answer, 42)) |
| 29 |
}) |
| 30 |
// Output: PASS |
| 31 |
} |
| 32 |
|
| 33 |
func ExampleDeepEquals() { |
| 34 |
runExampleTest(func(t testing.TB) { |
| 35 |
list := []int{42, 47} |
| 36 |
qt.Assert(t, qt.DeepEquals(list, []int{42, 47})) |
| 37 |
}) |
| 38 |
// Output: PASS |
| 39 |
} |
| 40 |
|
| 41 |
func ExampleCmpEquals() { |
| 42 |
runExampleTest(func(t testing.TB) { |
| 43 |
list := []int{42, 47} |
| 44 |
qt.Assert(t, qt.CmpEquals(list, []int{47, 42}, cmpopts.SortSlices(func(i, j int) bool { |
| 45 |
return i < j |
| 46 |
}))) |
| 47 |
}) |
| 48 |
// Output: PASS |
| 49 |
} |
| 50 |
|
| 51 |
type myStruct struct { |
| 52 |
a int |
| 53 |
} |
| 54 |
|
| 55 |
func customDeepEquals[T any](got, want T) qt.Checker { |
| 56 |
return qt.CmpEquals(got, want, cmp.AllowUnexported(myStruct{})) |
| 57 |
} |
| 58 |
|
| 59 |
func ExampleCmpEquals_customfunc() { |
| 60 |
runExampleTest(func(t testing.TB) { |
| 61 |
got := &myStruct{ |
| 62 |
a: 1234, |
| 63 |
} |
| 64 |
qt.Assert(t, customDeepEquals(got, &myStruct{ |
| 65 |
a: 1234, |
| 66 |
})) |
| 67 |
}) |
| 68 |
// Output: PASS |
| 69 |
} |
| 70 |
|
| 71 |
func ExampleContentEquals() { |
| 72 |
runExampleTest(func(t testing.TB) { |
| 73 |
got := []int{1, 23, 4, 5} |
| 74 |
qt.Assert(t, qt.ContentEquals(got, []int{1, 4, 5, 23})) |
| 75 |
}) |
| 76 |
// Output: PASS |
| 77 |
} |
| 78 |
|
| 79 |
func ExampleMatches() { |
| 80 |
runExampleTest(func(t testing.TB) { |
| 81 |
qt.Assert(t, qt.Matches("these are the voyages", "these are .*")) |
| 82 |
qt.Assert(t, qt.Matches(net.ParseIP("1.2.3.4").String(), "1.*")) |
| 83 |
}) |
| 84 |
// Output: PASS |
| 85 |
} |
| 86 |
|
| 87 |
func ExampleErrorMatches() { |
| 88 |
runExampleTest(func(t testing.TB) { |
| 89 |
err := errors.New("bad wolf at the door") |
| 90 |
qt.Assert(t, qt.ErrorMatches(err, "bad wolf .*")) |
| 91 |
}) |
| 92 |
// Output: PASS |
| 93 |
} |
| 94 |
|
| 95 |
func ExamplePanicMatches() { |
| 96 |
runExampleTest(func(t testing.TB) { |
| 97 |
divide := func(a, b int) int { |
| 98 |
return a / b |
| 99 |
} |
| 100 |
qt.Assert(t, qt.PanicMatches(func() { |
| 101 |
divide(5, 0) |
| 102 |
}, "runtime error: .*")) |
| 103 |
}) |
| 104 |
// Output: PASS |
| 105 |
} |
| 106 |
|
| 107 |
func ExampleIsNil() { |
| 108 |
runExampleTest(func(t testing.TB) { |
| 109 |
got := (*int)(nil) |
| 110 |
qt.Assert(t, qt.IsNil(got)) |
| 111 |
}) |
| 112 |
// Output: PASS |
| 113 |
} |
| 114 |
|
| 115 |
func ExampleIsNotNil() { |
| 116 |
runExampleTest(func(t testing.TB) { |
| 117 |
got := new(int) |
| 118 |
|
| 119 |
qt.Assert(t, qt.IsNotNil(got)) |
| 120 |
|
| 121 |
// Note that unlike reflection-based APIs, a nil |
| 122 |
// value inside an interface still counts as non-nil, |
| 123 |
// just as if we were comparing the actual interface |
| 124 |
// value against nil. |
| 125 |
nilValueInInterface := any((*int)(nil)) |
| 126 |
qt.Assert(t, qt.IsNotNil(nilValueInInterface)) |
| 127 |
}) |
| 128 |
// Output: PASS |
| 129 |
} |
| 130 |
|
| 131 |
func ExampleHasLen() { |
| 132 |
runExampleTest(func(t testing.TB) { |
| 133 |
qt.Assert(t, qt.HasLen([]int{42, 47}, 2)) |
| 134 |
|
| 135 |
myMap := map[string]int{ |
| 136 |
"a": 13, |
| 137 |
"b": 4, |
| 138 |
"c": 10, |
| 139 |
} |
| 140 |
qt.Assert(t, qt.HasLen(myMap, 3)) |
| 141 |
}) |
| 142 |
// Output: PASS |
| 143 |
} |
| 144 |
|
| 145 |
func ExampleImplements() { |
| 146 |
runExampleTest(func(t testing.TB) { |
| 147 |
var myReader struct { |
| 148 |
io.ReadCloser |
| 149 |
} |
| 150 |
qt.Assert(t, qt.Implements[io.ReadCloser](myReader)) |
| 151 |
}) |
| 152 |
// Output: PASS |
| 153 |
} |
| 154 |
|
| 155 |
func ExampleSatisfies() { |
| 156 |
runExampleTest(func(t testing.TB) { |
| 157 |
// Check that an error from os.Open satisfies os.IsNotExist. |
| 158 |
_, err := os.Open("/non-existent-file") |
| 159 |
qt.Assert(t, qt.Satisfies(err, os.IsNotExist)) |
| 160 |
|
| 161 |
// Check that a floating point number is a not-a-number. |
| 162 |
f := math.NaN() |
| 163 |
qt.Assert(t, qt.Satisfies(f, math.IsNaN)) |
| 164 |
|
| 165 |
}) |
| 166 |
// Output: PASS |
| 167 |
} |
| 168 |
|
| 169 |
func ExampleIsTrue() { |
| 170 |
runExampleTest(func(t testing.TB) { |
| 171 |
isValid := func() bool { |
| 172 |
return true |
| 173 |
} |
| 174 |
qt.Assert(t, qt.IsTrue(1 == 1)) |
| 175 |
qt.Assert(t, qt.IsTrue(isValid())) |
| 176 |
}) |
| 177 |
// Output: PASS |
| 178 |
|
| 179 |
} |
| 180 |
|
| 181 |
func ExampleIsFalse() { |
| 182 |
runExampleTest(func(t testing.TB) { |
| 183 |
isValid := func() bool { |
| 184 |
return false |
| 185 |
} |
| 186 |
qt.Assert(t, qt.IsFalse(1 == 0)) |
| 187 |
qt.Assert(t, qt.IsFalse(isValid())) |
| 188 |
}) |
| 189 |
// Output: PASS |
| 190 |
|
| 191 |
} |
| 192 |
|
| 193 |
func ExampleNot() { |
| 194 |
runExampleTest(func(t testing.TB) { |
| 195 |
|
| 196 |
got := []int{1, 2} |
| 197 |
qt.Assert(t, qt.Not(qt.IsNil(got))) |
| 198 |
|
| 199 |
answer := 13 |
| 200 |
qt.Assert(t, qt.Not(qt.Equals(answer, 42))) |
| 201 |
}) |
| 202 |
// Output: PASS |
| 203 |
} |
| 204 |
|
| 205 |
func ExampleStringContains() { |
| 206 |
runExampleTest(func(t testing.TB) { |
| 207 |
qt.Assert(t, qt.StringContains("hello world", "hello")) |
| 208 |
}) |
| 209 |
// Output: PASS |
| 210 |
} |
| 211 |
|
| 212 |
func ExampleSliceContains() { |
| 213 |
runExampleTest(func(t testing.TB) { |
| 214 |
qt.Assert(t, qt.SliceContains([]int{3, 5, 7, 99}, 99)) |
| 215 |
qt.Assert(t, qt.SliceContains([]string{"a", "cd", "e"}, "cd")) |
| 216 |
}) |
| 217 |
// Output: PASS |
| 218 |
} |
| 219 |
|
| 220 |
func ExampleMapContains() { |
| 221 |
runExampleTest(func(t testing.TB) { |
| 222 |
qt.Assert(t, qt.MapContains(map[string]int{ |
| 223 |
"hello": 1234, |
| 224 |
}, 1234)) |
| 225 |
}) |
| 226 |
// Output: PASS |
| 227 |
} |
| 228 |
|
| 229 |
func ExampleSliceAny() { |
| 230 |
runExampleTest(func(t testing.TB) { |
| 231 |
qt.Assert(t, qt.SliceAny([]int{3, 5, 7, 99}, qt.F2(qt.Equals[int], 7))) |
| 232 |
qt.Assert(t, qt.SliceAny([][]string{{"a", "b"}, {"c", "d"}}, qt.F2(qt.DeepEquals[[]string], []string{"c", "d"}))) |
| 233 |
}) |
| 234 |
// Output: PASS |
| 235 |
} |
| 236 |
|
| 237 |
func ExampleMapAny() { |
| 238 |
runExampleTest(func(t testing.TB) { |
| 239 |
qt.Assert(t, qt.MapAny(map[string]int{"x": 2, "y": 3}, qt.F2(qt.Equals[int], 3))) |
| 240 |
}) |
| 241 |
// Output: PASS |
| 242 |
} |
| 243 |
|
| 244 |
func ExampleSliceAll() { |
| 245 |
runExampleTest(func(t testing.TB) { |
| 246 |
qt.Assert(t, qt.SliceAll([]int{3, 5, 8}, func(e int) qt.Checker { |
| 247 |
return qt.Not(qt.Equals(e, 0)) |
| 248 |
})) |
| 249 |
qt.Assert(t, qt.SliceAll([][]string{{"a", "b"}, {"a", "b"}}, qt.F2(qt.DeepEquals[[]string], []string{"a", "b"}))) |
| 250 |
}) |
| 251 |
// Output: PASS |
| 252 |
} |
| 253 |
|
| 254 |
func ExampleMapAll() { |
| 255 |
runExampleTest(func(t testing.TB) { |
| 256 |
qt.Assert(t, qt.MapAll(map[string]int{ |
| 257 |
"x": 2, |
| 258 |
"y": 2, |
| 259 |
}, qt.F2(qt.Equals[int], 2))) |
| 260 |
}) |
| 261 |
// Output: PASS |
| 262 |
} |
| 263 |
|
| 264 |
func ExampleJSONEquals() { |
| 265 |
runExampleTest(func(t testing.TB) { |
| 266 |
data := `[1, 2, 3]` |
| 267 |
qt.Assert(t, qt.JSONEquals(data, []uint{1, 2, 3})) |
| 268 |
}) |
| 269 |
// Output: PASS |
| 270 |
} |
| 271 |
|
| 272 |
func ExampleErrorAs() { |
| 273 |
runExampleTest(func(t testing.TB) { |
| 274 |
_, err := os.Open("/non-existent-file") |
| 275 |
|
| 276 |
// Checking for a specific error type. |
| 277 |
qt.Assert(t, qt.ErrorAs(err, new(*os.PathError))) |
| 278 |
qt.Assert(t, qt.ErrorAs[*os.PathError](err, nil)) |
| 279 |
|
| 280 |
// Checking fields on a specific error type. |
| 281 |
var pathError *os.PathError |
| 282 |
if qt.Check(t, qt.ErrorAs(err, &pathError)) { |
| 283 |
qt.Assert(t, qt.Equals(pathError.Path, "/non-existent-file")) |
| 284 |
} |
| 285 |
}) |
| 286 |
// Output: PASS |
| 287 |
} |
| 288 |
|
| 289 |
func ExampleErrorIs() { |
| 290 |
runExampleTest(func(t testing.TB) { |
| 291 |
_, err := os.Open("/non-existent-file") |
| 292 |
|
| 293 |
qt.Assert(t, qt.ErrorIs(err, os.ErrNotExist)) |
| 294 |
}) |
| 295 |
// Output: PASS |
| 296 |
} |
| 297 |
|
| 298 |
func runExampleTest(f func(t testing.TB)) { |
| 299 |
defer func() { |
| 300 |
if err := recover(); err != nil && err != exampleTestFatal { |
| 301 |
panic(err) |
| 302 |
} |
| 303 |
}() |
| 304 |
var t exampleTestingT |
| 305 |
f(&t) |
| 306 |
if t.failed { |
| 307 |
fmt.Println("FAIL") |
| 308 |
} else { |
| 309 |
fmt.Println("PASS") |
| 310 |
} |
| 311 |
} |
| 312 |
|
| 313 |
type exampleTestingT struct { |
| 314 |
testing.TB |
| 315 |
failed bool |
| 316 |
} |
| 317 |
|
| 318 |
var exampleTestFatal = errors.New("example test fatal error") |
| 319 |
|
| 320 |
func (t *exampleTestingT) Helper() {} |
| 321 |
|
| 322 |
func (t *exampleTestingT) Error(args ...any) { |
| 323 |
fmt.Printf("ERROR: %s\n", fmt.Sprint(args...)) |
| 324 |
t.failed = true |
| 325 |
} |
| 326 |
|
| 327 |
func (t *exampleTestingT) Fatal(args ...any) { |
| 328 |
fmt.Printf("FATAL: %s\n", fmt.Sprint(args...)) |
| 329 |
t.failed = true |
| 330 |
panic(exampleTestFatal) |
| 331 |
} |