How to use TestLast method of td_test Package

Best Go-testdeep code snippet using td_test.TestLast

td_grep_test.go

Source:td_grep_test.go Github

copy

Full Screen

...465 equalTypes(t, td.First(td.Gt("x"), "x"), []string{})466 // Erroneous op467 equalTypes(t, td.First(42, 33), nil)468}469func TestLast(t *testing.T) {470 t.Run("basic", func(t *testing.T) {471 got := [...]int{-3, -2, -1, 0, 1, 2, 3}472 sgot := got[:]473 testCases := []struct {474 name string475 got any476 }{477 {"slice", sgot},478 {"array", got},479 {"*slice", &sgot},480 {"*array", &got},481 }482 for _, tc := range testCases {483 t.Run(tc.name, func(t *testing.T) {484 checkOK(t, tc.got, td.Last(td.Lt(0), -1))485 checkOK(t, tc.got, td.Last(td.Not(td.Between(1, 3)), 0))486 checkOK(t, tc.got, td.Last(487 func(x int) bool { return (x & 1) == 0 },488 2))489 checkOK(t, tc.got, td.Last(490 func(x int64) bool { return (x & 1) != 0 },491 3),492 "int64 filter vs int items")493 checkOK(t, tc.got, td.Last(494 func(x any) bool { return (x.(int) & 1) == 0 },495 2),496 "any filter vs int items")497 checkError(t, tc.got,498 td.Last(td.Gt(666), "never reached"),499 expectedError{500 Message: mustBe("item not found"),501 Path: mustBe("DATA"),502 Got: mustContain(`]int) (len=7 `),503 Expected: mustBe("Last(> 666)"),504 })505 })506 }507 })508 t.Run("struct", func(t *testing.T) {509 type person struct {510 ID int64511 Name string512 }513 got := [...]person{514 {ID: 1, Name: "Joe"},515 {ID: 2, Name: "Bob"},516 {ID: 3, Name: "Alice"},517 {ID: 4, Name: "Brian"},518 {ID: 5, Name: "Britt"},519 }520 sgot := got[:]521 testCases := []struct {522 name string523 got any524 }{525 {"slice", sgot},526 {"array", got},527 {"*slice", &sgot},528 {"*array", &got},529 }530 for _, tc := range testCases {531 t.Run(tc.name, func(t *testing.T) {532 checkOK(t, tc.got, td.Last(533 td.JSONPointer("/Name", td.HasPrefix("Br")),534 person{ID: 5, Name: "Britt"}))535 checkOK(t, tc.got, td.Last(536 func(p person) bool { return p.ID < 3 },537 person{ID: 2, Name: "Bob"}))538 })539 }540 })541 t.Run("interfaces", func(t *testing.T) {542 got := [...]any{-3, -2, -1, 0, 1, 2, 3}543 sgot := got[:]544 testCases := []struct {545 name string546 got any547 }{548 {"slice", sgot},549 {"array", got},550 {"*slice", &sgot},551 {"*array", &got},552 }553 for _, tc := range testCases {554 t.Run(tc.name, func(t *testing.T) {555 checkOK(t, tc.got, td.Last(td.Lt(0), -1))556 checkOK(t, tc.got, td.Last(td.Not(td.Between(1, 3)), 0))557 checkOK(t, tc.got, td.Last(558 func(x int) bool { return (x & 1) == 0 },559 2))560 checkOK(t, tc.got, td.Last(561 func(x int64) bool { return (x & 1) != 0 },562 3),563 "int64 filter vs any/int items")564 checkOK(t, tc.got, td.Last(565 func(x any) bool { return (x.(int) & 1) == 0 },566 2),567 "any filter vs any/int items")568 })569 }570 })571 t.Run("interfaces error", func(t *testing.T) {572 got := [...]any{123, "foo", 456}573 sgot := got[:]574 testCases := []struct {575 name string576 got any577 }{578 {"slice", sgot},579 {"array", got},580 {"*slice", &sgot},581 {"*array", &got},582 }583 for _, tc := range testCases {584 t.Run(tc.name, func(t *testing.T) {585 checkError(t, tc.got,586 td.Last(func(x int) bool { return false }, "never reached"),587 expectedError{588 Message: mustBe("incompatible parameter type"),589 Path: mustBe("DATA[1]"),590 Got: mustBe("string"),591 Expected: mustBe("int"),592 })593 })594 }595 })596 t.Run("nil slice", func(t *testing.T) {597 var got []int598 testCases := []struct {599 name string600 got any601 }{602 {"slice", got},603 {"*slice", &got},604 }605 for _, tc := range testCases {606 t.Run(tc.name, func(t *testing.T) {607 checkError(t, tc.got,608 td.Last(td.Gt(666), "never reached"),609 expectedError{610 Message: mustBe("item not found"),611 Path: mustBe("DATA"),612 Got: mustBe("([]int) <nil>"),613 Expected: mustBe("Last(> 666)"),614 })615 })616 }617 })618 t.Run("nil pointer", func(t *testing.T) {619 checkError(t, (*[]int)(nil), td.Last(td.Ignore(), 33),620 expectedError{621 Message: mustBe("nil pointer"),622 Path: mustBe("DATA"),623 Got: mustBe("nil *slice (*[]int type)"),624 Expected: mustBe("non-nil *slice OR *array"),625 })626 })627 t.Run("JSON", func(t *testing.T) {628 got := map[string]any{629 "values": []int{1, 2, 3, 4},630 }631 checkOK(t, got, td.JSON(`{"values": Last(Lt(3), 2)}`))632 })633 t.Run("errors", func(t *testing.T) {634 for _, filter := range []any{nil, 33} {635 checkError(t, "never tested",636 td.Last(filter, 42),637 expectedError{638 Message: mustBe("bad usage of Last operator"),639 Path: mustBe("DATA"),640 Summary: mustBe("usage: Last(FILTER_FUNC|FILTER_TESTDEEP_OPERATOR, TESTDEEP_OPERATOR|EXPECTED_VALUE), FILTER_FUNC must be a function or FILTER_TESTDEEP_OPERATOR a TestDeep operator"),641 },642 "filter:", filter)643 }644 for _, filter := range []any{645 func() bool { return true },646 func(a, b int) bool { return true },647 func(a ...int) bool { return true },648 } {649 checkError(t, "never tested",650 td.Last(filter, 42),651 expectedError{652 Message: mustBe("bad usage of Last operator"),653 Path: mustBe("DATA"),654 Summary: mustBe("usage: Last(FILTER_FUNC|FILTER_TESTDEEP_OPERATOR, TESTDEEP_OPERATOR|EXPECTED_VALUE), FILTER_FUNC must take only one non-variadic argument"),655 },656 "filter:", filter)657 }658 for _, filter := range []any{659 func(a int) {},660 func(a int) int { return 0 },661 func(a int) (bool, bool) { return true, true },662 } {663 checkError(t, "never tested",664 td.Last(filter, 42),665 expectedError{666 Message: mustBe("bad usage of Last operator"),667 Path: mustBe("DATA"),668 Summary: mustBe("usage: Last(FILTER_FUNC|FILTER_TESTDEEP_OPERATOR, TESTDEEP_OPERATOR|EXPECTED_VALUE), FILTER_FUNC must return bool"),669 },670 "filter:", filter)671 }672 checkError(t, &struct{}{}, td.Last(td.Ignore(), 33),673 expectedError{674 Message: mustBe("bad kind"),675 Path: mustBe("DATA"),676 Got: mustBe("*struct (*struct {} type)"),677 Expected: mustBe("slice OR array OR *slice OR *array"),678 })679 checkError(t, nil, td.Last(td.Ignore(), 33),680 expectedError{681 Message: mustBe("bad kind"),682 Path: mustBe("DATA"),683 Got: mustBe("nil"),684 Expected: mustBe("slice OR array OR *slice OR *array"),685 })686 })687}688func TestLastString(t *testing.T) {689 test.EqualStr(t,690 td.Last(func(n int) bool { return true }, 33).String(),691 "Last(func(int) bool)")692 test.EqualStr(t, td.Last(td.Gt(0), 33).String(), "Last(> 0)")693 // Erroneous op694 test.EqualStr(t, td.Last(42, 33).String(), "Last(<ERROR>)")695}696func TestLastTypeBehind(t *testing.T) {697 equalTypes(t, td.Last(func(n int) bool { return true }, 33), []int{})698 equalTypes(t, td.Last(td.Gt("x"), "x"), []string{})699 // Erroneous op700 equalTypes(t, td.Last(42, 33), nil)701}...

Full Screen

Full Screen

TestLast

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 t.TestLast()4 fmt.Println("Hello World")5}6import (7func main() {8 t.TestFirst()9 fmt.Println("Hello World")10}11import "fmt"12type TD_test struct {13}14func (t *TD_test) TestFirst() {15 fmt.Println("TestFirst")16}17func (t *TD_test) TestLast() {18 fmt.Println("TestLast")19}

Full Screen

Full Screen

TestLast

Using AI Code Generation

copy

Full Screen

1import "fmt"2import "test"3func main() {4 a.Add(1)5 a.Add(2)6 a.Add(3)7 fmt.Println(a.TestLast())8}9type Test struct {10}11func (t *Test) Add(i int) {12 t.data = append(t.data, i)13}14func (t *Test) TestLast() int {15 return t.data[len(t.data)-1]16}

Full Screen

Full Screen

TestLast

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 fmt.Println(testdata.TestLast())4}5import (6func main() {7 td := testdata.TestData{First: "First", Last: "Last"}8 fmt.Println(td.TestFirst())9}10import (11func main() {12 td := testdata.TestData{First: "First", Last: "Last"}13 fmt.Println(td.TestFirst())14 fmt.Println(td.TestLast())15}16import (17func main() {18 td := testdata.TestData{First: "First", Last: "Last"}19 fmt.Println(td.TestFirst())20 fmt.Println(td.TestLast())21 fmt.Println(td.TestFirstLast())22}23import (24func main() {25 td := testdata.TestData{First: "First", Last: "Last"}26 fmt.Println(td.TestFirst())27 fmt.Println(td

Full Screen

Full Screen

TestLast

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 fmt.Println(td_test.TestLast())4}5import (6func main() {7 fmt.Println(td_test.TestLast())8 fmt.Println(td_test2.TestLast())9}10import (11func main() {12 fmt.Println(td_test.TestLast())13}14import (15func main() {16 fmt.Println(td_test.TestLast())17}

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