-
Notifications
You must be signed in to change notification settings - Fork 2
/
song.spec.js
214 lines (182 loc) · 4.48 KB
/
song.spec.js
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
import {
forEachBarInSong,
forEachChordInSong,
forEachChordInChordLine,
getNthOfLabel,
} from '../../../../src/parser/helper/songs';
import parseSong from '../../../../src/parser/parseSong';
import parseChordLine from '../../../../src/parser/parseChordLine';
const song = `
E E... Eb.
You don't have to be rich to be my girl
D D... Eb.
You don't have to be cool to rule my world
E E... Eb.
Ain't no particular sign I'm more compatible with
D D... Eb.
I just want your extra time and your...Kiss`;
describe('forEachBarInSong', () => {
test('Does not mutate input', () => {
const parsed = parseSong(song);
const applied = forEachBarInSong(parsed.allLines, () => true);
expect(parsed).not.toBe(applied);
});
test('Should apply function on each bar', () => {
expect.assertions(8);
const parsed = parseSong(song);
const applied = forEachBarInSong(
parsed.allLines,
(bar) => (bar.applied = true)
);
applied.forEach((line) => {
if (line.type === 'chord') {
line.model.allBars.forEach((bar) => {
expect(bar.applied).toBe(true);
});
}
});
});
});
describe('forEachChordInSong', () => {
test('Module', () => {
expect(forEachChordInSong).toBeInstanceOf(Function);
});
test('Does not mutate input', () => {
const parsed = parseSong(song);
const applied = forEachChordInSong(parsed.allLines, () => true);
expect(parsed).not.toBe(applied);
});
test('Should apply function on each chord', () => {
expect.assertions(12);
const parsed = parseSong(song);
const applied = forEachChordInSong(
parsed.allLines,
(chord) => (chord.applied = true)
);
applied.forEach((line) => {
if (line.type === 'chord') {
line.model.allBars.forEach((bar) => {
bar.allChords.forEach((chord) => {
expect(chord.applied).toBe(true);
});
});
}
});
});
});
describe('forEachChordInChordLine', () => {
test('Module', () => {
expect(forEachChordInChordLine).toBeInstanceOf(Function);
});
test('Does not mutate input', () => {
const chordLine = 'Am... G/B. C';
const parsed = parseChordLine(chordLine);
const applied = forEachChordInChordLine(parsed, () => true);
expect(parsed).not.toBe(applied);
});
test('Should apply function on each chord', () => {
expect.assertions(3);
const chordLine = 'Am... G/B. C';
const parsed = parseChordLine(chordLine);
const applied = forEachChordInChordLine(
parsed,
(chord) => (chord.applied = true)
);
applied.allBars.forEach((bar) => {
bar.allChords.forEach((chord) => {
expect(chord.applied).toBe(true);
});
});
});
test('Should provide the proper barIndex and chordIndex', () => {
expect.assertions(32);
const chordLine = 'Am... G/B. C E. F. G. B7. Em';
const parsed = parseChordLine(chordLine);
const applied = forEachChordInChordLine(
parsed,
(chord, chordIndex, barIndex) => {
chord.chordIndex = chordIndex;
chord.barIndex = barIndex;
}
);
let i = 0;
const expected = [
[0, 0],
[0, 1],
[1, 0],
[2, 0],
[2, 1],
[2, 2],
[2, 3],
[3, 0],
];
applied.allBars.forEach((bar, barIndex) => {
bar.allChords.forEach((chord, chordIndex) => {
expect(chord.barIndex).toBe(expected[i][0]);
expect(chord.barIndex).toBe(barIndex);
expect(chord.chordIndex).toBe(expected[i][1]);
expect(chord.chordIndex).toBe(chordIndex);
i++;
});
});
});
});
describe('getNthOfLabel', () => {
test('Module', () => {
expect(getNthOfLabel).toBeInstanceOf(Function);
});
test('Should return the section identified by its label and index, without trailing directives', () => {
const song2 = `
#v
Verse1-line1
Verse1-line2
Verse1-line3
Verse1-line4
4/4
#v
Verse2-line1
Verse2-line2
Verse2-line3
Verse2-line4
key B
3/4
#v
Verse3-line1
Verse3-line2
Verse3-line3
Verse3-line4`;
const parsed = parseSong(song2);
const v1 = [
'Verse1-line1',
'Verse1-line2',
'Verse1-line3',
'Verse1-line4',
'',
];
const v2 = [
'Verse2-line1',
'Verse2-line2',
'Verse2-line3',
'Verse2-line4',
'',
'',
];
const v3 = [
'Verse3-line1',
'Verse3-line2',
'Verse3-line3',
'Verse3-line4',
];
expect(getNthOfLabel(parsed.allLines, 'v', 0)).toEqual([]);
expect(
getNthOfLabel(parsed.allLines, 'v', 1).map((line) => line.string)
).toEqual(v1);
expect(
getNthOfLabel(parsed.allLines, 'v', 2).map((line) => line.string)
).toEqual(v2);
expect(
getNthOfLabel(parsed.allLines, 'v', 3).map((line) => line.string)
).toEqual(v3);
expect(getNthOfLabel(parsed.allLines, 'v', 4)).toEqual([]);
});
});