How to use TestSStruct method of td_test Package

Best Go-testdeep code snippet using td_test.TestSStruct

td_struct_test.go

Source:td_struct_test.go Github

copy

Full Screen

...623 equalTypes(t, td.Struct(&MyStruct{}, nil), &MyStruct{})624 // Erroneous op625 equalTypes(t, td.Struct("test", nil), nil)626}627func TestSStruct(t *testing.T) {628 gotStruct := MyStruct{629 MyStructMid: MyStructMid{630 MyStructBase: MyStructBase{631 ValBool: true,632 },633 ValStr: "foobar",634 },635 ValInt: 123,636 }637 //638 // Using pointer639 checkOK(t, &gotStruct,640 td.SStruct(&MyStruct{}, td.StructFields{641 "ValBool": true,642 "ValStr": "foobar",643 "ValInt": 123,644 // nil Ptr645 }))646 checkOK(t, &gotStruct,647 td.SStruct(648 &MyStruct{649 MyStructMid: MyStructMid{650 ValStr: "zip",651 },652 ValInt: 666,653 },654 td.StructFields{655 "ValBool": true,656 "> ValStr": "foobar",657 ">ValInt": 123,658 }))659 checkOK(t, &gotStruct,660 td.SStruct((*MyStruct)(nil), td.StructFields{661 "ValBool": true,662 "ValStr": "foobar",663 "ValInt": 123,664 // nil Ptr665 }))666 checkError(t, 123,667 td.SStruct(&MyStruct{}, td.StructFields{}),668 expectedError{669 Message: mustBe("type mismatch"),670 Path: mustBe("DATA"),671 Got: mustContain("int"),672 Expected: mustContain("*td_test.MyStruct"),673 })674 checkError(t, &MyStructBase{},675 td.SStruct(&MyStruct{}, td.StructFields{}),676 expectedError{677 Message: mustBe("type mismatch"),678 Path: mustBe("DATA"),679 Got: mustContain("*td_test.MyStructBase"),680 Expected: mustContain("*td_test.MyStruct"),681 })682 checkError(t, &gotStruct,683 td.SStruct(&MyStruct{}, td.StructFields{684 // ValBool false ← does not match685 "ValStr": "foobar",686 "ValInt": 123,687 }),688 expectedError{689 Message: mustBe("values differ"),690 Path: mustBe("DATA.ValBool"),691 Got: mustContain("true"),692 Expected: mustContain("false"),693 })694 checkOK(t, &gotStruct,695 td.SStruct(&MyStruct{696 MyStructMid: MyStructMid{697 MyStructBase: MyStructBase{698 ValBool: true,699 },700 ValStr: "foobar",701 },702 ValInt: 123,703 }, nil))704 checkError(t, &gotStruct,705 td.SStruct(&MyStruct{706 MyStructMid: MyStructMid{707 MyStructBase: MyStructBase{708 ValBool: true,709 },710 ValStr: "foobax", // ← does not match711 },712 ValInt: 123,713 }, nil),714 expectedError{715 Message: mustBe("values differ"),716 Path: mustBe("DATA.ValStr"),717 Got: mustContain("foobar"),718 Expected: mustContain("foobax"),719 })720 // Zero values721 checkOK(t, &MyStruct{}, td.SStruct(&MyStruct{}, nil))722 checkOK(t, &MyStruct{}, td.SStruct(&MyStruct{}, td.StructFields{}))723 // nil cases724 checkError(t, nil, td.SStruct(&MyStruct{}, nil),725 expectedError{726 Message: mustBe("values differ"),727 Path: mustBe("DATA"),728 Got: mustContain("nil"),729 Expected: mustContain("*td_test.MyStruct"),730 })731 checkError(t, (*MyStruct)(nil), td.SStruct(&MyStruct{}, nil),732 expectedError{733 Message: mustBe("values differ"),734 Path: mustBe("DATA"),735 Got: mustContain("nil"),736 Expected: mustBe("non-nil"),737 })738 //739 // Without pointer740 checkOK(t, gotStruct,741 td.SStruct(MyStruct{}, td.StructFields{742 "ValBool": true,743 "ValStr": "foobar",744 "ValInt": 123,745 }))746 checkOK(t, gotStruct,747 td.SStruct(748 MyStruct{749 MyStructMid: MyStructMid{750 ValStr: "zip",751 },752 ValInt: 666,753 },754 td.StructFields{755 "ValBool": true,756 "> ValStr": "foobar",757 ">ValInt": 123,758 }))759 checkError(t, 123, td.SStruct(MyStruct{}, td.StructFields{}),760 expectedError{761 Message: mustBe("type mismatch"),762 Path: mustBe("DATA"),763 Got: mustContain("int"),764 Expected: mustContain("td_test.MyStruct"),765 })766 checkError(t, gotStruct,767 td.SStruct(MyStruct{}, td.StructFields{768 // "ValBool" false ← does not match769 "ValStr": "foobar",770 "ValInt": 123,771 }),772 expectedError{773 Message: mustBe("values differ"),774 Path: mustBe("DATA.ValBool"),775 Got: mustContain("true"),776 Expected: mustContain("false"),777 })778 checkOK(t, gotStruct,779 td.SStruct(MyStruct{780 MyStructMid: MyStructMid{781 MyStructBase: MyStructBase{782 ValBool: true,783 },784 ValStr: "foobar",785 },786 ValInt: 123,787 }, nil))788 checkError(t, gotStruct,789 td.SStruct(MyStruct{790 MyStructMid: MyStructMid{791 MyStructBase: MyStructBase{792 ValBool: true,793 },794 ValStr: "foobax", // ← does not match795 },796 ValInt: 123,797 }, nil),798 expectedError{799 Message: mustBe("values differ"),800 Path: mustBe("DATA.ValStr"),801 Got: mustContain("foobar"),802 Expected: mustContain("foobax"),803 })804 // Zero values805 checkOK(t, MyStruct{}, td.Struct(MyStruct{}, td.StructFields{}))806 checkOK(t, MyStruct{}, td.Struct(MyStruct{}, nil))807 // nil cases808 checkError(t, nil, td.SStruct(MyStruct{}, nil),809 expectedError{810 Message: mustBe("values differ"),811 Path: mustBe("DATA"),812 Got: mustContain("nil"),813 Expected: mustContain("td_test.MyStruct"),814 })815 checkError(t, (*MyStruct)(nil), td.SStruct(MyStruct{}, nil),816 expectedError{817 Message: mustBe("type mismatch"),818 Path: mustBe("DATA"),819 Got: mustBe("*td_test.MyStruct"),820 Expected: mustBe("td_test.MyStruct"),821 })822 //823 // Be lax...824 type Struct1 struct {825 name string826 age int827 }828 type Struct2 struct {829 name string830 age int831 }832 // Without Lax → error833 checkError(t,834 Struct1{name: "Bob", age: 42},835 td.SStruct(Struct2{name: "Bob", age: 42}, nil),836 expectedError{837 Message: mustBe("type mismatch"),838 })839 // With Lax → OK840 checkOK(t,841 Struct1{name: "Bob", age: 42},842 td.Lax(td.SStruct(Struct2{name: "Bob", age: 42}, nil)))843 //844 // IgnoreUnexported845 t.Run("IgnoreUnexported", func(tt *testing.T) {846 type SType struct {847 Public int848 private string849 }850 got := SType{Public: 42, private: "test"}851 expected := td.SStruct(SType{Public: 42}, nil)852 checkError(tt, got, expected,853 expectedError{854 Message: mustBe("values differ"),855 Path: mustBe("DATA.private"),856 Got: mustBe(`"test"`),857 Expected: mustBe(`""`),858 })859 // Ignore unexported globally860 defer func() { td.DefaultContextConfig.IgnoreUnexported = false }()861 td.DefaultContextConfig.IgnoreUnexported = true862 checkOK(tt, got, expected)863 td.DefaultContextConfig.IgnoreUnexported = false864 ttt := test.NewTestingTB(t.Name())865 t := td.NewT(ttt).IgnoreUnexported(SType{}) // ignore only for SType866 test.IsTrue(tt, t.Cmp(got, expected))867 })868 //869 // Bad usage870 checkError(t, "never tested",871 td.SStruct("test", nil),872 expectedError{873 Message: mustBe("bad usage of SStruct operator"),874 Path: mustBe("DATA"),875 Summary: mustBe("usage: SStruct(STRUCT|&STRUCT, EXPECTED_FIELDS), but received string as 1st parameter"),876 })877 i := 12878 checkError(t, "never tested",879 td.SStruct(&i, nil),880 expectedError{881 Message: mustBe("bad usage of SStruct operator"),882 Path: mustBe("DATA"),883 Summary: mustBe("usage: SStruct(STRUCT|&STRUCT, EXPECTED_FIELDS), but received *int (ptr) as 1st parameter"),884 })885 checkError(t, "never tested",886 td.SStruct(&MyStruct{}, td.StructFields{"UnknownField": 123}),887 expectedError{888 Message: mustBe("bad usage of SStruct operator"),889 Path: mustBe("DATA"),890 Summary: mustBe(`struct td_test.MyStruct has no field "UnknownField"`),891 })892 checkError(t, "never tested",893 td.SStruct(&MyStruct{}, td.StructFields{">\tUnknownField": 123}),894 expectedError{895 Message: mustBe("bad usage of SStruct operator"),896 Path: mustBe("DATA"),897 Summary: mustBe(`struct td_test.MyStruct has no field "UnknownField" (from ">\tUnknownField")`),898 })899 checkError(t, "never tested",900 td.SStruct(&MyStruct{}, td.StructFields{"ValBool": 123}),901 expectedError{902 Message: mustBe("bad usage of SStruct operator"),903 Path: mustBe("DATA"),904 Summary: mustBe("type int of field expected value ValBool differs from struct one (bool)"),905 })906 checkError(t, "never tested",907 td.SStruct(&MyStruct{}, td.StructFields{">ValBool": 123}),908 expectedError{909 Message: mustBe("bad usage of SStruct operator"),910 Path: mustBe("DATA"),911 Summary: mustBe(`type int of field expected value ValBool (from ">ValBool") differs from struct one (bool)`),912 })913 checkError(t, "never tested",914 td.SStruct(&MyStruct{}, td.StructFields{"ValBool": nil}),915 expectedError{916 Message: mustBe("bad usage of SStruct operator"),917 Path: mustBe("DATA"),918 Summary: mustBe("expected value of field ValBool cannot be nil as it is a bool"),919 })920 checkError(t, "never tested",921 td.SStruct(&MyStruct{922 MyStructMid: MyStructMid{923 MyStructBase: MyStructBase{924 ValBool: true,925 },926 },927 },928 td.StructFields{"ValBool": false}),929 expectedError{930 Message: mustBe("bad usage of SStruct operator"),931 Path: mustBe("DATA"),932 Summary: mustBe("non zero field ValBool in model already exists in expectedFields"),933 })934 //935 // String936 test.EqualStr(t,937 td.SStruct(MyStruct{938 MyStructMid: MyStructMid{939 ValStr: "foobar",940 },941 ValInt: 123,942 },943 td.StructFields{944 "ValBool": false,945 }).String(),946 `SStruct(td_test.MyStruct{947 Ptr: (*int)(<nil>)948 ValBool: false949 ValInt: 123950 ValStr: "foobar"951})`)952 test.EqualStr(t,953 td.SStruct(&MyStruct{954 MyStructMid: MyStructMid{955 ValStr: "foobar",956 },957 ValInt: 123,958 },959 td.StructFields{960 "ValBool": false,961 }).String(),962 `SStruct(*td_test.MyStruct{963 Ptr: (*int)(<nil>)964 ValBool: false965 ValInt: 123966 ValStr: "foobar"967})`)968 test.EqualStr(t,969 td.SStruct(&MyStruct{}, td.StructFields{}).String(),970 `SStruct(*td_test.MyStruct{971 Ptr: (*int)(<nil>)972 ValBool: false973 ValInt: 0974 ValStr: ""975})`)976 // Erroneous op977 test.EqualStr(t, td.SStruct("test", nil).String(), "SStruct(<ERROR>)")978}979func TestSStructPattern(t *testing.T) {980 // Patterns are already fully tested in TestStructPatterns981 type paAnon struct {982 alphaNum int983 betaNum int984 }985 type paTest struct {986 paAnon987 Num int988 }989 got := paTest{990 paAnon: paAnon{991 alphaNum: 1000,992 betaNum: 2000,993 },994 Num: 666,995 }996 checkOK(t, got,997 td.SStruct(paTest{},998 td.StructFields{999 "=*Num": td.Gte(666), // matches Num, alphaNum & betaNum1000 }))1001 checkOK(t, got,1002 td.SStruct(paTest{},1003 td.StructFields{1004 "=~Num$": td.Gte(666), // matches Num, alphaNum & betaNum1005 }))1006 checkOK(t, paTest{Num: 666},1007 td.SStruct(paTest{},1008 td.StructFields{1009 "=~^Num": 666, // only matches Num1010 // remaining fields are tested as 01011 }))1012}1013func TestSStructTypeBehind(t *testing.T) {1014 equalTypes(t, td.SStruct(MyStruct{}, nil), MyStruct{})1015 equalTypes(t, td.SStruct(&MyStruct{}, nil), &MyStruct{})1016 // Erroneous op1017 equalTypes(t, td.SStruct("test", nil), nil)1018}...

Full Screen

Full Screen

TestSStruct

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 t := td_test.TestStruct{}4 t.TestSStruct()5 fmt.Println(t)6}7{0 0}8{0 0}9{0 0}10import "fmt"11func main() {12 fmt.Println("Hello World")13}14import "testing"15func TestHelloWorld(t *testing.T) {16 fmt.Println("Hello World")17}

Full Screen

Full Screen

TestSStruct

Using AI Code Generation

copy

Full Screen

1import "td"2func main() {3 a.TestSStruct()4}5import "td"6func main() {7 a.TestSStruct()8}9 /usr/local/go/src/pkg/td (from $GOROOT)10 /home/username/go/src/pkg/td (from $GOPATH)

Full Screen

Full Screen

TestSStruct

Using AI Code Generation

copy

Full Screen

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

Full Screen

Full Screen

TestSStruct

Using AI Code Generation

copy

Full Screen

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

Full Screen

Full Screen

TestSStruct

Using AI Code Generation

copy

Full Screen

1import "fmt"2func main() {3 s.TestSStruct()4}5TestStruct::TestSStruct()6TestStruct::TestSStruct()7TestStruct::TestSStruct()8TestStruct::TestSStruct()9TestStruct::TestSStruct()10TestStruct::TestSStruct()11TestStruct::TestSStruct()12TestStruct::TestSStruct()13TestStruct::TestSStruct()14TestStruct::TestSStruct()15TestStruct::TestSStruct()16TestStruct::TestSStruct()17TestStruct::TestSStruct()18TestStruct::TestSStruct()19TestStruct::TestSStruct()20TestStruct::TestSStruct()21TestStruct::TestSStruct()22TestStruct::TestSStruct()23TestStruct::TestSStruct()24TestStruct::TestSStruct()25TestStruct::TestSStruct()26TestStruct::TestSStruct()27TestStruct::TestSStruct()28TestStruct::TestSStruct()

Full Screen

Full Screen

TestSStruct

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 fmt.Println("Hello, playground")4 t := td_test.NewTestStruct()5 fmt.Println(t.TestSStruct())6}

Full Screen

Full Screen

TestSStruct

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 var td = td_test.Td_test{}4 td.TestStruct()5}6import (7func main() {8 var td = td_test.Td_test{}9 td.TestStruct()10}11import (12func main() {13 var td = td_test.Td_test{}14 td.TestStruct()15}16import (17func main() {18 var td = td_test.Td_test{}19 td.TestStruct()20}21import (22func main() {23 var td = td_test.Td_test{}24 td.TestStruct()25}26import (27func main() {28 var td = td_test.Td_test{}29 td.TestStruct()30}31import (32func main() {33 var td = td_test.Td_test{}34 td.TestStruct()

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