mirror of
https://github.com/jwetzell/osc-go.git
synced 2026-09-06 14:09:00 +00:00
add layout for testing OSCBundle error cases
This commit is contained in:
@@ -34,11 +34,11 @@ func (b *OSCBundle) ToBytes() ([]byte, error) {
|
|||||||
|
|
||||||
func BundleFromBytes(bytes []byte) (*OSCBundle, []byte, error) {
|
func BundleFromBytes(bytes []byte) (*OSCBundle, []byte, error) {
|
||||||
if len(bytes) < 20 {
|
if len(bytes) < 20 {
|
||||||
return nil, bytes, errors.New("bundle has to be at least 20 bytes")
|
return nil, bytes, errors.New("OSC Bundle has to be at least 20 bytes")
|
||||||
}
|
}
|
||||||
|
|
||||||
if bytes[0] != 35 {
|
if bytes[0] != 35 {
|
||||||
return nil, bytes, errors.New("bundle must start with a #")
|
return nil, bytes, errors.New("OSC Bundle must start with a #")
|
||||||
}
|
}
|
||||||
|
|
||||||
bundleHeader, bytesAfterBundleHeader, err := readOSCString(bytes)
|
bundleHeader, bytesAfterBundleHeader, err := readOSCString(bytes)
|
||||||
@@ -48,7 +48,7 @@ func BundleFromBytes(bytes []byte) (*OSCBundle, []byte, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if bundleHeader != "#bundle" {
|
if bundleHeader != "#bundle" {
|
||||||
return nil, bytesAfterBundleHeader, errors.New("bundle must start with #bundle string")
|
return nil, bytesAfterBundleHeader, errors.New("OSC Bundle must start with #bundle string")
|
||||||
}
|
}
|
||||||
|
|
||||||
timeTag, bytesAfterTimeTag, err := readOSCTimeTag(bytesAfterBundleHeader)
|
timeTag, bytesAfterTimeTag, err := readOSCTimeTag(bytesAfterBundleHeader)
|
||||||
|
|||||||
+74
-5
@@ -5,7 +5,7 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestOSCBundleEncoding(t *testing.T) {
|
func TestGoodOSCBundleEncoding(t *testing.T) {
|
||||||
|
|
||||||
testCases := []struct {
|
testCases := []struct {
|
||||||
name string
|
name string
|
||||||
@@ -13,15 +13,15 @@ func TestOSCBundleEncoding(t *testing.T) {
|
|||||||
expected []byte
|
expected []byte
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
"simple contents single message",
|
name: "simple contents single message",
|
||||||
&OSCBundle{
|
bundle: &OSCBundle{
|
||||||
TimeTag: OSCTimeTag{
|
TimeTag: OSCTimeTag{
|
||||||
seconds: 32,
|
seconds: 32,
|
||||||
fractionalSeconds: 0,
|
fractionalSeconds: 0,
|
||||||
},
|
},
|
||||||
Contents: []OSCPacket{&OSCMessage{Address: "/oscillator/4/frequency", Args: []OSCArg{{Type: "f", Value: float32(440)}}}},
|
Contents: []OSCPacket{&OSCMessage{Address: "/oscillator/4/frequency", Args: []OSCArg{{Type: "f", Value: float32(440)}}}},
|
||||||
},
|
},
|
||||||
[]byte{35, 98, 117, 110, 100, 108, 101, 0, 0, 0, 0,
|
expected: []byte{35, 98, 117, 110, 100, 108, 101, 0, 0, 0, 0,
|
||||||
32, 0, 0, 0, 0, 0, 0, 0, 32, 47, 111,
|
32, 0, 0, 0, 0, 0, 0, 0, 32, 47, 111,
|
||||||
115, 99, 105, 108, 108, 97, 116, 111, 114, 47, 52,
|
115, 99, 105, 108, 108, 97, 116, 111, 114, 47, 52,
|
||||||
47, 102, 114, 101, 113, 117, 101, 110, 99, 121, 0,
|
47, 102, 114, 101, 113, 117, 101, 110, 99, 121, 0,
|
||||||
@@ -45,7 +45,31 @@ func TestOSCBundleEncoding(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestOSCBundleDecoding(t *testing.T) {
|
func TestBadOSCBundleEncoding(t *testing.T) {
|
||||||
|
|
||||||
|
testCases := []struct {
|
||||||
|
name string
|
||||||
|
bundle *OSCBundle
|
||||||
|
errorString string
|
||||||
|
}{}
|
||||||
|
|
||||||
|
for _, testCase := range testCases {
|
||||||
|
t.Run(testCase.name, func(t *testing.T) {
|
||||||
|
|
||||||
|
got, err := testCase.bundle.ToBytes()
|
||||||
|
|
||||||
|
if err == nil {
|
||||||
|
t.Fatalf("OSCBundle.ToBytes() expected to fail but got: %+v", got)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err.Error() != testCase.errorString {
|
||||||
|
t.Fatalf("OSCBundle.ToBytes() got error '%s', expected '%s'", err.Error(), testCase.errorString)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestGoodOSCBundleDecoding(t *testing.T) {
|
||||||
testCases := []struct {
|
testCases := []struct {
|
||||||
name string
|
name string
|
||||||
expected *OSCBundle
|
expected *OSCBundle
|
||||||
@@ -86,3 +110,48 @@ func TestOSCBundleDecoding(t *testing.T) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestBadOSCBundleDecoding(t *testing.T) {
|
||||||
|
testCases := []struct {
|
||||||
|
name string
|
||||||
|
bytes []byte
|
||||||
|
errorString string
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "empty byte array",
|
||||||
|
bytes: []byte{},
|
||||||
|
errorString: "OSC Bundle has to be at least 20 bytes",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "does not start with #",
|
||||||
|
bytes: []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
|
||||||
|
errorString: "OSC Bundle must start with a #",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "does not start with #bundle",
|
||||||
|
bytes: []byte{35, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
|
||||||
|
errorString: "OSC Bundle must start with #bundle string",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "bundle header not properly null terminated",
|
||||||
|
bytes: []byte{35, 98, 117, 110, 100, 108, 101, 35, 0, 0, 0,
|
||||||
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||||
|
0, 0, 0, 0},
|
||||||
|
errorString: "OSC Bundle must start with #bundle string",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, testCase := range testCases {
|
||||||
|
t.Run(testCase.name, func(t *testing.T) {
|
||||||
|
got, _, err := BundleFromBytes(testCase.bytes)
|
||||||
|
|
||||||
|
if err == nil {
|
||||||
|
t.Fatalf("BundleFromBytes expected to fail but got: %+v", got)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err.Error() != testCase.errorString {
|
||||||
|
t.Fatalf("BundleFromBytes got error '%s', expected '%s'", err.Error(), testCase.errorString)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user