Skip to content

Commit 57ece70

Browse files
committed
test: cover _unknownFields-only structs and named byte slice types
1 parent 33a028c commit 57ece70

File tree

2 files changed

+67
-1
lines changed

2 files changed

+67
-1
lines changed

internal/reflect/decoder_test.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,45 @@ func TestDecodeUnknownFields(t *testing.T) {
366366
assert.BytesEqual(t, testb, p._unknownFields)
367367
}
368368

369+
func TestDecodeOnlyUnknownFields(t *testing.T) {
370+
type Msg0 struct {
371+
I0 int32 `thrift:"i0,2" frugal:"2,default,i32"`
372+
S0 string `thrift:"s0,3" frugal:"3,default,string"`
373+
}
374+
375+
roundTrip := func(t *testing.T, p any) {
376+
msg := Msg0{I0: 42, S0: "hello"}
377+
b, err := Append(nil, msg)
378+
assert.Nil(t, err)
379+
380+
n, err := Decode(b, p)
381+
assert.Nil(t, err)
382+
assert.Equal(t, len(b), n)
383+
384+
b2, err := Append(nil, p)
385+
assert.Nil(t, err)
386+
387+
msg2 := &Msg0{}
388+
_, err = Decode(b2, msg2)
389+
assert.Nil(t, err)
390+
assert.Equal(t, msg.I0, msg2.I0)
391+
assert.Equal(t, msg.S0, msg2.S0)
392+
}
393+
394+
a := &struct {
395+
_unknownFields []byte
396+
}{}
397+
roundTrip(t, a)
398+
assert.True(t, len(a._unknownFields) > 0)
399+
400+
type UnknownFieldsType []byte
401+
b := &struct {
402+
_unknownFields UnknownFieldsType
403+
}{}
404+
roundTrip(t, b)
405+
assert.True(t, len(b._unknownFields) > 0)
406+
}
407+
369408
func TestDecodeNoCopy(t *testing.T) {
370409
type Msg struct {
371410
A string `frugal:"1,default,string,nocopy"`

internal/reflect/encoder_test.go

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ func TestEncodeStructOther(t *testing.T) {
128128
}
129129

130130
func TestEncodeUnknownFields(t *testing.T) {
131-
type Msg1 struct { // without S0, I1
131+
type Msg1 struct {
132132
I0 int32 `thrift:"i0,2" frugal:"2,default,i32"`
133133
S1 string `thrift:"s1,4" frugal:"4,default,string"`
134134

@@ -145,6 +145,33 @@ func TestEncodeUnknownFields(t *testing.T) {
145145
assert.True(t, strings.Contains(string(b), string(append([]byte("helloworld")[:], byte(tSTOP)))))
146146
}
147147

148+
func TestEncodeOnlyUnknownFields(t *testing.T) {
149+
type Msg struct {
150+
_unknownFields []byte
151+
}
152+
m := &Msg{_unknownFields: []byte("helloworld")}
153+
154+
n := EncodedSize(m)
155+
b, err := Append(nil, m)
156+
assert.Nil(t, err)
157+
assert.Equal(t, n, len(b))
158+
assert.BytesEqual(t, append([]byte("helloworld"), byte(tSTOP)), b)
159+
}
160+
161+
func TestEncodeOnlyUnknownFieldsNamedType(t *testing.T) {
162+
type UnknownFieldsType []byte
163+
type Msg struct {
164+
_unknownFields UnknownFieldsType
165+
}
166+
m := &Msg{_unknownFields: UnknownFieldsType("helloworld")}
167+
168+
n := EncodedSize(m)
169+
b, err := Append(nil, m)
170+
assert.Nil(t, err)
171+
assert.Equal(t, n, len(b))
172+
assert.BytesEqual(t, append([]byte("helloworld"), byte(tSTOP)), b)
173+
}
174+
148175
func TestNestedListMapStruct(t *testing.T) {
149176
type Msg1 struct {
150177
A string `frugal:"1,default,string"`

0 commit comments

Comments
 (0)