Add more tests for snbt parser

This commit is contained in:
Tnze
2023-11-14 21:10:26 +08:00
parent 44d33f6e51
commit 5ae7ff14ca
3 changed files with 32 additions and 15 deletions

View File

@ -21,8 +21,6 @@ func writeValue(e *Encoder, d *decodeState, ifWriteTag bool, tagName string) err
switch d.opcode {
case scanError:
return d.error(d.scan.errContext)
default:
panic(phasePanicMsg)
case scanBeginLiteral:
start := d.readIndex()
@ -52,30 +50,32 @@ func writeValue(e *Encoder, d *decodeState, ifWriteTag bool, tagName string) err
case scanBeginList:
_, err := writeListOrArray(e, d, ifWriteTag, tagName)
return err
default:
panic(phasePanicMsg)
}
}
func writeLiteralPayload(e *Encoder, v any) (err error) {
switch v.(type) {
switch v := v.(type) {
case string:
str := v.(string)
err = writeInt16(e.w, int16(len(str)))
err = writeInt16(e.w, int16(len(v)))
if err != nil {
return
}
_, err = e.w.Write([]byte(str))
_, err = e.w.Write([]byte(v))
case int8:
_, err = e.w.Write([]byte{byte(v.(int8))})
_, err = e.w.Write([]byte{byte(v)})
case int16:
err = writeInt16(e.w, v.(int16))
err = writeInt16(e.w, v)
case int32:
err = writeInt32(e.w, v.(int32))
err = writeInt32(e.w, v)
case int64:
err = writeInt64(e.w, v.(int64))
err = writeInt64(e.w, v)
case float32:
err = writeInt32(e.w, int32(math.Float32bits(v.(float32))))
err = writeInt32(e.w, int32(math.Float32bits(v)))
case float64:
err = writeInt64(e.w, int64(math.Float64bits(v.(float64))))
err = writeInt64(e.w, int64(math.Float64bits(v)))
}
return
}
@ -508,7 +508,7 @@ func parseLiteral(literal []byte) (byte, any, error) {
case 'S', 's':
num, err := strconv.ParseInt(string(literal[:strlen]), 10, 16)
return TagShort, int16(num), err
default:
case 'I', 'i', 0:
num, err := strconv.ParseInt(string(literal[:strlen]), 10, 32)
return TagInt, int32(num), err
case 'L', 'l':

View File

@ -2,6 +2,7 @@ package nbt
import (
"bytes"
"io"
"strings"
"testing"
)
@ -41,7 +42,7 @@ var testCases = []testCase{
{`[B; ]`, TagByteArray, []byte{7, 0, 0, 0, 0, 0, 0}},
{`[B; 1b ,2B,3B]`, TagByteArray, []byte{7, 0, 0, 0, 0, 0, 3, 1, 2, 3}},
{`[I;]`, TagIntArray, []byte{11, 0, 0, 0, 0, 0, 0}},
{`[I; 1, 2 ,3]`, TagIntArray, []byte{11, 0, 0, 0, 0, 0, 3, 0, 0, 0, 1, 0, 0, 0, 2, 0, 0, 0, 3}},
{`[I; -1, 0, 1, 2 ,3]`, TagIntArray, []byte{11, 0, 0, 0, 0, 0, 5, 255, 255, 255, 255, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 2, 0, 0, 0, 3}},
{`[L;]`, TagLongArray, []byte{12, 0, 0, 0, 0, 0, 0}},
{`[ L; 1L,2L,3L]`, TagLongArray, []byte{12, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 3}},
{`{a:[B;]}`, TagCompound, []byte{10, 0, 0, 7, 0, 1, 'a', 0, 0, 0, 0, 0}},
@ -82,6 +83,20 @@ func TestEncoder_writeSNBT(t *testing.T) {
}
}
func TestEncoder_ErrorInput(t *testing.T) {
for _, s := range []string{
"][",
"[I; 1, b, 3]",
"[I; 1, -2a, 3]",
"[I; 1, -2/I, 3]",
} {
err := NewEncoder(io.Discard).Encode(StringifiedMessage(s), "")
if err == nil {
t.Errorf("String %q is not valid SNBT, expeted to got a error", s)
}
}
}
func TestEncoder_WriteSNBT_bigTest(t *testing.T) {
var buf bytes.Buffer
e := NewEncoder(&buf)

View File

@ -74,6 +74,8 @@ func TestSNBT_list(t *testing.T) {
`[B,C,D]`, `[L, "abc"]`, // List of string (like array)
`[B; 01B, 02B, 3B, 10B, 127B]`, // Array
`[I;]`, `[B; ]`, // Empty array
`[I; 1, 2, 3, -1, -2, -3]`, // Int array with negtive numbers
`[L; 123L, -123L]`, // Long array with negtive numbers
`{a:[],b:[B;]}`, // List or Array in TagCompound
}
var s scanner