go.temporal.io/server@v1.23.0/common/testing/protomock/matchers.go (about) 1 // The MIT License 2 // 3 // Copyright (c) 2020 Temporal Technologies Inc. All rights reserved. 4 // 5 // Copyright (c) 2020 Uber Technologies, Inc. 6 // 7 // Permission is hereby granted, free of charge, to any person obtaining a copy 8 // of this software and associated documentation files (the "Software"), to deal 9 // in the Software without restriction, including without limitation the rights 10 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 // copies of the Software, and to permit persons to whom the Software is 12 // furnished to do so, subject to the following conditions: 13 // 14 // The above copyright notice and this permission notice shall be included in 15 // all copies or substantial portions of the Software. 16 // 17 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 23 // THE SOFTWARE. 24 25 // Proto-type adapters for the mock library 26 package protomock 27 28 import ( 29 "fmt" 30 31 "github.com/golang/mock/gomock" 32 "go.temporal.io/api/temporalproto" 33 ) 34 35 type protoEq struct { 36 want any 37 } 38 39 type protoSliceEq struct { 40 want []any 41 } 42 43 // Eq returns a gomock.Matcher that uses proto.Equal to check equality 44 func Eq(want any) gomock.Matcher { 45 return protoEq{want} 46 } 47 48 // Matches returns true when the provided value equals the expectation 49 func (m protoEq) Matches(x any) bool { 50 if m.want == nil && x == nil { 51 return true 52 } 53 54 return temporalproto.DeepEqual(m.want, x) 55 } 56 57 func (m protoEq) String() string { 58 return fmt.Sprintf("is equal to %v (%T)", m.want, m.want) 59 } 60 61 func SliceEq(want []any) gomock.Matcher { 62 return protoSliceEq{want} 63 } 64 65 func (m protoSliceEq) Matches(x any) bool { 66 xs, ok := x.([]any) 67 if !ok || len(m.want) != len(xs) { 68 return false 69 } 70 71 for i := range m.want { 72 if !temporalproto.DeepEqual(m.want[i], xs[i]) { 73 return false 74 } 75 } 76 return true 77 } 78 79 func (m protoSliceEq) String() string { 80 return fmt.Sprintf("is equal to %v", m.want) 81 }