GoSBHPF/SBHPFv1/serializer_test.go
2025-02-14 12:04:11 +01:00

101 lines
2.5 KiB
Go

package sbhpfv1_test
import (
"bytes"
"slices"
"testing"
sbhpfv1 "git.zervo.org/FLUX/GoSBHPF/SBHPFv1"
)
var (
prop_ser_targetbytes = []byte{
0x05, // Key length = 5
0x0c, // Value type = string
0x6f, 0x77, 0x6e, 0x65, 0x72, // Key = "owner"
0x05, 0x00, // String length = 5
0x7a, 0x65, 0x72, 0x76, 0x6f, // String = "zervo"
}
node_ser_targetbytes = []byte{
// ROOT NODE
0x33, 0x00, 0x00, 0x00, // Node size = 33
0x02, 0x00, // Property count = 2
0x01, 0x00, // Child count = 1
0x06, // Name length = 6
0x70, 0x6c, 0x61, 0x79, 0x65, 0x72, // Node name = "player"
// ROOT NODE -> PROPERTY A
0x06, // Key length = 6
0x0b, // Value type = bool
0x61, 0x63, 0x74, 0x69, 0x76, 0x65, // Key = "active"
0x01, // Value = true
// ROOT NODE -> PROPERTY B
0x05, // Key length = 5
0x03, // Value type = 16-bit signed int
0x6c, 0x65, 0x76, 0x65, 0x6c, // Key = "level"
0x1b, 0x00, // Value = 27
// ROOT NODE -> CHILD NODE
0x12, 0x00, 0x00, 0x00, // Node size = 12
0x00, 0x00, // Property count = 0
0x00, 0x00, // Child count = 0
0x09, // Name length = 9
0x69, 0x6e, 0x76, 0x65, 0x6e, 0x74, 0x6f, 0x72, 0x79, // Node name = "inventory"
}
)
func TestPropertySerialization(t *testing.T) {
prop := sbhpfv1.Property{
Key: "owner",
Type: sbhpfv1.TypeString,
Value: "zervo",
}
buf := &bytes.Buffer{}
err := sbhpfv1.SerializeProperty(buf, prop)
if err != nil {
t.Fatalf("Property serialization failed: %v", err)
}
if !slices.Equal(buf.Bytes(), prop_ser_targetbytes) {
t.Fatalf("Property serialization generated bad data: %v", buf.Bytes())
}
}
func TestNodeSerialization(t *testing.T) {
prop_a := sbhpfv1.Property{
Key: "active",
Type: sbhpfv1.TypeBool,
Value: true,
}
prop_b := sbhpfv1.Property{
Key: "level",
Type: sbhpfv1.TypeInt16,
Value: int16(27),
}
child_node := sbhpfv1.Node{
Name: "inventory",
}
node := sbhpfv1.Node{
Name: "player",
Properties: []sbhpfv1.Property{
prop_a,
prop_b,
},
Children: []*sbhpfv1.Node{
&child_node,
},
}
var w bytes.Buffer
err := sbhpfv1.SerializeNodeStream(&w, &node)
if err != nil {
t.Fatalf("Node serialization failed: %v", err)
}
if !slices.Equal(w.Bytes(), node_ser_targetbytes) {
t.Fatalf("Node serialization generated bad data: %v", w.Bytes())
}
}