| 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 |
"testing" |
| 11 |
|
| 12 |
"github.com/maxatome/go-testdeep/internal/test" |
| 13 |
"github.com/maxatome/go-testdeep/td" |
| 14 |
) |
| 15 |
|
| 16 |
func TestEmpty(t *testing.T) { |
| 17 |
checkOK(t, nil, td.Empty()) |
| 18 |
checkOK(t, "", td.Empty()) |
| 19 |
checkOK(t, ([]int)(nil), td.Empty()) |
| 20 |
checkOK(t, []int{}, td.Empty()) |
| 21 |
checkOK(t, (map[string]bool)(nil), td.Empty()) |
| 22 |
checkOK(t, map[string]bool{}, td.Empty()) |
| 23 |
checkOK(t, (chan int)(nil), td.Empty()) |
| 24 |
checkOK(t, make(chan int), td.Empty()) |
| 25 |
checkOK(t, [0]int{}, td.Empty()) |
| 26 |
|
| 27 |
type MySlice []int |
| 28 |
checkOK(t, MySlice{}, td.Empty()) |
| 29 |
checkOK(t, &MySlice{}, td.Empty()) |
| 30 |
|
| 31 |
l1 := &MySlice{} |
| 32 |
l2 := &l1 |
| 33 |
l3 := &l2 |
| 34 |
checkOK(t, &l3, td.Empty()) |
| 35 |
|
| 36 |
l1 = nil |
| 37 |
checkOK(t, &l3, td.Empty()) |
| 38 |
|
| 39 |
checkError(t, 12, td.Empty(), |
| 40 |
expectedError{ |
| 41 |
Message: mustBe("bad kind"), |
| 42 |
Path: mustBe("DATA"), |
| 43 |
Got: mustBe("int"), |
| 44 |
Expected: mustBe("array OR chan OR map OR slice OR string OR pointer(s) on them"), |
| 45 |
}) |
| 46 |
|
| 47 |
num := 12 |
| 48 |
n1 := &num |
| 49 |
n2 := &n1 |
| 50 |
n3 := &n2 |
| 51 |
checkError(t, &n3, td.Empty(), |
| 52 |
expectedError{ |
| 53 |
Message: mustBe("bad kind"), |
| 54 |
Path: mustBe("DATA"), |
| 55 |
Got: mustBe("****int"), |
| 56 |
Expected: mustBe("array OR chan OR map OR slice OR string OR pointer(s) on them"), |
| 57 |
}) |
| 58 |
|
| 59 |
n1 = nil |
| 60 |
checkError(t, &n3, td.Empty(), |
| 61 |
expectedError{ |
| 62 |
Message: mustBe("bad kind"), |
| 63 |
Path: mustBe("DATA"), |
| 64 |
Got: mustBe("****int"), |
| 65 |
Expected: mustBe("array OR chan OR map OR slice OR string OR pointer(s) on them"), |
| 66 |
}) |
| 67 |
|
| 68 |
checkError(t, "foobar", td.Empty(), |
| 69 |
expectedError{ |
| 70 |
Message: mustBe("not empty"), |
| 71 |
Path: mustBe("DATA"), |
| 72 |
Got: mustContain(`"foobar"`), |
| 73 |
Expected: mustBe("empty"), |
| 74 |
}) |
| 75 |
checkError(t, []int{1}, td.Empty(), |
| 76 |
expectedError{ |
| 77 |
Message: mustBe("not empty"), |
| 78 |
Path: mustBe("DATA"), |
| 79 |
Got: mustContain("1"), |
| 80 |
Expected: mustBe("empty"), |
| 81 |
}) |
| 82 |
checkError(t, map[string]bool{"foo": true}, td.Empty(), |
| 83 |
expectedError{ |
| 84 |
Message: mustBe("not empty"), |
| 85 |
Path: mustBe("DATA"), |
| 86 |
Got: mustContain(`"foo": (bool) true`), |
| 87 |
Expected: mustBe("empty"), |
| 88 |
}) |
| 89 |
|
| 90 |
ch := make(chan int, 1) |
| 91 |
ch <- 42 |
| 92 |
checkError(t, ch, td.Empty(), |
| 93 |
expectedError{ |
| 94 |
Message: mustBe("not empty"), |
| 95 |
Path: mustBe("DATA"), |
| 96 |
Got: mustContain("(chan int)"), |
| 97 |
Expected: mustBe("empty"), |
| 98 |
}) |
| 99 |
|
| 100 |
checkError(t, [3]int{}, td.Empty(), |
| 101 |
expectedError{ |
| 102 |
Message: mustBe("not empty"), |
| 103 |
Path: mustBe("DATA"), |
| 104 |
Got: mustContain("0"), |
| 105 |
Expected: mustBe("empty"), |
| 106 |
}) |
| 107 |
|
| 108 |
// |
| 109 |
// String |
| 110 |
test.EqualStr(t, td.Empty().String(), "Empty()") |
| 111 |
} |
| 112 |
|
| 113 |
func TestNotEmpty(t *testing.T) { |
| 114 |
checkOK(t, "foobar", td.NotEmpty()) |
| 115 |
checkOK(t, []int{1}, td.NotEmpty()) |
| 116 |
checkOK(t, map[string]bool{"foo": true}, td.NotEmpty()) |
| 117 |
checkOK(t, [3]int{}, td.NotEmpty()) |
| 118 |
|
| 119 |
ch := make(chan int, 1) |
| 120 |
ch <- 42 |
| 121 |
checkOK(t, ch, td.NotEmpty()) |
| 122 |
|
| 123 |
type MySlice []int |
| 124 |
checkOK(t, MySlice{1}, td.NotEmpty()) |
| 125 |
checkOK(t, &MySlice{1}, td.NotEmpty()) |
| 126 |
|
| 127 |
l1 := &MySlice{1} |
| 128 |
l2 := &l1 |
| 129 |
l3 := &l2 |
| 130 |
checkOK(t, &l3, td.NotEmpty()) |
| 131 |
|
| 132 |
checkError(t, 12, td.NotEmpty(), |
| 133 |
expectedError{ |
| 134 |
Message: mustBe("bad kind"), |
| 135 |
Path: mustBe("DATA"), |
| 136 |
Got: mustBe("int"), |
| 137 |
Expected: mustBe("array OR chan OR map OR slice OR string OR pointer(s) on them"), |
| 138 |
}) |
| 139 |
|
| 140 |
checkError(t, nil, td.NotEmpty(), |
| 141 |
expectedError{ |
| 142 |
Message: mustBe("empty"), |
| 143 |
Path: mustBe("DATA"), |
| 144 |
Got: mustContain("nil"), |
| 145 |
Expected: mustBe("not empty"), |
| 146 |
}) |
| 147 |
checkError(t, "", td.NotEmpty(), |
| 148 |
expectedError{ |
| 149 |
Message: mustBe("empty"), |
| 150 |
Path: mustBe("DATA"), |
| 151 |
Got: mustContain(`""`), |
| 152 |
Expected: mustBe("not empty"), |
| 153 |
}) |
| 154 |
checkError(t, ([]int)(nil), td.NotEmpty(), |
| 155 |
expectedError{ |
| 156 |
Message: mustBe("empty"), |
| 157 |
Path: mustBe("DATA"), |
| 158 |
Got: mustBe("([]int) <nil>"), |
| 159 |
Expected: mustBe("not empty"), |
| 160 |
}) |
| 161 |
checkError(t, []int{}, td.NotEmpty(), |
| 162 |
expectedError{ |
| 163 |
Message: mustBe("empty"), |
| 164 |
Path: mustBe("DATA"), |
| 165 |
Got: mustContain("([]int)"), |
| 166 |
Expected: mustBe("not empty"), |
| 167 |
}) |
| 168 |
checkError(t, (map[string]bool)(nil), td.NotEmpty(), |
| 169 |
expectedError{ |
| 170 |
Message: mustBe("empty"), |
| 171 |
Path: mustBe("DATA"), |
| 172 |
Got: mustContain("(map[string]bool) <nil>"), |
| 173 |
Expected: mustBe("not empty"), |
| 174 |
}) |
| 175 |
checkError(t, map[string]bool{}, td.NotEmpty(), |
| 176 |
expectedError{ |
| 177 |
Message: mustBe("empty"), |
| 178 |
Path: mustBe("DATA"), |
| 179 |
Got: mustContain("(map[string]bool)"), |
| 180 |
Expected: mustBe("not empty"), |
| 181 |
}) |
| 182 |
checkError(t, (chan int)(nil), td.NotEmpty(), |
| 183 |
expectedError{ |
| 184 |
Message: mustBe("empty"), |
| 185 |
Path: mustBe("DATA"), |
| 186 |
Got: mustContain("(chan int) <nil>"), |
| 187 |
Expected: mustBe("not empty"), |
| 188 |
}) |
| 189 |
checkError(t, make(chan int), td.NotEmpty(), |
| 190 |
expectedError{ |
| 191 |
Message: mustBe("empty"), |
| 192 |
Path: mustBe("DATA"), |
| 193 |
Got: mustContain("(chan int)"), |
| 194 |
Expected: mustBe("not empty"), |
| 195 |
}) |
| 196 |
checkError(t, [0]int{}, td.NotEmpty(), |
| 197 |
expectedError{ |
| 198 |
Message: mustBe("empty"), |
| 199 |
Path: mustBe("DATA"), |
| 200 |
Got: mustContain("([0]int)"), |
| 201 |
Expected: mustBe("not empty"), |
| 202 |
}) |
| 203 |
|
| 204 |
// |
| 205 |
// String |
| 206 |
test.EqualStr(t, td.NotEmpty().String(), "NotEmpty()") |
| 207 |
} |
| 208 |
|
| 209 |
func TestEmptyTypeBehind(t *testing.T) { |
| 210 |
equalTypes(t, td.Empty(), nil) |
| 211 |
equalTypes(t, td.NotEmpty(), nil) |
| 212 |
} |