-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMusicXML.test.ts
More file actions
470 lines (403 loc) · 16.1 KB
/
Copy pathMusicXML.test.ts
File metadata and controls
470 lines (403 loc) · 16.1 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
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
import * as examples from './examples';
import { EXAMPLES } from './examples';
import * as asserts from './generated/asserts';
import * as elements from './generated/elements';
import * as operations from './lib/operations';
import { MusicXML } from './MusicXML';
describe('MusicXML', () => {
describe('README', () => {
it('matches the parse and serialize documented README behavior', () => {
const xml = `<?xml version="1.0" encoding="UTF-8"?>
<score-partwise version="4.0">
<part-list>
<score-part id="P1">
<part-name></part-name>
</score-part>
</part-list>
<part id="P1">
<measure number=""/>
</part>
</score-partwise>`;
// parse
const musicXml = MusicXML.parse(xml);
// serialize
expect(musicXml.serialize()).toBe(xml);
});
it('matches the create MusicXML partwise documented README behavior', () => {
const musicXml = MusicXML.createPartwise();
const root = musicXml.getRoot();
expect(asserts.isScorePartwise(root)).toBeTrue();
});
it('matches the create MusicXML timewise documented README behavior', () => {
const musicXml = MusicXML.createTimewise();
const root = musicXml.getRoot();
expect(asserts.isScoreTimewise(root)).toBeTrue();
});
it('matches the narrowing documented README behavior', () => {
const note = new elements.Note();
const noteVariation = note.getVariation();
// In the README, we showed that the value must match one of the type predicates.
expect(
asserts.isTiedNote(noteVariation) ||
asserts.isCuedNote(noteVariation) ||
asserts.isTiedGraceNote(noteVariation) ||
asserts.isCuedGraceNote(noteVariation)
).toBeTrue();
});
it('allows a measure element to be created and updated', () => {
const measure = new elements.MeasurePartwise({ attributes: { number: '1', implicit: 'no' } });
expect(measure.getNumber()).toBe('1');
measure.setNumber('4');
expect(measure.getNumber()).toBe('4');
const note = new elements.Note();
measure.setValues([...measure.getValues(), new elements.Note()]);
const measureValues = measure.getValues();
expect(measureValues).toHaveLength(1);
expect(measureValues).toStrictEqual([note]);
});
it('allows setters to be chained', () => {
const note = new elements.Note();
note.setColor('#800080').setStaff(new elements.Staff()).getStaff()!.setValue(4);
expect(note.getColor()).toBe('#800080');
expect(note.getStaff()).not.toBeNull();
expect(note.getStaff()!.getValue()).toBe(4);
});
it('creates a document with a single measure and a single note', () => {
const musicXml = MusicXML.createPartwise();
musicXml
.getRoot()
.setPartList(
new elements.PartList({
contents: [
new Array<elements.PartGroup>(),
new elements.ScorePart({
attributes: { id: 'P1' },
contents: [
null, // elements.Identification
new Array<elements.PartLink>(),
new elements.PartName({ contents: ['Part 1'] }),
null, // elements.PartNameDisplay
null, // elements.PartAbbreviation
null, // elements.PartAbbreviationDisplay
new Array<elements.Group>(),
new Array<elements.ScoreInstrument>(),
new Array<elements.Player>(),
new Array<elements.MidiDevice | elements.MidiInstrument>(),
],
}),
new Array<elements.PartGroup | elements.ScorePart>(),
],
})
)
.setParts([
new elements.PartPartwise({
attributes: { id: 'P1' },
}).setMeasures([
new elements.MeasurePartwise({
attributes: { number: '1' },
contents: [
[
new elements.Attributes({
attributes: { divisions: 1 },
contents: [
null, // elements.Footnote
null, // elements.Level
null, // elements.Divisions
new Array<elements.Key>(),
new Array<elements.Time>(
new elements.Time({
contents: [
[
[
[
new elements.Beats({
contents: ['4'],
}),
new elements.BeatType({
contents: ['4'],
}),
],
],
null,
],
],
})
),
null, // elements.Staves
null, // elements.PartSymbol
null, // elements.Instruments
new Array<elements.Clef>(),
new Array<elements.StaffDetails>(),
new Array<elements.Transpose>(),
new Array<elements.Directive>(),
new Array<elements.MeasureStyle>(),
],
}),
new elements.Note({
contents: [
[
null, // elements.TiedNote
new elements.Pitch({
contents: [
new elements.Step({
contents: ['C'],
}),
null, // elements.Alter
new elements.Octave({
contents: [4],
}),
],
}),
new elements.Duration({
contents: [4],
}),
[], // elements.Tie,
],
new Array<elements.Instrument>(),
null, // elements.Footnote
null, // elements.Level
null, // elements.Voice
null, // elements.Type
new Array<elements.Dot>(),
null, // elements.Accidental
null, // elements.TimeModification
null, // elements.Stem
null, // elements.Notehead
null, // elements.NoteheadText
null, // elements.Staff
[], // elements.Beam
new Array<elements.Notations>(),
new Array<elements.Lyric>(),
null, // elements.Play
null, // elements.Listen
],
}),
],
],
}),
]),
]);
expect(musicXml.serialize()).toBe(`<?xml version="1.0" encoding="UTF-8"?>
<score-partwise version="4.0">
<part-list>
<score-part id="P1">
<part-name>Part 1</part-name>
</score-part>
</part-list>
<part id="P1">
<measure number="1">
<attributes>
<time>
<beats>4</beats>
<beat-type>4</beat-type>
</time>
</attributes>
<note>
<pitch>
<step>C</step>
<octave>4</octave>
</pitch>
<duration>4</duration>
</note>
</measure>
</part>
</score-partwise>`);
});
});
describe('parse', () => {
it('preserves values of a valid MusicXML document', () => {
const xmlStr = examples.loadExample(EXAMPLES.HELLO_WORLD);
const musicXml = MusicXML.parse(xmlStr);
expect(musicXml.getRoot()).toBeInstanceOf(elements.ScorePartwise);
const scorePartwise = musicXml.getRoot();
if (!asserts.isScorePartwise(scorePartwise)) {
fail(`expected ScorePartwise, got: ${scorePartwise}`);
}
expect(scorePartwise.getVersion()).toBe('4.0');
const partList = scorePartwise.getPartList();
expect(partList).toBeInstanceOf(elements.PartList);
const scorePart = partList.getScorePart();
expect(scorePart).toBeInstanceOf(elements.ScorePart);
expect(scorePart.getId()).toBe('P1');
const partName = scorePart.getPartName();
expect(partName).toBeInstanceOf(elements.PartName);
expect(partName.getText()).toBe('Music');
const parts = scorePartwise.getParts();
expect(parts).toBeArray();
expect(parts).toHaveLength(1);
const part = parts[0];
expect(part.getId()).toBe('P1');
const measures = part.getMeasures();
expect(measures).toBeArray();
expect(measures).toHaveLength(1);
const measure = measures[0];
expect(measure).toBeInstanceOf(elements.MeasurePartwise);
expect(measure.getNumber()).toBe('1');
const measureValues = measure.getValues();
expect(measureValues).toBeArray();
expect(measureValues).toHaveLength(2);
const attributes = measureValues[0];
if (!asserts.isAttributes(attributes)) {
fail(`expected Attributes, got: ${attributes}`);
}
const divisions = attributes.getDivisions();
expect(divisions).toBeInstanceOf(elements.Divisions);
expect(divisions!.getPositiveDivisions()).toBe(1);
const keys = attributes.getKeys();
expect(keys).toBeArray();
expect(keys).toHaveLength(1);
const key = keys[0];
expect(key).toBeInstanceOf(elements.Key);
const keyValue = key.getValue();
if (!asserts.isTranditionalKey(keyValue)) {
fail(`expected TraditionalKey, got: ${keyValue}`);
}
const fifths = keyValue[1];
expect(fifths.getValue()).toBe(0);
const times = attributes.getTimes();
expect(times).toBeArray();
expect(times).toHaveLength(1);
const time = times[0];
expect(time).toBeInstanceOf(elements.Time);
const timeValue = time.getValue();
if (!asserts.isTimeSignature(timeValue)) {
fail(`expected TimeSignature, got: ${timeValue}`);
}
expect(timeValue).toBeArray();
expect(timeValue).toHaveLength(2);
const beats = timeValue[0][0][0];
expect(beats).toBeInstanceOf(elements.Beats);
expect(beats.getText()).toBe('4');
const beatType = timeValue[0][0][1];
expect(beatType).toBeInstanceOf(elements.BeatType);
expect(beatType.getText()).toBe('4');
const clefs = attributes.getClefs();
expect(clefs).toBeArray();
expect(clefs).toHaveLength(1);
const clef = clefs[0];
expect(clef).toBeInstanceOf(elements.Clef);
const sign = clef.getSign();
expect(sign).toBeInstanceOf(elements.Sign);
expect(sign.getClefSign()).toBe('G');
const line = clef.getLine();
expect(line).toBeInstanceOf(elements.Line);
expect(line!.getStaffLinePosition()).toBe(2);
const note = measureValues[1];
if (!asserts.isNote(note)) {
fail(`expected Note, got: ${note}`);
}
const noteVariation = note.getVariation();
expect(asserts.isTiedNote(noteVariation)).toBeTrue();
expect(noteVariation).toBeArray();
expect(noteVariation).toHaveLength(4);
expect(noteVariation[0]).toBeNull();
expect(noteVariation[1]).toBeInstanceOf(elements.Pitch);
expect(noteVariation[2]).toBeInstanceOf(elements.Duration);
expect(noteVariation[3]).toStrictEqual([]);
const type = note.getType();
expect(type).toBeInstanceOf(elements.Type);
expect(type!.getNoteTypeValue()).toBe('whole');
});
it('replaces invalid values with zero values', () => {
const xmlStr = examples.loadExample(EXAMPLES.PARTIALLY_INVALID);
const musicXml = MusicXML.parse(xmlStr);
const scorePartwise = musicXml.getRoot();
if (!asserts.isScorePartwise(scorePartwise)) {
fail(`expected ScorePartwise, got: ${scorePartwise}`);
}
const parts = scorePartwise.getParts();
expect(parts).toHaveLength(1);
const part = parts[0];
const measures = part.getMeasures();
expect(measures).toHaveLength(1);
const measure = measures[0];
const contents = measure.getValues();
expect(contents).toHaveLength(2);
const attributes = contents[0];
if (!asserts.isAttributes(attributes)) {
fail(`expected Attributes, got ${attributes}`);
}
const divisions = attributes.getDivisions();
expect(divisions).not.toBeNull();
expect(divisions!.getPositiveDivisions()).toBe(1);
const note = contents[1];
if (!asserts.isNote(note)) {
fail(`expected Note, got ${note}`);
}
const noteVariation = note.getVariation();
if (!asserts.isTiedNote(noteVariation)) {
fail(`expected TiedNoteValue, got ${noteVariation}`);
}
const duration = noteVariation[2];
expect(duration.getPositiveDivisions()).toBe(1);
});
it('throws an error when there are multiple score-partwise elements', () => {
const xmlStr = examples.loadExample(EXAMPLES.INVALID_ROOT);
expect(() => MusicXML.parse(xmlStr)).toThrowError();
});
});
describe('createPartwise', () => {
it('creates a MusicXML object with a ScorePartwise element as the root', () => {
const musicXml = MusicXML.createPartwise();
expect(musicXml.getRoot()).toBeInstanceOf(elements.ScorePartwise);
});
it('creates a valid MusicXML object', () => {
const musicXml = MusicXML.createPartwise();
const scorePartwise = musicXml.getRoot();
expect(operations.validate(scorePartwise, elements.ScorePartwise)).toBeTrue();
});
it('creates a MusicXML object that serializes to a valid MusicXML document', async () => {
const musicXml = MusicXML.createPartwise();
await expect(musicXml.serialize()).toBeValidMusicXML();
});
});
describe('createTimewise', () => {
it('creates a MusicXML object with a ScoreTimewise element as the root', () => {
const musicXml = MusicXML.createTimewise();
expect(musicXml.getRoot()).toBeInstanceOf(elements.ScoreTimewise);
});
it('creates a valid MusicXML object', () => {
const musicXml = MusicXML.createTimewise();
const scoreTimewise = musicXml.getRoot();
expect(operations.validate(scoreTimewise, elements.ScoreTimewise)).toBeTrue();
});
it('creates a MusicXML object that serializes to a valid MusicXML document', async () => {
const musicXml = MusicXML.createTimewise();
await expect(musicXml.serialize()).toBeValidMusicXML();
});
});
describe('isScorePartwise', () => {
it('asserts ScorePartwise elements', () => {
expect(asserts.isScorePartwise(new elements.ScorePartwise())).toBeTrue();
});
it('refutes the ScorePartwise constructor', () => {
expect(asserts.isScorePartwise(elements.ScorePartwise)).toBeFalse();
});
it('refutes non-ScorePartwise elements', () => {
expect(asserts.isScorePartwise(new elements.ScoreTimewise())).toBeFalse();
});
});
describe('isScoreTimewise', () => {
it('asserts ScoreTimewise elements', () => {
expect(asserts.isScoreTimewise(new elements.ScoreTimewise())).toBeTrue();
});
it('refutes the ScoreTimewise constructor', () => {
expect(asserts.isScoreTimewise(elements.ScoreTimewise)).toBeFalse();
});
it('refutes non-ScoreTimewise elements', () => {
expect(asserts.isScoreTimewise(new elements.ScorePartwise())).toBeFalse();
});
});
describe('serialize', () => {
it('serializes a valid MusicXML document', () => {
const xmlStr = examples.loadExample(EXAMPLES.HELLO_WORLD);
const musicXml = MusicXML.parse(xmlStr);
expect(() => musicXml.serialize()).not.toThrow();
});
it('serializes an invalid MusicXML document into a valid document: %s', async () => {
const xmlStr = examples.loadExample(EXAMPLES.MOSTLY_INVALID);
const musicXml = MusicXML.parse(xmlStr);
await expect(musicXml.serialize()).toBeValidMusicXML();
});
});
});