| 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 |
"math" |
| 11 |
"testing" |
| 12 |
|
| 13 |
"github.com/maxatome/go-testdeep/internal/test" |
| 14 |
"github.com/maxatome/go-testdeep/td" |
| 15 |
) |
| 16 |
|
| 17 |
func TestLen(t *testing.T) { |
| 18 |
checkOK(t, "abcd", td.Len(4)) |
| 19 |
checkOK(t, "abcd", td.Len(td.Between(4, 6))) |
| 20 |
checkOK(t, "abcd", td.Len(td.Between(6, 4))) |
| 21 |
|
| 22 |
checkOK(t, []byte("abcd"), td.Len(4)) |
| 23 |
checkOK(t, []byte("abcd"), td.Len(td.Between(4, 6))) |
| 24 |
|
| 25 |
checkOK(t, [5]int{}, td.Len(5)) |
| 26 |
checkOK(t, [5]int{}, td.Len(int8(5))) |
| 27 |
checkOK(t, [5]int{}, td.Len(int16(5))) |
| 28 |
checkOK(t, [5]int{}, td.Len(int32(5))) |
| 29 |
checkOK(t, [5]int{}, td.Len(int64(5))) |
| 30 |
checkOK(t, [5]int{}, td.Len(uint(5))) |
| 31 |
checkOK(t, [5]int{}, td.Len(uint8(5))) |
| 32 |
checkOK(t, [5]int{}, td.Len(uint16(5))) |
| 33 |
checkOK(t, [5]int{}, td.Len(uint32(5))) |
| 34 |
checkOK(t, [5]int{}, td.Len(uint64(5))) |
| 35 |
checkOK(t, [5]int{}, td.Len(float32(5))) |
| 36 |
checkOK(t, [5]int{}, td.Len(float64(5))) |
| 37 |
checkOK(t, [5]int{}, td.Len(td.Between(4, 6))) |
| 38 |
|
| 39 |
checkOK(t, map[int]bool{1: true, 2: false}, td.Len(2)) |
| 40 |
checkOK(t, map[int]bool{1: true, 2: false}, |
| 41 |
td.Len(td.Between(1, 6))) |
| 42 |
|
| 43 |
checkOK(t, make(chan int, 3), td.Len(0)) |
| 44 |
|
| 45 |
checkError(t, [5]int{}, td.Len(4), |
| 46 |
expectedError{ |
| 47 |
Message: mustBe("bad length"), |
| 48 |
Path: mustBe("DATA"), |
| 49 |
Got: mustBe("5"), |
| 50 |
Expected: mustBe("4"), |
| 51 |
}) |
| 52 |
|
| 53 |
checkError(t, [5]int{}, td.Len(td.Lt(4)), |
| 54 |
expectedError{ |
| 55 |
Message: mustBe("values differ"), |
| 56 |
Path: mustBe("len(DATA)"), |
| 57 |
Got: mustBe("5"), |
| 58 |
Expected: mustBe("< 4"), |
| 59 |
}) |
| 60 |
|
| 61 |
checkError(t, 123, td.Len(4), |
| 62 |
expectedError{ |
| 63 |
Message: mustBe("bad kind"), |
| 64 |
Path: mustBe("DATA"), |
| 65 |
Got: mustBe("int"), |
| 66 |
Expected: mustBe("array OR chan OR map OR slice OR string"), |
| 67 |
}) |
| 68 |
|
| 69 |
// |
| 70 |
// Bad usage |
| 71 |
checkError(t, "never tested", |
| 72 |
td.Len(nil), |
| 73 |
expectedError{ |
| 74 |
Message: mustBe("bad usage of Len operator"), |
| 75 |
Path: mustBe("DATA"), |
| 76 |
Summary: mustBe("usage: Len(TESTDEEP_OPERATOR|INT), but received nil as 1st parameter"), |
| 77 |
}) |
| 78 |
|
| 79 |
checkError(t, "never tested", |
| 80 |
td.Len("12"), |
| 81 |
expectedError{ |
| 82 |
Message: mustBe("bad usage of Len operator"), |
| 83 |
Path: mustBe("DATA"), |
| 84 |
Summary: mustBe("usage: Len(TESTDEEP_OPERATOR|INT), but received string as 1st parameter"), |
| 85 |
}) |
| 86 |
|
| 87 |
// out of bounds |
| 88 |
checkError(t, "never tested", |
| 89 |
td.Len(uint64(math.MaxUint64)), |
| 90 |
expectedError{ |
| 91 |
Message: mustBe("bad usage of Len operator"), |
| 92 |
Path: mustBe("DATA"), |
| 93 |
Summary: mustBe("usage: Len(TESTDEEP_OPERATOR|INT), but received an out of bounds or not integer 1st parameter (18446744073709551615), should be in int range"), |
| 94 |
}) |
| 95 |
|
| 96 |
checkError(t, "never tested", |
| 97 |
td.Len(float64(math.MaxUint64)), |
| 98 |
expectedError{ |
| 99 |
Message: mustBe("bad usage of Len operator"), |
| 100 |
Path: mustBe("DATA"), |
| 101 |
Summary: mustBe("usage: Len(TESTDEEP_OPERATOR|INT), but received an out of bounds or not integer 1st parameter (1.8446744073709552e+19), should be in int range"), |
| 102 |
}) |
| 103 |
|
| 104 |
checkError(t, "never tested", |
| 105 |
td.Len(float64(-math.MaxUint64)), |
| 106 |
expectedError{ |
| 107 |
Message: mustBe("bad usage of Len operator"), |
| 108 |
Path: mustBe("DATA"), |
| 109 |
Summary: mustBe("usage: Len(TESTDEEP_OPERATOR|INT), but received an out of bounds or not integer 1st parameter (-1.8446744073709552e+19), should be in int range"), |
| 110 |
}) |
| 111 |
|
| 112 |
checkError(t, "never tested", |
| 113 |
td.Len(3.1), |
| 114 |
expectedError{ |
| 115 |
Message: mustBe("bad usage of Len operator"), |
| 116 |
Path: mustBe("DATA"), |
| 117 |
Summary: mustBe("usage: Len(TESTDEEP_OPERATOR|INT), but received an out of bounds or not integer 1st parameter (3.1), should be in int range"), |
| 118 |
}) |
| 119 |
|
| 120 |
// |
| 121 |
// String |
| 122 |
test.EqualStr(t, td.Len(3).String(), "len=3") |
| 123 |
test.EqualStr(t, |
| 124 |
td.Len(td.Between(3, 8)).String(), "len: 3 ≤ got ≤ 8") |
| 125 |
test.EqualStr(t, td.Len(td.Gt(8)).String(), "len: > 8") |
| 126 |
|
| 127 |
// Erroneous |
| 128 |
test.EqualStr(t, td.Len("12").String(), "Len(<ERROR>)") |
| 129 |
} |
| 130 |
|
| 131 |
func TestCap(t *testing.T) { |
| 132 |
checkOK(t, make([]byte, 0, 4), td.Cap(4)) |
| 133 |
checkOK(t, make([]byte, 0, 4), td.Cap(td.Between(4, 6))) |
| 134 |
|
| 135 |
checkOK(t, [5]int{}, td.Cap(5)) |
| 136 |
checkOK(t, [5]int{}, td.Cap(int8(5))) |
| 137 |
checkOK(t, [5]int{}, td.Cap(int16(5))) |
| 138 |
checkOK(t, [5]int{}, td.Cap(int32(5))) |
| 139 |
checkOK(t, [5]int{}, td.Cap(int64(5))) |
| 140 |
checkOK(t, [5]int{}, td.Cap(uint(5))) |
| 141 |
checkOK(t, [5]int{}, td.Cap(uint8(5))) |
| 142 |
checkOK(t, [5]int{}, td.Cap(uint16(5))) |
| 143 |
checkOK(t, [5]int{}, td.Cap(uint32(5))) |
| 144 |
checkOK(t, [5]int{}, td.Cap(uint64(5))) |
| 145 |
checkOK(t, [5]int{}, td.Cap(float32(5))) |
| 146 |
checkOK(t, [5]int{}, td.Cap(float64(5))) |
| 147 |
checkOK(t, [5]int{}, td.Cap(td.Between(4, 6))) |
| 148 |
|
| 149 |
checkOK(t, make(chan int, 3), td.Cap(3)) |
| 150 |
|
| 151 |
checkError(t, [5]int{}, td.Cap(4), |
| 152 |
expectedError{ |
| 153 |
Message: mustBe("bad capacity"), |
| 154 |
Path: mustBe("DATA"), |
| 155 |
Got: mustBe("5"), |
| 156 |
Expected: mustBe("4"), |
| 157 |
}) |
| 158 |
|
| 159 |
checkError(t, [5]int{}, td.Cap(td.Between(2, 4)), |
| 160 |
expectedError{ |
| 161 |
Message: mustBe("values differ"), |
| 162 |
Path: mustBe("cap(DATA)"), |
| 163 |
Got: mustBe("5"), |
| 164 |
Expected: mustBe("2 ≤ got ≤ 4"), |
| 165 |
}) |
| 166 |
|
| 167 |
checkError(t, map[int]int{1: 2}, td.Cap(1), |
| 168 |
expectedError{ |
| 169 |
Message: mustBe("bad kind"), |
| 170 |
Path: mustBe("DATA"), |
| 171 |
Got: mustBe("map (map[int]int type)"), |
| 172 |
Expected: mustBe("array OR chan OR slice"), |
| 173 |
}) |
| 174 |
|
| 175 |
// |
| 176 |
// Bad usage |
| 177 |
checkError(t, "never tested", |
| 178 |
td.Cap(nil), |
| 179 |
expectedError{ |
| 180 |
Message: mustBe("bad usage of Cap operator"), |
| 181 |
Path: mustBe("DATA"), |
| 182 |
Summary: mustBe("usage: Cap(TESTDEEP_OPERATOR|INT), but received nil as 1st parameter"), |
| 183 |
}) |
| 184 |
|
| 185 |
checkError(t, "never tested", |
| 186 |
td.Cap("12"), |
| 187 |
expectedError{ |
| 188 |
Message: mustBe("bad usage of Cap operator"), |
| 189 |
Path: mustBe("DATA"), |
| 190 |
Summary: mustBe("usage: Cap(TESTDEEP_OPERATOR|INT), but received string as 1st parameter"), |
| 191 |
}) |
| 192 |
|
| 193 |
// out of bounds |
| 194 |
checkError(t, "never tested", |
| 195 |
td.Cap(uint64(math.MaxUint64)), |
| 196 |
expectedError{ |
| 197 |
Message: mustBe("bad usage of Cap operator"), |
| 198 |
Path: mustBe("DATA"), |
| 199 |
Summary: mustBe("usage: Cap(TESTDEEP_OPERATOR|INT), but received an out of bounds or not integer 1st parameter (18446744073709551615), should be in int range"), |
| 200 |
}) |
| 201 |
|
| 202 |
checkError(t, "never tested", |
| 203 |
td.Cap(float64(math.MaxUint64)), |
| 204 |
expectedError{ |
| 205 |
Message: mustBe("bad usage of Cap operator"), |
| 206 |
Path: mustBe("DATA"), |
| 207 |
Summary: mustBe("usage: Cap(TESTDEEP_OPERATOR|INT), but received an out of bounds or not integer 1st parameter (1.8446744073709552e+19), should be in int range"), |
| 208 |
}) |
| 209 |
|
| 210 |
checkError(t, "never tested", |
| 211 |
td.Cap(float64(-math.MaxUint64)), |
| 212 |
expectedError{ |
| 213 |
Message: mustBe("bad usage of Cap operator"), |
| 214 |
Path: mustBe("DATA"), |
| 215 |
Summary: mustBe("usage: Cap(TESTDEEP_OPERATOR|INT), but received an out of bounds or not integer 1st parameter (-1.8446744073709552e+19), should be in int range"), |
| 216 |
}) |
| 217 |
|
| 218 |
checkError(t, "never tested", |
| 219 |
td.Cap(3.1), |
| 220 |
expectedError{ |
| 221 |
Message: mustBe("bad usage of Cap operator"), |
| 222 |
Path: mustBe("DATA"), |
| 223 |
Summary: mustBe("usage: Cap(TESTDEEP_OPERATOR|INT), but received an out of bounds or not integer 1st parameter (3.1), should be in int range"), |
| 224 |
}) |
| 225 |
|
| 226 |
// |
| 227 |
// String |
| 228 |
test.EqualStr(t, td.Cap(3).String(), "cap=3") |
| 229 |
test.EqualStr(t, |
| 230 |
td.Cap(td.Between(3, 8)).String(), "cap: 3 ≤ got ≤ 8") |
| 231 |
test.EqualStr(t, td.Cap(td.Gt(8)).String(), "cap: > 8") |
| 232 |
|
| 233 |
// Erroneous op |
| 234 |
test.EqualStr(t, td.Cap("12").String(), "Cap(<ERROR>)") |
| 235 |
} |
| 236 |
|
| 237 |
func TestLenCapTypeBehind(t *testing.T) { |
| 238 |
equalTypes(t, td.Cap(3), nil) |
| 239 |
equalTypes(t, td.Len(3), nil) |
| 240 |
|
| 241 |
// Erroneous op |
| 242 |
equalTypes(t, td.Cap("12"), nil) |
| 243 |
equalTypes(t, td.Len("12"), nil) |
| 244 |
} |