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

View File

@ -2,6 +2,7 @@ package nbt
import ( import (
"bytes" "bytes"
"io"
"strings" "strings"
"testing" "testing"
) )
@ -41,7 +42,7 @@ var testCases = []testCase{
{`[B; ]`, TagByteArray, []byte{7, 0, 0, 0, 0, 0, 0}}, {`[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}}, {`[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;]`, 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;]`, 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}}, {`[ 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}}, {`{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) { func TestEncoder_WriteSNBT_bigTest(t *testing.T) {
var buf bytes.Buffer var buf bytes.Buffer
e := NewEncoder(&buf) e := NewEncoder(&buf)

View File

@ -74,7 +74,9 @@ func TestSNBT_list(t *testing.T) {
`[B,C,D]`, `[L, "abc"]`, // List of string (like array) `[B,C,D]`, `[L, "abc"]`, // List of string (like array)
`[B; 01B, 02B, 3B, 10B, 127B]`, // Array `[B; 01B, 02B, 3B, 10B, 127B]`, // Array
`[I;]`, `[B; ]`, // Empty array `[I;]`, `[B; ]`, // Empty array
`{a:[],b:[B;]}`, // List or Array in TagCompound `[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 var s scanner
scan := func(str string) bool { scan := func(str string) bool {