How to use CmpTrue method of td Package

Best Go-testdeep code snippet using td.CmpTrue

test_api_test.go

Source:test_api_test.go Github

copy

Full Screen

...508 }).509 Failed())510 td.CmpEmpty(t, mockT.LogBuf())511 mockT = tdutil.NewT("test")512 td.CmpTrue(t,513 tdhttp.NewTestAPI(mockT, mux).514 Get("/any/cookies").515 CmpCookies([]*http.Cookie{516 {517 Name: "first",518 Value: "cookie1",519 MaxAge: 123456,520 Expires: time.Date(2021, time.August, 12, 11, 22, 33, 0, time.UTC),521 },522 }).523 Failed())524 td.CmpContains(t, mockT.LogBuf(),525 "Failed test 'cookies should match'")526 td.CmpContains(t, mockT.LogBuf(),527 "Response.Cookie: comparing slices, from index #1")528 // 2 cookies are here whatever their order is using Bag529 mockT = tdutil.NewT("test")530 td.CmpFalse(t,531 tdhttp.NewTestAPI(mockT, mux).532 Get("/any/cookies").533 CmpCookies(td.Bag(534 td.Smuggle("Name", "second"),535 td.Smuggle("Name", "first"),536 )).537 Failed())538 td.CmpEmpty(t, mockT.LogBuf())539 // Testing only Name & Value whatever their order is using Bag540 mockT = tdutil.NewT("test")541 td.CmpFalse(t,542 tdhttp.NewTestAPI(mockT, mux).543 Get("/any/cookies").544 CmpCookies(td.Bag(545 td.Struct(&http.Cookie{Name: "first", Value: "cookie1"}, nil),546 td.Struct(&http.Cookie{Name: "second", Value: "cookie2"}, nil),547 )).548 Failed())549 td.CmpEmpty(t, mockT.LogBuf())550 // Testing the presence of only one using SuperBagOf551 mockT = tdutil.NewT("test")552 td.CmpFalse(t,553 tdhttp.NewTestAPI(mockT, mux).554 Get("/any/cookies").555 CmpCookies(td.SuperBagOf(556 td.Struct(&http.Cookie{Name: "first", Value: "cookie1"}, nil),557 )).558 Failed())559 td.CmpEmpty(t, mockT.LogBuf())560 // Testing only the number of cookies561 mockT = tdutil.NewT("test")562 td.CmpFalse(t,563 tdhttp.NewTestAPI(mockT, mux).564 Get("/any/cookies").565 CmpCookies(td.Len(2)).566 Failed())567 td.CmpEmpty(t, mockT.LogBuf())568 // Error followed by a success: Failed() should return true anyway569 mockT = tdutil.NewT("test")570 td.CmpTrue(t,571 tdhttp.NewTestAPI(mockT, mux).572 Get("/any").573 CmpCookies(td.Len(100)). // fails574 CmpCookies(td.Len(2)). // succeeds575 Failed())576 td.CmpContains(t, mockT.LogBuf(),577 "Failed test 'cookies should match'")578 // AutoDumpResponse579 mockT = tdutil.NewT("test")580 td.CmpTrue(t,581 tdhttp.NewTestAPI(mockT, mux).582 AutoDumpResponse().583 Get("/any/cookies").584 Name("my test").585 CmpCookies(td.Len(100)).586 Failed())587 td.CmpContains(t, mockT.LogBuf(),588 "Failed test 'my test: cookies should match'")589 td.CmpContains(t, mockT.LogBuf(), "Response.Cookie: bad length")590 td.Cmp(t, mockT.LogBuf(), td.Contains("Received response:\n"))591 // Request not sent592 mockT = tdutil.NewT("test")593 ta := tdhttp.NewTestAPI(mockT, mux).594 Name("my test").595 CmpCookies(td.Len(2))596 td.CmpTrue(t, ta.Failed())597 td.CmpContains(t, mockT.LogBuf(), "Failed test 'my test: request is sent'\n")598 td.CmpContains(t, mockT.LogBuf(), "Request not sent!\n")599 td.CmpContains(t, mockT.LogBuf(), "A request must be sent before testing status, header, body or full response\n")600 td.CmpNot(t, mockT.LogBuf(), td.Contains("No response received yet\n"))601 })602 t.Run("Trailer", func(t *testing.T) {603 mockT := tdutil.NewT("test")604 td.CmpFalse(t,605 tdhttp.NewTestAPI(mockT, mux).606 Get("/any").607 CmpStatus(200).608 CmpTrailer(nil). // No trailer at all609 Failed())610 mockT = tdutil.NewT("test")611 td.CmpFalse(t,612 tdhttp.NewTestAPI(mockT, mux).613 Get("/any/trailer").614 CmpStatus(200).615 CmpTrailer(containsKey).616 Failed())617 mockT = tdutil.NewT("test")618 td.CmpFalse(t,619 tdhttp.NewTestAPI(mockT, mux).620 Get("/any/trailer").621 CmpStatus(200).622 CmpTrailer(http.Header{623 "X-Testdeep-Method": {"GET"},624 "X-Testdeep-Foo": {"bar"},625 }).626 Failed())627 // AutoDumpResponse628 mockT = tdutil.NewT("test")629 td.CmpTrue(t,630 tdhttp.NewTestAPI(mockT, mux).631 AutoDumpResponse().632 Get("/any/trailer").633 Name("my test").634 CmpTrailer(http.Header{}).635 Failed())636 td.CmpContains(t, mockT.LogBuf(),637 "Failed test 'my test: trailer should match'")638 td.Cmp(t, mockT.LogBuf(), td.Contains("Received response:\n"))639 // OrDumpResponse640 mockT = tdutil.NewT("test")641 td.CmpTrue(t,642 tdhttp.NewTestAPI(mockT, mux).643 Get("/any/trailer").644 Name("my test").645 CmpTrailer(http.Header{}).646 OrDumpResponse().647 OrDumpResponse(). // only one log648 Failed())649 td.CmpContains(t, mockT.LogBuf(),650 "Failed test 'my test: trailer should match'")651 logPos := strings.Index(mockT.LogBuf(), "Received response:\n")652 if td.Cmp(t, logPos, td.Gte(0)) {653 // Only one occurrence654 td.Cmp(t,655 strings.Index(mockT.LogBuf()[logPos+1:], "Received response:\n"),656 -1)657 }658 mockT = tdutil.NewT("test")659 ta := tdhttp.NewTestAPI(mockT, mux).660 Name("my test").661 CmpTrailer(http.Header{})662 td.CmpTrue(t, ta.Failed())663 td.CmpContains(t, mockT.LogBuf(), "Failed test 'my test: request is sent'\n")664 td.CmpContains(t, mockT.LogBuf(), "Request not sent!\n")665 td.CmpContains(t, mockT.LogBuf(), "A request must be sent before testing status, header, body or full response\n")666 td.CmpNot(t, mockT.LogBuf(), td.Contains("No response received yet\n"))667 end := len(mockT.LogBuf())668 ta.OrDumpResponse()669 td.CmpContains(t, mockT.LogBuf()[end:], "No response received yet\n")670 })671 t.Run("Status error", func(t *testing.T) {672 mockT := tdutil.NewT("test")673 td.CmpTrue(t,674 tdhttp.NewTestAPI(mockT, mux).675 Get("/any").676 CmpStatus(400).677 Failed())678 td.CmpContains(t, mockT.LogBuf(),679 "Failed test 'status code should match'")680 // Error followed by a success: Failed() should return true anyway681 mockT = tdutil.NewT("test")682 td.CmpTrue(t,683 tdhttp.NewTestAPI(mockT, mux).684 Get("/any").685 CmpStatus(400). // fails686 CmpStatus(200). // succeeds687 Failed())688 td.CmpContains(t, mockT.LogBuf(),689 "Failed test 'status code should match'")690 mockT = tdutil.NewT("test")691 td.CmpTrue(t,692 tdhttp.NewTestAPI(mockT, mux).693 Get("/any").694 Name("my test").695 CmpStatus(400).696 Failed())697 td.CmpContains(t, mockT.LogBuf(),698 "Failed test 'my test: status code should match'")699 td.CmpNot(t, mockT.LogBuf(), td.Contains("Received response:\n"))700 // AutoDumpResponse701 mockT = tdutil.NewT("test")702 td.CmpTrue(t,703 tdhttp.NewTestAPI(mockT, mux).704 AutoDumpResponse().705 Get("/any").706 Name("my test").707 CmpStatus(400).708 Failed())709 td.CmpContains(t, mockT.LogBuf(),710 "Failed test 'my test: status code should match'")711 td.Cmp(t, mockT.LogBuf(), td.Contains("Received response:\n"))712 // OrDumpResponse713 mockT = tdutil.NewT("test")714 td.CmpTrue(t,715 tdhttp.NewTestAPI(mockT, mux).716 Get("/any").717 Name("my test").718 CmpStatus(400).719 OrDumpResponse().720 OrDumpResponse(). // only one log721 Failed())722 td.CmpContains(t, mockT.LogBuf(),723 "Failed test 'my test: status code should match'")724 logPos := strings.Index(mockT.LogBuf(), "Received response:\n")725 if td.Cmp(t, logPos, td.Gte(0)) {726 // Only one occurrence727 td.Cmp(t,728 strings.Index(mockT.LogBuf()[logPos+1:], "Received response:\n"),729 -1)730 }731 mockT = tdutil.NewT("test")732 ta := tdhttp.NewTestAPI(mockT, mux).733 Name("my test").734 CmpStatus(400)735 td.CmpTrue(t, ta.Failed())736 td.CmpContains(t, mockT.LogBuf(), "Failed test 'my test: request is sent'\n")737 td.CmpContains(t, mockT.LogBuf(), "Request not sent!\n")738 td.CmpContains(t, mockT.LogBuf(), "A request must be sent before testing status, header, body or full response\n")739 td.CmpNot(t, mockT.LogBuf(), td.Contains("No response received yet\n"))740 end := len(mockT.LogBuf())741 ta.OrDumpResponse()742 td.CmpContains(t, mockT.LogBuf()[end:], "No response received yet\n")743 })744 t.Run("Header error", func(t *testing.T) {745 mockT := tdutil.NewT("test")746 td.CmpTrue(t,747 tdhttp.NewTestAPI(mockT, mux).748 Get("/any").749 CmpHeader(td.Not(containsKey)).750 Failed())751 td.CmpContains(t, mockT.LogBuf(),752 "Failed test 'header should match'")753 // Error followed by a success: Failed() should return true anyway754 mockT = tdutil.NewT("test")755 td.CmpTrue(t,756 tdhttp.NewTestAPI(mockT, mux).757 Get("/any").758 CmpHeader(td.Not(containsKey)). // fails759 CmpHeader(td.Ignore()). // succeeds760 Failed())761 td.CmpContains(t, mockT.LogBuf(),762 "Failed test 'header should match'")763 mockT = tdutil.NewT("test")764 td.CmpTrue(t,765 tdhttp.NewTestAPI(mockT, mux).766 Get("/any").767 Name("my test").768 CmpHeader(td.Not(containsKey)).769 Failed())770 td.CmpContains(t, mockT.LogBuf(),771 "Failed test 'my test: header should match'")772 td.CmpNot(t, mockT.LogBuf(), td.Contains("Received response:\n"))773 // AutoDumpResponse774 mockT = tdutil.NewT("test")775 td.CmpTrue(t,776 tdhttp.NewTestAPI(mockT, mux).777 AutoDumpResponse().778 Get("/any").779 Name("my test").780 CmpHeader(td.Not(containsKey)).781 Failed())782 td.CmpContains(t, mockT.LogBuf(),783 "Failed test 'my test: header should match'")784 td.Cmp(t, mockT.LogBuf(), td.Contains("Received response:\n"))785 mockT = tdutil.NewT("test")786 td.CmpTrue(t,787 tdhttp.NewTestAPI(mockT, mux).788 Name("my test").789 CmpHeader(td.Not(containsKey)).790 Failed())791 td.CmpContains(t, mockT.LogBuf(), "Failed test 'my test: request is sent'\n")792 td.CmpContains(t, mockT.LogBuf(), "Request not sent!\n")793 td.CmpContains(t, mockT.LogBuf(), "A request must be sent before testing status, header, body or full response\n")794 })795 t.Run("Body error", func(t *testing.T) {796 mockT := tdutil.NewT("test")797 td.CmpTrue(t,798 tdhttp.NewTestAPI(mockT, mux).799 Get("/any").800 CmpBody("xxx").801 Failed())802 td.CmpContains(t, mockT.LogBuf(), "Failed test 'body contents is OK'")803 td.CmpContains(t, mockT.LogBuf(), "Response.Body: values differ\n")804 td.CmpContains(t, mockT.LogBuf(), `expected: "xxx"`)805 td.CmpContains(t, mockT.LogBuf(), `got: "GET!"`)806 // Error followed by a success: Failed() should return true anyway807 mockT = tdutil.NewT("test")808 td.CmpTrue(t,809 tdhttp.NewTestAPI(mockT, mux).810 Get("/any").811 CmpBody("xxx"). // fails812 CmpBody(td.Ignore()). // succeeds813 Failed())814 // Without AutoDumpResponse815 mockT = tdutil.NewT("test")816 td.CmpTrue(t,817 tdhttp.NewTestAPI(mockT, mux).818 Get("/any").819 Name("my test").820 CmpBody("xxx").821 Failed())822 td.CmpContains(t, mockT.LogBuf(), "Failed test 'my test: body contents is OK'")823 td.CmpNot(t, mockT.LogBuf(), td.Contains("Received response:\n"))824 // AutoDumpResponse825 mockT = tdutil.NewT("test")826 td.CmpTrue(t,827 tdhttp.NewTestAPI(mockT, mux).828 AutoDumpResponse().829 Get("/any").830 Name("my test").831 CmpBody("xxx").832 Failed())833 td.CmpContains(t, mockT.LogBuf(), "Failed test 'my test: body contents is OK'")834 td.Cmp(t, mockT.LogBuf(), td.Contains("Received response:\n"))835 mockT = tdutil.NewT("test")836 td.CmpTrue(t,837 tdhttp.NewTestAPI(mockT, mux).838 Name("my test").839 CmpBody("xxx").840 Failed())841 td.CmpContains(t, mockT.LogBuf(), "Failed test 'my test: request is sent'\n")842 td.CmpContains(t, mockT.LogBuf(), "Request not sent!\n")843 td.CmpContains(t, mockT.LogBuf(), "A request must be sent before testing status, header, body or full response\n")844 // NoBody845 mockT = tdutil.NewT("test")846 td.CmpTrue(t,847 tdhttp.NewTestAPI(mockT, mux).848 Name("my test").849 NoBody().850 Failed())851 td.CmpContains(t, mockT.LogBuf(), "Failed test 'my test: request is sent'\n")852 td.CmpContains(t, mockT.LogBuf(), "Request not sent!\n")853 td.CmpContains(t, mockT.LogBuf(), "A request must be sent before testing status, header, body or full response\n")854 td.CmpNot(t, mockT.LogBuf(), td.Contains("Received response:\n"))855 // Error followed by a success: Failed() should return true anyway856 mockT = tdutil.NewT("test")857 td.CmpTrue(t,858 tdhttp.NewTestAPI(mockT, mux).859 Name("my test").860 Head("/any").861 CmpBody("fail"). // fails862 NoBody(). // succeeds863 Failed())864 // No JSON body865 mockT = tdutil.NewT("test")866 td.CmpTrue(t,867 tdhttp.NewTestAPI(mockT, mux).868 Head("/any").869 CmpStatus(200).870 CmpHeader(containsKey).871 CmpJSONBody(json.RawMessage(`{}`)).872 Failed())873 td.CmpContains(t, mockT.LogBuf(), "Failed test 'body should not be empty'")874 td.CmpContains(t, mockT.LogBuf(), "Response body is empty!")875 td.CmpContains(t, mockT.LogBuf(), "Body cannot be empty when using CmpJSONBody")876 td.CmpNot(t, mockT.LogBuf(), td.Contains("Received response:\n"))877 // Error followed by a success: Failed() should return true anyway878 mockT = tdutil.NewT("test")879 td.CmpTrue(t,880 tdhttp.NewTestAPI(mockT, mux).881 Get("/any/json").882 CmpStatus(200).883 CmpHeader(containsKey).884 CmpJSONBody(json.RawMessage(`{}`)). // fails885 CmpJSONBody(td.Ignore()). // succeeds886 Failed())887 // No JSON body + AutoDumpResponse888 mockT = tdutil.NewT("test")889 td.CmpTrue(t,890 tdhttp.NewTestAPI(mockT, mux).891 AutoDumpResponse().892 Head("/any").893 CmpStatus(200).894 CmpHeader(containsKey).895 CmpJSONBody(json.RawMessage(`{}`)).896 Failed())897 td.CmpContains(t, mockT.LogBuf(), "Failed test 'body should not be empty'")898 td.CmpContains(t, mockT.LogBuf(), "Response body is empty!")899 td.CmpContains(t, mockT.LogBuf(), "Body cannot be empty when using CmpJSONBody")900 td.Cmp(t, mockT.LogBuf(), td.Contains("Received response:\n"))901 // No XML body902 mockT = tdutil.NewT("test")903 td.CmpTrue(t,904 tdhttp.NewTestAPI(mockT, mux).905 Head("/any").906 CmpStatus(200).907 CmpHeader(containsKey).908 CmpXMLBody(struct{ Test string }{}).909 Failed())910 td.CmpContains(t, mockT.LogBuf(), "Failed test 'body should not be empty'")911 td.CmpContains(t, mockT.LogBuf(), "Response body is empty!")912 td.CmpContains(t, mockT.LogBuf(), "Body cannot be empty when using CmpXMLBody")913 })914 t.Run("Response error", func(t *testing.T) {915 mockT := tdutil.NewT("test")916 td.CmpTrue(t,917 tdhttp.NewTestAPI(mockT, mux).918 Get("/any").919 CmpResponse(nil).920 Failed())921 td.CmpContains(t, mockT.LogBuf(), "Failed test 'full response should match'")922 td.CmpContains(t, mockT.LogBuf(), "Response: values differ")923 td.CmpContains(t, mockT.LogBuf(), "got: (*http.Response)(")924 td.CmpContains(t, mockT.LogBuf(), "expected: nil")925 // Error followed by a success: Failed() should return true anyway926 mockT = tdutil.NewT("test")927 td.CmpTrue(t,928 tdhttp.NewTestAPI(mockT, mux).929 Get("/any").930 CmpResponse(nil). // fails931 CmpResponse(td.Ignore()). // succeeds932 Failed())933 // Without AutoDumpResponse934 mockT = tdutil.NewT("test")935 td.CmpTrue(t,936 tdhttp.NewTestAPI(mockT, mux).937 Get("/any").938 Name("my test").939 CmpResponse(nil).940 Failed())941 td.CmpContains(t, mockT.LogBuf(), "Failed test 'my test: full response should match'")942 td.CmpNot(t, mockT.LogBuf(), td.Contains("Received response:\n"))943 // AutoDumpResponse944 mockT = tdutil.NewT("test")945 td.CmpTrue(t,946 tdhttp.NewTestAPI(mockT, mux).947 AutoDumpResponse().948 Get("/any").949 Name("my test").950 CmpResponse(nil).951 Failed())952 td.CmpContains(t, mockT.LogBuf(), "Failed test 'my test: full response should match'")953 td.Cmp(t, mockT.LogBuf(), td.Contains("Received response:\n"))954 mockT = tdutil.NewT("test")955 td.CmpTrue(t,956 tdhttp.NewTestAPI(mockT, mux).957 Name("my test").958 CmpResponse(nil).959 Failed())960 td.CmpContains(t, mockT.LogBuf(), "Failed test 'my test: request is sent'\n")961 td.CmpContains(t, mockT.LogBuf(), "Request not sent!\n")962 td.CmpContains(t, mockT.LogBuf(), "A request must be sent before testing status, header, body or full response\n")963 })964 t.Run("Request error", func(t *testing.T) {965 var ta *tdhttp.TestAPI966 checkFatal := func(fn func()) {967 mockT := tdutil.NewT("test")968 td.CmpTrue(t, mockT.CatchFailNow(func() {969 ta = tdhttp.NewTestAPI(mockT, mux)970 fn()971 }))972 td.Cmp(t,973 mockT.LogBuf(),974 td.Contains("headersQueryParams... can only contains string, http.Header, http.Cookie, url.Values and tdhttp.Q, not bool"),975 )976 }977 empty := strings.NewReader("")978 checkFatal(func() { ta.Get("/path", true) })979 checkFatal(func() { ta.Head("/path", true) })980 checkFatal(func() { ta.Options("/path", empty, true) })981 checkFatal(func() { ta.Post("/path", empty, true) })982 checkFatal(func() { ta.PostForm("/path", nil, true) })983 checkFatal(func() { ta.PostMultipartFormData("/path", &tdhttp.MultipartBody{}, true) })984 checkFatal(func() { ta.Put("/path", empty, true) })985 checkFatal(func() { ta.Patch("/path", empty, true) })986 checkFatal(func() { ta.Delete("/path", empty, true) })987 checkFatal(func() { ta.NewJSONRequest("ZIP", "/path", nil, true) })988 checkFatal(func() { ta.PostJSON("/path", nil, true) })989 checkFatal(func() { ta.PutJSON("/path", nil, true) })990 checkFatal(func() { ta.PatchJSON("/path", nil, true) })991 checkFatal(func() { ta.DeleteJSON("/path", nil, true) })992 checkFatal(func() { ta.NewXMLRequest("ZIP", "/path", nil, true) })993 checkFatal(func() { ta.PostXML("/path", nil, true) })994 checkFatal(func() { ta.PutXML("/path", nil, true) })995 checkFatal(func() { ta.PatchXML("/path", nil, true) })996 checkFatal(func() { ta.DeleteXML("/path", nil, true) })997 })998}999func TestWith(t *testing.T) {1000 mux := server()1001 ta := tdhttp.NewTestAPI(tdutil.NewT("test1"), mux)1002 td.CmpFalse(t, ta.Head("/any").CmpStatus(200).Failed())1003 nt := tdutil.NewT("test2")1004 nta := ta.With(nt)1005 td.Cmp(t, nta.T(), td.Not(td.Shallow(ta.T())))1006 td.CmpTrue(t, nta.CmpStatus(200).Failed()) // as no request sent yet1007 td.CmpContains(t, nt.LogBuf(),1008 "A request must be sent before testing status, header, body or full response")1009 td.CmpFalse(t, ta.CmpStatus(200).Failed()) // request already sent, so OK1010 nt = tdutil.NewT("test3")1011 nta = ta.With(nt)1012 td.CmpTrue(t, nta.Head("/any").1013 CmpStatus(400).1014 OrDumpResponse().1015 Failed())1016 td.CmpContains(t, nt.LogBuf(), "Response.Status: values differ")1017 td.CmpContains(t, nt.LogBuf(), "X-Testdeep-Method: HEAD") // Header dumped1018}1019func TestOr(t *testing.T) {1020 mux := server()1021 t.Run("Success", func(t *testing.T) {1022 var orCalled bool1023 for i, fn := range []any{1024 func(body string) { orCalled = true },1025 func(t *td.T, body string) { orCalled = true },1026 func(body []byte) { orCalled = true },1027 func(t *td.T, body []byte) { orCalled = true },1028 func(t *td.T, r *httptest.ResponseRecorder) { orCalled = true },1029 } {1030 orCalled = false1031 // As CmpStatus succeeds, Or function is not called1032 td.CmpFalse(t,1033 tdhttp.NewTestAPI(tdutil.NewT("test"), mux).1034 Head("/any").1035 CmpStatus(200).1036 Or(fn).1037 Failed(),1038 "Not failed #%d", i)1039 td.CmpFalse(t, orCalled, "called #%d", i)1040 }1041 })1042 t.Run("No request sent", func(t *testing.T) {1043 var ok, orCalled bool1044 for i, fn := range []any{1045 func(body string) { orCalled = true; ok = body == "" },1046 func(t *td.T, body string) { orCalled = true; ok = t != nil && body == "" },1047 func(body []byte) { orCalled = true; ok = body == nil },1048 func(t *td.T, body []byte) { orCalled = true; ok = t != nil && body == nil },1049 func(t *td.T, r *httptest.ResponseRecorder) { orCalled = true; ok = t != nil && r == nil },1050 } {1051 orCalled, ok = false, false1052 // Check status without sending a request → fail1053 td.CmpTrue(t,1054 tdhttp.NewTestAPI(tdutil.NewT("test"), mux).1055 CmpStatus(123).1056 Or(fn).1057 Failed(),1058 "Failed #%d", i)1059 td.CmpTrue(t, orCalled, "called #%d", i)1060 td.CmpTrue(t, ok, "OK #%d", i)1061 }1062 })1063 t.Run("Empty bodies", func(t *testing.T) {1064 var ok, orCalled bool1065 for i, fn := range []any{1066 func(body string) { orCalled = true; ok = body == "" },1067 func(t *td.T, body string) { orCalled = true; ok = t != nil && body == "" },1068 func(body []byte) { orCalled = true; ok = body == nil },1069 func(t *td.T, body []byte) { orCalled = true; ok = t != nil && body == nil },1070 func(t *td.T, r *httptest.ResponseRecorder) {1071 orCalled = true1072 ok = t != nil && r != nil && r.Body.Len() == 01073 },1074 } {1075 orCalled, ok = false, false1076 // HEAD /any = no body + CmpStatus fails1077 td.CmpTrue(t,1078 tdhttp.NewTestAPI(tdutil.NewT("test"), mux).1079 Head("/any").1080 CmpStatus(123).1081 Or(fn).1082 Failed(),1083 "Failed #%d", i)1084 td.CmpTrue(t, orCalled, "called #%d", i)1085 td.CmpTrue(t, ok, "OK #%d", i)1086 }1087 })1088 t.Run("Body", func(t *testing.T) {1089 var ok, orCalled bool1090 for i, fn := range []any{1091 func(body string) { orCalled = true; ok = body == "GET!" },1092 func(t *td.T, body string) { orCalled = true; ok = t != nil && body == "GET!" },1093 func(body []byte) { orCalled = true; ok = string(body) == "GET!" },1094 func(t *td.T, body []byte) { orCalled = true; ok = t != nil && string(body) == "GET!" },1095 func(t *td.T, r *httptest.ResponseRecorder) {1096 orCalled = true1097 ok = t != nil && r != nil && r.Body.String() == "GET!"1098 },1099 } {1100 orCalled, ok = false, false1101 // GET /any = "GET!" body + CmpStatus fails1102 td.CmpTrue(t,1103 tdhttp.NewTestAPI(tdutil.NewT("test"), mux).1104 Get("/any").1105 CmpStatus(123).1106 Or(fn).1107 Failed(),1108 "Failed #%d", i)1109 td.CmpTrue(t, orCalled, "called #%d", i)1110 td.CmpTrue(t, ok, "OK #%d", i)1111 }1112 })1113 tt := tdutil.NewT("test")1114 ta := tdhttp.NewTestAPI(tt, mux)1115 if td.CmpTrue(t, tt.CatchFailNow(func() { ta.Or(123) })) {1116 td.CmpContains(t, tt.LogBuf(),1117 "usage: Or(func([*td.T,]string) | func([*td.T,][]byte) | func(*td.T,*httptest.ResponseRecorder)), but received int as 1st parameter")1118 }1119}1120func TestRun(t *testing.T) {1121 mux := server()1122 ta := tdhttp.NewTestAPI(tdutil.NewT("test"), mux)1123 ok := ta.Run("Test", func(ta *tdhttp.TestAPI) {1124 td.CmpFalse(t, ta.Get("/any").CmpStatus(200).Failed())1125 })1126 td.CmpTrue(t, ok)1127 ok = ta.Run("Test", func(ta *tdhttp.TestAPI) {1128 td.CmpTrue(t, ta.Get("/any").CmpStatus(123).Failed())1129 })1130 td.CmpFalse(t, ok)1131}...

Full Screen

Full Screen

t_anchor_test.go

Source:t_anchor_test.go Github

copy

Full Screen

...36 Str: "Pipo bingo",37 Time: timeParse(tt, "2019-01-02T11:22:33.123456Z"),38 }39 // Using T.Anchor()40 td.CmpTrue(tt,41 t.Cmp(got, MyStruct{42 PNum: t.Anchor(td.Ptr(td.Between(40, 45))).(*int),43 Num: t.Anchor(td.Between(int64(135), int64(137))).(int64),44 Str: t.Anchor(td.HasPrefix("Pipo"), "").(string),45 Time: t.Anchor(td.TruncTime(timeParse(tt, "2019-01-02T11:22:00Z"), time.Minute)).(time.Time),46 }))47 // Using T.A()48 td.CmpTrue(tt,49 t.Cmp(got, MyStruct{50 PNum: t.A(td.Ptr(td.Between(40, 45))).(*int),51 Num: t.A(td.Between(int64(135), int64(137))).(int64),52 Str: t.A(td.HasPrefix("Pipo"), "").(string),53 Time: t.A(td.TruncTime(timeParse(tt, "2019-01-02T11:22:00Z"), time.Minute)).(time.Time),54 }))55 // Testing persistence56 got = MyStruct{Num: 136}57 tt.Run("without persistence", func(tt *testing.T) {58 numOp := t.Anchor(td.Between(int64(135), int64(137))).(int64)59 td.CmpTrue(tt, t.Cmp(got, MyStruct{Num: numOp}))60 td.CmpFalse(tt, t.Cmp(got, MyStruct{Num: numOp}))61 })62 tt.Run("with persistence", func(tt *testing.T) {63 numOp := t.Anchor(td.Between(int64(135), int64(137))).(int64)64 defer t.AnchorsPersistTemporarily()()65 td.CmpTrue(tt, t.Cmp(got, MyStruct{Num: numOp}))66 td.CmpTrue(tt, t.Cmp(got, MyStruct{Num: numOp}))67 t.ResetAnchors() // force reset anchored operators68 td.CmpFalse(tt, t.Cmp(got, MyStruct{Num: numOp}))69 })70 // Errors71 tt.Run("errors", func(tt *testing.T) {72 td.Cmp(tt, ttt.CatchFatal(func() { t.Anchor(nil) }),73 "Cannot anchor a nil TestDeep operator")74 td.Cmp(tt, ttt.CatchFatal(func() { t.Anchor(td.Ignore(), 1, 2) }),75 "usage: Anchor(OPERATOR[, MODEL]), too many parameters")76 td.Cmp(tt, ttt.CatchFatal(func() { t.Anchor(td.Ignore(), nil) }),77 "Untyped nil value is not valid as model for an anchor")78 td.Cmp(tt, ttt.CatchFatal(func() { t.Anchor(td.Between(1, 2), 12.3) }),79 "Operator Between TypeBehind() returned int which differs from model type float64. Omit model or ensure its type is int")80 td.Cmp(tt, ttt.CatchFatal(func() { t.Anchor(td.Ignore()) }),81 "Cannot anchor operator Ignore as TypeBehind() returned nil. Use model parameter to specify the type to return")82 })83}84type privStruct struct {85 num int6486}87func (p privStruct) Num() int64 {88 return p.num89}90func TestAddAnchorableStructType(tt *testing.T) {91 type MyStruct struct {92 Priv privStruct93 }94 ttt := test.NewTestingTB(tt.Name())95 t := td.NewT(ttt)96 // We want to anchor this operator97 op := td.Smuggle((privStruct).Num, int64(42))98 // Without making privStruct anchorable, it does not work99 td.Cmp(tt, ttt.CatchFatal(func() { t.A(op, privStruct{}) }),100 "td_test.privStruct struct type is not supported as an anchor. Try AddAnchorableStructType")101 // Make privStruct anchorable102 td.AddAnchorableStructType(func(nextAnchor int) privStruct {103 return privStruct{num: int64(2e9 - nextAnchor)}104 })105 td.CmpTrue(tt,106 t.Cmp(MyStruct{Priv: privStruct{num: 42}},107 MyStruct{108 Priv: t.A(op, privStruct{}).(privStruct), // ← now it works109 }))110 // Error111 test.CheckPanic(tt,112 func() { td.AddAnchorableStructType(123) },113 "usage: AddAnchorableStructType(func (nextAnchor int) STRUCT_TYPE)")114}115func TestAnchorsPersist(tt *testing.T) {116 ttt := test.NewTestingTB(tt.Name())117 t1 := td.NewT(ttt)118 t2 := td.NewT(ttt)119 t3 := td.NewT(t1)120 tt.Run("without anchors persistence", func(tt *testing.T) {121 // Anchors persistence is shared for a same testing.TB122 td.CmpFalse(tt, t1.DoAnchorsPersist())123 td.CmpFalse(tt, t2.DoAnchorsPersist())124 td.CmpFalse(tt, t3.DoAnchorsPersist())125 func() {126 defer t1.AnchorsPersistTemporarily()()127 td.CmpTrue(tt, t1.DoAnchorsPersist())128 td.CmpTrue(tt, t2.DoAnchorsPersist())129 td.CmpTrue(tt, t3.DoAnchorsPersist())130 }()131 td.CmpFalse(tt, t1.DoAnchorsPersist())132 td.CmpFalse(tt, t2.DoAnchorsPersist())133 td.CmpFalse(tt, t3.DoAnchorsPersist())134 })135 tt.Run("with anchors persistence", func(tt *testing.T) {136 t3.SetAnchorsPersist(true)137 td.CmpTrue(tt, t1.DoAnchorsPersist())138 td.CmpTrue(tt, t2.DoAnchorsPersist())139 td.CmpTrue(tt, t3.DoAnchorsPersist())140 func() {141 defer t1.AnchorsPersistTemporarily()()142 td.CmpTrue(tt, t1.DoAnchorsPersist())143 td.CmpTrue(tt, t2.DoAnchorsPersist())144 td.CmpTrue(tt, t3.DoAnchorsPersist())145 }()146 td.CmpTrue(tt, t1.DoAnchorsPersist())147 td.CmpTrue(tt, t2.DoAnchorsPersist())148 td.CmpTrue(tt, t3.DoAnchorsPersist())149 })150}...

Full Screen

Full Screen

combatantinfo_test.go

Source:combatantinfo_test.go Github

copy

Full Screen

...28 found = true29 break30 }31 }32 td.CmpTrue(t, found, "found meta_not_activated")33}34func TestCombatantInfo_NoEnchant(t *testing.T) {35 ev := testutils.LoadJSONData(t, "testdata/combatantinfo_no_enchant.json")36 analysis := &models.Analysis{37 Data: make(map[int64]*models.PlayerAnalysis),38 }39 analysis.SetPlayerAnalysis(12, &models.PlayerAnalysis{40 Fights: make(map[string]*models.FightAnalysis),41 })42 pa := analysis.GetPlayerAnalysis(12)43 pa.SetFight(&models.FightAnalysis{Name: "test"})44 fa := pa.GetFight("test")45 err := events.Process(context.TODO(), ev, analysis, "test")46 td.CmpNoError(t, err)47 found := false48 td.CmpLen(t, fa.Remarks, td.Gt(0))49 for _, r := range fa.Remarks {50 if r.Type == remark.Type_NoEnchant {51 found = true52 td.Cmp(t, r.Slot, "Tête")53 break54 }55 }56 td.CmpTrue(t, found, "found meta_not_activated")57}58func TestCombatantInfo_MissingGems(t *testing.T) {59 ev := testutils.LoadJSONData(t, "testdata/combatantinfo_missing_gems.json")60 analysis := &models.Analysis{61 Data: make(map[int64]*models.PlayerAnalysis),62 }63 analysis.SetPlayerAnalysis(4, &models.PlayerAnalysis{64 Fights: make(map[string]*models.FightAnalysis),65 })66 pa := analysis.GetPlayerAnalysis(4)67 pa.SetFight(&models.FightAnalysis{Name: "test"})68 fa := pa.GetFight("test")69 err := events.Process(context.TODO(), ev, analysis, "test")70 td.CmpNoError(t, err)71 found := false72 td.CmpLen(t, fa.Remarks, td.Gt(0))73 for _, r := range fa.Remarks {74 if r.Type == remark.Type_MissingGems {75 found = true76 td.Cmp(t, r.ItemWowheadAttr, "domain=fr.tbc&gems=28461&item=28193")77 break78 }79 }80 td.CmpTrue(t, found, "found missing_gems")81}82func TestCombatantInfo_InvalidGem(t *testing.T) {83 ev := testutils.LoadJSONData(t, "testdata/combatantinfo_invalid_gem.json")84 analysis := &models.Analysis{85 Data: make(map[int64]*models.PlayerAnalysis),86 }87 analysis.SetPlayerAnalysis(10, &models.PlayerAnalysis{88 Fights: make(map[string]*models.FightAnalysis),89 })90 pa := analysis.GetPlayerAnalysis(10)91 pa.SetFight(&models.FightAnalysis{Name: "test"})92 fa := pa.GetFight("test")93 err := events.Process(context.TODO(), ev, analysis, "test")94 td.CmpNoError(t, err)95 found := false96 td.CmpLen(t, fa.Remarks, td.Gt(0))97 for _, r := range fa.Remarks {98 if r.Type == remark.Type_InvalidGem {99 found = true100 td.Cmp(t, r.ItemWowheadAttr, "domain=fr.tbc&ench=2657&gems=23095%3A24058&item=28608")101 break102 }103 }104 td.CmpTrue(t, found, "found invalid_gem")105}106func TestCombatantInfo_ComplexRestriction(t *testing.T) {107 ev := testutils.LoadJSONData(t, "testdata/combatantinfo_warlock_leotheras.json")108 analysis := &models.Analysis{109 Data: make(map[int64]*models.PlayerAnalysis),110 }111 analysis.SetPlayerAnalysis(26, &models.PlayerAnalysis{112 Fights: make(map[string]*models.FightAnalysis),113 })114 pa := analysis.GetPlayerAnalysis(26)115 pa.SetFight(&models.FightAnalysis{Name: "Leotheras the Blind"})116 fa := pa.GetFight("Leotheras the Blind")117 err := events.Process(context.TODO(), ev, analysis, "Leotheras the Blind")118 td.CmpNoError(t, err)119 for _, r := range fa.Remarks {120 td.CmpNot(t, r.Type, remark.Type_InvalidEnchant, "no invalid_enchant remark found")121 }122}123func TestCombatantInfo_ComplexRestrictionShouldTrigger(t *testing.T) {124 ev := testutils.LoadJSONData(t, "testdata/combatantinfo_warlock_leotheras.json")125 analysis := &models.Analysis{126 Data: make(map[int64]*models.PlayerAnalysis),127 }128 analysis.SetPlayerAnalysis(26, &models.PlayerAnalysis{129 Fights: make(map[string]*models.FightAnalysis),130 })131 pa := analysis.GetPlayerAnalysis(26)132 pa.SetFight(&models.FightAnalysis{Name: "should trigger"})133 fa := pa.GetFight("should trigger")134 err := events.Process(context.TODO(), ev, analysis, "should trigger")135 td.CmpNoError(t, err)136 found := false137 td.CmpLen(t, fa.Remarks, td.Gt(0))138 for _, r := range fa.Remarks {139 if r.Type == remark.Type_InvalidEnchant && r.Slot == "Mains" {140 found = true141 td.Cmp(t, r.ItemWowheadAttr, "domain=fr.tbc&ench=2613&item=30764")142 break143 }144 }145 td.CmpTrue(t, found, "found invalid_enchant")146}...

Full Screen

Full Screen

CmpTrue

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 dmp := diffmatchpatch.New()4 diffs := dmp.DiffMain("Hello World", "Hello World!", false)5 fmt.Println(dmp.DiffPrettyText(diffs))6}

Full Screen

Full Screen

CmpTrue

Using AI Code Generation

copy

Full Screen

1import (2func TestCmpTrue(t *testing.T) {3 assert := assert.New(t)4 assert.True(result, "Result is true")5}6import (7func TestCmpFalse(t *testing.T) {8 assert := assert.New(t)9 assert.False(result, "Result is false")10}11import (12func TestCmpNil(t *testing.T) {13 assert := assert.New(t)14 assert.Nil(result, "Result is nil")15}16import (17func TestCmpNotNil(t *testing.T) {18 assert := assert.New(t)19 result = new(int)20 assert.NotNil(result, "Result is not nil")21}22import (23func TestCmpEmpty(t *testing.T) {24 assert := assert.New(t)25 assert.Empty(result, "Result is empty")26}27import (28func TestCmpNotEmpty(t *testing.T) {29 assert := assert.New(t)30 assert.NotEmpty(result, "Result is not empty")31}32import (33func TestCmpContains(t *testing.T) {34 assert := assert.New(t)35 assert.Contains(result, "ell", "Result contains ell")36}

Full Screen

Full Screen

CmpTrue

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 dmp := diffmatchpatch.New()4 diffs := dmp.DiffMain("Hello World", "Hello Go", false)5 patch := dmp.PatchMake(diffs)6 text1_p, _ := dmp.PatchApply(patch, "Hello World")7 fmt.Println(text1_p)8}

Full Screen

Full Screen

CmpTrue

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 fmt.Println("Enter two numbers a and b")4 fmt.Scanf("%d%d", &a, &b)5 res := td.CmpTrue(a, b)6 fmt.Println("Result is: ", res)7}8import (9func main() {10 fmt.Println("Enter two numbers a and b")11 fmt.Scanf("%d%d", &a, &b)12 res := td.CmpFalse(a, b)13 fmt.Println("Result is: ", res)14}15import (16func main() {17 fmt.Println("Enter two numbers a and b")18 fmt.Scanf("%d%d", &a, &b)19 res := td.CmpNotEqual(a, b)20 fmt.Println("Result is: ", res)21}22import (23func main() {24 fmt.Println("Enter two numbers a and b")25 fmt.Scanf("%d%d", &a, &b)26 res := td.CmpLess(a, b)27 fmt.Println("Result is: ", res)28}29import (30func main() {31 fmt.Println("Enter two numbers a and b")32 fmt.Scanf("%d%d", &a, &b)33 res := td.CmpLessEqual(a, b)34 fmt.Println("Result is: ", res)35}36import (37func main() {38 fmt.Println("Enter two numbers a and b")39 fmt.Scanf("%d%d", &a, &b)40 res := td.CmpGreater(a, b)41 fmt.Println("Result is: ", res)42}

Full Screen

Full Screen

CmpTrue

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 var x interface{} = 14 var y interface{} = 25 var z interface{} = 36 var a interface{} = 17 var b interface{} = 28 var c interface{} = 39 var d interface{} = 110 var e interface{} = 211 var f interface{} = 312 catch.Try(func() {13 catch.CmpTrue(x == y, "x should not equal y")14 catch.CmpTrue(y == z, "y should not equal z")15 catch.CmpTrue(z == a, "z should not equal a")16 catch.CmpTrue(a == b, "a should not equal b")17 catch.CmpTrue(b == c, "b should not equal c")18 catch.CmpTrue(c == d, "c should not equal d")19 catch.CmpTrue(d == e, "d should not equal e")20 catch.CmpTrue(e == f, "e should not equal f")21 }).Catch(func(e catch.Exception) {22 fmt.Println(e.Message())23 })24}

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