Best Go-testdeep code snippet using td_test.Num
td_struct_test.go
Source:td_struct_test.go
...515 })516}517func TestStructPatterns(t *testing.T) {518 type paAnon struct {519 alphaNum int520 betaNum int521 }522 type paTest struct {523 paAnon524 Num int525 }526 got := paTest{527 paAnon: paAnon{528 alphaNum: 1000,529 betaNum: 2000,530 },531 Num: 666,532 }533 t.Run("Shell pattern", func(t *testing.T) {534 checkOK(t, got,535 td.Struct(paTest{Num: 666},536 td.StructFields{537 "=*Num": td.Gte(1000), // matches alphaNum & betaNum538 }))539 checkOK(t, got,540 td.Struct(paTest{Num: 666},541 td.StructFields{542 "=a*Num": td.Lt(0), // no remaining fields to match543 "=*": td.Gte(1000), // first, matches alphaNum & betaNum544 "=b*Num": td.Lt(0), // no remaining fields to match545 }),546 "Default sorting uses patterns")547 checkOK(t, got,548 td.Struct(paTest{Num: 666},549 td.StructFields{550 "1 = a*Num": td.Between(999, 1001), // matches alphaNum551 "2 = *": td.Gte(2000), // matches betaNum552 "3 = b*Num": td.Gt(3000), // no remaining fields to match553 }),554 "Explicitly sorted")555 checkOK(t, got,556 td.Struct(paTest{Num: 666},557 td.StructFields{558 "1 ! beta*": 1000, // matches alphaNum559 "2 = *": 2000, // matches betaNum560 }),561 "negative shell pattern")562 checkError(t, "never tested",563 td.Struct(paTest{Num: 666}, td.StructFields{"= al[pha": 123}),564 expectedError{565 Message: mustBe("bad usage of Struct operator"),566 Path: mustBe("DATA"),567 Summary: mustContain("bad shell pattern field `= al[pha`: "),568 })569 checkError(t, "never tested",570 td.Struct(paTest{Num: 666}, td.StructFields{"= alpha*": nil}), expectedError{571 Message: mustBe("bad usage of Struct operator"),572 Path: mustBe("DATA"),573 Summary: mustBe("expected value of field alphaNum (from pattern `= alpha*`) cannot be nil as it is a int"),574 })575 })576 t.Run("Regexp", func(t *testing.T) {577 checkOK(t, got,578 td.Struct(paTest{Num: 666},579 td.StructFields{580 "=~Num$": td.Gte(1000), // matches alphaNum & betaNum581 }))582 checkOK(t, got,583 td.Struct(paTest{Num: 666},584 td.StructFields{585 "=~^a.*Num$": td.Lt(0), // no remaining fields to match586 "=~.": td.Gte(1000), // first, matches alphaNum & betaNum587 "=~^b.*Num$": td.Lt(0), // no remaining fields to match588 }),589 "Default sorting uses patterns")590 checkOK(t, got,591 td.Struct(paTest{Num: 666},592 td.StructFields{593 "1 =~ ^a.*Num$": td.Between(999, 1001), // matches alphaNum594 "2 =~ .": td.Gte(2000), // matches betaNum595 "3 =~ ^b.*Num$": td.Gt(3000), // no remaining fields to match596 }),597 "Explicitly sorted")598 checkOK(t, got,599 td.Struct(paTest{Num: 666},600 td.StructFields{601 "1 !~ ^beta": 1000, // matches alphaNum602 "2 =~ .": 2000, // matches betaNum603 }),604 "negative regexp")605 checkError(t, "never tested",606 td.Struct(paTest{Num: 666}, td.StructFields{"=~ al(*": 123}),607 expectedError{608 Message: mustBe("bad usage of Struct operator"),609 Path: mustBe("DATA"),610 Summary: mustContain("bad regexp field `=~ al(*`: "),611 })612 checkError(t, "never tested",613 td.Struct(paTest{Num: 666}, td.StructFields{"=~ alpha": nil}),614 expectedError{615 Message: mustBe("bad usage of Struct operator"),616 Path: mustBe("DATA"),617 Summary: mustBe("expected value of field alphaNum (from pattern `=~ alpha`) cannot be nil as it is a int"),618 })619 })620}621func TestStructTypeBehind(t *testing.T) {622 equalTypes(t, td.Struct(MyStruct{}, nil), MyStruct{})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}...
td_between_test.go
Source:td_between_test.go
1// Copyright (c) 2018-2022, Maxime Soulé2// All rights reserved.3//4// This source code is licensed under the BSD-style license found in the5// LICENSE file in the root directory of this source tree.6package td_test7import (8 "fmt"9 "math"10 "testing"11 "time"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{},698 compareType(0),699 lessType(0),700 } {701 equalTypes(t, td.Between(typ, typ), typ)702 equalTypes(t, td.Gt(typ), typ)703 equalTypes(t, td.Gte(typ), typ)704 equalTypes(t, td.Lt(typ), typ)705 equalTypes(t, td.Lte(typ), typ)706 }707 equalTypes(t, td.N(int64(23), int64(5)), int64(0))708 // Erroneous op709 equalTypes(t, td.Between("test", 12), nil)710 equalTypes(t, td.N(10, 1, 2), nil)711 equalTypes(t, td.Gt([]byte("test")), nil)712 equalTypes(t, td.Gte([]byte("test")), nil)713 equalTypes(t, td.Lt([]byte("test")), nil)714 equalTypes(t, td.Lte([]byte("test")), nil)715}...
td_map_test.go
Source:td_map_test.go
1// Copyright (c) 2018, Maxime Soulé2// All rights reserved.3//4// This source code is licensed under the BSD-style license found in the5// LICENSE file in the root directory of this source tree.6package td_test7import (8 "testing"9 "github.com/maxatome/go-testdeep/internal/test"10 "github.com/maxatome/go-testdeep/td"11)12func TestMap(t *testing.T) {13 type MyMap map[string]int14 //15 // Map16 checkOK(t, (map[string]int)(nil), td.Map(map[string]int{}, nil))17 checkError(t, nil, td.Map(map[string]int{}, nil),18 expectedError{19 Message: mustBe("values differ"),20 Path: mustBe(`DATA`),21 Got: mustBe("nil"),22 Expected: mustBe("map[string]int{}"),23 })24 gotMap := map[string]int{"foo": 1, "bar": 2}25 checkOK(t, gotMap, td.Map(map[string]int{"foo": 1, "bar": 2}, nil))26 checkOK(t, gotMap,27 td.Map(map[string]int{"foo": 1}, td.MapEntries{"bar": 2}))28 checkOK(t, gotMap,29 td.Map(map[string]int{}, td.MapEntries{"foo": 1, "bar": 2}))30 checkOK(t, gotMap,31 td.Map((map[string]int)(nil), td.MapEntries{"foo": 1, "bar": 2}))32 one := 133 checkOK(t, map[string]*int{"foo": nil, "bar": &one},34 td.Map(map[string]*int{}, td.MapEntries{"foo": nil, "bar": &one}))35 checkError(t, gotMap, td.Map(map[string]int{"foo": 1, "bar": 3}, nil),36 expectedError{37 Message: mustBe("values differ"),38 Path: mustBe(`DATA["bar"]`),39 Got: mustBe("2"),40 Expected: mustBe("3"),41 })42 checkError(t, gotMap, td.Map(map[string]int{}, nil),43 expectedError{44 Message: mustBe("comparing hash keys of %%"),45 Path: mustBe("DATA"),46 Summary: mustMatch(`^Extra 2 keys: \("bar",\s+"foo"\)\z`),47 })48 checkError(t, gotMap, td.Map(map[string]int{"test": 2}, nil),49 expectedError{50 Message: mustBe("comparing hash keys of %%"),51 Path: mustBe("DATA"),52 Summary: mustMatch(53 `^ Missing key: \("test"\)\nExtra 2 keys: \("bar",\s+"foo"\)\z`),54 })55 checkError(t, gotMap,56 td.Map(map[string]int{}, td.MapEntries{"test": 2}),57 expectedError{58 Message: mustBe("comparing hash keys of %%"),59 Path: mustBe("DATA"),60 Summary: mustMatch(61 `^ Missing key: \("test"\)\nExtra 2 keys: \("bar",\s+"foo"\)\z`),62 })63 checkError(t, gotMap,64 td.Map(map[string]int{}, td.MapEntries{"foo": 1, "bar": 2, "test": 2}),65 expectedError{66 Message: mustBe("comparing hash keys of %%"),67 Path: mustBe("DATA"),68 Summary: mustBe(`Missing key: ("test")`),69 })70 checkError(t, gotMap,71 td.Map(MyMap{}, td.MapEntries{"foo": 1, "bar": 2}),72 expectedError{73 Message: mustBe("type mismatch"),74 Path: mustBe("DATA"),75 Got: mustBe("map[string]int"),76 Expected: mustBe("td_test.MyMap"),77 })78 //79 // Map type80 gotTypedMap := MyMap{"foo": 1, "bar": 2}81 checkOK(t, gotTypedMap, td.Map(MyMap{"foo": 1, "bar": 2}, nil))82 checkOK(t, gotTypedMap,83 td.Map(MyMap{"foo": 1}, td.MapEntries{"bar": 2}))84 checkOK(t, gotTypedMap,85 td.Map(MyMap{}, td.MapEntries{"foo": 1, "bar": 2}))86 checkOK(t, gotTypedMap,87 td.Map((MyMap)(nil), td.MapEntries{"foo": 1, "bar": 2}))88 checkOK(t, &gotTypedMap, td.Map(&MyMap{"foo": 1, "bar": 2}, nil))89 checkOK(t, &gotTypedMap,90 td.Map(&MyMap{"foo": 1}, td.MapEntries{"bar": 2}))91 checkOK(t, &gotTypedMap,92 td.Map(&MyMap{}, td.MapEntries{"foo": 1, "bar": 2}))93 checkOK(t, &gotTypedMap,94 td.Map((*MyMap)(nil), td.MapEntries{"foo": 1, "bar": 2}))95 checkError(t, gotTypedMap, td.Map(MyMap{"foo": 1, "bar": 3}, nil),96 expectedError{97 Message: mustBe("values differ"),98 Path: mustBe(`DATA["bar"]`),99 Got: mustBe("2"),100 Expected: mustBe("3"),101 })102 checkError(t, gotTypedMap, td.Map(MyMap{}, nil),103 expectedError{104 Message: mustBe("comparing hash keys of %%"),105 Path: mustBe("DATA"),106 Summary: mustMatch(`^Extra 2 keys: \("bar",\s+"foo"\)\z`),107 })108 checkError(t, gotTypedMap, td.Map(MyMap{"test": 2}, nil),109 expectedError{110 Message: mustBe("comparing hash keys of %%"),111 Path: mustBe("DATA"),112 Summary: mustMatch(113 `^ Missing key: \("test"\)\nExtra 2 keys: \("bar",\s+"foo"\)\z`),114 })115 checkError(t, gotTypedMap, td.Map(MyMap{}, td.MapEntries{"test": 2}),116 expectedError{117 Message: mustBe("comparing hash keys of %%"),118 Path: mustBe("DATA"),119 Summary: mustMatch(120 `^ Missing key: \("test"\)\nExtra 2 keys: \("bar",\s+"foo"\)\z`),121 })122 checkError(t, gotTypedMap,123 td.Map(MyMap{}, td.MapEntries{"foo": 1, "bar": 2, "test": 2}),124 expectedError{125 Message: mustBe("comparing hash keys of %%"),126 Path: mustBe("DATA"),127 Summary: mustBe(`Missing key: ("test")`),128 })129 checkError(t, &gotTypedMap, td.Map(&MyMap{"foo": 1, "bar": 3}, nil),130 expectedError{131 Message: mustBe("values differ"),132 Path: mustBe(`DATA["bar"]`),133 Got: mustBe("2"),134 Expected: mustBe("3"),135 })136 checkError(t, &gotTypedMap, td.Map(&MyMap{}, nil),137 expectedError{138 Message: mustBe("comparing hash keys of %%"),139 Path: mustBe("DATA"),140 Summary: mustMatch(`^Extra 2 keys: \("bar",\s+"foo"\)\z`),141 })142 checkError(t, &gotTypedMap, td.Map(&MyMap{"test": 2}, nil),143 expectedError{144 Message: mustBe("comparing hash keys of %%"),145 Path: mustBe("DATA"),146 Summary: mustMatch(147 `^ Missing key: \("test"\)\nExtra 2 keys: \("bar",\s+"foo"\)\z`),148 })149 checkError(t, &gotTypedMap, td.Map(&MyMap{}, td.MapEntries{"test": 2}),150 expectedError{151 Message: mustBe("comparing hash keys of %%"),152 Path: mustBe("DATA"),153 Summary: mustMatch(154 `^ Missing key: \("test"\)\nExtra 2 keys: \("bar",\s+"foo"\)\z`),155 })156 checkError(t, &gotTypedMap,157 td.Map(&MyMap{}, td.MapEntries{"foo": 1, "bar": 2, "test": 2}),158 expectedError{159 Message: mustBe("comparing hash keys of %%"),160 Path: mustBe("DATA"),161 Summary: mustBe(`Missing key: ("test")`),162 })163 checkError(t, &gotMap, td.Map(&MyMap{}, nil),164 expectedError{165 Message: mustBe("type mismatch"),166 Path: mustBe("DATA"),167 Got: mustBe("*map[string]int"),168 Expected: mustBe("*td_test.MyMap"),169 })170 checkError(t, gotMap, td.Map(&MyMap{}, nil),171 expectedError{172 Message: mustBe("type mismatch"),173 Path: mustBe("DATA"),174 Got: mustBe("map[string]int"),175 Expected: mustBe("*td_test.MyMap"),176 })177 checkError(t, nil, td.Map(&MyMap{}, nil),178 expectedError{179 Message: mustBe("values differ"),180 Path: mustBe("DATA"),181 Got: mustBe("nil"),182 Expected: mustBe("*td_test.MyMap{}"),183 })184 checkError(t, nil, td.Map(MyMap{}, nil),185 expectedError{186 Message: mustBe("values differ"),187 Path: mustBe("DATA"),188 Got: mustBe("nil"),189 Expected: mustBe("td_test.MyMap{}"),190 })191 //192 // nil cases193 var (194 gotNilMap map[string]int195 gotNilTypedMap MyMap196 )197 checkOK(t, gotNilMap, td.Map(map[string]int{}, nil))198 checkOK(t, gotNilTypedMap, td.Map(MyMap{}, nil))199 checkOK(t, &gotNilTypedMap, td.Map(&MyMap{}, nil))200 // Be lax...201 // Without Lax â error202 checkError(t, MyMap{}, td.Map(map[string]int{}, nil),203 expectedError{204 Message: mustBe("type mismatch"),205 })206 checkError(t, map[string]int{}, td.Map(MyMap{}, nil),207 expectedError{208 Message: mustBe("type mismatch"),209 })210 // With Lax â OK211 checkOK(t, MyMap{}, td.Lax(td.Map(map[string]int{}, nil)))212 checkOK(t, map[string]int{}, td.Lax(td.Map(MyMap{}, nil)))213 //214 // SuperMapOf215 checkOK(t, gotMap, td.SuperMapOf(map[string]int{"foo": 1}, nil))216 checkOK(t, gotMap,217 td.SuperMapOf(map[string]int{"foo": 1}, td.MapEntries{"bar": 2}))218 checkOK(t, gotMap,219 td.SuperMapOf(map[string]int{}, td.MapEntries{"foo": 1, "bar": 2}))220 checkError(t, gotMap,221 td.SuperMapOf(map[string]int{"foo": 1, "bar": 3}, nil),222 expectedError{223 Message: mustBe("values differ"),224 Path: mustBe(`DATA["bar"]`),225 Got: mustBe("2"),226 Expected: mustBe("3"),227 })228 checkError(t, gotMap, td.SuperMapOf(map[string]int{"test": 2}, nil),229 expectedError{230 Message: mustBe("comparing hash keys of %%"),231 Path: mustBe("DATA"),232 Summary: mustBe(`Missing key: ("test")`),233 })234 checkError(t, gotMap,235 td.SuperMapOf(map[string]int{}, td.MapEntries{"test": 2}),236 expectedError{237 Message: mustBe("comparing hash keys of %%"),238 Path: mustBe("DATA"),239 Summary: mustBe(`Missing key: ("test")`),240 })241 checkOK(t, gotNilMap, td.SuperMapOf(map[string]int{}, nil))242 checkOK(t, gotNilTypedMap, td.SuperMapOf(MyMap{}, nil))243 checkOK(t, &gotNilTypedMap, td.SuperMapOf(&MyMap{}, nil))244 //245 // SubMapOf246 checkOK(t, gotMap,247 td.SubMapOf(map[string]int{"foo": 1, "bar": 2, "tst": 3}, nil))248 checkOK(t, gotMap,249 td.SubMapOf(map[string]int{"foo": 1, "tst": 3}, td.MapEntries{"bar": 2}))250 checkOK(t, gotMap,251 td.SubMapOf(map[string]int{}, td.MapEntries{"foo": 1, "bar": 2, "tst": 3}))252 checkError(t, gotMap,253 td.SubMapOf(map[string]int{"foo": 1, "bar": 3}, nil),254 expectedError{255 Message: mustBe("values differ"),256 Path: mustBe(`DATA["bar"]`),257 Got: mustBe("2"),258 Expected: mustBe("3"),259 })260 checkError(t, gotMap, td.SubMapOf(map[string]int{"foo": 1}, nil),261 expectedError{262 Message: mustBe("comparing hash keys of %%"),263 Path: mustBe("DATA"),264 Summary: mustBe(`Extra key: ("bar")`),265 })266 checkError(t, gotMap,267 td.SubMapOf(map[string]int{}, td.MapEntries{"foo": 1, "test": 2}),268 expectedError{269 Message: mustBe("comparing hash keys of %%"),270 Path: mustBe("DATA"),271 Summary: mustBe(`Missing key: ("test")272 Extra key: ("bar")`),273 })274 checkOK(t, gotNilMap, td.SubMapOf(map[string]int{"foo": 1}, nil))275 checkOK(t, gotNilTypedMap, td.SubMapOf(MyMap{"foo": 1}, nil))276 checkOK(t, &gotNilTypedMap, td.SubMapOf(&MyMap{"foo": 1}, nil))277 //278 // Bad usage279 checkError(t, "never tested",280 td.Map("test", nil),281 expectedError{282 Message: mustBe("bad usage of Map operator"),283 Path: mustBe("DATA"),284 Summary: mustContain("usage: Map("),285 })286 checkError(t, "never tested",287 td.SuperMapOf("test", nil),288 expectedError{289 Message: mustBe("bad usage of SuperMapOf operator"),290 Path: mustBe("DATA"),291 Summary: mustContain("usage: SuperMapOf("),292 })293 checkError(t, "never tested",294 td.SubMapOf("test", nil),295 expectedError{296 Message: mustBe("bad usage of SubMapOf operator"),297 Path: mustBe("DATA"),298 Summary: mustContain("usage: SubMapOf("),299 })300 num := 12301 checkError(t, "never tested",302 td.Map(&num, nil),303 expectedError{304 Message: mustBe("bad usage of Map operator"),305 Path: mustBe("DATA"),306 Summary: mustContain("usage: Map("),307 })308 checkError(t, "never tested",309 td.SuperMapOf(&num, nil),310 expectedError{311 Message: mustBe("bad usage of SuperMapOf operator"),312 Path: mustBe("DATA"),313 Summary: mustContain("usage: SuperMapOf("),314 })315 checkError(t, "never tested",316 td.SubMapOf(&num, nil),317 expectedError{318 Message: mustBe("bad usage of SubMapOf operator"),319 Path: mustBe("DATA"),320 Summary: mustContain("usage: SubMapOf("),321 })322 checkError(t, "never tested",323 td.Map(&MyMap{}, td.MapEntries{1: 2}),324 expectedError{325 Message: mustBe("bad usage of Map operator"),326 Path: mustBe("DATA"),327 Summary: mustBe("expected key 1 type mismatch: int != model key type (string)"),328 })329 checkError(t, "never tested",330 td.SuperMapOf(&MyMap{}, td.MapEntries{1: 2}),331 expectedError{332 Message: mustBe("bad usage of SuperMapOf operator"),333 Path: mustBe("DATA"),334 Summary: mustBe("expected key 1 type mismatch: int != model key type (string)"),335 })336 checkError(t, "never tested",337 td.SubMapOf(&MyMap{}, td.MapEntries{1: 2}),338 expectedError{339 Message: mustBe("bad usage of SubMapOf operator"),340 Path: mustBe("DATA"),341 Summary: mustBe("expected key 1 type mismatch: int != model key type (string)"),342 })343 checkError(t, "never tested",344 td.Map(&MyMap{}, td.MapEntries{"foo": nil}),345 expectedError{346 Message: mustBe("bad usage of Map operator"),347 Path: mustBe("DATA"),348 Summary: mustBe(`expected key "foo" value cannot be nil as entries value type is int`),349 })350 checkError(t, "never tested",351 td.Map(&MyMap{}, td.MapEntries{"foo": uint16(2)}),352 expectedError{353 Message: mustBe("bad usage of Map operator"),354 Path: mustBe("DATA"),355 Summary: mustBe(`expected key "foo" value type mismatch: uint16 != model key type (int)`),356 })357 checkError(t, "never tested",358 td.Map(&MyMap{"foo": 1}, td.MapEntries{"foo": 1}),359 expectedError{360 Message: mustBe("bad usage of Map operator"),361 Path: mustBe("DATA"),362 Summary: mustBe(`"foo" entry exists in both model & expectedEntries`),363 })364 //365 // String366 test.EqualStr(t, td.Map(MyMap{}, nil).String(),367 "td_test.MyMap{}")368 test.EqualStr(t, td.Map(&MyMap{}, nil).String(),369 "*td_test.MyMap{}")370 test.EqualStr(t, td.Map(&MyMap{"foo": 2}, nil).String(),371 `*td_test.MyMap{372 "foo": 2,373}`)374 test.EqualStr(t, td.SubMapOf(MyMap{}, nil).String(),375 "SubMapOf(td_test.MyMap{})")376 test.EqualStr(t, td.SubMapOf(&MyMap{}, nil).String(),377 "SubMapOf(*td_test.MyMap{})")378 test.EqualStr(t, td.SubMapOf(&MyMap{"foo": 2}, nil).String(),379 `SubMapOf(*td_test.MyMap{380 "foo": 2,381})`)382 test.EqualStr(t, td.SuperMapOf(MyMap{}, nil).String(),383 "SuperMapOf(td_test.MyMap{})")384 test.EqualStr(t, td.SuperMapOf(&MyMap{}, nil).String(),385 "SuperMapOf(*td_test.MyMap{})")386 test.EqualStr(t, td.SuperMapOf(&MyMap{"foo": 2}, nil).String(),387 `SuperMapOf(*td_test.MyMap{388 "foo": 2,389})`)390 // Erroneous op391 test.EqualStr(t, td.Map(12, nil).String(), "Map(<ERROR>)")392 test.EqualStr(t, td.SubMapOf(12, nil).String(), "SubMapOf(<ERROR>)")393 test.EqualStr(t, td.SuperMapOf(12, nil).String(), "SuperMapOf(<ERROR>)")394}395func TestMapTypeBehind(t *testing.T) {396 type MyMap map[string]int397 // Map398 equalTypes(t, td.Map(map[string]int{}, nil), map[string]int{})399 equalTypes(t, td.Map(MyMap{}, nil), MyMap{})400 equalTypes(t, td.Map(&MyMap{}, nil), &MyMap{})401 // SubMap402 equalTypes(t, td.SubMapOf(map[string]int{}, nil), map[string]int{})403 equalTypes(t, td.SubMapOf(MyMap{}, nil), MyMap{})404 equalTypes(t, td.SubMapOf(&MyMap{}, nil), &MyMap{})405 // SuperMap406 equalTypes(t, td.SuperMapOf(map[string]int{}, nil), map[string]int{})407 equalTypes(t, td.SuperMapOf(MyMap{}, nil), MyMap{})408 equalTypes(t, td.SuperMapOf(&MyMap{}, nil), &MyMap{})409 // Erroneous op410 equalTypes(t, td.Map(12, nil), nil)411 equalTypes(t, td.SubMapOf(12, nil), nil)412 equalTypes(t, td.SuperMapOf(12, nil), nil)413}...
Num
Using AI Code Generation
1import "fmt"2func main() {3 fmt.Println(Num(10))4}5import "fmt"6func main() {7 fmt.Println(Num(10))8}9import "fmt"10func main() {11 fmt.Println(Num(10))12}13import "fmt"14func main() {15 fmt.Println(Num(10))16}17import "fmt"18func main() {19 fmt.Println(Num(10))20}21import "fmt"22func main() {23 fmt.Println(Num(10))24}25import "fmt"26func main() {27 fmt.Println(Num(10))28}29import "fmt"30func main() {31 fmt.Println(Num(10))32}33import "fmt"34func main() {35 fmt.Println(Num(10))36}37import "fmt"38func main() {39 fmt.Println(Num(10))40}41import "fmt"42func main() {43 fmt.Println(Num(10))44}45import "fmt"46func main() {47 fmt.Println(Num(10))48}49import "fmt"50func main() {51 fmt.Println(Num(10))52}53import
Num
Using AI Code Generation
1import (2func main() {3 fmt.Println("a.Num = ", a.Num)4}5import (6func main() {7 fmt.Println("a.Num = ", a.Num)8}9import (10func main() {11 fmt.Println("a.Num = ", a.Num)12}13import (14func main() {15 fmt.Println("a.Num = ", a.Num)16}17import (18func main() {19 fmt.Println("a.Num = ", a.Num)20}21import (22func main() {23 fmt.Println("
Num
Using AI Code Generation
1import (2func main() {3 fmt.Println(t.Num)4}5import (6func main() {7 fmt.Println(t.Num)8}9import (10func main() {11 fmt.Println(t.Num)12}13import (14func main() {15 fmt.Println(t.Num)16}17import (18func main() {19 fmt.Println(t.Num)20}21import (22func main() {23 fmt.Println(t.Num)24}25import (26func main() {27 fmt.Println(t.Num)28}29import (30func main() {31 fmt.Println(t.Num)32}33import (
Num
Using AI Code Generation
1import (2func main() {3 fmt.Println(td_test.Num(2))4}5import (6func main() {7 fmt.Println("Hello, World!")8}
Num
Using AI Code Generation
1import "fmt"2import "github.com/tejaswini22199/test"3func main() {4 fmt.Print("Enter the number of elements : ")5 fmt.Scanf("%d",&n)6 t.Num(n)7 fmt.Println("The number of elements are : ",t.Num(n))8}9import "fmt"10import "github.com/tejaswini22199/test"11func main() {12 t.Calc()13 fmt.Println("The sum of the elements are : ",t.Calc())14}15import "fmt"16import "github.com/tejaswini22199/test"17func main() {18 t.Calc()19 fmt.Println("The average of the elements are : ",t.Calc())20}21import "fmt"22import "github.com/tejaswini22199/test"23func main() {24 t.Calc()25 fmt.Println("The maximum of the elements are : ",t.Calc())26}27import "fmt"28import "github.com/tejaswini22199/test"29func main() {30 t.Calc()31 fmt.Println("The minimum of the elements are : ",t.Calc())32}33import "fmt"34import "github.com/tejaswini22199/test"35func main() {36 t.Calc()37 fmt.Println("The sum of the elements are : ",t.Calc())38}
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!!