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
5package tag_test
6
7import (
8	"reflect"
9	"testing"
10
11	"google.golang.org/protobuf/internal/encoding/tag"
12	fdesc "google.golang.org/protobuf/internal/filedesc"
13	"google.golang.org/protobuf/proto"
14	pdesc "google.golang.org/protobuf/reflect/protodesc"
15	pref "google.golang.org/protobuf/reflect/protoreflect"
16)
17
18func Test(t *testing.T) {
19	fd := new(fdesc.Field)
20	fd.L0.ParentFile = fdesc.SurrogateProto3
21	fd.L0.FullName = "foo_field"
22	fd.L1.Number = 1337
23	fd.L1.Cardinality = pref.Repeated
24	fd.L1.Kind = pref.BytesKind
25	fd.L1.Default = fdesc.DefaultValue(pref.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(pdesc.ToFieldDescriptorProto(gotFD), pdesc.ToFieldDescriptorProto(wantFD)) {
38		t.Errorf("Umarshal() mismatch:\ngot  %v\nwant %v", gotFD, wantFD)
39	}
40}
41