add marshal test for nbt library

This commit is contained in:
Tnze
2020-04-08 13:18:26 +08:00
parent 73727eccb9
commit bcddc6698b
2 changed files with 40 additions and 1 deletions

View File

@ -1 +0,0 @@
package nbt

40
nbt/marshal_test.go Normal file
View File

@ -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)
}
}