From bcddc6698bb958b8d949837f00d348b2a38a389b Mon Sep 17 00:00:00 2001 From: Tnze Date: Wed, 8 Apr 2020 13:18:26 +0800 Subject: [PATCH] add marshal test for nbt library --- nbt/interface_test.go | 1 - nbt/marshal_test.go | 40 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) delete mode 100644 nbt/interface_test.go create mode 100644 nbt/marshal_test.go diff --git a/nbt/interface_test.go b/nbt/interface_test.go deleted file mode 100644 index 41ad9a7..0000000 --- a/nbt/interface_test.go +++ /dev/null @@ -1 +0,0 @@ -package nbt diff --git a/nbt/marshal_test.go b/nbt/marshal_test.go new file mode 100644 index 0000000..59b10f4 --- /dev/null +++ b/nbt/marshal_test.go @@ -0,0 +1,40 @@ +package nbt + +import ( + "bytes" + "testing" +) + +func TestMarshal_IntArray(t *testing.T) { + // Test marshal pure Int array + v := []int32{0, -10, 3} + out := []byte{TagIntArray, 0x00, 0x00, 0, 0, 0, 3, + 0x00, 0x00, 0x00, 0x00, + 0xff, 0xff, 0xff, 0xf6, + 0x00, 0x00, 0x00, 0x03, + } + var buf bytes.Buffer + if err := Marshal(&buf, v); err != nil { + t.Error(err) + } else if !bytes.Equal(buf.Bytes(), out) { + t.Errorf("output binary not right: get % 02x, want % 02x ", buf.Bytes(), out) + } + + // Test marshal in a struct + v2 := struct { + Ary []int32 `nbt:"ary"` + }{[]int32{0, -10, 3}} + out = []byte{TagCompound, 0x00, 0x00, + TagIntArray, 0x00, 0x03, 'a', 'r', 'y', 0, 0, 0, 3, + 0x00, 0x00, 0x00, 0x00, + 0xff, 0xff, 0xff, 0xf6, + 0x00, 0x00, 0x00, 0x03, + TagEnd, + } + buf.Reset() + if err := Marshal(&buf, v2); err != nil { + t.Error(err) + } else if !bytes.Equal(buf.Bytes(), out) { + t.Errorf("output binary not right: get % 02x, want % 02x ", buf.Bytes(), out) + } +}