Debian Codegraph @

td/cmp_funcs_misc_test.go

raw
1 // Copyright (c) 2018, Maxime Soulé
2 // All rights reserved.
3 //
4 // This source code is licensed under the BSD-style license found in the
5 // LICENSE file in the root directory of this source tree.
6
7 package td_test
8
9 import (
10 "fmt"
11 "testing"
12
13 "github.com/maxatome/go-testdeep/td"
14 )
15
16 func ExampleCmpTrue() {
17 t := &testing.T{}
18
19 got := true
20 ok := td.CmpTrue(t, got, "check that got is true!")
21 fmt.Println(ok)
22
23 got = false
24 ok = td.CmpTrue(t, got, "check that got is true!")
25 fmt.Println(ok)
26
27 // Output:
28 // true
29 // false
30 }
31
32 func ExampleCmpFalse() {
33 t := &testing.T{}
34
35 got := false
36 ok := td.CmpFalse(t, got, "check that got is false!")
37 fmt.Println(ok)
38
39 got = true
40 ok = td.CmpFalse(t, got, "check that got is false!")
41 fmt.Println(ok)
42
43 // Output:
44 // true
45 // false
46 }
47
48 func ExampleCmpError() {
49 t := &testing.T{}
50
51 got := fmt.Errorf("Error #%d", 42)
52 ok := td.CmpError(t, got, "An error occurred")
53 fmt.Println(ok)
54
55 got = nil
56 ok = td.CmpError(t, got, "An error occurred") // fails
57 fmt.Println(ok)
58
59 // Output:
60 // true
61 // false
62 }
63
64 func ExampleCmpNoError() {
65 t := &testing.T{}
66
67 got := fmt.Errorf("Error #%d", 42)
68 ok := td.CmpNoError(t, got, "An error occurred") // fails
69 fmt.Println(ok)
70
71 got = nil
72 ok = td.CmpNoError(t, got, "An error occurred")
73 fmt.Println(ok)
74
75 // Output:
76 // false
77 // true
78 }
79
80 func ExampleCmpNotPanic() {
81 t := &testing.T{}
82
83 ok := td.CmpNotPanic(t, func() {})
84 fmt.Println("checks a panic DID NOT occur:", ok)
85
86 // Classic panic
87 ok = td.CmpNotPanic(t, func() { panic("I am panicking!") },
88 "Hope it does not panic!")
89 fmt.Println("still no panic?", ok)
90
91 // Can detect panic(nil)
92 ok = td.CmpNotPanic(t, func() { panic(nil) }, "Checks for panic(nil)")
93 fmt.Println("last no panic?", ok)
94
95 // Output:
96 // checks a panic DID NOT occur: true
97 // still no panic? false
98 // last no panic? false
99 }