Skip to content

Commit 44b41d9

Browse files
committed
added more tests
1 parent 63f85a4 commit 44b41d9

12 files changed

+130
-170
lines changed

Makefile

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
test:
22
@go test -coverprofile cover.out
33

4-
.PHONY: test
4+
coverage:
5+
@go test -coverprofile cover.out && go tool cover -html=cover.out
6+
7+
.PHONY: coverage test

argument.go

+17
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package osc
22

33
import (
44
"bytes"
5+
"encoding/base64"
56
"encoding/binary"
67
"fmt"
78
"io"
@@ -19,6 +20,7 @@ type Argument interface {
1920
ReadBool() (bool, error)
2021
ReadString() (string, error)
2122
ReadBlob() ([]byte, error)
23+
String() string
2224
Typetag() byte
2325
}
2426

@@ -49,6 +51,9 @@ func (i Int) ReadString() (string, error) { return "", ErrInvalidTypeTag }
4951
// ReadBlob reads a slice of bytes from the arg.
5052
func (i Int) ReadBlob() ([]byte, error) { return nil, ErrInvalidTypeTag }
5153

54+
// String converts the arg to a string.
55+
func (i Int) String() string { return fmt.Sprintf("Int(%d)", i) }
56+
5257
// Typetag returns the argument's type tag.
5358
func (i Int) Typetag() byte { return TypetagInt }
5459

@@ -85,6 +90,9 @@ func (f Float) ReadString() (string, error) { return "", ErrInvalidTypeTag }
8590
// ReadBlob reads a slice of bytes from the arg.
8691
func (f Float) ReadBlob() ([]byte, error) { return nil, ErrInvalidTypeTag }
8792

93+
// String converts the arg to a string.
94+
func (f Float) String() string { return fmt.Sprintf("Float(%f)", f) }
95+
8896
// Typetag returns the argument's type tag.
8997
func (f Float) Typetag() byte { return TypetagFloat }
9098

@@ -121,6 +129,9 @@ func (b Bool) ReadString() (string, error) { return "", ErrInvalidTypeTag }
121129
// ReadBlob reads a slice of bytes from the arg.
122130
func (b Bool) ReadBlob() ([]byte, error) { return nil, ErrInvalidTypeTag }
123131

132+
// String converts the arg to a string.
133+
func (b Bool) String() string { return fmt.Sprintf("Bool(%t)", b) }
134+
124135
// Typetag returns the argument's type tag.
125136
func (b Bool) Typetag() byte {
126137
if bool(b) {
@@ -162,6 +173,9 @@ func (s String) ReadString() (string, error) { return string(s), nil }
162173
// ReadBlob reads a slice of bytes from the arg.
163174
func (s String) ReadBlob() ([]byte, error) { return nil, ErrInvalidTypeTag }
164175

176+
// String converts the arg to a string.
177+
func (s String) String() string { return string(s) }
178+
165179
// Typetag returns the argument's type tag.
166180
func (s String) Typetag() byte { return TypetagString }
167181

@@ -200,6 +214,9 @@ func (b Blob) ReadString() (string, error) { return "", ErrInvalidTypeTag }
200214
// ReadBlob reads a slice of bytes from the arg.
201215
func (b Blob) ReadBlob() ([]byte, error) { return []byte(b), nil }
202216

217+
// String converts the arg to a string.
218+
func (b Blob) String() string { return base64.StdEncoding.EncodeToString([]byte(b)) }
219+
203220
// Typetag returns the argument's type tag.
204221
func (b Blob) Typetag() byte { return TypetagBlob }
205222

argument_test.go

+56-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package osc
22

33
import (
44
"bytes"
5+
"encoding/base64"
56
"io"
67
"io/ioutil"
78
"testing"
@@ -13,6 +14,15 @@ func TestInt(t *testing.T) {
1314
if err != nil {
1415
t.Fatal(err)
1516
}
17+
if other := Int(0); !arg.Equal(other) {
18+
t.Fatal("expected %s to equal %s", arg, other)
19+
}
20+
if other := Int(2); arg.Equal(other) {
21+
t.Fatalf("expected %s to not equal %s", arg, other)
22+
}
23+
if other := String("foo"); arg.Equal(other) {
24+
t.Fatalf("expected %s to not equal %s", arg, other)
25+
}
1626
if expected, got := int32(0), i; expected != got {
1727
t.Fatalf("expected %d, got %d", expected, got)
1828
}
@@ -28,6 +38,9 @@ func TestInt(t *testing.T) {
2838
if _, err := arg.ReadBlob(); err != ErrInvalidTypeTag {
2939
t.Fatalf("expected ErrInvalidTypeTag, got %+v", err)
3040
}
41+
if expected, got := "Int(0)", arg.String(); expected != got {
42+
t.Fatalf("expected %s to equal %s", expected, got)
43+
}
3144
if expected, got := TypetagInt, arg.Typetag(); expected != got {
3245
t.Fatalf("expected %c, got %c", expected, got)
3346
}
@@ -42,6 +55,15 @@ func TestFloat(t *testing.T) {
4255
if err != nil {
4356
t.Fatal(err)
4457
}
58+
if other := Float(0); !arg.Equal(other) {
59+
t.Fatal("expected %s to equal %s", arg, other)
60+
}
61+
if other := Float(3.14); arg.Equal(other) {
62+
t.Fatal("expected %s to not equal %s", arg, other)
63+
}
64+
if other := String("foo"); arg.Equal(other) {
65+
t.Fatal("expected %s to not equal %s", arg, other)
66+
}
4567
if expected, got := float32(0), f; expected != got {
4668
t.Fatalf("expected %f, got %f", expected, got)
4769
}
@@ -57,6 +79,9 @@ func TestFloat(t *testing.T) {
5779
if _, err := arg.ReadBlob(); err != ErrInvalidTypeTag {
5880
t.Fatalf("expected ErrInvalidTypeTag, got %+v", err)
5981
}
82+
if expected, got := "Float(0.000000)", arg.String(); expected != got {
83+
t.Fatalf("expected %s to equal %s", expected, got)
84+
}
6085
if expected, got := TypetagFloat, arg.Typetag(); expected != got {
6186
t.Fatalf("expected %c, got %c", expected, got)
6287
}
@@ -71,6 +96,12 @@ func TestBool(t *testing.T) {
7196
if err != nil {
7297
t.Fatal(err)
7398
}
99+
if other := Bool(false); !arg.Equal(other) {
100+
t.Fatal("expected %s to equal %s", arg, other)
101+
}
102+
if other := Int(3); arg.Equal(other) {
103+
t.Fatal("expected %s to not equal %s", arg, other)
104+
}
74105
if expected, got := false, b; expected != got {
75106
t.Fatalf("expected %f, got %f", expected, got)
76107
}
@@ -86,6 +117,9 @@ func TestBool(t *testing.T) {
86117
if _, err := arg.ReadBlob(); err != ErrInvalidTypeTag {
87118
t.Fatalf("expected ErrInvalidTypeTag, got %+v", err)
88119
}
120+
if expected, got := "Bool(false)", arg.String(); expected != got {
121+
t.Fatalf("expected %s to equal %s", expected, got)
122+
}
89123
if expected, got := TypetagFalse, arg.Typetag(); expected != got {
90124
t.Fatalf("expected %c, got %c", expected, got)
91125
}
@@ -106,8 +140,14 @@ func TestString(t *testing.T) {
106140
if err != nil {
107141
t.Fatal(err)
108142
}
143+
if other := String("foo"); !arg.Equal(other) {
144+
t.Fatalf("expected %s to equal %s", arg, other)
145+
}
146+
if other := Int(4); arg.Equal(other) {
147+
t.Fatal("expected %s to not equal %s", arg, other)
148+
}
109149
if expected, got := "foo", s; expected != got {
110-
t.Fatalf("expected %f, got %f", expected, got)
150+
t.Fatalf("expected %s, got %s", expected, got)
111151
}
112152
if _, err := arg.ReadInt32(); err != ErrInvalidTypeTag {
113153
t.Fatalf("expected ErrInvalidTypeTag, got %+v", err)
@@ -121,6 +161,9 @@ func TestString(t *testing.T) {
121161
if _, err := arg.ReadBlob(); err != ErrInvalidTypeTag {
122162
t.Fatalf("expected ErrInvalidTypeTag, got %+v", err)
123163
}
164+
if expected, got := "foo", arg.String(); expected != got {
165+
t.Fatalf("expected %s to equal %s", expected, got)
166+
}
124167
if expected, got := TypetagString, arg.Typetag(); expected != got {
125168
t.Fatalf("expected %c, got %c", expected, got)
126169
}
@@ -135,6 +178,15 @@ func TestBlob(t *testing.T) {
135178
if err != nil {
136179
t.Fatal(err)
137180
}
181+
if other := Blob([]byte{'f', 'o', 'o'}); !arg.Equal(other) {
182+
t.Fatalf("expected %s to equal %s", arg, other)
183+
}
184+
if other := Blob([]byte{'f'}); arg.Equal(other) {
185+
t.Fatalf("expected %s to not equal %s", arg, other)
186+
}
187+
if other := String("bar"); arg.Equal(other) {
188+
t.Fatalf("expected %s to not equal %s", arg, other)
189+
}
138190
if expected, got := []byte{'f', 'o', 'o'}, b; !bytes.Equal(expected, got) {
139191
t.Fatalf("expected %f, got %f", expected, got)
140192
}
@@ -150,6 +202,9 @@ func TestBlob(t *testing.T) {
150202
if _, err := arg.ReadString(); err != ErrInvalidTypeTag {
151203
t.Fatalf("expected ErrInvalidTypeTag, got %+v", err)
152204
}
205+
if expected, got := base64.StdEncoding.EncodeToString(b), arg.String(); expected != got {
206+
t.Fatalf("expected %s, got %s", expected, got)
207+
}
153208
if expected, got := TypetagBlob, arg.Typetag(); expected != got {
154209
t.Fatalf("expected %c, got %c", expected, got)
155210
}

bundle.go

+1-145
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
package osc
22

33
import (
4-
"bytes"
5-
"encoding/binary"
64
"errors"
7-
"fmt"
85
"net"
96
"time"
107
)
@@ -14,11 +11,6 @@ var (
1411
ErrEarlyTimetag = errors.New("enclosing bundle's timetag was later than the nested bundle's")
1512
)
1613

17-
var (
18-
bundlePrefix = []byte{'#', 'b', 'u', 'n', 'd', 'l', 'e', 0}
19-
bundlePrefixLen = len(bundlePrefix)
20-
)
21-
2214
// Bundle is an OSC bundle.
2315
// An OSC Bundle consists of the OSC-string "#bundle" followed by an OSC Time Tag,
2416
// followed by zero or more bundle elements. The OSC-timetag is a 64-bit fixed
@@ -40,141 +32,5 @@ func NewBundle(t time.Time, packets ...Packet) *Bundle {
4032
// Contents returns the contents of the bundle as a
4133
// slice of bytes.
4234
func (b *Bundle) Contents() ([]byte, error) {
43-
var (
44-
buf = &bytes.Buffer{}
45-
bytesWritten int64
46-
)
47-
48-
// Add the '#bundle' string
49-
bw, err := buf.Write(bundlePrefix)
50-
if err != nil {
51-
return nil, err
52-
}
53-
bytesWritten += int64(bw)
54-
55-
for i := bytesWritten; i%4 != 0; i++ {
56-
if err := buf.WriteByte(0); err != nil {
57-
return nil, err
58-
}
59-
bytesWritten++
60-
}
61-
62-
// Add the timetag
63-
if err := binary.Write(buf, byteOrder, b.Timetag); err != nil {
64-
return nil, err
65-
}
66-
bytesWritten += 8
67-
68-
// Process all OSC Messages
69-
for _, p := range b.Packets {
70-
contents, err := p.Contents()
71-
if err != nil {
72-
return nil, err
73-
}
74-
75-
size := int32(len(contents))
76-
if err := binary.Write(buf, byteOrder, size); err != nil {
77-
return nil, err
78-
}
79-
80-
bw, err := buf.Write(contents)
81-
if err != nil {
82-
return nil, err
83-
}
84-
bytesWritten += int64(bw)
85-
}
86-
87-
return buf.Bytes(), nil
88-
}
89-
90-
// Invoke invokes an OSC method for each element of a
91-
// bundle recursively.
92-
// If the timetag of the receiver is in the future, then this
93-
// method will block until its time has come.
94-
func (b *Bundle) Invoke(address string, method Method) error {
95-
until := b.Timetag.Time().Sub(time.Now())
96-
97-
if until > 0 {
98-
time.Sleep(until)
99-
}
100-
101-
for _, p := range b.Packets {
102-
if msg, ok := p.(*Message); ok {
103-
matched, err := msg.Match(address)
104-
if err != nil {
105-
return err
106-
}
107-
if matched {
108-
if err := method(msg); err != nil {
109-
return err
110-
}
111-
}
112-
continue
113-
}
114-
if bundle, ok := p.(*Bundle); ok {
115-
if bundle.Timetag < b.Timetag {
116-
return ErrEarlyTimetag
117-
}
118-
if err := bundle.Invoke(address, method); err != nil {
119-
return err
120-
}
121-
}
122-
}
123-
return nil
124-
}
125-
126-
// parseBundle parses an OSC bundle from a slice of bytes.
127-
func parseBundle(data []byte, sender net.Addr) (*Bundle, error) {
128-
var (
129-
i = 0
130-
b = &Bundle{Sender: sender}
131-
)
132-
if len(data) < len(bundlePrefix) {
133-
return nil, fmt.Errorf("invalid bundle: %q", data)
134-
}
135-
if prefix := data[0:bundlePrefixLen]; 0 != bytes.Compare(prefix, bundlePrefix) {
136-
return nil, fmt.Errorf("invalid bundle prefix: %q", prefix)
137-
}
138-
i = bundlePrefixLen
139-
140-
timetag, err := parseTimetag(data[i:])
141-
if err != nil {
142-
return nil, err
143-
}
144-
b.Timetag = timetag
145-
i += TimetagSize
146-
147-
var (
148-
r = bytes.NewReader(data[i:])
149-
size int32
150-
)
151-
j := 0
152-
153-
ReadPackets:
154-
for err := binary.Read(r, byteOrder, &size); err == nil; err = binary.Read(r, byteOrder, &size) {
155-
i += 4
156-
switch data[i] {
157-
case MessageChar:
158-
pkt, err := parseMessage(data[i:], sender)
159-
if err != nil {
160-
return nil, err
161-
}
162-
b.Packets = append(b.Packets, pkt)
163-
case BundleTag[0]:
164-
pkt, err := parseBundle(data[i:], sender)
165-
if err != nil {
166-
return nil, err
167-
}
168-
b.Packets = append(b.Packets, pkt)
169-
case 0:
170-
break ReadPackets
171-
default:
172-
return nil, ErrParse
173-
}
174-
j++
175-
i += int(size)
176-
r = bytes.NewReader(data[i:])
177-
}
178-
179-
return b, nil
35+
return []byte{}, nil
18036
}

bundle_test.go

+9
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,16 @@ package osc
22

33
import (
44
"testing"
5+
"time"
56
)
67

78
func TestBundle(t *testing.T) {
9+
msg, err := NewMessage("/foo")
10+
if err != nil {
11+
t.Fatal(err)
12+
}
13+
b := NewBundle(time.Now(), msg)
14+
if _, err := b.Contents(); err != nil {
15+
t.Fatal(err)
16+
}
817
}

0 commit comments

Comments
 (0)