Move test to nbt_test.go & don't ignore errors

This commit is contained in:
Mark Asp
2020-09-18 09:00:40 -05:00
parent f0fed0e287
commit 5d3f46b58f
3 changed files with 135 additions and 187 deletions

View File

@ -38,11 +38,12 @@ func (e *Encoder) Encode(v interface{}) error {
return e.marshal(val, getTagType(val.Type()), "") return e.marshal(val, getTagType(val.Type()), "")
} }
func (e *Encoder) marshal(val reflect.Value, tagType byte, tagName string) (err error) { func (e *Encoder) marshal(val reflect.Value, tagType byte, tagName string) error {
err = e.writeHeader(val, tagType, tagName) if err := e.writeHeader(val, tagType, tagName); err != nil {
err = e.writeValue(val, tagType)
return err return err
} }
return e.writeValue(val, tagType)
}
func (e *Encoder) writeHeader(val reflect.Value, tagType byte, tagName string) (err error) { func (e *Encoder) writeHeader(val reflect.Value, tagType byte, tagName string) (err error) {
if tagType == TagList { if tagType == TagList {

View File

@ -2,7 +2,6 @@ package nbt
import ( import (
"bytes" "bytes"
"io/ioutil"
"math" "math"
"testing" "testing"
) )
@ -167,79 +166,3 @@ func TestMarshal_StructArray(t *testing.T) {
}) })
} }
} }
// This test is for compliance with the "bigtest.dat" described in detail here:
// https://wiki.vg/NBT#bigtest.nbt
func TestMarshal_BigTest(t *testing.T) {
byteValues := make([]byte, 1000)
for n := 0; n < 1000; n++ {
byteValues[n] = byte((n*n*255 + n*7) % 100)
}
type NestedCompound struct {
Name string `nbt:"name"`
Value float32 `nbt:"value"`
}
type NestedCompoundCont struct {
Ham NestedCompound `nbt:"ham"`
Egg NestedCompound `nbt:"egg"`
}
type ListCompound struct {
Name string `nbt:"name"`
CreatedOn int64 `nbt:"created-on"`
}
val := struct {
LongTest int64 `nbt:"longTest"`
ShortTest int16 `nbt:"shortTest"`
StringTest string `nbt:"stringTest"`
FloatTest float32 `nbt:"floatTest"`
IntTest int32 `nbt:"intTest"`
NestedCompoundTest NestedCompoundCont `nbt:"nested compound test"`
ListTestLong []int64 `nbt:"listTest (long)" nbt_type:"noarray"`
ListTestCompound []ListCompound `nbt:"listTest (compound)"`
ByteTest byte `nbt:"byteTest"`
ByteArrayTest []byte `nbt:"byteArrayTest (the first 1000 values of (n*n*255+n*7)%100, starting with n=0 (0, 62, 34, 16, 8, ...))"`
DoubleTest float64 `nbt:"doubleTest"`
}{
LongTest: 9223372036854775807,
ShortTest: 32767,
StringTest: "HELLO WORLD THIS IS A TEST STRING \xc3\x85\xc3\x84\xc3\x96!",
FloatTest: 0.49823147058486938,
IntTest: 2147483647,
NestedCompoundTest: NestedCompoundCont{
NestedCompound{"Hampus", 0.75},
NestedCompound{"Eggbert", 0.5},
},
ListTestLong: []int64{11, 12, 13, 14, 15},
ListTestCompound: []ListCompound{
{"Compound tag #0", 1264099775885},
{"Compound tag #1", 1264099775885},
},
ByteTest: 127,
ByteArrayTest: byteValues,
DoubleTest: 0.49312871321823148,
}
var b bytes.Buffer
err := MarshalCompound(&b, val, "Level")
if err != nil {
t.Error(err)
}
want, err := ioutil.ReadFile("bigtest.nbt")
if err != nil {
t.Error(err)
}
err = ioutil.WriteFile("bigtest_got.nbt", b.Bytes(), 0644)
if err != nil {
t.Error(err)
}
if !bytes.Equal(b.Bytes(), want) {
t.Errorf("got:\n[% 2x]\nwant:\n[% 2x]", b.Bytes(), want)
}
}

View File

@ -3,6 +3,7 @@ package nbt
import ( import (
"bytes" "bytes"
"compress/gzip" "compress/gzip"
"io/ioutil"
"reflect" "reflect"
"testing" "testing"
) )
@ -60,9 +61,8 @@ func TestUnmarshal_simple(t *testing.T) {
} }
} }
func TestUnmarshal_bitTest(t *testing.T) {
// Generated by vscode-hexdump // Generated by vscode-hexdump
data := []byte{ var bigTestData = [...]byte{
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0xed, 0x54, 0xcf, 0x4f, 0x1a, 0x41, 0x00, 0x00, 0xed, 0x54, 0xcf, 0x4f, 0x1a, 0x41,
0x14, 0x7e, 0xc2, 0x02, 0xcb, 0x96, 0x82, 0xb1, 0x14, 0x7e, 0xc2, 0x02, 0xcb, 0x96, 0x82, 0xb1,
@ -129,42 +129,34 @@ func TestUnmarshal_bitTest(t *testing.T) {
0x06, 0x00, 0x00, 0x06, 0x00, 0x00,
} }
type BitTestStruct struct { type BigTestStruct struct {
LongTest int64 `nbt:"longTest"`
ShortTest int16 `nbt:"shortTest"`
StringTest string `nbt:"stringTest"`
FloatTest float32 `nbt:"floatTest"`
IntTest int32 `nbt:"intTest"`
NCT struct { NCT struct {
Egg struct {
Name string `nbt:"name"`
Value float32 `nbt:"value"`
} `nbt:"egg"`
Ham struct { Ham struct {
Name string `nbt:"name"` Name string `nbt:"name"`
Value float32 `nbt:"value"` Value float32 `nbt:"value"`
} `nbt:"ham"` } `nbt:"ham"`
} `nbt:"nested compound test"` Egg struct {
IntTest int `nbt:"intTest"`
ByteTest byte `nbt:"byteTest"`
StringTest string `nbt:"stringTest"`
ListTest []int64 `nbt:"listTest (long)"`
DoubleTest float64 `nbt:"doubleTest"`
LongTest int64 `nbt:"longTest"`
ListTest2 [2]struct {
CreatedOn int64 `nbt:"created-on"`
Name string `nbt:"name"` Name string `nbt:"name"`
Value float32 `nbt:"value"`
} `nbt:"egg"`
} `nbt:"nested compound test"`
ListTest []int64 `nbt:"listTest (long)" nbt_type:"noarray"`
ListTest2 [2]struct {
Name string `nbt:"name"`
CreatedOn int64 `nbt:"created-on"`
} `nbt:"listTest (compound)"` } `nbt:"listTest (compound)"`
ByteTest byte `nbt:"byteTest"`
ByteArrayTest []byte `nbt:"byteArrayTest (the first 1000 values of (n*n*255+n*7)%100, starting with n=0 (0, 62, 34, 16, 8, ...))"` ByteArrayTest []byte `nbt:"byteArrayTest (the first 1000 values of (n*n*255+n*7)%100, starting with n=0 (0, 62, 34, 16, 8, ...))"`
ShortTest int16 `nbt:"shortTest"` DoubleTest float64 `nbt:"doubleTest"`
} }
//test parse func MakeBigTestStruct() BigTestStruct {
var value BitTestStruct var want BigTestStruct
r, err := gzip.NewReader(bytes.NewReader(data))
if err != nil {
t.Fatal(err)
}
if err := NewDecoder(r).Decode(&value); err != nil {
t.Fatal(err)
}
var want BitTestStruct
want.NCT.Egg.Name = "Eggbert" want.NCT.Egg.Name = "Eggbert"
want.NCT.Egg.Value = 0.5 want.NCT.Egg.Value = 0.5
want.NCT.Ham.Name = "Hampus" want.NCT.Ham.Name = "Hampus"
@ -174,6 +166,7 @@ func TestUnmarshal_bitTest(t *testing.T) {
want.StringTest = "HELLO WORLD THIS IS A TEST STRING \xc3\x85\xc3\x84\xc3\x96!" want.StringTest = "HELLO WORLD THIS IS A TEST STRING \xc3\x85\xc3\x84\xc3\x96!"
want.ListTest = []int64{11, 12, 13, 14, 15} want.ListTest = []int64{11, 12, 13, 14, 15}
want.DoubleTest = 0.49312871321823148 want.DoubleTest = 0.49312871321823148
want.FloatTest = 0.49823147058486938
want.LongTest = 9223372036854775807 want.LongTest = 9223372036854775807
want.ListTest2[0].CreatedOn = 1264099775885 want.ListTest2[0].CreatedOn = 1264099775885
want.ListTest2[0].Name = "Compound tag #0" want.ListTest2[0].Name = "Compound tag #0"
@ -184,14 +177,28 @@ func TestUnmarshal_bitTest(t *testing.T) {
want.ByteArrayTest[n] = byte((n*n*255 + n*7) % 100) want.ByteArrayTest[n] = byte((n*n*255 + n*7) % 100)
} }
want.ShortTest = 32767 want.ShortTest = 32767
return want
}
func TestUnmarshal_bigTest(t *testing.T) {
//test parse
var value BigTestStruct
r, err := gzip.NewReader(bytes.NewReader(bigTestData[:]))
if err != nil {
t.Fatal(err)
}
if err := NewDecoder(r).Decode(&value); err != nil {
t.Fatal(err)
}
want := MakeBigTestStruct()
if !reflect.DeepEqual(value, want) { if !reflect.DeepEqual(value, want) {
t.Errorf("parse fail, expect %v, get %v", want, value) t.Errorf("parse fail, expect %v, get %v", want, value)
} }
//test rawRead //test rawRead
var empty struct{} var empty struct{}
r, err = gzip.NewReader(bytes.NewReader(data)) r, err = gzip.NewReader(bytes.NewReader(bigTestData[:]))
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -199,9 +206,8 @@ func TestUnmarshal_bitTest(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
//test unmarshal to interface{}
var inf interface{} var inf interface{}
r, err = gzip.NewReader(bytes.NewReader(data)) r, err = gzip.NewReader(bytes.NewReader(bigTestData[:]))
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -211,6 +217,24 @@ func TestUnmarshal_bitTest(t *testing.T) {
// t.Log(inf) // t.Log(inf)
} }
func TestMarshal_bigTest(t *testing.T) {
var b bytes.Buffer
err := MarshalCompound(&b, MakeBigTestStruct(), "Level")
if err != nil {
t.Error(err)
}
rd, _ := gzip.NewReader(bytes.NewReader(bigTestData[:]))
want, err := ioutil.ReadAll(rd)
if err != nil {
t.Error(err)
}
if !bytes.Equal(b.Bytes(), want) {
t.Errorf("got:\n[% 2x]\nwant:\n[% 2x]", b.Bytes(), want)
}
}
func TestUnmarshal_IntArray(t *testing.T) { func TestUnmarshal_IntArray(t *testing.T) {
data := []byte{ data := []byte{
TagIntArray, 0, 0, TagIntArray, 0, 0,