github.com/ars0915/glossika-exercise@v0.0.0-20240904180321-a5617a00d892/util/cTest/matcher.go (about) 1 package ctest 2 3 import ( 4 "reflect" 5 "slices" 6 "time" 7 8 "github.com/golang/mock/gomock" 9 "github.com/kr/pretty" 10 "github.com/sirupsen/logrus" 11 ) 12 13 // reference: https://github.com/golang/mock/issues/616#issuecomment-1040155899 14 type mockDiffWrapper struct { 15 v interface{} 16 } 17 18 func DiffWrapper(v interface{}) *mockDiffWrapper { 19 return &mockDiffWrapper{ 20 v: v, 21 } 22 } 23 24 // Implement the gomock.Matcher interface 25 func (m *mockDiffWrapper) Matches(v interface{}) bool { 26 ok := gomock.Eq(m.v).Matches(v) // wrap the gomock.Eq matcher 27 if !ok { 28 logrus.Errorf("This is the diff want: %s", pretty.Diff(m.v, v)) 29 } 30 return ok 31 } 32 33 // Implement the gomock.Matcher interface 34 func (m *mockDiffWrapper) String() string { 35 return gomock.Eq(m.v).String() // again wrap the gomock.Eq matcher 36 } 37 38 type exceptMatcher struct { 39 v interface{} 40 fields []string 41 } 42 43 func ExceptMatcher(v interface{}, fields []string) *exceptMatcher { 44 return &exceptMatcher{v, fields} 45 } 46 47 func (m *exceptMatcher) Matches(x interface{}) (result bool) { 48 defer func() { 49 if !result { 50 logrus.Errorf("This is the diff want: %s", pretty.Diff(m.v, x)) 51 } 52 }() 53 54 expectValue := reflect.Indirect(reflect.ValueOf(m.v)) 55 actualValue := reflect.Indirect(reflect.ValueOf(x)) 56 57 for i := 0; i < expectValue.NumField(); i++ { 58 typeField := expectValue.Type().Field(i) 59 60 if slices.Contains(m.fields, typeField.Name) { 61 continue 62 } 63 64 fieldExpect := expectValue.Field(i) 65 fieldActual := actualValue.FieldByName(typeField.Name) 66 67 if result = checkFieldEqual(fieldExpect, fieldActual); !result { 68 return 69 } 70 } 71 72 result = true 73 return 74 } 75 76 func (m *exceptMatcher) String() string { 77 return gomock.Eq(m.v).String() 78 } 79 80 func checkFieldEqual(expected, actual reflect.Value) bool { 81 if !expected.IsValid() || !actual.IsValid() { 82 return false 83 } 84 85 expectedType := expected.Type() 86 actualType := actual.Type() 87 if expectedType != actualType { 88 return false 89 } 90 91 switch expectedType.Kind() { 92 case reflect.Interface: 93 if ok := checkFieldEqual(expected.Elem(), actual.Elem()); !ok { 94 return false 95 } 96 case reflect.Map: 97 expectedKeys := expected.MapKeys() 98 actualKeys := actual.MapKeys() 99 100 if len(expectedKeys) != len(actualKeys) { 101 return false 102 } 103 104 for _, i := range expectedKeys { 105 if ok := checkFieldEqual(expected.MapIndex(i), actual.MapIndex(i)); !ok { 106 return false 107 } 108 } 109 case reflect.Ptr: 110 switch { 111 case expected.IsNil() && !actual.IsNil(): 112 return false 113 case !expected.IsNil() && actual.IsNil(): 114 return false 115 case !expected.IsNil() && !actual.IsNil(): 116 if ok := checkFieldEqual(expected.Elem(), actual.Elem()); !ok { 117 return false 118 } 119 } 120 case reflect.Array, reflect.Slice: 121 lenA := expected.Len() 122 lenB := actual.Len() 123 if lenA != lenB { 124 return false 125 } 126 for i := 0; i < lenA; i++ { 127 if ok := checkFieldEqual(expected.Index(i), actual.Index(i)); !ok { 128 return false 129 } 130 } 131 case reflect.Struct: 132 switch e := expected.Interface().(type) { 133 case time.Time: 134 a, ok := actual.Interface().(time.Time) 135 if !ok { 136 return false 137 } 138 139 if e.Unix() != a.Unix() { 140 return false 141 } 142 143 return true 144 } 145 for i := 0; i < expected.NumField(); i++ { 146 if ok := checkFieldEqual(expected.Field(i), actual.Field(i)); !ok { 147 return false 148 } 149 } 150 default: 151 if !expected.CanInterface() || !actual.CanInterface() { 152 return false 153 } 154 if expected.Interface() != actual.Interface() { 155 return false 156 } 157 } 158 159 return true 160 }