1 // Copyright 2022 Google LLC 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package test 16 17 import ( 18 "encoding/json" 19 "reflect" 20 "testing" 21 ) 22 23 func Equals(t *testing.T, o1, o2 interface{}) bool { 24 return reflect.DeepEqual(normalize(t, o1), normalize(t, o2)) 25 } 26 27 func normalize(t *testing.T, obj interface{}) map[string]interface{} { 28 // Only applicable to types that can me marshaled as a JSON object. 29 b, err := json.Marshal(&obj) 30 if err != nil { 31 t.Fatalf("error marshaling as JSON: %v", err) 32 } 33 var ret map[string]interface{} 34 if err := json.Unmarshal(b, &ret); err != nil { 35 t.Fatalf("error unmarshaling JSON bytes: %v", err) 36 } 37 return ret 38 } 39