| 1 |
// Copyright (c) 2022, 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 |
"time" |
| 12 |
|
| 13 |
"github.com/maxatome/go-testdeep/internal/test" |
| 14 |
"github.com/maxatome/go-testdeep/internal/types" |
| 15 |
"github.com/maxatome/go-testdeep/td" |
| 16 |
) |
| 17 |
|
| 18 |
func TestRecv(t *testing.T) { |
| 19 |
fillCh := func(ch chan int, val int) { |
| 20 |
ch <- val // td.Cmp |
| 21 |
ch <- val // EqDeeply aka boolean context |
| 22 |
ch <- val // EqDeeplyError |
| 23 |
ch <- val // interface + td.Cmp |
| 24 |
ch <- val // interface + EqDeeply aka boolean context |
| 25 |
ch <- val // interface + EqDeeplyError |
| 26 |
} |
| 27 |
mkCh := func(val int) chan int { |
| 28 |
ch := make(chan int, 6) |
| 29 |
fillCh(ch, val) |
| 30 |
close(ch) |
| 31 |
return ch |
| 32 |
} |
| 33 |
|
| 34 |
t.Run("all good", func(t *testing.T) { |
| 35 |
ch := mkCh(1) |
| 36 |
checkOK(t, ch, td.Recv(1)) |
| 37 |
checkOK(t, ch, td.Recv(td.RecvClosed, 10*time.Microsecond)) |
| 38 |
|
| 39 |
ch = mkCh(42) |
| 40 |
checkOK(t, ch, td.Recv(td.Between(40, 45))) |
| 41 |
checkOK(t, ch, td.Recv(td.RecvClosed)) |
| 42 |
}) |
| 43 |
|
| 44 |
t.Run("complete cycle", func(t *testing.T) { |
| 45 |
ch := make(chan int, 6) |
| 46 |
|
| 47 |
t.Run("empty", func(t *testing.T) { |
| 48 |
checkOK(t, ch, td.Recv(td.RecvNothing)) |
| 49 |
checkOK(t, ch, td.Recv(td.RecvNothing, 10*time.Microsecond)) |
| 50 |
|
| 51 |
checkOK(t, &ch, td.Recv(td.RecvNothing)) |
| 52 |
checkOK(t, &ch, td.Recv(td.RecvNothing, 10*time.Microsecond)) |
| 53 |
}) |
| 54 |
|
| 55 |
t.Run("just filled", func(t *testing.T) { |
| 56 |
fillCh(ch, 33) |
| 57 |
checkOK(t, ch, td.Recv(33)) |
| 58 |
|
| 59 |
fillCh(ch, 34) |
| 60 |
checkOK(t, &ch, td.Recv(34)) |
| 61 |
}) |
| 62 |
|
| 63 |
t.Run("nothing to recv on channel", func(t *testing.T) { |
| 64 |
checkError(t, ch, td.Recv(td.RecvClosed), |
| 65 |
expectedError{ |
| 66 |
Message: mustBe("values differ"), |
| 67 |
Path: mustBe("recv(DATA)"), |
| 68 |
Got: mustBe("nothing received on channel"), |
| 69 |
Expected: mustBe("channel is closed"), |
| 70 |
}) |
| 71 |
checkError(t, &ch, td.Recv(td.RecvClosed), |
| 72 |
expectedError{ |
| 73 |
Message: mustBe("values differ"), |
| 74 |
Path: mustBe("recv(DATA)"), |
| 75 |
Got: mustBe("nothing received on channel"), |
| 76 |
Expected: mustBe("channel is closed"), |
| 77 |
}) |
| 78 |
|
| 79 |
checkError(t, ch, td.Recv(42), |
| 80 |
expectedError{ |
| 81 |
Message: mustBe("values differ"), |
| 82 |
Path: mustBe("recv(DATA)"), |
| 83 |
Got: mustBe("nothing received on channel"), |
| 84 |
Expected: mustBe("42"), |
| 85 |
}) |
| 86 |
checkError(t, &ch, td.Recv(42), |
| 87 |
expectedError{ |
| 88 |
Message: mustBe("values differ"), |
| 89 |
Path: mustBe("recv(DATA)"), |
| 90 |
Got: mustBe("nothing received on channel"), |
| 91 |
Expected: mustBe("42"), |
| 92 |
}) |
| 93 |
}) |
| 94 |
|
| 95 |
close(ch) |
| 96 |
|
| 97 |
t.Run("closed channel", func(t *testing.T) { |
| 98 |
checkError(t, ch, td.Recv(td.RecvNothing), |
| 99 |
expectedError{ |
| 100 |
Message: mustBe("values differ"), |
| 101 |
Path: mustBe("recv(DATA)"), |
| 102 |
Got: mustBe("channel is closed"), |
| 103 |
Expected: mustBe("nothing received on channel"), |
| 104 |
}) |
| 105 |
checkError(t, &ch, td.Recv(td.RecvNothing), |
| 106 |
expectedError{ |
| 107 |
Message: mustBe("values differ"), |
| 108 |
Path: mustBe("recv(DATA)"), |
| 109 |
Got: mustBe("channel is closed"), |
| 110 |
Expected: mustBe("nothing received on channel"), |
| 111 |
}) |
| 112 |
|
| 113 |
checkError(t, ch, td.Recv(42), |
| 114 |
expectedError{ |
| 115 |
Message: mustBe("values differ"), |
| 116 |
Path: mustBe("recv(DATA)"), |
| 117 |
Got: mustBe("channel is closed"), |
| 118 |
Expected: mustBe("42"), |
| 119 |
}) |
| 120 |
checkError(t, &ch, td.Recv(42), |
| 121 |
expectedError{ |
| 122 |
Message: mustBe("values differ"), |
| 123 |
Path: mustBe("recv(DATA)"), |
| 124 |
Got: mustBe("channel is closed"), |
| 125 |
Expected: mustBe("42"), |
| 126 |
}) |
| 127 |
}) |
| 128 |
}) |
| 129 |
|
| 130 |
t.Run("nil channel", func(t *testing.T) { |
| 131 |
var ch chan int |
| 132 |
checkError(t, ch, td.Recv(42), |
| 133 |
expectedError{ |
| 134 |
Message: mustBe("values differ"), |
| 135 |
Path: mustBe("recv(DATA)"), |
| 136 |
Got: mustBe("nothing received on channel"), |
| 137 |
Expected: mustBe("42"), |
| 138 |
}) |
| 139 |
checkError(t, &ch, td.Recv(42), |
| 140 |
expectedError{ |
| 141 |
Message: mustBe("values differ"), |
| 142 |
Path: mustBe("recv(DATA)"), |
| 143 |
Got: mustBe("nothing received on channel"), |
| 144 |
Expected: mustBe("42"), |
| 145 |
}) |
| 146 |
}) |
| 147 |
|
| 148 |
t.Run("nil pointer", func(t *testing.T) { |
| 149 |
checkError(t, (*chan int)(nil), td.Recv(42), |
| 150 |
expectedError{ |
| 151 |
Message: mustBe("nil pointer"), |
| 152 |
Path: mustBe("DATA"), |
| 153 |
Got: mustBe("nil *chan (*chan int type)"), |
| 154 |
Expected: mustBe("non-nil *chan"), |
| 155 |
}) |
| 156 |
}) |
| 157 |
|
| 158 |
t.Run("chan any", func(t *testing.T) { |
| 159 |
ch := make(chan any, 6) |
| 160 |
fillCh := func(val any) { |
| 161 |
ch <- val // td.Cmp |
| 162 |
ch <- val // EqDeeply aka boolean context |
| 163 |
ch <- val // EqDeeplyError |
| 164 |
ch <- val // interface + td.Cmp |
| 165 |
ch <- val // interface + EqDeeply aka boolean context |
| 166 |
ch <- val // interface + EqDeeplyError |
| 167 |
} |
| 168 |
|
| 169 |
fillCh(1) |
| 170 |
checkOK(t, ch, td.Recv(1)) |
| 171 |
|
| 172 |
fillCh(nil) |
| 173 |
checkOK(t, ch, td.Recv(nil)) |
| 174 |
|
| 175 |
close(ch) |
| 176 |
checkOK(t, ch, td.Recv(td.RecvClosed)) |
| 177 |
}) |
| 178 |
|
| 179 |
t.Run("errors", func(t *testing.T) { |
| 180 |
checkError(t, "never tested", |
| 181 |
td.Recv(23, time.Second, time.Second), |
| 182 |
expectedError{ |
| 183 |
Message: mustBe("bad usage of Recv operator"), |
| 184 |
Path: mustBe("DATA"), |
| 185 |
Summary: mustBe("usage: Recv(EXPECTED[, TIMEOUT]), too many parameters"), |
| 186 |
}) |
| 187 |
|
| 188 |
checkError(t, 42, td.Recv(33), |
| 189 |
expectedError{ |
| 190 |
Message: mustBe("bad kind"), |
| 191 |
Path: mustBe("DATA"), |
| 192 |
Got: mustBe("int"), |
| 193 |
Expected: mustBe("chan OR *chan"), |
| 194 |
}) |
| 195 |
|
| 196 |
checkError(t, &struct{}{}, td.Recv(33), |
| 197 |
expectedError{ |
| 198 |
Message: mustBe("bad kind"), |
| 199 |
Path: mustBe("DATA"), |
| 200 |
Got: mustBe("*struct (*struct {} type)"), |
| 201 |
Expected: mustBe("chan OR *chan"), |
| 202 |
}) |
| 203 |
|
| 204 |
checkError(t, nil, td.Recv(33), |
| 205 |
expectedError{ |
| 206 |
Message: mustBe("bad kind"), |
| 207 |
Path: mustBe("DATA"), |
| 208 |
Got: mustBe("nil"), |
| 209 |
Expected: mustBe("chan OR *chan"), |
| 210 |
}) |
| 211 |
}) |
| 212 |
} |
| 213 |
|
| 214 |
func TestRecvString(t *testing.T) { |
| 215 |
test.EqualStr(t, td.Recv(3).String(), "recv=3") |
| 216 |
test.EqualStr(t, td.Recv(td.Between(3, 8)).String(), "recv: 3 ≤ got ≤ 8") |
| 217 |
test.EqualStr(t, td.Recv(td.Gt(8)).String(), "recv: > 8") |
| 218 |
|
| 219 |
// Erroneous op |
| 220 |
test.EqualStr(t, td.Recv(3, 0, 0).String(), "Recv(<ERROR>)") |
| 221 |
} |
| 222 |
|
| 223 |
func TestRecvTypeBehind(t *testing.T) { |
| 224 |
equalTypes(t, td.Recv(3), 0) |
| 225 |
equalTypes(t, td.Recv(td.Between(3, 4)), 0) |
| 226 |
|
| 227 |
// Erroneous op |
| 228 |
equalTypes(t, td.Recv(3, 0, 0), nil) |
| 229 |
} |
| 230 |
|
| 231 |
func TestRecvKind(t *testing.T) { |
| 232 |
test.IsTrue(t, td.RecvNothing == types.RecvNothing) |
| 233 |
test.IsTrue(t, td.RecvClosed == types.RecvClosed) |
| 234 |
} |