-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtracklist_test.go
149 lines (118 loc) · 2.9 KB
/
tracklist_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
package tracklist
import (
"encoding/csv"
"errors"
"github.com/gieseladev/tracklist/common"
"github.com/gieseladev/tracklist/timestamp"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"io"
"io/ioutil"
"os"
"path/filepath"
"strings"
"testing"
)
type TestCase struct {
Name string
Text string
Expected List
}
func (c TestCase) Run(t *testing.T) {
actual, err := Parse(c.Text)
require.NoError(t, err)
assert.EqualValues(t, c.Expected, actual)
}
func loadTracklist(r io.Reader) (List, error) {
reader := csv.NewReader(r)
reader.ReuseRecord = true
tl := List{}
for {
record, err := reader.Read()
if err == io.EOF {
break
} else if err != nil {
return tl, err
}
hasEndOffset := len(record) == 3
if !hasEndOffset && len(record) != 2 {
return tl, errors.New("unexpected field count")
}
startOffset, err := timestamp.ParseTimestamp([]byte(strings.TrimSpace(record[0])))
if err != nil {
return tl, err
}
nameFieldIndex := 1
if hasEndOffset {
nameFieldIndex = 2
}
track := common.Track{
StartOffsetMS: 1000 * startOffset,
Name: record[nameFieldIndex],
}
if hasEndOffset {
endOffset, err := timestamp.ParseTimestamp([]byte(strings.TrimSpace(record[1])))
if err != nil {
return tl, err
}
track.EndOffsetMS = 1000 * endOffset
} else if len(tl.Tracks) > 0 {
tl.Tracks[len(tl.Tracks)-1].EndOffsetMS = track.StartOffsetMS
}
tl.Tracks = append(tl.Tracks, track)
}
return tl, nil
}
func LoadTestCase(textPath, tracklistPath string) (TestCase, error) {
textFile, err := os.Open(textPath)
if err != nil {
return TestCase{}, err
}
defer func() { _ = textFile.Close() }()
data, err := ioutil.ReadAll(textFile)
if err != nil {
return TestCase{}, err
}
testCase := TestCase{
Name: strings.TrimSuffix(filepath.Base(textPath), filepath.Ext(textPath)),
Text: string(data),
}
tracklistFile, err := os.Open(tracklistPath)
if err != nil {
return testCase, err
}
defer func() { _ = tracklistFile.Close() }()
tracklist, err := loadTracklist(tracklistFile)
if err != nil {
return testCase, err
}
testCase.Expected = tracklist
return testCase, nil
}
func LoadTestCases(dirname string) ([]TestCase, error) {
files, err := ioutil.ReadDir(dirname)
if err != nil {
return nil, err
}
if len(files)%2 != 0 {
return nil, errors.New("even amount of files required")
}
var cases []TestCase
for i := 1; i < len(files); i += 2 {
tracklistPath := filepath.Join(dirname, files[i-1].Name())
textPath := filepath.Join(dirname, files[i].Name())
testCase, err := LoadTestCase(textPath, tracklistPath)
if err != nil {
return cases, err
}
cases = append(cases, testCase)
}
return cases, nil
}
func TestParse(t *testing.T) {
testCases, err := LoadTestCases("test/data/tracklists")
require.NoError(t, err)
for _, testCase := range testCases {
t.Run(testCase.Name, testCase.Run)
}
}