-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnote_test.go
More file actions
68 lines (63 loc) · 1.64 KB
/
note_test.go
File metadata and controls
68 lines (63 loc) · 1.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package ultrastar
import (
"bytes"
"encoding/gob"
"fmt"
"testing"
)
func TestNote_String(t *testing.T) {
cases := map[string]struct {
note Note
expected string
}{
"regular note": {Note{NoteTypeRegular, 15, 4, 8, "go"}, ": 15 4 8 go"},
"note with spaces in text": {Note{NoteTypeGolden, 7, 1, -2, " hey "}, "* 7 1 -2 hey "},
"line break": {Note{NoteTypeEndOfPhrase, 12, 7, 3, "\n"}, "- 12"},
}
for name, c := range cases {
t.Run(name, func(t *testing.T) {
actual := c.note.String()
if actual != c.expected {
t.Errorf("%v.String() = %q, expected %q", c.note, actual, c.expected)
}
})
}
}
func TestNote_GobEncode(t *testing.T) {
cases := map[string]Note{
"regular note": {NoteTypeRegular, 15, 4, 8, "go"},
"note with spaces in text": {NoteTypeGolden, 7, 1, -2, " hey "},
"note with high numbers": {NoteTypeRap, 550, 20, -40, " hey "},
"line break": {NoteTypeEndOfPhrase, 12, 0, 0, "\n"},
}
for name, note := range cases {
t.Run(name, func(t *testing.T) {
buf := &bytes.Buffer{}
e := gob.NewEncoder(buf)
err := e.Encode(note)
if err != nil {
t.Fatalf("GobEncode(%v) caused an unexpected error: %s", note, err)
}
var n Note
d := gob.NewDecoder(buf)
err = d.Decode(&n)
if err != nil {
t.Fatalf("GobDecode() caused an unexpected error: %s", err)
}
if n != note {
t.Fatalf("GobDecode(GobEncode(%v)) = %v, expected %v", note, n, note)
}
})
}
}
func ExampleNote_String() {
n := Note{
Type: NoteTypeGolden,
Start: 15,
Duration: 4,
Pitch: 8,
Text: "Go",
}
fmt.Println(n.String())
// Output: * 15 4 8 Go
}