| 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 ExampleT_True() { |
| 17 |
t := td.NewT(&testing.T{}) |
| 18 |
|
| 19 |
got := true |
| 20 |
ok := t.True(got, "check that got is true!") |
| 21 |
fmt.Println(ok) |
| 22 |
|
| 23 |
got = false |
| 24 |
ok = t.True(got, "check that got is true!") |
| 25 |
fmt.Println(ok) |
| 26 |
|
| 27 |
// Output: |
| 28 |
// true |
| 29 |
// false |
| 30 |
} |
| 31 |
|
| 32 |
func ExampleT_False() { |
| 33 |
t := td.NewT(&testing.T{}) |
| 34 |
|
| 35 |
got := false |
| 36 |
ok := t.False(got, "check that got is false!") |
| 37 |
fmt.Println(ok) |
| 38 |
|
| 39 |
got = true |
| 40 |
ok = t.False(got, "check that got is false!") |
| 41 |
fmt.Println(ok) |
| 42 |
|
| 43 |
// Output: |
| 44 |
// true |
| 45 |
// false |
| 46 |
} |
| 47 |
|
| 48 |
func ExampleT_CmpError() { |
| 49 |
t := td.NewT(&testing.T{}) |
| 50 |
|
| 51 |
got := fmt.Errorf("Error #%d", 42) |
| 52 |
ok := t.CmpError(got, "An error occurred") |
| 53 |
fmt.Println(ok) |
| 54 |
|
| 55 |
got = nil |
| 56 |
ok = t.CmpError(got, "An error occurred") // fails |
| 57 |
fmt.Println(ok) |
| 58 |
|
| 59 |
// Output: |
| 60 |
// true |
| 61 |
// false |
| 62 |
} |
| 63 |
|
| 64 |
func ExampleT_CmpNoError() { |
| 65 |
t := td.NewT(&testing.T{}) |
| 66 |
|
| 67 |
got := fmt.Errorf("Error #%d", 42) |
| 68 |
ok := t.CmpNoError(got, "An error occurred") // fails |
| 69 |
fmt.Println(ok) |
| 70 |
|
| 71 |
got = nil |
| 72 |
ok = t.CmpNoError(got, "An error occurred") |
| 73 |
fmt.Println(ok) |
| 74 |
|
| 75 |
// Output: |
| 76 |
// false |
| 77 |
// true |
| 78 |
} |
| 79 |
|
| 80 |
func ExampleT_CmpNotPanic() { |
| 81 |
t := td.NewT(&testing.T{}) |
| 82 |
|
| 83 |
ok := t.CmpNotPanic(func() {}, nil) |
| 84 |
fmt.Println("checks a panic DID NOT occur:", ok) |
| 85 |
|
| 86 |
// Classic panic |
| 87 |
ok = t.CmpNotPanic(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 = t.CmpNotPanic(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 |
} |