forked from TrueCloudLab/frostfs-api-go
Add stable marshaller helper for bool
Signed-off-by: Alex Vanin <alexey@nspcc.ru>
This commit is contained in:
parent
311b76b75b
commit
89bd8f3915
4 changed files with 147 additions and 22 deletions
|
@ -12,6 +12,7 @@ import (
|
|||
type stablePrimitives struct {
|
||||
FieldA []byte
|
||||
FieldB string
|
||||
FieldC bool
|
||||
}
|
||||
|
||||
func (s *stablePrimitives) stableMarshal(buf []byte, wrongField bool) ([]byte, error) {
|
||||
|
@ -43,7 +44,17 @@ func (s *stablePrimitives) stableMarshal(buf []byte, wrongField bool) ([]byte, e
|
|||
}
|
||||
offset, err = proto.StringMarshal(fieldNum, buf, s.FieldB)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "can't marshal field a")
|
||||
return nil, errors.Wrap(err, "can't marshal field b")
|
||||
}
|
||||
i += offset
|
||||
|
||||
fieldNum = 200
|
||||
if wrongField {
|
||||
fieldNum++
|
||||
}
|
||||
offset, err = proto.BoolMarshal(fieldNum, buf, s.FieldC)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "can't marshal field c")
|
||||
}
|
||||
i += offset
|
||||
|
||||
|
@ -52,7 +63,8 @@ func (s *stablePrimitives) stableMarshal(buf []byte, wrongField bool) ([]byte, e
|
|||
|
||||
func (s *stablePrimitives) stableSize() int {
|
||||
return proto.BytesSize(1, s.FieldA) +
|
||||
proto.StringSize(2, s.FieldB)
|
||||
proto.StringSize(2, s.FieldB) +
|
||||
proto.BoolSize(200, s.FieldC)
|
||||
}
|
||||
|
||||
func TestBytesMarshal(t *testing.T) {
|
||||
|
@ -71,6 +83,29 @@ func TestBytesMarshal(t *testing.T) {
|
|||
})
|
||||
}
|
||||
|
||||
func TestStringMarshal(t *testing.T) {
|
||||
t.Run("not empty", func(t *testing.T) {
|
||||
data := "Hello World"
|
||||
testStringMarshal(t, data, false)
|
||||
testStringMarshal(t, data, true)
|
||||
})
|
||||
|
||||
t.Run("empty", func(t *testing.T) {
|
||||
testStringMarshal(t, "", false)
|
||||
})
|
||||
}
|
||||
|
||||
func TestBoolMarshal(t *testing.T) {
|
||||
t.Run("true", func(t *testing.T) {
|
||||
testBoolMarshal(t, true, false)
|
||||
testBoolMarshal(t, true, true)
|
||||
})
|
||||
|
||||
t.Run("false", func(t *testing.T) {
|
||||
testBoolMarshal(t, false, false)
|
||||
})
|
||||
}
|
||||
|
||||
func testBytesMarshal(t *testing.T, data []byte, wrongField bool) {
|
||||
var (
|
||||
wire []byte
|
||||
|
@ -107,18 +142,6 @@ func testBytesMarshal(t *testing.T, data []byte, wrongField bool) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestStringMarshal(t *testing.T) {
|
||||
t.Run("not empty", func(t *testing.T) {
|
||||
data := "Hello World"
|
||||
testStringMarshal(t, data, false)
|
||||
testStringMarshal(t, data, true)
|
||||
})
|
||||
|
||||
t.Run("empty", func(t *testing.T) {
|
||||
testStringMarshal(t, "", false)
|
||||
})
|
||||
}
|
||||
|
||||
func testStringMarshal(t *testing.T, s string, wrongField bool) {
|
||||
var (
|
||||
wire []byte
|
||||
|
@ -154,3 +177,36 @@ func testStringMarshal(t *testing.T, s string, wrongField bool) {
|
|||
require.Len(t, result.FieldB, 0)
|
||||
}
|
||||
}
|
||||
|
||||
func testBoolMarshal(t *testing.T, b bool, wrongField bool) {
|
||||
var (
|
||||
wire []byte
|
||||
err error
|
||||
|
||||
custom = stablePrimitives{FieldC: b}
|
||||
transport = test.Primitives{FieldC: b}
|
||||
)
|
||||
|
||||
wire, err = custom.stableMarshal(nil, wrongField)
|
||||
require.NoError(t, err)
|
||||
|
||||
wireGen, err := transport.Marshal()
|
||||
require.NoError(t, err)
|
||||
|
||||
if !wrongField {
|
||||
// we can check equality because single field cannot be unstable marshalled
|
||||
require.Equal(t, wireGen, wire)
|
||||
} else {
|
||||
require.NotEqual(t, wireGen, wire)
|
||||
}
|
||||
|
||||
result := new(test.Primitives)
|
||||
err = result.Unmarshal(wire)
|
||||
require.NoError(t, err)
|
||||
|
||||
if !wrongField {
|
||||
require.Equal(t, b, result.FieldC)
|
||||
} else {
|
||||
require.False(t, false, result.FieldC)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue