| 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 |
"bytes" |
| 11 |
"fmt" |
| 12 |
"testing" |
| 13 |
|
| 14 |
"github.com/maxatome/go-testdeep/internal/test" |
| 15 |
"github.com/maxatome/go-testdeep/td" |
| 16 |
) |
| 17 |
|
| 18 |
func TestNil(t *testing.T) { |
| 19 |
checkOK(t, (func())(nil), td.Nil()) |
| 20 |
checkOK(t, ([]int)(nil), td.Nil()) |
| 21 |
checkOK(t, (map[bool]bool)(nil), td.Nil()) |
| 22 |
checkOK(t, (*int)(nil), td.Nil()) |
| 23 |
checkOK(t, (chan int)(nil), td.Nil()) |
| 24 |
checkOK(t, nil, td.Nil()) |
| 25 |
checkOK(t, |
| 26 |
map[string]any{"foo": nil}, |
| 27 |
map[string]any{"foo": td.Nil()}, |
| 28 |
) |
| 29 |
|
| 30 |
var got fmt.Stringer = (*bytes.Buffer)(nil) |
| 31 |
checkOK(t, got, td.Nil()) |
| 32 |
|
| 33 |
checkError(t, 42, td.Nil(), |
| 34 |
expectedError{ |
| 35 |
Message: mustBe("non-nil"), |
| 36 |
Path: mustBe("DATA"), |
| 37 |
Got: mustBe("42"), |
| 38 |
Expected: mustBe("nil"), |
| 39 |
}) |
| 40 |
|
| 41 |
num := 42 |
| 42 |
checkError(t, &num, td.Nil(), |
| 43 |
expectedError{ |
| 44 |
Message: mustBe("non-nil"), |
| 45 |
Path: mustBe("DATA"), |
| 46 |
Got: mustMatch(`\(\*int\).*42`), |
| 47 |
Expected: mustBe("nil"), |
| 48 |
}) |
| 49 |
|
| 50 |
// |
| 51 |
// String |
| 52 |
test.EqualStr(t, td.Nil().String(), "nil") |
| 53 |
} |
| 54 |
|
| 55 |
func TestNotNil(t *testing.T) { |
| 56 |
num := 42 |
| 57 |
checkOK(t, func() {}, td.NotNil()) |
| 58 |
checkOK(t, []int{}, td.NotNil()) |
| 59 |
checkOK(t, map[bool]bool{}, td.NotNil()) |
| 60 |
checkOK(t, &num, td.NotNil()) |
| 61 |
checkOK(t, 42, td.NotNil()) |
| 62 |
|
| 63 |
checkError(t, (func())(nil), td.NotNil(), |
| 64 |
expectedError{ |
| 65 |
Message: mustBe("nil value"), |
| 66 |
Path: mustBe("DATA"), |
| 67 |
Got: mustContain("nil"), |
| 68 |
Expected: mustBe("not nil"), |
| 69 |
}) |
| 70 |
checkError(t, ([]int)(nil), td.NotNil(), |
| 71 |
expectedError{ |
| 72 |
Message: mustBe("nil value"), |
| 73 |
Path: mustBe("DATA"), |
| 74 |
Got: mustContain("nil"), |
| 75 |
Expected: mustBe("not nil"), |
| 76 |
}) |
| 77 |
checkError(t, (map[bool]bool)(nil), td.NotNil(), |
| 78 |
expectedError{ |
| 79 |
Message: mustBe("nil value"), |
| 80 |
Path: mustBe("DATA"), |
| 81 |
Got: mustContain("nil"), |
| 82 |
Expected: mustBe("not nil"), |
| 83 |
}) |
| 84 |
checkError(t, (*int)(nil), td.NotNil(), |
| 85 |
expectedError{ |
| 86 |
Message: mustBe("nil value"), |
| 87 |
Path: mustBe("DATA"), |
| 88 |
Got: mustContain("nil"), |
| 89 |
Expected: mustBe("not nil"), |
| 90 |
}) |
| 91 |
checkError(t, (chan int)(nil), td.NotNil(), |
| 92 |
expectedError{ |
| 93 |
Message: mustBe("nil value"), |
| 94 |
Path: mustBe("DATA"), |
| 95 |
Got: mustContain("nil"), |
| 96 |
Expected: mustBe("not nil"), |
| 97 |
}) |
| 98 |
checkError(t, nil, td.NotNil(), |
| 99 |
expectedError{ |
| 100 |
Message: mustBe("nil value"), |
| 101 |
Path: mustBe("DATA"), |
| 102 |
Got: mustBe("nil"), |
| 103 |
Expected: mustBe("not nil"), |
| 104 |
}) |
| 105 |
|
| 106 |
var got fmt.Stringer = (*bytes.Buffer)(nil) |
| 107 |
checkError(t, got, td.NotNil(), |
| 108 |
expectedError{ |
| 109 |
Message: mustBe("nil value"), |
| 110 |
Path: mustBe("DATA"), |
| 111 |
Got: mustContain("<nil>"), |
| 112 |
Expected: mustBe("not nil"), |
| 113 |
}) |
| 114 |
|
| 115 |
// |
| 116 |
// String |
| 117 |
test.EqualStr(t, td.NotNil().String(), "not nil") |
| 118 |
} |
| 119 |
|
| 120 |
func TestNilTypeBehind(t *testing.T) { |
| 121 |
equalTypes(t, td.Nil(), nil) |
| 122 |
equalTypes(t, td.NotNil(), nil) |
| 123 |
} |