go.temporal.io/server@v1.23.0/common/testing/protorequire/require.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 package protorequire 26 27 import ( 28 "github.com/stretchr/testify/require" 29 "google.golang.org/protobuf/proto" 30 31 "go.temporal.io/server/common/testing/protoassert" 32 ) 33 34 type helper interface { 35 Helper() 36 } 37 38 type ProtoAssertions struct { 39 t require.TestingT 40 } 41 42 func New(t require.TestingT) ProtoAssertions { 43 return ProtoAssertions{t} 44 } 45 46 func ProtoEqual(t require.TestingT, a proto.Message, b proto.Message) { 47 if th, ok := t.(helper); ok { 48 th.Helper() 49 } 50 if !protoassert.ProtoEqual(t, a, b) { 51 t.FailNow() 52 } 53 } 54 55 func NotProtoEqual(t require.TestingT, a proto.Message, b proto.Message) { 56 if th, ok := t.(helper); ok { 57 th.Helper() 58 } 59 if !protoassert.NotProtoEqual(t, a, b) { 60 t.FailNow() 61 } 62 } 63 64 // ProtoSliceEqual compares elements in a slice of proto.Message. 65 // This is not a method on the suite type because methods cannot have 66 // generic parameters and slice casting (say from []historyEvent) to 67 // []proto.Message is impossible 68 func ProtoSliceEqual[T proto.Message](t require.TestingT, a []T, b []T) { 69 if th, ok := t.(helper); ok { 70 th.Helper() 71 } 72 if !protoassert.ProtoSliceEqual(t, a, b) { 73 t.FailNow() 74 } 75 } 76 77 func (x ProtoAssertions) ProtoEqual(a proto.Message, b proto.Message) { 78 if th, ok := x.t.(helper); ok { 79 th.Helper() 80 } 81 if !protoassert.ProtoEqual(x.t, a, b) { 82 x.t.FailNow() 83 } 84 } 85 86 func (x ProtoAssertions) NotProtoEqual(a proto.Message, b proto.Message) { 87 if th, ok := x.t.(helper); ok { 88 th.Helper() 89 } 90 if !protoassert.NotProtoEqual(x.t, a, b) { 91 x.t.FailNow() 92 } 93 } 94 95 func (x ProtoAssertions) DeepEqual(a any, b any) { 96 if th, ok := x.t.(helper); ok { 97 th.Helper() 98 } 99 if !protoassert.DeepEqual(x.t, a, b) { 100 x.t.FailNow() 101 } 102 } 103 104 func (x ProtoAssertions) ProtoElementsMatch(a any, b any) bool { 105 if th, ok := x.t.(helper); ok { 106 th.Helper() 107 } 108 109 return protoassert.ProtoElementsMatch(x.t, a, b) 110 }