-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdecoder_test.go
173 lines (154 loc) · 3.84 KB
/
decoder_test.go
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package drum
import (
"crypto/sha256"
"encoding/hex"
"fmt"
"hash"
"io"
"os"
"path"
"testing"
)
func TestDecodeFile(t *testing.T) {
tData := []struct {
path string
output string
}{
{"pattern_1.splice",
`Saved with HW Version: 0.808-alpha
Tempo: 120
(0) kick |x---|x---|x---|x---|
(1) snare |----|x---|----|x---|
(2) clap |----|x-x-|----|----|
(3) hh-open |--x-|--x-|x-x-|--x-|
(4) hh-close |x---|x---|----|x--x|
(5) cowbell |----|----|--x-|----|
`,
},
{"pattern_2.splice",
`Saved with HW Version: 0.808-alpha
Tempo: 98.4
(0) kick |x---|----|x---|----|
(1) snare |----|x---|----|x---|
(3) hh-open |--x-|--x-|x-x-|--x-|
(5) cowbell |----|----|x---|----|
`,
},
{"pattern_3.splice",
`Saved with HW Version: 0.808-alpha
Tempo: 118
(40) kick |x---|----|x---|----|
(1) clap |----|x---|----|x---|
(3) hh-open |--x-|--x-|x-x-|--x-|
(5) low-tom |----|---x|----|----|
(12) mid-tom |----|----|x---|----|
(9) hi-tom |----|----|-x--|----|
`,
},
{"pattern_4.splice",
`Saved with HW Version: 0.909
Tempo: 240
(0) SubKick |----|----|----|----|
(1) Kick |x---|----|x---|----|
(99) Maracas |x-x-|x-x-|x-x-|x-x-|
(255) Low Conga |----|x---|----|x---|
`,
},
{"pattern_5.splice",
`Saved with HW Version: 0.708-alpha
Tempo: 999
(1) Kick |x---|----|x---|----|
(2) HiHat |x-x-|x-x-|x-x-|x-x-|
`,
},
}
for _, exp := range tData {
decoded, err := DecodeFile(path.Join("fixtures", exp.path))
if err != nil {
t.Fatalf("something went wrong decoding %s - %v", exp.path, err)
}
if fmt.Sprint(decoded) != exp.output {
t.Logf("decoded:\n%#v\n", fmt.Sprint(decoded))
t.Logf("expected:\n%#v\n", exp.output)
t.Fatalf("%s wasn't decoded as expect.\nGot:\n%s\nExpected:\n%s",
exp.path, decoded, exp.output)
}
}
}
func TestWriteFile(t *testing.T) {
inputFileName := "pattern_1.splice"
inputPath := path.Join("fixtures", inputFileName)
inputHash := fileSHA256(inputPath)
decoded, err := DecodeFile(inputPath)
if err != nil {
t.Fatalf("something went wrong decoding %s - %v", inputFileName, err)
}
outputFileName := inputFileName + "_tmp"
outputPath := path.Join("fixtures", outputFileName)
err = decoded.Write(outputPath)
defer os.Remove(outputPath)
if err != nil {
t.Fatal("Error writing", err)
}
outputHash := fileSHA256(outputPath)
if inputHash != outputHash {
t.Fatal("Output file does not match input file")
}
}
func TestModifyFile(t *testing.T) {
tData := []struct {
path string
output string
}{
{"pattern_1.splice",
`Saved with HW Version: 1.0-golang
Tempo: 240
(0) kick |x---|x---|x---|x---|
(1) snare |----|x---|----|x---|
(2) clap |----|x-x-|----|----|
(3) hh-open |--x-|--x-|x-x-|--x-|
(4) hh-close |x---|x---|----|x--x|
(5) cowbell |x-x-|x-x-|x-x-|x-x-|
`,
},
}
for _, exp := range tData {
decoded, err := DecodeFile(path.Join("fixtures", exp.path))
if err != nil {
t.Fatalf("something went wrong decoding %s - %v", exp.path, err)
}
decoded.Version = "1.0-golang"
decoded.Tempo = 240
for j := 0; j < 16; j++ {
decoded.Tracks[5].Set(j, j%2 == 0)
}
decoded.Write(path.Join("fixtures", exp.path+"_tmp"))
defer os.Remove(path.Join("fixtures", exp.path+"_tmp"))
modified, err := DecodeFile(path.Join("fixtures", exp.path+"_tmp"))
if err != nil {
t.Fatalf("something went wrong decoding %s - %v", exp.path, err)
}
if fmt.Sprint(modified) != exp.output {
t.Logf("decoded:\n%#v\n", fmt.Sprint(modified))
t.Logf("expected:\n%#v\n", exp.output)
t.Fatalf("%s wasn't decoded as expect.\nGot:\n%s\nExpected:\n%s",
exp.path, modified, exp.output)
}
}
}
func fileHash(path string, hasher hash.Hash) error {
f, err := os.Open(path)
if err != nil {
return err
}
defer f.Close()
if _, err := io.Copy(hasher, f); err != nil {
return err
}
return nil
}
func fileSHA256(path string) string {
hasher := sha256.New()
fileHash(path, hasher)
return hex.EncodeToString(hasher.Sum(nil))
}