Best Go-testdeep code snippet using td.Lax
td_between_test.go
Source:td_between_test.go
...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.Time...
td_array_test.go
Source:td_array_test.go
...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{636 Message: mustBe("bad usage of SuperSliceOf operator"),...
td_lax_test.go
Source:td_lax_test.go
...8 "testing"9 "github.com/maxatome/go-testdeep/internal/test"10 "github.com/maxatome/go-testdeep/td"11)12func TestLax(t *testing.T) {13 checkOK(t, int64(1234), td.Lax(1234))14 type MyInt int3215 checkOK(t, int64(123), td.Lax(MyInt(123)))16 checkOK(t, MyInt(123), td.Lax(int64(123)))17 type gotStruct struct {18 name string19 age int20 }21 type expectedStruct struct {22 name string23 age int24 }25 checkOK(t,26 gotStruct{27 name: "bob",28 age: 42,29 },30 td.Lax(expectedStruct{31 name: "bob",32 age: 42,33 }))34 checkOK(t,35 &gotStruct{36 name: "bob",37 age: 42,38 },39 td.Lax(&expectedStruct{40 name: "bob",41 age: 42,42 }))43 checkError(t, int64(123), td.Between(120, 125),44 expectedError{45 Message: mustBe("type mismatch"),46 })47 checkOK(t, int64(123), td.Lax(td.Between(120, 125)))48 // nil cases49 checkOK(t, nil, td.Lax(nil))50 checkOK(t, (*gotStruct)(nil), td.Lax((*expectedStruct)(nil)))51 checkOK(t, (*gotStruct)(nil), td.Lax(nil))52 checkOK(t, (chan int)(nil), td.Lax(nil))53 checkOK(t, (func())(nil), td.Lax(nil))54 checkOK(t, (map[int]int)(nil), td.Lax(nil))55 checkOK(t, ([]int)(nil), td.Lax(nil))56 //57 // String58 test.EqualStr(t, td.Lax(6).String(), "Lax(6)")59}60func TestLaxTypeBehind(t *testing.T) {61 equalTypes(t, td.Lax(nil), nil)62 type MyBool bool63 equalTypes(t, td.Lax(MyBool(false)), false)64 equalTypes(t, td.Lax(0), int64(0))65 equalTypes(t, td.Lax(uint8(0)), uint64(0))66 equalTypes(t, td.Lax(float32(0)), float64(0))67 equalTypes(t, td.Lax(complex64(complex(1, 1))), complex128(complex(1, 1)))68 type MyString string69 equalTypes(t, td.Lax(MyString("")), "")70 type MyBytes []byte71 equalTypes(t, td.Lax([]byte{}), []byte{})72 equalTypes(t, td.Lax(MyBytes{}), MyBytes{})73 // Another TestDeep operator delegation74 equalTypes(t, td.Lax(td.Struct(MyStruct{}, nil)), MyStruct{})75 equalTypes(t, td.Lax(td.Any(1, 1.2)), nil)76}...
Lax
Using AI Code Generation
1import (2func main() {3 http.HandleFunc("/", index)4 http.ListenAndServe(":8080", nil)5}6func index(w http.ResponseWriter, r *http.Request) {7 tpl, err := template.ParseFiles("index.gohtml")8 if err != nil {9 http.Error(w, err.Error(), http.StatusInternalServerError)10 }11 tpl.ExecuteTemplate(w, "index.gohtml", nil)12}13import (14func main() {15 http.HandleFunc("/", index)16 http.ListenAndServe(":8080", nil)17}18func index(w http.ResponseWriter, r *http.Request) {19 tpl, err := template.ParseFiles("index.gohtml")20 if err != nil {21 http.Error(w, err.Error(), http.StatusInternalServerError)22 }23 tpl.ExecuteTemplate(w, "index.gohtml", nil)24}
Lax
Using AI Code Generation
1import (2func main() {3 http.HandleFunc("/", index)4 http.HandleFunc("/process", process)5 http.ListenAndServe(":8080", nil)6}7func index(w http.ResponseWriter, req *http.Request) {8 tpl, err := template.ParseFiles("index.gohtml")9 if err != nil {10 log.Fatalln(err)11 }12 tpl.ExecuteTemplate(w, "index.gohtml", nil)13}14func process(w http.ResponseWriter, req *http.Request) {15 err := req.ParseForm()16 if err != nil {17 log.Fatalln(err)18 }19 fmt.Println(req.Form)20 fmt.Println(req.Form["q"])21 fmt.Println(req.FormValue("q"))22}
Lax
Using AI Code Generation
1import (2func main() {3 if err != nil {4 fmt.Println("error in loading url")5 }6 doc.Find("td").Each(func(i int, s *goquery.Selection) {7 band := s.Text()8 fmt.Printf("Review %d: %s9 })10}
Lax
Using AI Code Generation
1import (2func main() {3 r := httprouter.New()4 r.GET("/hello/:name", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {5 w.Write([]byte("hello, " + ps.ByName("name")))6 })7 http.ListenAndServe(":8080", r)8}9import (10func main() {11 r := httprouter.New()12 r.GET("/hello/:name", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {13 w.Write([]byte("hello, " + ps.ByName("name")))14 })15 http.ListenAndServe(":8080", r)16}17import (18func main() {19 r := httprouter.New()20 r.GET("/hello/:name", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {21 w.Write([]byte("hello, " + ps.ByName("name")))22 })23 http.ListenAndServe(":8080", r)24}25import (26func main() {27 r := httprouter.New()28 r.GET("/hello/:name", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {29 w.Write([]byte("hello, " + ps.ByName("name")))30 })31 http.ListenAndServe(":8080", r)32}33import (34func main() {35 r := httprouter.New()36 r.GET("/hello/:name", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
Lax
Using AI Code Generation
1import (2type Person struct {3}4func main() {5 tmpl, err := template.ParseFiles("1.gohtml")6 if err != nil {7 fmt.Println(err)8 }9 p1 := Person{10 }11 err = tmpl.Execute(os.Stdout, p1)12 if err != nil {13 fmt.Println(err)14 }15}
Lax
Using AI Code Generation
1import (2func main() {3 xlFile, err := xlsx.OpenFile("test.xlsx")4 if err != nil {5 fmt.Println(err)6 }7 cell := sheet.Cell(1, 1)8 err = xlFile.Save("test.xlsx")9 if err != nil {10 fmt.Println(err)11 }12}13import (14func main() {15 xlFile, err := xlsx.OpenFile("test.xlsx")16 if err != nil {17 fmt.Println(err)18 }19 cell := sheet.Cell(1, 1)20 err = xlFile.Save("test.xlsx")21 if err != nil {22 fmt.Println(err)23 }24}25import (26func main() {27 xlFile, err := xlsx.OpenFile("test.xlsx")28 if err != nil {29 fmt.Println(err)30 }31 cell := sheet.Cell(1, 1)32 err = xlFile.Save("test.xlsx")33 if err != nil {34 fmt.Println(err)35 }36}
Lax
Using AI Code Generation
1import (2func main() {3 if len(os.Args) != 2 {4 fmt.Println("Usage: 2.go <url>")5 os.Exit(1)6 }7 form := url.Values{}8 form.Add("file", "file.txt")9 form.Add("file", "file2.txt")10 req, err := http.NewRequest("POST", url, strings.NewReader(form.Encode()))11 if err != nil {12 panic(err)13 }14 req.Header.Set("Content-Type", "application/x-www-form-urlencoded")15 client := &http.Client{}16 resp, err := client.Do(req)17 if err != nil {18 panic(err)19 }20 defer resp.Body.Close()21 if resp.StatusCode != http.StatusOK {22 fmt.Printf("bad status: %s", resp.Status)23 }24 body, err := ioutil.ReadAll(resp.Body)25 if err != nil {26 panic(err)27 }28 fmt.Printf("%s", body)29}30import (31func main() {32 if len(os.Args) != 2 {33 fmt.Println("Usage: 2.go <url>")34 os.Exit(1)35 }36 form := url.Values{}37 form.Add("file", "file.txt")38 form.Add("file", "file2.txt")39 req, err := http.NewRequest("POST", url, strings.NewReader(form.Encode()))40 if err != nil {41 panic(err
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.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!