go.temporal.io/server@v1.23.0/common/testing/protoassert/assert.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 // Assert wraps testify's require package with useful helpers 26 package protoassert 27 28 import ( 29 "github.com/google/go-cmp/cmp" 30 "github.com/stretchr/testify/assert" 31 "google.golang.org/protobuf/proto" 32 "google.golang.org/protobuf/testing/protocmp" 33 34 "go.temporal.io/api/temporalproto" 35 ) 36 37 type helper interface { 38 Helper() 39 } 40 41 type ProtoAssertions struct { 42 t assert.TestingT 43 } 44 45 func New(t assert.TestingT) ProtoAssertions { 46 return ProtoAssertions{t} 47 } 48 49 // ProtoEqual compares two proto.Message objects for equality 50 func ProtoEqual(t assert.TestingT, a proto.Message, b proto.Message) bool { 51 if th, ok := t.(helper); ok { 52 th.Helper() 53 } 54 if diff := cmp.Diff(a, b, protocmp.Transform()); diff != "" { 55 return assert.Fail(t, "Proto mismatch (-want +got):\n", diff) 56 } 57 return true 58 } 59 60 func NotProtoEqual(t assert.TestingT, a proto.Message, b proto.Message) bool { 61 if th, ok := t.(helper); ok { 62 th.Helper() 63 } 64 if diff := cmp.Diff(a, b, protocmp.Transform()); diff == "" { 65 return assert.Fail(t, "Expected protos to differ but they did not") 66 } 67 return true 68 } 69 70 // ProtoSliceEqual compares elements in a slice of proto.Message. 71 // This is not a method on the suite type because methods cannot have 72 // generic parameters and slice casting (say from []historyEvent) to 73 // []proto.Message is impossible 74 func ProtoSliceEqual[T proto.Message](t assert.TestingT, a []T, b []T) bool { 75 if th, ok := t.(helper); ok { 76 th.Helper() 77 } 78 if len(a) != len(b) { 79 return false 80 } 81 for i := 0; i < len(a); i++ { 82 if diff := cmp.Diff(a[i], b[i], protocmp.Transform()); diff != "" { 83 return assert.Fail(t, "Proto mismatch at index %d (-want +got):\n%v", i, diff) 84 } 85 } 86 87 return true 88 } 89 90 // ProtoElementsMatch behaves like assert.ElementsMatch except in that it works for 91 // google/protobuf-generated structs 92 func ProtoElementsMatch(t assert.TestingT, a any, b any, msgAndArgs ...any) bool { 93 if th, ok := t.(helper); ok { 94 th.Helper() 95 } 96 97 if isEmpty(a) && isEmpty(b) { 98 return true 99 } 100 101 extraA, extraB := diffLists(a, b) 102 if len(extraA) == 0 && len(extraB) == 0 { 103 return true 104 } 105 106 return assert.Fail(t, formatListDiff(a, b, extraA, extraB), msgAndArgs...) 107 } 108 109 func DeepEqual(t assert.TestingT, a any, b any) bool { 110 if th, ok := t.(helper); ok { 111 th.Helper() 112 } 113 114 return temporalproto.DeepEqual(a, b) 115 } 116 117 func (x ProtoAssertions) ProtoEqual(a proto.Message, b proto.Message) bool { 118 if th, ok := x.t.(helper); ok { 119 th.Helper() 120 } 121 return ProtoEqual(x.t, a, b) 122 } 123 124 func (x ProtoAssertions) NotProtoEqual(a proto.Message, b proto.Message) bool { 125 if th, ok := x.t.(helper); ok { 126 th.Helper() 127 } 128 return NotProtoEqual(x.t, a, b) 129 } 130 131 func (x ProtoAssertions) DeepEqual(a any, b any) bool { 132 if th, ok := x.t.(helper); ok { 133 th.Helper() 134 } 135 136 return temporalproto.DeepEqual(a, b) 137 } 138 139 func (x ProtoAssertions) ProtoElementsMatch(a any, b any) bool { 140 if th, ok := x.t.(helper); ok { 141 th.Helper() 142 } 143 144 return ProtoElementsMatch(x.t, a, b) 145 }