github.com/grafana/pyroscope@v1.18.0/pkg/testhelper/proto.go (about)

     1  package testhelper
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/google/go-cmp/cmp"
     7  	"google.golang.org/protobuf/proto"
     8  )
     9  
    10  // CloneProto clones a protobuf message.
    11  func CloneProto[T proto.Message](t *testing.T, in T) T {
    12  	t.Helper()
    13  	return proto.Clone(in).(T)
    14  }
    15  
    16  // Equal compares two protobuf messages ignoring extra generated proto fields.
    17  func EqualProto(t *testing.T, expected, actual interface{}) {
    18  	t.Helper()
    19  	if diff := cmp.Diff(expected, actual, ignoreProtoFields()); diff != "" {
    20  		t.Errorf("result mismatch (-want +got):\n%s", diff)
    21  	}
    22  }
    23  
    24  func ignoreProtoFields() cmp.Option {
    25  	return cmp.FilterPath(func(p cmp.Path) bool {
    26  		switch p[len(p)-1].String() {
    27  		case ".state", ".sizeCache", ".unknownFields":
    28  			return true
    29  		}
    30  		return false
    31  	}, cmp.Ignore())
    32  }