protocol_test: split TestPacketEncodeDecode

This commit is contained in:
CPunch 2023-11-26 16:58:33 -06:00
parent e8f5e5fc9c
commit 81de857670

View File

@ -16,14 +16,14 @@ type TestPacketData struct {
} }
var ( var (
// this is the data we expect to get from encoding the above struct with: testStruct = TestPacketData{
// A: 1,
// Encode(TestPacketData{ B: 2,
// A: 1, UTF16Str: "hello world",
// B: 2, C: 3,
// UTF16Str: "hello world", }
// C: 3,
// }) // this is the data we expect to get from encoding the above struct
testData = [...]byte{ testData = [...]byte{
0x01, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
0x68, 0x00, 0x65, 0x00, 0x6c, 0x00, 0x6c, 0x00, 0x68, 0x00, 0x65, 0x00, 0x6c, 0x00, 0x6c, 0x00,
@ -52,30 +52,31 @@ var (
} }
) )
func TestPacketEncodeDecode(t *testing.T) { func TestPacketEncode(t *testing.T) {
buf := &bytes.Buffer{} buf := bytes.NewBuffer(nil)
pkt := protocol.NewPacket(buf) pkt := protocol.NewPacket(buf)
if err := pkt.Encode(TestPacketData{ if err := pkt.Encode(testStruct); err != nil {
A: 1,
B: 2,
UTF16Str: "hello world",
C: 3,
}); err != nil {
t.Error(err) t.Error(err)
} }
if !bytes.Equal(buf.Bytes(), testData[:]) { if !bytes.Equal(buf.Bytes(), testData[:]) {
t.Error("packet data does not match!") t.Error("packet data does not match!")
} }
}
func TestPacketDecode(t *testing.T) {
buf := bytes.NewBuffer(nil)
pkt := protocol.NewPacket(buf)
buf.Write(testData[:])
var test TestPacketData var test TestPacketData
if err := pkt.Decode(&test); err != nil { if err := pkt.Decode(&test); err != nil {
t.Error(err) t.Error(err)
} }
if test.A != 1 || test.B != 2 || test.C != 3 || test.UTF16Str != "hello world" { if test != testStruct {
t.Error("decoded packet data does not match!") t.Error("packet data does not match!")
} }
} }