github.com/whiteCcinn/protobuf-go@v1.0.9/internal/encoding/tag/tag_test.go (about)

     1  // Copyright 2018 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package tag_test
     6  
     7  import (
     8  	"reflect"
     9  	"testing"
    10  
    11  	"github.com/whiteCcinn/protobuf-go/internal/encoding/tag"
    12  	"github.com/whiteCcinn/protobuf-go/internal/filedesc"
    13  	"github.com/whiteCcinn/protobuf-go/proto"
    14  	"github.com/whiteCcinn/protobuf-go/reflect/protodesc"
    15  	"github.com/whiteCcinn/protobuf-go/reflect/protoreflect"
    16  )
    17  
    18  func Test(t *testing.T) {
    19  	fd := new(filedesc.Field)
    20  	fd.L0.ParentFile = filedesc.SurrogateProto3
    21  	fd.L0.FullName = "foo_field"
    22  	fd.L1.Number = 1337
    23  	fd.L1.Cardinality = protoreflect.Repeated
    24  	fd.L1.Kind = protoreflect.BytesKind
    25  	fd.L1.Default = filedesc.DefaultValue(protoreflect.ValueOf([]byte("hello, \xde\xad\xbe\xef\n")), nil)
    26  
    27  	// Marshal test.
    28  	gotTag := tag.Marshal(fd, "")
    29  	wantTag := `bytes,1337,rep,name=foo_field,json=fooField,proto3,def=hello, \336\255\276\357\n`
    30  	if gotTag != wantTag {
    31  		t.Errorf("Marshal() = `%v`, want `%v`", gotTag, wantTag)
    32  	}
    33  
    34  	// Unmarshal test.
    35  	gotFD := tag.Unmarshal(wantTag, reflect.TypeOf([]byte{}), nil)
    36  	wantFD := fd
    37  	if !proto.Equal(protodesc.ToFieldDescriptorProto(gotFD), protodesc.ToFieldDescriptorProto(wantFD)) {
    38  		t.Errorf("Umarshal() mismatch:\ngot  %v\nwant %v", gotFD, wantFD)
    39  	}
    40  }