github.com/status-im/status-go@v1.1.0/services/wallet/testutils/helpers.go (about) 1 package testutils 2 3 import ( 4 "reflect" 5 "sort" 6 7 "github.com/golang/mock/gomock" 8 9 "github.com/ethereum/go-ethereum/common" 10 ) 11 12 const EthSymbol = "ETH" 13 const SntSymbol = "SNT" 14 const DaiSymbol = "DAI" 15 16 func SliceContains[T comparable](slice []T, value T) bool { 17 for _, v := range slice { 18 if v == value { 19 return true 20 } 21 } 22 return false 23 } 24 func StructExistsInSlice[T any](target T, slice []T) bool { 25 for _, item := range slice { 26 if reflect.DeepEqual(target, item) { 27 return true 28 } 29 } 30 return false 31 } 32 33 func Filter[T any](ss []T, test func(T) bool) (ret []T) { 34 for _, s := range ss { 35 if test(s) { 36 ret = append(ret, s) 37 } 38 } 39 return 40 } 41 42 // AddressSliceMatcher is a custom matcher for comparing common.Address slices regardless of order. 43 type AddressSliceMatcher struct { 44 expected []common.Address 45 } 46 47 func NewAddressSliceMatcher(expected []common.Address) gomock.Matcher { 48 return &AddressSliceMatcher{expected: expected} 49 } 50 51 func (m *AddressSliceMatcher) Matches(x interface{}) bool { 52 actual, ok := x.([]common.Address) 53 if !ok { 54 return false 55 } 56 57 if len(m.expected) != len(actual) { 58 return false 59 } 60 61 // Create copies of the slices to sort them 62 expectedCopy := make([]common.Address, len(m.expected)) 63 actualCopy := make([]common.Address, len(actual)) 64 copy(expectedCopy, m.expected) 65 copy(actualCopy, actual) 66 67 sort.Slice(expectedCopy, func(i, j int) bool { return expectedCopy[i].Hex() < expectedCopy[j].Hex() }) 68 sort.Slice(actualCopy, func(i, j int) bool { return actualCopy[i].Hex() < actualCopy[j].Hex() }) 69 70 for i := range expectedCopy { 71 if expectedCopy[i] != actualCopy[i] { 72 return false 73 } 74 } 75 76 return true 77 } 78 79 func (m *AddressSliceMatcher) String() string { 80 return "matches Address slice regardless of order" 81 } 82 83 // Uint64SliceMatcher is a custom matcher for comparing uint64 slices regardless of order. 84 type Uint64SliceMatcher struct { 85 expected []uint64 86 } 87 88 func NewUint64SliceMatcher(expected []uint64) gomock.Matcher { 89 return &Uint64SliceMatcher{expected: expected} 90 } 91 92 func (m *Uint64SliceMatcher) Matches(x interface{}) bool { 93 actual, ok := x.([]uint64) 94 if !ok { 95 return false 96 } 97 98 if len(m.expected) != len(actual) { 99 return false 100 } 101 102 // Create copies of the slices to sort them 103 expectedCopy := make([]uint64, len(m.expected)) 104 actualCopy := make([]uint64, len(actual)) 105 copy(expectedCopy, m.expected) 106 copy(actualCopy, actual) 107 108 sort.Slice(expectedCopy, func(i, j int) bool { return expectedCopy[i] < expectedCopy[j] }) 109 sort.Slice(actualCopy, func(i, j int) bool { return actualCopy[i] < actualCopy[j] }) 110 111 for i := range expectedCopy { 112 if expectedCopy[i] != actualCopy[i] { 113 return false 114 } 115 } 116 117 return true 118 } 119 120 func (m *Uint64SliceMatcher) String() string { 121 return "matches uint64 slice regardless of order" 122 }