How to use checkOK method of td_test Package

Best Go-testdeep code snippet using td_test.checkOK

td_between_test.go

Source:td_between_test.go Github

copy

Full Screen

...12 "github.com/maxatome/go-testdeep/internal/test"13 "github.com/maxatome/go-testdeep/td"14)15func TestBetween(t *testing.T) {16 checkOK(t, 12, td.Between(9, 13))17 checkOK(t, 12, td.Between(13, 9))18 checkOK(t, 12, td.Between(9, 12, td.BoundsOutIn))19 checkOK(t, 12, td.Between(12, 13, td.BoundsInOut))20 checkError(t, 10, td.Between(10, 15, td.BoundsOutIn),21 expectedError{22 Message: mustBe("values differ"),23 Path: mustBe("DATA"),24 Got: mustBe("10"),25 Expected: mustBe("10 < got ≤ 15"),26 })27 checkError(t, 10, td.Between(10, 15, td.BoundsOutOut),28 expectedError{29 Message: mustBe("values differ"),30 Path: mustBe("DATA"),31 Got: mustBe("10"),32 Expected: mustBe("10 < got < 15"),33 })34 checkError(t, 15, td.Between(10, 15, td.BoundsInOut),35 expectedError{36 Message: mustBe("values differ"),37 Path: mustBe("DATA"),38 Got: mustBe("15"),39 Expected: mustBe("10 ≤ got < 15"),40 })41 checkError(t, 15, td.Between(10, 15, td.BoundsOutOut),42 expectedError{43 Message: mustBe("values differ"),44 Path: mustBe("DATA"),45 Got: mustBe("15"),46 Expected: mustBe("10 < got < 15"),47 })48 checkError(t, 15, td.Between(uint(10), uint(15), td.BoundsOutOut),49 expectedError{50 Message: mustBe("type mismatch"),51 Path: mustBe("DATA"),52 Got: mustBe("int"),53 Expected: mustBe("uint"),54 })55 checkOK(t, uint16(12), td.Between(uint16(9), uint16(13)))56 checkOK(t, uint16(12), td.Between(uint16(13), uint16(9)))57 checkOK(t, uint16(12),58 td.Between(uint16(9), uint16(12), td.BoundsOutIn))59 checkOK(t, uint16(12),60 td.Between(uint16(12), uint16(13), td.BoundsInOut))61 checkOK(t, 12.1, td.Between(9.5, 13.1))62 checkOK(t, 12.1, td.Between(13.1, 9.5))63 checkOK(t, 12.1, td.Between(9.5, 12.1, td.BoundsOutIn))64 checkOK(t, 12.1, td.Between(12.1, 13.1, td.BoundsInOut))65 checkOK(t, "abc", td.Between("aaa", "bbb"))66 checkOK(t, "abc", td.Between("bbb", "aaa"))67 checkOK(t, "abc", td.Between("aaa", "abc", td.BoundsOutIn))68 checkOK(t, "abc", td.Between("abc", "bbb", td.BoundsInOut))69 checkOK(t, 12*time.Hour, td.Between(60*time.Second, 24*time.Hour))70 //71 // Bad usage72 checkError(t, "never tested",73 td.Between([]byte("test"), []byte("test")),74 expectedError{75 Message: mustBe("bad usage of Between operator"),76 Path: mustBe("DATA"),77 Summary: mustBe("usage: Between(NUM|STRING|TIME, NUM|STRING|TIME/DURATION[, BOUNDS_KIND]), but received []uint8 (slice) as 1st parameter"),78 })79 checkError(t, "never tested",80 td.Between(12, "test"),81 expectedError{82 Message: mustBe("bad usage of Between operator"),83 Path: mustBe("DATA"),84 Summary: mustBe("Between(FROM, TO): FROM and TO must have the same type: int ≠ string"),85 })86 checkError(t, "never tested",87 td.Between("test", 12),88 expectedError{89 Message: mustBe("bad usage of Between operator"),90 Path: mustBe("DATA"),91 Summary: mustBe("Between(FROM, TO): FROM and TO must have the same type: string ≠ int"),92 })93 checkError(t, "never tested",94 td.Between(1, 2, td.BoundsInIn, td.BoundsInOut),95 expectedError{96 Message: mustBe("bad usage of Between operator"),97 Path: mustBe("DATA"),98 Summary: mustBe("usage: Between(NUM|STRING|TIME, NUM|STRING|TIME/DURATION[, BOUNDS_KIND]), too many parameters"),99 })100 type notTime struct{}101 checkError(t, "never tested",102 td.Between(notTime{}, notTime{}),103 expectedError{104 Message: mustBe("bad usage of Between operator"),105 Path: mustBe("DATA"),106 Summary: mustBe("usage: Between(NUM|STRING|TIME, NUM|STRING|TIME/DURATION[, BOUNDS_KIND]), but received td_test.notTime (struct) as 1st parameter"),107 })108 // Erroneous op109 test.EqualStr(t, td.Between("test", 12).String(), "Between(<ERROR>)")110}111func TestN(t *testing.T) {112 //113 // Unsigned114 checkOK(t, uint(12), td.N(uint(12)))115 checkOK(t, uint(11), td.N(uint(12), uint(1)))116 checkOK(t, uint(13), td.N(uint(12), uint(1)))117 checkError(t, 10, td.N(uint(12), uint(1)),118 expectedError{119 Message: mustBe("type mismatch"),120 Path: mustBe("DATA"),121 Got: mustBe("int"),122 Expected: mustBe("uint"),123 })124 checkOK(t, uint8(12), td.N(uint8(12)))125 checkOK(t, uint8(11), td.N(uint8(12), uint8(1)))126 checkOK(t, uint8(13), td.N(uint8(12), uint8(1)))127 checkError(t, 10, td.N(uint8(12), uint8(1)),128 expectedError{129 Message: mustBe("type mismatch"),130 Path: mustBe("DATA"),131 Got: mustBe("int"),132 Expected: mustBe("uint8"),133 })134 checkOK(t, uint16(12), td.N(uint16(12)))135 checkOK(t, uint16(11), td.N(uint16(12), uint16(1)))136 checkOK(t, uint16(13), td.N(uint16(12), uint16(1)))137 checkError(t, 10, td.N(uint16(12), uint16(1)),138 expectedError{139 Message: mustBe("type mismatch"),140 Path: mustBe("DATA"),141 Got: mustBe("int"),142 Expected: mustBe("uint16"),143 })144 checkOK(t, uint32(12), td.N(uint32(12)))145 checkOK(t, uint32(11), td.N(uint32(12), uint32(1)))146 checkOK(t, uint32(13), td.N(uint32(12), uint32(1)))147 checkError(t, 10, td.N(uint32(12), uint32(1)),148 expectedError{149 Message: mustBe("type mismatch"),150 Path: mustBe("DATA"),151 Got: mustBe("int"),152 Expected: mustBe("uint32"),153 })154 checkOK(t, uint64(12), td.N(uint64(12)))155 checkOK(t, uint64(11), td.N(uint64(12), uint64(1)))156 checkOK(t, uint64(13), td.N(uint64(12), uint64(1)))157 checkError(t, 10, td.N(uint64(12), uint64(1)),158 expectedError{159 Message: mustBe("type mismatch"),160 Path: mustBe("DATA"),161 Got: mustBe("int"),162 Expected: mustBe("uint64"),163 })164 checkOK(t, uint64(math.MaxUint64),165 td.N(uint64(math.MaxUint64), uint64(2)))166 checkError(t, uint64(0), td.N(uint64(math.MaxUint64), uint64(2)),167 expectedError{168 Message: mustBe("values differ"),169 Path: mustBe("DATA"),170 Got: mustBe("(uint64) 0"),171 Expected: mustBe(fmt.Sprintf("(uint64) %v ≤ got ≤ (uint64) %v",172 uint64(math.MaxUint64)-2, uint64(math.MaxUint64))),173 })174 checkOK(t, uint64(0), td.N(uint64(0), uint64(2)))175 checkError(t, uint64(math.MaxUint64), td.N(uint64(0), uint64(2)),176 expectedError{177 Message: mustBe("values differ"),178 Path: mustBe("DATA"),179 Got: mustBe(fmt.Sprintf("(uint64) %v", uint64(math.MaxUint64))),180 Expected: mustBe("(uint64) 0 ≤ got ≤ (uint64) 2"),181 })182 //183 // Signed184 checkOK(t, 12, td.N(12))185 checkOK(t, 11, td.N(12, 1))186 checkOK(t, 13, td.N(12, 1))187 checkError(t, 10, td.N(12, 1),188 expectedError{189 Message: mustBe("values differ"),190 Path: mustBe("DATA"),191 Got: mustBe("10"),192 Expected: mustBe("11 ≤ got ≤ 13"),193 })194 checkError(t, 10, td.N(12, 0),195 expectedError{196 Message: mustBe("values differ"),197 Path: mustBe("DATA"),198 Got: mustBe("10"),199 Expected: mustBe("12 ≤ got ≤ 12"),200 })201 checkOK(t, int8(12), td.N(int8(12)))202 checkOK(t, int8(11), td.N(int8(12), int8(1)))203 checkOK(t, int8(13), td.N(int8(12), int8(1)))204 checkError(t, 10, td.N(int8(12), int8(1)),205 expectedError{206 Message: mustBe("type mismatch"),207 Path: mustBe("DATA"),208 Got: mustBe("int"),209 Expected: mustBe("int8"),210 })211 checkOK(t, int16(12), td.N(int16(12)))212 checkOK(t, int16(11), td.N(int16(12), int16(1)))213 checkOK(t, int16(13), td.N(int16(12), int16(1)))214 checkError(t, 10, td.N(int16(12), int16(1)),215 expectedError{216 Message: mustBe("type mismatch"),217 Path: mustBe("DATA"),218 Got: mustBe("int"),219 Expected: mustBe("int16"),220 })221 checkOK(t, int32(12), td.N(int32(12)))222 checkOK(t, int32(11), td.N(int32(12), int32(1)))223 checkOK(t, int32(13), td.N(int32(12), int32(1)))224 checkError(t, 10, td.N(int32(12), int32(1)),225 expectedError{226 Message: mustBe("type mismatch"),227 Path: mustBe("DATA"),228 Got: mustBe("int"),229 Expected: mustBe("int32"),230 })231 checkOK(t, int64(12), td.N(int64(12)))232 checkOK(t, int64(11), td.N(int64(12), int64(1)))233 checkOK(t, int64(13), td.N(int64(12), int64(1)))234 checkError(t, 10, td.N(int64(12), int64(1)),235 expectedError{236 Message: mustBe("type mismatch"),237 Path: mustBe("DATA"),238 Got: mustBe("int"),239 Expected: mustBe("int64"),240 })241 checkOK(t, int64(math.MaxInt64), td.N(int64(math.MaxInt64), int64(2)))242 checkError(t, int64(0), td.N(int64(math.MaxInt64), int64(2)),243 expectedError{244 Message: mustBe("values differ"),245 Path: mustBe("DATA"),246 Got: mustBe("(int64) 0"),247 Expected: mustBe(fmt.Sprintf("(int64) %v ≤ got ≤ (int64) %v",248 int64(math.MaxInt64)-2, int64(math.MaxInt64))),249 })250 checkOK(t, int64(math.MinInt64), td.N(int64(math.MinInt64), int64(2)))251 checkError(t, int64(0), td.N(int64(math.MinInt64), int64(2)),252 expectedError{253 Message: mustBe("values differ"),254 Path: mustBe("DATA"),255 Got: mustBe("(int64) 0"),256 Expected: mustBe(fmt.Sprintf("(int64) %v ≤ got ≤ (int64) %v",257 int64(math.MinInt64), int64(math.MinInt64)+2)),258 })259 //260 // Float261 checkOK(t, 12.1, td.N(12.1))262 checkOK(t, 11.9, td.N(12.0, 0.1))263 checkOK(t, 12.1, td.N(12.0, 0.1))264 checkError(t, 11.8, td.N(12.0, 0.1),265 expectedError{266 Message: mustBe("values differ"),267 Path: mustBe("DATA"),268 Got: mustBe("11.8"),269 Expected: mustBe("11.9 ≤ got ≤ 12.1"),270 })271 checkOK(t, float32(12.1), td.N(float32(12.1)))272 checkOK(t, float32(11.9), td.N(float32(12), float32(0.1)))273 checkOK(t, float32(12.1), td.N(float32(12), float32(0.1)))274 checkError(t, 11.8, td.N(float32(12), float32(0.1)),275 expectedError{276 Message: mustBe("type mismatch"),277 Path: mustBe("DATA"),278 Got: mustBe("float64"),279 Expected: mustBe("float32"),280 })281 floatTol := 10e304282 checkOK(t, float64(math.MaxFloat64),283 td.N(float64(math.MaxFloat64), floatTol))284 checkError(t, float64(0), td.N(float64(math.MaxFloat64), floatTol),285 expectedError{286 Message: mustBe("values differ"),287 Path: mustBe("DATA"),288 Got: mustBe("0.0"),289 Expected: mustBe(fmt.Sprintf("%v ≤ got ≤ +Inf",290 float64(math.MaxFloat64)-floatTol)),291 })292 checkOK(t, -float64(math.MaxFloat64),293 td.N(-float64(math.MaxFloat64), float64(2)))294 checkError(t, float64(0), td.N(-float64(math.MaxFloat64), floatTol),295 expectedError{296 Message: mustBe("values differ"),297 Path: mustBe("DATA"),298 Got: mustBe("0.0"),299 Expected: mustBe(fmt.Sprintf("-Inf ≤ got ≤ %v",300 -float64(math.MaxFloat64)+floatTol)),301 })302 //303 // Bad usage304 checkError(t, "never tested",305 td.N("test"),306 expectedError{307 Message: mustBe("bad usage of N operator"),308 Path: mustBe("DATA"),309 Summary: mustBe("usage: N({,U}INT{,8,16,32,64}|FLOAT{32,64}[, TOLERANCE]), but received string as 1st parameter"),310 })311 checkError(t, "never tested",312 td.N(10, 1, 2),313 expectedError{314 Message: mustBe("bad usage of N operator"),315 Path: mustBe("DATA"),316 Summary: mustBe("usage: N({,U}INT{,8,16,32,64}|FLOAT{32,64}[, TOLERANCE]), too many parameters"),317 })318 checkError(t, "never tested",319 td.N(10, "test"),320 expectedError{321 Message: mustBe("bad usage of N operator"),322 Path: mustBe("DATA"),323 Summary: mustBe("N(NUM, TOLERANCE): NUM and TOLERANCE must have the same type: int ≠ string"),324 })325 // Erroneous op326 test.EqualStr(t, td.N(10, 1, 2).String(), "N(<ERROR>)")327}328func TestLGt(t *testing.T) {329 checkOK(t, 12, td.Gt(11))330 checkOK(t, 12, td.Gte(12))331 checkOK(t, 12, td.Lt(13))332 checkOK(t, 12, td.Lte(12))333 checkOK(t, uint16(12), td.Gt(uint16(11)))334 checkOK(t, uint16(12), td.Gte(uint16(12)))335 checkOK(t, uint16(12), td.Lt(uint16(13)))336 checkOK(t, uint16(12), td.Lte(uint16(12)))337 checkOK(t, 12.3, td.Gt(12.2))338 checkOK(t, 12.3, td.Gte(12.3))339 checkOK(t, 12.3, td.Lt(12.4))340 checkOK(t, 12.3, td.Lte(12.3))341 checkOK(t, "abc", td.Gt("abb"))342 checkOK(t, "abc", td.Gte("abc"))343 checkOK(t, "abc", td.Lt("abd"))344 checkOK(t, "abc", td.Lte("abc"))345 checkError(t, 12, td.Gt(12),346 expectedError{347 Message: mustBe("values differ"),348 Path: mustBe("DATA"),349 Got: mustBe("12"),350 Expected: mustBe("> 12"),351 })352 checkError(t, 12, td.Lt(12),353 expectedError{354 Message: mustBe("values differ"),355 Path: mustBe("DATA"),356 Got: mustBe("12"),357 Expected: mustBe("< 12"),358 })359 checkError(t, 12, td.Gte(13),360 expectedError{361 Message: mustBe("values differ"),362 Path: mustBe("DATA"),363 Got: mustBe("12"),364 Expected: mustBe("≥ 13"),365 })366 checkError(t, 12, td.Lte(11),367 expectedError{368 Message: mustBe("values differ"),369 Path: mustBe("DATA"),370 Got: mustBe("12"),371 Expected: mustBe("≤ 11"),372 })373 checkError(t, "abc", td.Gt("abc"),374 expectedError{375 Message: mustBe("values differ"),376 Path: mustBe("DATA"),377 Got: mustBe(`"abc"`),378 Expected: mustBe(`> "abc"`),379 })380 checkError(t, "abc", td.Lt("abc"),381 expectedError{382 Message: mustBe("values differ"),383 Path: mustBe("DATA"),384 Got: mustBe(`"abc"`),385 Expected: mustBe(`< "abc"`),386 })387 checkError(t, "abc", td.Gte("abd"),388 expectedError{389 Message: mustBe("values differ"),390 Path: mustBe("DATA"),391 Got: mustBe(`"abc"`),392 Expected: mustBe(`≥ "abd"`),393 })394 checkError(t, "abc", td.Lte("abb"),395 expectedError{396 Message: mustBe("values differ"),397 Path: mustBe("DATA"),398 Got: mustBe(`"abc"`),399 Expected: mustBe(`≤ "abb"`),400 })401 gotDate := time.Date(2018, time.March, 4, 1, 2, 3, 0, time.UTC)402 expectedDate := gotDate403 checkOK(t, gotDate, td.Gte(expectedDate))404 checkOK(t, gotDate, td.Lte(expectedDate))405 checkError(t, gotDate, td.Gt(expectedDate),406 expectedError{407 Message: mustBe("values differ"),408 Path: mustBe("DATA"),409 Got: mustBe("(time.Time) 2018-03-04 01:02:03 +0000 UTC"),410 Expected: mustBe("> (time.Time) 2018-03-04 01:02:03 +0000 UTC"),411 })412 checkError(t, gotDate, td.Lt(expectedDate),413 expectedError{414 Message: mustBe("values differ"),415 Path: mustBe("DATA"),416 Got: mustBe("(time.Time) 2018-03-04 01:02:03 +0000 UTC"),417 Expected: mustBe("< (time.Time) 2018-03-04 01:02:03 +0000 UTC"),418 })419 //420 // Bad usage421 checkError(t, "never tested",422 td.Gt([]byte("test")),423 expectedError{424 Message: mustBe("bad usage of Gt operator"),425 Path: mustBe("DATA"),426 Summary: mustBe("usage: Gt(NUM|STRING|TIME), but received []uint8 (slice) as 1st parameter"),427 })428 checkError(t, "never tested",429 td.Gte([]byte("test")),430 expectedError{431 Message: mustBe("bad usage of Gte operator"),432 Path: mustBe("DATA"),433 Summary: mustBe("usage: Gte(NUM|STRING|TIME), but received []uint8 (slice) as 1st parameter"),434 })435 checkError(t, "never tested",436 td.Lt([]byte("test")),437 expectedError{438 Message: mustBe("bad usage of Lt operator"),439 Path: mustBe("DATA"),440 Summary: mustBe("usage: Lt(NUM|STRING|TIME), but received []uint8 (slice) as 1st parameter"),441 })442 checkError(t, "never tested",443 td.Lte([]byte("test")),444 expectedError{445 Message: mustBe("bad usage of Lte operator"),446 Path: mustBe("DATA"),447 Summary: mustBe("usage: Lte(NUM|STRING|TIME), but received []uint8 (slice) as 1st parameter"),448 })449 // Erroneous op450 test.EqualStr(t, td.Gt([]byte("test")).String(), "Gt(<ERROR>)")451 test.EqualStr(t, td.Gte([]byte("test")).String(), "Gte(<ERROR>)")452 test.EqualStr(t, td.Lt([]byte("test")).String(), "Lt(<ERROR>)")453 test.EqualStr(t, td.Lte([]byte("test")).String(), "Lte(<ERROR>)")454}455func TestBetweenTime(t *testing.T) {456 type MyTime time.Time457 now := time.Now()458 checkOK(t, now, td.Between(now, now))459 checkOK(t, now, td.Between(now.Add(-time.Second), now.Add(time.Second)))460 checkOK(t, now, td.Between(now.Add(time.Second), now.Add(-time.Second)))461 // (TIME, DURATION)462 checkOK(t, now, td.Between(now.Add(-time.Second), 2*time.Second))463 checkOK(t, now, td.Between(now.Add(time.Second), -2*time.Second))464 checkOK(t, MyTime(now),465 td.Between(466 MyTime(now.Add(-time.Second)),467 MyTime(now.Add(time.Second))))468 // (TIME, DURATION)469 checkOK(t, MyTime(now),470 td.Between(471 MyTime(now.Add(-time.Second)),472 2*time.Second))473 checkOK(t, MyTime(now),474 td.Between(475 MyTime(now.Add(time.Second)),476 -2*time.Second))477 // Lax mode478 checkOK(t, MyTime(now),479 td.Lax(td.Between(480 now.Add(time.Second),481 now.Add(-time.Second))))482 checkOK(t, now,483 td.Lax(td.Between(484 MyTime(now.Add(time.Second)),485 MyTime(now.Add(-time.Second)))))486 checkOK(t, MyTime(now),487 td.Lax(td.Between(488 now.Add(-time.Second),489 2*time.Second)))490 checkOK(t, now,491 td.Lax(td.Between(492 MyTime(now.Add(-time.Second)),493 2*time.Second)))494 date := time.Date(2018, time.March, 4, 0, 0, 0, 0, time.UTC)495 checkError(t, date,496 td.Between(date.Add(-2*time.Second), date.Add(-time.Second)),497 expectedError{498 Message: mustBe("values differ"),499 Path: mustBe("DATA"),500 Got: mustBe("(time.Time) 2018-03-04 00:00:00 +0000 UTC"),501 Expected: mustBe("(time.Time) 2018-03-03 23:59:58 +0000 UTC" +502 " ≤ got ≤ " +503 "(time.Time) 2018-03-03 23:59:59 +0000 UTC"),504 })505 checkError(t, date,506 td.Between(date.Add(-2*time.Second), date, td.BoundsInOut),507 expectedError{508 Message: mustBe("values differ"),509 Path: mustBe("DATA"),510 Got: mustBe("(time.Time) 2018-03-04 00:00:00 +0000 UTC"),511 Expected: mustBe("(time.Time) 2018-03-03 23:59:58 +0000 UTC" +512 " ≤ got < " +513 "(time.Time) 2018-03-04 00:00:00 +0000 UTC"),514 })515 checkError(t, date,516 td.Between(date, date.Add(2*time.Second), td.BoundsOutIn),517 expectedError{518 Message: mustBe("values differ"),519 Path: mustBe("DATA"),520 Got: mustBe("(time.Time) 2018-03-04 00:00:00 +0000 UTC"),521 Expected: mustBe("(time.Time) 2018-03-04 00:00:00 +0000 UTC" +522 " < got ≤ " +523 "(time.Time) 2018-03-04 00:00:02 +0000 UTC"),524 })525 checkError(t, "string",526 td.Between(date, date.Add(2*time.Second), td.BoundsOutIn),527 expectedError{528 Message: mustBe("type mismatch"),529 Path: mustBe("DATA"),530 Got: mustBe("string"),531 Expected: mustBe("time.Time"),532 })533 checkError(t, "string",534 td.Between(MyTime(date), MyTime(date.Add(2*time.Second)), td.BoundsOutIn),535 expectedError{536 Message: mustBe("type mismatch"),537 Path: mustBe("DATA"),538 Got: mustBe("string"),539 Expected: mustBe("td_test.MyTime"),540 })541 checkError(t, "never tested",542 td.Between(date, 12), // (Time, Time) or (Time, Duration)543 expectedError{544 Message: mustBe("bad usage of Between operator"),545 Path: mustBe("DATA"),546 Summary: mustBe("Between(FROM, TO): when FROM type is time.Time, TO must have the same type or time.Duration: int ≠ time.Time|time.Duration"),547 })548 checkError(t, "never tested",549 td.Between(MyTime(date), 12), // (MyTime, MyTime) or (MyTime, Duration)550 expectedError{551 Message: mustBe("bad usage of Between operator"),552 Path: mustBe("DATA"),553 Summary: mustBe("Between(FROM, TO): when FROM type is td_test.MyTime, TO must have the same type or time.Duration: int ≠ td_test.MyTime|time.Duration"),554 })555 checkOK(t, now, td.Gt(now.Add(-time.Second)))556 checkOK(t, now, td.Lt(now.Add(time.Second)))557}558type compareType int559func (i compareType) Compare(j compareType) int {560 if i < j {561 return -1562 }563 if i > j {564 return 1565 }566 return 0567}568type lessType int569func (i lessType) Less(j lessType) bool {570 return i < j571}572func TestBetweenCmp(t *testing.T) {573 t.Run("compareType", func(t *testing.T) {574 checkOK(t, compareType(5), td.Between(compareType(4), compareType(6)))575 checkOK(t, compareType(5), td.Between(compareType(6), compareType(4)))576 checkOK(t, compareType(5), td.Between(compareType(5), compareType(6)))577 checkOK(t, compareType(5), td.Between(compareType(4), compareType(5)))578 checkOK(t, compareType(5),579 td.Between(compareType(4), compareType(6), td.BoundsOutOut))580 checkError(t, compareType(5),581 td.Between(compareType(5), compareType(6), td.BoundsOutIn),582 expectedError{583 Message: mustBe("values differ"),584 Path: mustBe("DATA"),585 Got: mustBe("(td_test.compareType) 5"),586 Expected: mustBe("(td_test.compareType) 5 < got ≤ (td_test.compareType) 6"),587 })588 checkError(t, compareType(5),589 td.Between(compareType(4), compareType(5), td.BoundsInOut),590 expectedError{591 Message: mustBe("values differ"),592 Path: mustBe("DATA"),593 Got: mustBe("(td_test.compareType) 5"),594 Expected: mustBe("(td_test.compareType) 4 ≤ got < (td_test.compareType) 5"),595 })596 // Other between forms597 checkOK(t, compareType(5), td.Gt(compareType(4)))598 checkOK(t, compareType(5), td.Gte(compareType(5)))599 checkOK(t, compareType(5), td.Lt(compareType(6)))600 checkOK(t, compareType(5), td.Lte(compareType(5)))601 // BeLax or not BeLax602 for i, op := range []td.TestDeep{603 td.Between(compareType(4), compareType(6)),604 td.Gt(compareType(4)),605 td.Gte(compareType(5)),606 td.Lt(compareType(6)),607 td.Lte(compareType(5)),608 } {609 // Type mismatch if BeLax not enabled610 checkError(t, 5, op,611 expectedError{612 Message: mustBe("type mismatch"),613 Path: mustBe("DATA"),614 Got: mustBe("int"),615 Expected: mustBe("td_test.compareType"),616 },617 "Op #%d", i)618 // BeLax enabled is OK619 checkOK(t, 5, td.Lax(op), "Op #%d", i)620 }621 // In a private field622 type private struct {623 num compareType624 }625 checkOK(t, private{num: 5},626 td.Struct(private{},627 td.StructFields{628 "num": td.Between(compareType(4), compareType(6)),629 }))630 })631 t.Run("lessType", func(t *testing.T) {632 checkOK(t, lessType(5), td.Between(lessType(4), lessType(6)))633 checkOK(t, lessType(5), td.Between(lessType(6), lessType(4)))634 checkOK(t, lessType(5), td.Between(lessType(5), lessType(6)))635 checkOK(t, lessType(5), td.Between(lessType(4), lessType(5)))636 checkOK(t, lessType(5),637 td.Between(lessType(4), lessType(6), td.BoundsOutOut))638 checkError(t, lessType(5),639 td.Between(lessType(5), lessType(6), td.BoundsOutIn),640 expectedError{641 Message: mustBe("values differ"),642 Path: mustBe("DATA"),643 Got: mustBe("(td_test.lessType) 5"),644 Expected: mustBe("(td_test.lessType) 5 < got ≤ (td_test.lessType) 6"),645 })646 checkError(t, lessType(5),647 td.Between(lessType(4), lessType(5), td.BoundsInOut),648 expectedError{649 Message: mustBe("values differ"),650 Path: mustBe("DATA"),651 Got: mustBe("(td_test.lessType) 5"),652 Expected: mustBe("(td_test.lessType) 4 ≤ got < (td_test.lessType) 5"),653 })654 // Other between forms655 checkOK(t, lessType(5), td.Gt(lessType(4)))656 checkOK(t, lessType(5), td.Gte(lessType(5)))657 checkOK(t, lessType(5), td.Lt(lessType(6)))658 checkOK(t, lessType(5), td.Lte(lessType(5)))659 // BeLax or not BeLax660 for i, op := range []td.TestDeep{661 td.Between(lessType(4), lessType(6)),662 td.Gt(lessType(4)),663 td.Gte(lessType(5)),664 td.Lt(lessType(6)),665 td.Lte(lessType(5)),666 } {667 // Type mismatch if BeLax not enabled668 checkError(t, 5, op,669 expectedError{670 Message: mustBe("type mismatch"),671 Path: mustBe("DATA"),672 Got: mustBe("int"),673 Expected: mustBe("td_test.lessType"),674 },675 "Op #%d", i)676 // BeLax enabled is OK677 checkOK(t, 5, td.Lax(op), "Op #%d", i)678 }679 // In a private field680 type private struct {681 num lessType682 }683 checkOK(t, private{num: 5},684 td.Struct(private{},685 td.StructFields{686 "num": td.Between(lessType(4), lessType(6)),687 }))688 })689}690func TestBetweenTypeBehind(t *testing.T) {691 type MyTime time.Time692 for _, typ := range []any{693 10,694 int64(23),695 int32(23),696 time.Time{},697 MyTime{},...

Full Screen

Full Screen

td_array_test.go

Source:td_array_test.go Github

copy

Full Screen

...12func TestArray(t *testing.T) {13 type MyArray [5]int14 //15 // Simple array16 checkOK(t, [5]int{}, td.Array([5]int{}, nil))17 checkOK(t, [5]int{0, 0, 0, 4}, td.Array([5]int{0, 0, 0, 4}, nil))18 checkOK(t, [5]int{1, 0, 3},19 td.Array([5]int{}, td.ArrayEntries{2: 3, 0: 1}))20 checkOK(t, [5]int{1, 2, 3},21 td.Array([5]int{0, 2}, td.ArrayEntries{2: 3, 0: 1}))22 checkOK(t, [5]any{1, 2, nil, 4, nil},23 td.Array([5]any{nil, 2, nil, 4}, td.ArrayEntries{0: 1, 2: nil}))24 zero, one, two := 0, 1, 225 checkOK(t, [5]*int{nil, &zero, &one, &two},26 td.Array(27 [5]*int{}, td.ArrayEntries{1: &zero, 2: &one, 3: &two, 4: nil}))28 gotArray := [...]int{1, 2, 3, 4, 5}29 checkError(t, gotArray, td.Array(MyArray{}, nil),30 expectedError{31 Message: mustBe("type mismatch"),32 Path: mustBe("DATA"),33 Got: mustBe("[5]int"),34 Expected: mustBe("td_test.MyArray"),35 })36 checkError(t, gotArray, td.Array([5]int{1, 2, 3, 4, 6}, nil),37 expectedError{38 Message: mustBe("values differ"),39 Path: mustBe("DATA[4]"),40 Got: mustBe("5"),41 Expected: mustBe("6"),42 })43 checkError(t, gotArray,44 td.Array([5]int{1, 2, 3, 4}, td.ArrayEntries{4: 6}),45 expectedError{46 Message: mustBe("values differ"),47 Path: mustBe("DATA[4]"),48 Got: mustBe("5"),49 Expected: mustBe("6"),50 })51 checkError(t, nil,52 td.Array([1]int{42}, nil),53 expectedError{54 Message: mustBe("values differ"),55 Path: mustBe("DATA"),56 Got: mustBe("nil"),57 Expected: mustContain("Array("),58 })59 //60 // Array type61 checkOK(t, MyArray{}, td.Array(MyArray{}, nil))62 checkOK(t, MyArray{0, 0, 0, 4}, td.Array(MyArray{0, 0, 0, 4}, nil))63 checkOK(t, MyArray{1, 0, 3},64 td.Array(MyArray{}, td.ArrayEntries{2: 3, 0: 1}))65 checkOK(t, MyArray{1, 2, 3},66 td.Array(MyArray{0, 2}, td.ArrayEntries{2: 3, 0: 1}))67 checkOK(t, &MyArray{}, td.Array(&MyArray{}, nil))68 checkOK(t, &MyArray{0, 0, 0, 4}, td.Array(&MyArray{0, 0, 0, 4}, nil))69 checkOK(t, &MyArray{1, 0, 3},70 td.Array(&MyArray{}, td.ArrayEntries{2: 3, 0: 1}))71 checkOK(t, &MyArray{1, 0, 3},72 td.Array((*MyArray)(nil), td.ArrayEntries{2: 3, 0: 1}))73 checkOK(t, &MyArray{1, 2, 3},74 td.Array(&MyArray{0, 2}, td.ArrayEntries{2: 3, 0: 1}))75 gotTypedArray := MyArray{1, 2, 3, 4, 5}76 checkError(t, 123, td.Array(&MyArray{}, td.ArrayEntries{}),77 expectedError{78 Message: mustBe("type mismatch"),79 Path: mustBe("DATA"),80 Got: mustBe("int"),81 Expected: mustBe("*td_test.MyArray"),82 })83 checkError(t, &MyStruct{},84 td.Array(&MyArray{}, td.ArrayEntries{}),85 expectedError{86 Message: mustBe("type mismatch"),87 Path: mustBe("DATA"),88 Got: mustBe("*td_test.MyStruct"),89 Expected: mustBe("*td_test.MyArray"),90 })91 checkError(t, gotTypedArray, td.Array([5]int{}, nil),92 expectedError{93 Message: mustBe("type mismatch"),94 Path: mustBe("DATA"),95 Got: mustBe("td_test.MyArray"),96 Expected: mustBe("[5]int"),97 })98 checkError(t, gotTypedArray, td.Array(MyArray{1, 2, 3, 4, 6}, nil),99 expectedError{100 Message: mustBe("values differ"),101 Path: mustBe("DATA[4]"),102 Got: mustBe("5"),103 Expected: mustBe("6"),104 })105 checkError(t, gotTypedArray,106 td.Array(MyArray{1, 2, 3, 4}, td.ArrayEntries{4: 6}),107 expectedError{108 Message: mustBe("values differ"),109 Path: mustBe("DATA[4]"),110 Got: mustBe("5"),111 Expected: mustBe("6"),112 })113 checkError(t, &gotTypedArray, td.Array([5]int{}, nil),114 expectedError{115 Message: mustBe("type mismatch"),116 Path: mustBe("DATA"),117 Got: mustBe("*td_test.MyArray"),118 Expected: mustBe("[5]int"),119 })120 checkError(t, &gotTypedArray, td.Array(&MyArray{1, 2, 3, 4, 6}, nil),121 expectedError{122 Message: mustBe("values differ"),123 Path: mustBe("DATA[4]"),124 Got: mustBe("5"),125 Expected: mustBe("6"),126 })127 checkError(t, &gotTypedArray,128 td.Array(&MyArray{1, 2, 3, 4}, td.ArrayEntries{4: 6}),129 expectedError{130 Message: mustBe("values differ"),131 Path: mustBe("DATA[4]"),132 Got: mustBe("5"),133 Expected: mustBe("6"),134 })135 // Be lax...136 // Without Lax → error137 checkError(t, MyArray{}, td.Array([5]int{}, nil),138 expectedError{139 Message: mustBe("type mismatch"),140 })141 checkError(t, [5]int{}, td.Array(MyArray{}, nil),142 expectedError{143 Message: mustBe("type mismatch"),144 })145 checkOK(t, MyArray{}, td.Lax(td.Array([5]int{}, nil)))146 checkOK(t, [5]int{}, td.Lax(td.Array(MyArray{}, nil)))147 //148 // Bad usage149 checkError(t, "never tested",150 td.Array("test", nil),151 expectedError{152 Message: mustBe("bad usage of Array operator"),153 Path: mustBe("DATA"),154 Summary: mustBe("usage: Array(ARRAY|&ARRAY, EXPECTED_ENTRIES), but received string as 1st parameter"),155 })156 checkError(t, "never tested",157 td.Array(&MyStruct{}, nil),158 expectedError{159 Message: mustBe("bad usage of Array operator"),160 Path: mustBe("DATA"),161 Summary: mustBe("usage: Array(ARRAY|&ARRAY, EXPECTED_ENTRIES), but received *td_test.MyStruct (ptr) as 1st parameter"),162 })163 checkError(t, "never tested",164 td.Array([]int{}, nil),165 expectedError{166 Message: mustBe("bad usage of Array operator"),167 Path: mustBe("DATA"),168 Summary: mustBe("usage: Array(ARRAY|&ARRAY, EXPECTED_ENTRIES), but received []int (slice) as 1st parameter"),169 })170 checkError(t, "never tested",171 td.Array([1]int{}, td.ArrayEntries{1: 34}),172 expectedError{173 Message: mustBe("bad usage of Array operator"),174 Path: mustBe("DATA"),175 Summary: mustBe("array length is 1, so cannot have #1 expected index"),176 })177 checkError(t, "never tested",178 td.Array([3]int{}, td.ArrayEntries{1: nil}),179 expectedError{180 Message: mustBe("bad usage of Array operator"),181 Path: mustBe("DATA"),182 Summary: mustBe("expected value of #1 cannot be nil as items type is int"),183 })184 checkError(t, "never tested",185 td.Array([3]int{}, td.ArrayEntries{1: "bad"}),186 expectedError{187 Message: mustBe("bad usage of Array operator"),188 Path: mustBe("DATA"),189 Summary: mustBe("type string of #1 expected value differs from array contents (int)"),190 })191 checkError(t, "never tested",192 td.Array([1]int{12}, td.ArrayEntries{0: 21}),193 expectedError{194 Message: mustBe("bad usage of Array operator"),195 Path: mustBe("DATA"),196 Summary: mustBe("non zero #0 entry in model already exists in expectedEntries"),197 })198 //199 // String200 test.EqualStr(t,201 td.Array(MyArray{0, 0, 4}, td.ArrayEntries{1: 3, 0: 2}).String(),202 `Array(td_test.MyArray{203 0: 2204 1: 3205 2: 4206 3: 0207 4: 0208})`)209 test.EqualStr(t,210 td.Array(&MyArray{0, 0, 4}, td.ArrayEntries{1: 3, 0: 2}).String(),211 `Array(*td_test.MyArray{212 0: 2213 1: 3214 2: 4215 3: 0216 4: 0217})`)218 test.EqualStr(t, td.Array([0]int{}, td.ArrayEntries{}).String(),219 `Array([0]int{})`)220 // Erroneous op221 test.EqualStr(t,222 td.Array([3]int{}, td.ArrayEntries{1: "bad"}).String(),223 "Array(<ERROR>)")224}225func TestArrayTypeBehind(t *testing.T) {226 type MyArray [12]int227 equalTypes(t, td.Array([12]int{}, nil), [12]int{})228 equalTypes(t, td.Array(MyArray{}, nil), MyArray{})229 equalTypes(t, td.Array(&MyArray{}, nil), &MyArray{})230 // Erroneous op231 equalTypes(t, td.Array([3]int{}, td.ArrayEntries{1: "bad"}), nil)232}233func TestSlice(t *testing.T) {234 type MySlice []int235 //236 // Simple slice237 checkOK(t, []int{}, td.Slice([]int{}, nil))238 checkOK(t, []int{0, 3}, td.Slice([]int{0, 3}, nil))239 checkOK(t, []int{2, 3},240 td.Slice([]int{}, td.ArrayEntries{1: 3, 0: 2}))241 checkOK(t, []int{2, 3},242 td.Slice(([]int)(nil), td.ArrayEntries{1: 3, 0: 2}))243 checkOK(t, []int{2, 3, 4},244 td.Slice([]int{0, 0, 4}, td.ArrayEntries{1: 3, 0: 2}))245 checkOK(t, []int{2, 3, 4},246 td.Slice([]int{2, 3}, td.ArrayEntries{2: 4}))247 checkOK(t, []int{2, 3, 4, 0, 6},248 td.Slice([]int{2, 3}, td.ArrayEntries{2: 4, 4: 6}))249 gotSlice := []int{2, 3, 4}250 checkError(t, gotSlice, td.Slice(MySlice{}, nil),251 expectedError{252 Message: mustBe("type mismatch"),253 Path: mustBe("DATA"),254 Got: mustBe("[]int"),255 Expected: mustBe("td_test.MySlice"),256 })257 checkError(t, gotSlice, td.Slice([]int{2, 3, 5}, nil),258 expectedError{259 Message: mustBe("values differ"),260 Path: mustBe("DATA[2]"),261 Got: mustBe("4"),262 Expected: mustBe("5"),263 })264 checkError(t, gotSlice,265 td.Slice([]int{2, 3}, td.ArrayEntries{2: 5}),266 expectedError{267 Message: mustBe("values differ"),268 Path: mustBe("DATA[2]"),269 Got: mustBe("4"),270 Expected: mustBe("5"),271 })272 checkError(t, nil,273 td.Slice([]int{2, 3}, nil),274 expectedError{275 Message: mustBe("values differ"),276 Path: mustBe("DATA"),277 Got: mustBe("nil"),278 Expected: mustContain("Slice("),279 })280 //281 // Slice type282 checkOK(t, MySlice{}, td.Slice(MySlice{}, nil))283 checkOK(t, MySlice{0, 3}, td.Slice(MySlice{0, 3}, nil))284 checkOK(t, MySlice{2, 3},285 td.Slice(MySlice{}, td.ArrayEntries{1: 3, 0: 2}))286 checkOK(t, MySlice{2, 3},287 td.Slice((MySlice)(nil), td.ArrayEntries{1: 3, 0: 2}))288 checkOK(t, MySlice{2, 3, 4},289 td.Slice(MySlice{0, 0, 4}, td.ArrayEntries{1: 3, 0: 2}))290 checkOK(t, MySlice{2, 3, 4, 0, 6},291 td.Slice(MySlice{2, 3}, td.ArrayEntries{2: 4, 4: 6}))292 checkOK(t, &MySlice{}, td.Slice(&MySlice{}, nil))293 checkOK(t, &MySlice{0, 3}, td.Slice(&MySlice{0, 3}, nil))294 checkOK(t, &MySlice{2, 3},295 td.Slice(&MySlice{}, td.ArrayEntries{1: 3, 0: 2}))296 checkOK(t, &MySlice{2, 3},297 td.Slice((*MySlice)(nil), td.ArrayEntries{1: 3, 0: 2}))298 checkOK(t, &MySlice{2, 3, 4},299 td.Slice(&MySlice{0, 0, 4}, td.ArrayEntries{1: 3, 0: 2}))300 checkOK(t, &MySlice{2, 3, 4, 0, 6},301 td.Slice(&MySlice{2, 3}, td.ArrayEntries{2: 4, 4: 6}))302 gotTypedSlice := MySlice{2, 3, 4}303 checkError(t, 123, td.Slice(&MySlice{}, td.ArrayEntries{}),304 expectedError{305 Message: mustBe("type mismatch"),306 Path: mustBe("DATA"),307 Got: mustBe("int"),308 Expected: mustBe("*td_test.MySlice"),309 })310 checkError(t, &MyStruct{},311 td.Slice(&MySlice{}, td.ArrayEntries{}),312 expectedError{313 Message: mustBe("type mismatch"),314 Path: mustBe("DATA"),315 Got: mustBe("*td_test.MyStruct"),316 Expected: mustBe("*td_test.MySlice"),317 })318 checkError(t, gotTypedSlice, td.Slice([]int{}, nil),319 expectedError{320 Message: mustBe("type mismatch"),321 Path: mustBe("DATA"),322 Got: mustBe("td_test.MySlice"),323 Expected: mustBe("[]int"),324 })325 checkError(t, gotTypedSlice, td.Slice(MySlice{2, 3, 5}, nil),326 expectedError{327 Message: mustBe("values differ"),328 Path: mustBe("DATA[2]"),329 Got: mustBe("4"),330 Expected: mustBe("5"),331 })332 checkError(t, gotTypedSlice,333 td.Slice(MySlice{2, 3}, td.ArrayEntries{2: 5}),334 expectedError{335 Message: mustBe("values differ"),336 Path: mustBe("DATA[2]"),337 Got: mustBe("4"),338 Expected: mustBe("5"),339 })340 checkError(t, gotTypedSlice,341 td.Slice(MySlice{2, 3, 4}, td.ArrayEntries{3: 5}),342 expectedError{343 Message: mustBe("expected value out of range"),344 Path: mustBe("DATA[3]"),345 Got: mustBe("<non-existent value>"),346 Expected: mustBe("5"),347 })348 checkError(t, gotTypedSlice, td.Slice(MySlice{2, 3}, nil),349 expectedError{350 Message: mustBe("got value out of range"),351 Path: mustBe("DATA[2]"),352 Got: mustBe("4"),353 Expected: mustBe("<non-existent value>"),354 })355 checkError(t, &gotTypedSlice, td.Slice([]int{}, nil),356 expectedError{357 Message: mustBe("type mismatch"),358 Path: mustBe("DATA"),359 Got: mustBe("*td_test.MySlice"),360 Expected: mustBe("[]int"),361 })362 checkError(t, &gotTypedSlice, td.Slice(&MySlice{2, 3, 5}, nil),363 expectedError{364 Message: mustBe("values differ"),365 Path: mustBe("DATA[2]"),366 Got: mustBe("4"),367 Expected: mustBe("5"),368 })369 checkError(t, &gotTypedSlice,370 td.Slice(&MySlice{2, 3}, td.ArrayEntries{2: 5}),371 expectedError{372 Message: mustBe("values differ"),373 Path: mustBe("DATA[2]"),374 Got: mustBe("4"),375 Expected: mustBe("5"),376 })377 checkError(t, &gotTypedSlice, td.Slice(&MySlice{2, 3}, nil),378 expectedError{379 Message: mustBe("got value out of range"),380 Path: mustBe("DATA[2]"),381 Got: mustBe("4"),382 Expected: mustBe("<non-existent value>"),383 })384 //385 // nil cases386 var (387 gotNilSlice []int388 gotNilTypedSlice MySlice389 )390 checkOK(t, gotNilSlice, td.Slice([]int{}, nil))391 checkOK(t, gotNilTypedSlice, td.Slice(MySlice{}, nil))392 checkOK(t, &gotNilTypedSlice, td.Slice(&MySlice{}, nil))393 // Be lax...394 // Without Lax → error395 checkError(t, MySlice{}, td.Slice([]int{}, nil),396 expectedError{397 Message: mustBe("type mismatch"),398 })399 checkError(t, []int{}, td.Slice(MySlice{}, nil),400 expectedError{401 Message: mustBe("type mismatch"),402 })403 checkOK(t, MySlice{}, td.Lax(td.Slice([]int{}, nil)))404 checkOK(t, []int{}, td.Lax(td.Slice(MySlice{}, nil)))405 //406 // Bad usage407 checkError(t, "never tested",408 td.Slice("test", nil),409 expectedError{410 Message: mustBe("bad usage of Slice operator"),411 Path: mustBe("DATA"),412 Summary: mustBe("usage: Slice(SLICE|&SLICE, EXPECTED_ENTRIES), but received string as 1st parameter"),413 })414 checkError(t, "never tested",415 td.Slice(&MyStruct{}, nil),416 expectedError{417 Message: mustBe("bad usage of Slice operator"),418 Path: mustBe("DATA"),419 Summary: mustBe("usage: Slice(SLICE|&SLICE, EXPECTED_ENTRIES), but received *td_test.MyStruct (ptr) as 1st parameter"),420 })421 checkError(t, "never tested",422 td.Slice([0]int{}, nil),423 expectedError{424 Message: mustBe("bad usage of Slice operator"),425 Path: mustBe("DATA"),426 Summary: mustBe("usage: Slice(SLICE|&SLICE, EXPECTED_ENTRIES), but received [0]int (array) as 1st parameter"),427 })428 checkError(t, "never tested",429 td.Slice([]int{}, td.ArrayEntries{1: "bad"}),430 expectedError{431 Message: mustBe("bad usage of Slice operator"),432 Path: mustBe("DATA"),433 Summary: mustBe("type string of #1 expected value differs from slice contents (int)"),434 })435 checkError(t, "never tested",436 td.Slice([]int{12}, td.ArrayEntries{0: 21}),437 expectedError{438 Message: mustBe("bad usage of Slice operator"),439 Path: mustBe("DATA"),440 Summary: mustBe("non zero #0 entry in model already exists in expectedEntries"),441 })442 //443 // String444 test.EqualStr(t,445 td.Slice(MySlice{0, 0, 4}, td.ArrayEntries{1: 3, 0: 2}).String(),446 `Slice(td_test.MySlice{447 0: 2448 1: 3449 2: 4450})`)451 test.EqualStr(t,452 td.Slice(&MySlice{0, 0, 4}, td.ArrayEntries{1: 3, 0: 2}).String(),453 `Slice(*td_test.MySlice{454 0: 2455 1: 3456 2: 4457})`)458 test.EqualStr(t, td.Slice(&MySlice{}, td.ArrayEntries{}).String(),459 `Slice(*td_test.MySlice{})`)460 // Erroneous op461 test.EqualStr(t,462 td.Slice([]int{}, td.ArrayEntries{1: "bad"}).String(),463 "Slice(<ERROR>)")464}465func TestSliceTypeBehind(t *testing.T) {466 type MySlice []int467 equalTypes(t, td.Slice([]int{}, nil), []int{})468 equalTypes(t, td.Slice(MySlice{}, nil), MySlice{})469 equalTypes(t, td.Slice(&MySlice{}, nil), &MySlice{})470 // Erroneous op471 equalTypes(t, td.Slice([]int{}, td.ArrayEntries{1: "bad"}), nil)472}473func TestSuperSliceOf(t *testing.T) {474 t.Run("interface array", func(t *testing.T) {475 got := [5]any{"foo", "bar", nil, 666, 777}476 checkOK(t, got,477 td.SuperSliceOf([5]any{1: "bar"}, td.ArrayEntries{2: td.Nil()}))478 checkOK(t, got,479 td.SuperSliceOf([5]any{1: "bar"}, td.ArrayEntries{2: nil}))480 checkOK(t, got,481 td.SuperSliceOf([5]any{1: "bar"}, td.ArrayEntries{3: 666}))482 checkOK(t, got,483 td.SuperSliceOf([5]any{1: "bar"}, td.ArrayEntries{3: td.Between(665, 667)}))484 checkOK(t, &got,485 td.SuperSliceOf(&[5]any{1: "bar"}, td.ArrayEntries{3: td.Between(665, 667)}))486 checkError(t, got,487 td.SuperSliceOf([5]any{1: "foo"}, td.ArrayEntries{2: td.Nil()}),488 expectedError{489 Message: mustBe("values differ"),490 Path: mustBe("DATA[1]"),491 Got: mustBe(`"bar"`),492 Expected: mustBe(`"foo"`),493 })494 checkError(t, got,495 td.SuperSliceOf([5]any{1: 666}, td.ArrayEntries{2: td.Nil()}),496 expectedError{497 Message: mustBe("type mismatch"),498 Path: mustBe("DATA[1]"),499 Got: mustBe("string"),500 Expected: mustBe("int"),501 })502 checkError(t, &got,503 td.SuperSliceOf([5]any{1: 666}, td.ArrayEntries{2: td.Nil()}),504 expectedError{505 Message: mustBe("type mismatch"),506 Path: mustBe("DATA"),507 Got: mustBe("*[5]interface {}"),508 Expected: mustBe("[5]interface {}"),509 })510 checkError(t, got,511 td.SuperSliceOf(&[5]any{1: 666}, td.ArrayEntries{2: td.Nil()}),512 expectedError{513 Message: mustBe("type mismatch"),514 Path: mustBe("DATA"),515 Got: mustBe("[5]interface {}"),516 Expected: mustBe("*[5]interface {}"),517 })518 })519 t.Run("ints array", func(t *testing.T) {520 type MyArray [5]int521 checkOK(t, MyArray{}, td.SuperSliceOf(MyArray{}, nil))522 got := MyArray{3: 4}523 checkOK(t, got, td.SuperSliceOf(MyArray{}, nil))524 checkOK(t, got, td.SuperSliceOf(MyArray{3: 4}, nil))525 checkOK(t, got, td.SuperSliceOf(MyArray{}, td.ArrayEntries{3: 4}))526 checkError(t, got,527 td.SuperSliceOf(MyArray{}, td.ArrayEntries{1: 666}),528 expectedError{529 Message: mustBe("values differ"),530 Path: mustBe("DATA[1]"),531 Got: mustBe(`0`),532 Expected: mustBe(`666`),533 })534 // Be lax...535 // Without Lax → error536 checkError(t, got,537 td.SuperSliceOf([5]int{}, td.ArrayEntries{3: 4}),538 expectedError{539 Message: mustBe("type mismatch"),540 Path: mustBe("DATA"),541 Got: mustBe(`td_test.MyArray`),542 Expected: mustBe(`[5]int`),543 })544 checkOK(t, got, td.Lax(td.SuperSliceOf([5]int{}, td.ArrayEntries{3: 4})))545 checkError(t, [5]int{3: 4},546 td.SuperSliceOf(MyArray{}, td.ArrayEntries{3: 4}),547 expectedError{548 Message: mustBe("type mismatch"),549 Path: mustBe("DATA"),550 Got: mustBe(`[5]int`),551 Expected: mustBe(`td_test.MyArray`),552 })553 checkOK(t, [5]int{3: 4},554 td.Lax(td.SuperSliceOf(MyArray{}, td.ArrayEntries{3: 4})))555 checkError(t, "never tested",556 td.SuperSliceOf(MyArray{}, td.ArrayEntries{8: 34}),557 expectedError{558 Message: mustBe("bad usage of SuperSliceOf operator"),559 Path: mustBe("DATA"),560 Summary: mustBe("array length is 5, so cannot have #8 expected index"),561 })562 })563 t.Run("ints slice", func(t *testing.T) {564 type MySlice []int565 checkOK(t, MySlice{}, td.SuperSliceOf(MySlice{}, nil))566 checkOK(t, MySlice(nil), td.SuperSliceOf(MySlice{}, nil))567 got := MySlice{3: 4}568 checkOK(t, got, td.SuperSliceOf(MySlice{}, td.ArrayEntries{3: td.N(5, 1)}))569 checkOK(t, got, td.SuperSliceOf(MySlice{3: 4}, td.ArrayEntries{2: 0}))570 checkError(t, got,571 td.SuperSliceOf(MySlice{}, td.ArrayEntries{1: 666}),572 expectedError{573 Message: mustBe("values differ"),574 Path: mustBe("DATA[1]"),575 Got: mustBe(`0`),576 Expected: mustBe(`666`),577 })578 checkError(t, got,579 td.SuperSliceOf(MySlice{}, td.ArrayEntries{3: 0}),580 expectedError{581 Message: mustBe("values differ"),582 Path: mustBe("DATA[3]"),583 Got: mustBe(`4`),584 Expected: mustBe(`0`),585 })586 checkError(t, got,587 td.SuperSliceOf(MySlice{}, td.ArrayEntries{28: 666}),588 expectedError{589 Message: mustBe("expected value out of range"),590 Path: mustBe("DATA[28]"),591 Got: mustBe(`<non-existent value>`),592 Expected: mustBe(`666`),593 })594 checkError(t, got,595 td.SuperSliceOf(MySlice{28: 666}, nil),596 expectedError{597 Message: mustBe("expected value out of range"),598 Path: mustBe("DATA[28]"),599 Got: mustBe(`<non-existent value>`),600 Expected: mustBe(`666`),601 })602 // Be lax...603 // Without Lax → error604 checkError(t, got,605 td.SuperSliceOf([]int{}, td.ArrayEntries{3: 4}),606 expectedError{607 Message: mustBe("type mismatch"),608 Path: mustBe("DATA"),609 Got: mustBe(`td_test.MySlice`),610 Expected: mustBe(`[]int`),611 })612 checkOK(t, got, td.Lax(td.SuperSliceOf([]int{}, td.ArrayEntries{3: 4})))613 checkError(t, []int{3: 4},614 td.SuperSliceOf(MySlice{}, td.ArrayEntries{3: 4}),615 expectedError{616 Message: mustBe("type mismatch"),617 Path: mustBe("DATA"),618 Got: mustBe(`[]int`),619 Expected: mustBe(`td_test.MySlice`),620 })621 checkOK(t, []int{3: 4},622 td.Lax(td.SuperSliceOf(MySlice{}, td.ArrayEntries{3: 4})))623 })624 //625 // Bad usage626 checkError(t, "never tested",627 td.SuperSliceOf("test", nil),628 expectedError{629 Message: mustBe("bad usage of SuperSliceOf operator"),630 Path: mustBe("DATA"),631 Summary: mustBe("usage: SuperSliceOf(ARRAY|&ARRAY|SLICE|&SLICE, EXPECTED_ENTRIES), but received string as 1st parameter"),632 })633 checkError(t, "never tested",634 td.SuperSliceOf(&MyStruct{}, nil),635 expectedError{...

Full Screen

Full Screen

td_isa_test.go

Source:td_isa_test.go Github

copy

Full Screen

...20 ValStr: "foobar",21 },22 ValInt: 123,23 }24 checkOK(t, &gotStruct, td.Isa(&MyStruct{}))25 checkOK(t, (*MyStruct)(nil), td.Isa(&MyStruct{}))26 checkOK(t, (*MyStruct)(nil), td.Isa((*MyStruct)(nil)))27 checkOK(t, gotStruct, td.Isa(MyStruct{}))28 checkOK(t, bytes.NewBufferString("foobar"),29 td.Isa((*fmt.Stringer)(nil)),30 "checks bytes.NewBufferString() implements fmt.Stringer")31 // does bytes.NewBufferString("foobar") implements fmt.Stringer?32 checkOK(t, bytes.NewBufferString("foobar"), td.Isa((*fmt.Stringer)(nil)))33 checkError(t, &gotStruct, td.Isa(&MyStructBase{}),34 expectedError{35 Message: mustBe("type mismatch"),36 Path: mustBe("DATA"),37 Got: mustContain("*td_test.MyStruct"),38 Expected: mustContain("*td_test.MyStructBase"),39 })40 checkError(t, (*MyStruct)(nil), td.Isa(&MyStructBase{}),41 expectedError{42 Message: mustBe("type mismatch"),43 Path: mustBe("DATA"),44 Got: mustContain("*td_test.MyStruct"),45 Expected: mustContain("*td_test.MyStructBase"),46 })47 checkError(t, gotStruct, td.Isa(&MyStruct{}),48 expectedError{49 Message: mustBe("type mismatch"),50 Path: mustBe("DATA"),51 Got: mustContain("td_test.MyStruct"),52 Expected: mustContain("*td_test.MyStruct"),53 })54 checkError(t, &gotStruct, td.Isa(MyStruct{}),55 expectedError{56 Message: mustBe("type mismatch"),57 Path: mustBe("DATA"),58 Got: mustContain("*td_test.MyStruct"),59 Expected: mustContain("td_test.MyStruct"),60 })61 gotSlice := []int{1, 2, 3}62 checkOK(t, gotSlice, td.Isa([]int{}))63 checkOK(t, &gotSlice, td.Isa(((*[]int)(nil))))64 checkError(t, &gotSlice, td.Isa([]int{}),65 expectedError{66 Message: mustBe("type mismatch"),67 Path: mustBe("DATA"),68 Got: mustContain("*[]int"),69 Expected: mustContain("[]int"),70 })71 checkError(t, gotSlice, td.Isa((*[]int)(nil)),72 expectedError{73 Message: mustBe("type mismatch"),74 Path: mustBe("DATA"),75 Got: mustContain("[]int"),76 Expected: mustContain("*[]int"),77 })...

Full Screen

Full Screen

checkOK

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 fmt.Println("Hello, playground")4 td_test.CheckOK()5}6import (7func main() {8 fmt.Println("Hello, playground")9 td_test.CheckOK()10}11import (12func main() {13 fmt.Println("Hello, playground")14 td_test.CheckOK()15}16import (17func main() {18 fmt.Println("Hello, playground")19 td_test.CheckOK()20}21import (22func main() {23 fmt.Println("Hello, playground")24 td_test.CheckOK()25}26import (27func main() {28 fmt.Println("Hello, playground")29 td_test.CheckOK()30}31import (32func main() {33 fmt.Println("Hello, playground")34 td_test.CheckOK()35}36import (37func main() {38 fmt.Println("Hello, playground")39 td_test.CheckOK()40}41import (42func main() {43 fmt.Println("Hello, playground")44 td_test.CheckOK()45}46import (47func main() {48 fmt.Println("Hello, playground")49 td_test.CheckOK()50}

Full Screen

Full Screen

checkOK

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 fmt.Println("Hello, playground")4 td := test.Td_test{}5 td.CheckOK()6}7import (8func main() {9 fmt.Println("Hello, playground")10 td := test.Td_test{}11 td.CheckOK()12}13import (14func main() {15 fmt.Println("Hello, playground")16 td := test.Td_test{}17 td.CheckOK()18}19import (20func main() {21 fmt.Println("Hello, playground")22 td := test.Td_test{}23 td.CheckOK()24}25import (26func main() {27 fmt.Println("Hello, playground")28 td := test.Td_test{}29 td.CheckOK()30}31import (32func main() {33 fmt.Println("Hello, playground")34 td := test.Td_test{}35 td.CheckOK()36}37import (38func main() {39 fmt.Println("Hello, playground")40 td := test.Td_test{}41 td.CheckOK()42}43import (44func main() {45 fmt.Println("Hello, playground")46 td := test.Td_test{}47 td.CheckOK()48}49import (50func main() {51 fmt.Println("Hello, playground")52 td := test.Td_test{}53 td.CheckOK()54}

Full Screen

Full Screen

checkOK

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 fmt.Println("Hello, world.")4 fmt.Println(td_test.CheckOK())5}6import (7func main() {8 fmt.Println("Hello, world.")9 fmt.Println(td_test.CheckOK())10}11import (12func main() {13 fmt.Println("Hello, world.")14 fmt.Println(td_test.CheckOK())15}16import (17func main() {18 fmt.Println("Hello, world.")19 fmt.Println(td_test.CheckOK())20}21import (22func main() {23 fmt.Println("Hello, world.")24 fmt.Println(td_test.CheckOK())25}26import (27func main() {28 fmt.Println("Hello, world.")29 fmt.Println(td_test.CheckOK())30}31import (32func main() {33 fmt.Println("Hello, world.")34 fmt.Println(td_test.CheckOK())35}36import (37func main() {38 fmt.Println("Hello, world.")39 fmt.Println(td_test.CheckOK())40}41import (42func main() {43 fmt.Println("Hello, world.")44 fmt.Println(td_test.CheckOK())45}

Full Screen

Full Screen

checkOK

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 fmt.Println("Hello, playground")4 var t = td_test{}5 t.checkOK()6}7import "fmt"8type td_test struct {9}10func (t td_test) checkOK() {11 fmt.Println("checkOK")12}13I am trying to use the checkOK() method of td_test class in the 2.go file. But I am getting the following error:14import (15func main() {16 fmt.Println("Hello, playground")17 var t = testdata.td_test{}18 t.checkOK()19}

Full Screen

Full Screen

checkOK

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 td.Set(10)4 fmt.Println(td.CheckOK())5}6import (7func main() {8 td.Set(10)9 fmt.Println(td.CheckOK())10}

Full Screen

Full Screen

checkOK

Using AI Code Generation

copy

Full Screen

1import "fmt"2import "testing"3func main() {4 fmt.Println("Hello, 世界")5 myTest := new(td_test)6 myTest.checkOK()7}8./2.go:15: cannot use new(td_test) (type *td_test) as type testing.TB in argument to myTest.checkOK:9 *td_test does not implement testing.TB (wrong type for Errorf method)10 have Errorf(string, ...interface {})11 want Errorf(string, ...interface {}) error12cannot use new(td_test) (type *td_test) as type testing.TB in argument to myTest.checkOK: *td_test does not implement testing.TB (missing Errorf method)13func (t *td_test) checkOK() {14 t.t.Helper()15 t.t.Errorf("I am an error")16}17func main() {18 myTest := new(td_test)19 myTest.checkOK()20}21func (t *td_test) checkOK() {22 t.t.Helper()23 t.t.Errorf("I am an error")

Full Screen

Full Screen

checkOK

Using AI Code Generation

copy

Full Screen

1import "fmt"2func main() {3 t.checkOK()4}5import "fmt"6import "test"7func main() {8 t.checkOK()9}10./3.go:8: t.checkOK undefined (type test.td_test has no field or method checkOK)11import "fmt"12import "test"13func main() {14 t.CheckOK()15}16type td_test struct {17}18func (t td_test) CheckOK() {19 fmt.Println("ok")20}21import "fmt"22import "test"23func main() {24 t.CheckOK()25}

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Go-testdeep automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Most used method in

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful