-
Notifications
You must be signed in to change notification settings - Fork 254
Expand file tree
/
Copy pathAlphaTexExporter.test.ts
More file actions
185 lines (147 loc) · 6.9 KB
/
AlphaTexExporter.test.ts
File metadata and controls
185 lines (147 loc) · 6.9 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
import { AlphaTexExporter } from '@coderline/alphatab/exporter/AlphaTexExporter';
import { AlphaTexErrorWithDiagnostics } from '@coderline/alphatab/importer/AlphaTexImporter';
import { ScoreLoader } from '@coderline/alphatab/importer/ScoreLoader';
import type { Score } from '@coderline/alphatab/model/Score';
import { Settings } from '@coderline/alphatab/Settings';
import { ComparisonHelpers } from 'test/model/ComparisonHelpers';
import { TestPlatform } from 'test/TestPlatform';
import { assert } from 'chai';
describe('AlphaTexExporterTest', () => {
async function loadScore(name: string): Promise<Score | null> {
const data = await TestPlatform.loadFile(`test-data/${name}`);
try {
return ScoreLoader.loadScoreFromBytes(data);
} catch {
return null;
}
}
function exportAlphaTex(score: Score, settings: Settings | null = null): string {
return new AlphaTexExporter().exportToString(score, settings);
}
async function testRoundTripEqual(name: string, ignoreKeys: string[] | null = null): Promise<void> {
const fileName = name.substring(name.lastIndexOf('/') + 1);
let exported: string = '';
try {
const expected = await loadScore(name);
if (!expected) {
return;
}
ComparisonHelpers.alphaTexExportRoundtripPrepare(expected);
exported = exportAlphaTex(expected);
const actual = ScoreLoader.loadAlphaTex(exported);
ComparisonHelpers.alphaTexExportRoundtripEqual(fileName, actual, expected, ignoreKeys);
} catch (e) {
const errorLines: string[] = [];
const error = e as Error;
const unwrapped = error.cause instanceof AlphaTexErrorWithDiagnostics ? error.cause! : error;
if (unwrapped instanceof AlphaTexErrorWithDiagnostics) {
const withDiag = unwrapped as AlphaTexErrorWithDiagnostics;
const lines = exported.split('\n');
for (const d of withDiag.iterateDiagnostics()) {
errorLines.push(`Error Line ${lines[d.start!.line - 1]}`);
}
}
assert.fail(
`<${fileName}>${unwrapped.toString()}\n${errorLines.join('\n')}${error.stack}\n Tex:\n${exported}`
);
}
}
async function testRoundTripFolderEqual(name: string, ignoredFiles?: string[]): Promise<void> {
const files: string[] = await TestPlatform.listDirectory(`test-data/${name}`);
const ignoredFilesLookup = new Set<string>(ignoredFiles);
for (const file of files.filter(f => !f.endsWith('.png'))) {
if (!ignoredFilesLookup.has(file) && !file.endsWith('.png')) {
await testRoundTripEqual(`${name}/${file}`, null);
}
}
}
it('notation-legend-roundtrip', async () => {
const score = (await loadScore('visual-tests/notation-legend/notation-legend.gp'))!;
// fill some more details to cover all features
score.title = 'Notation Legend';
score.subTitle = 'for test suite';
score.artist = 'alphaTab';
const settings = new Settings();
settings.exporter.comments = true;
settings.exporter.indent = 2;
ComparisonHelpers.alphaTexExportRoundtripPrepare(score);
const exported = exportAlphaTex(score!, settings);
const reimportedScore = ScoreLoader.loadAlphaTex(exported);
ComparisonHelpers.alphaTexExportRoundtripPrepare(reimportedScore);
ComparisonHelpers.alphaTexExportRoundtripEqual('export-roundtrip', reimportedScore, score);
});
it('exact-contents-formatted', async () => {
const score = (await loadScore('visual-tests/notation-legend/notation-legend.gp'))!;
// fill some more details to cover all features
score.title = 'Notation Legend';
score.subTitle = 'for test suite';
score.artist = 'alphaTab';
const settings = new Settings();
settings.exporter.comments = true;
settings.exporter.indent = 2;
let data = exportAlphaTex(score!, settings);
let expected = await TestPlatform.loadFileAsString('test-data/exporter/notation-legend-formatted.atex');
data = data.replaceAll('\r', '').trim();
expected = expected.replaceAll('\r', '').trim();
const expectedLines = expected.split('\n');
const actualLines = data.split('\n');
const lines = Math.min(expectedLines.length, actualLines.length);
const errors: string[] = [];
if (expectedLines.length !== actualLines.length) {
errors.push(`Expected ${expectedLines.length} lines, but only got ${actualLines.length}`);
}
for (let i = 0; i < lines; i++) {
if (actualLines[i].trimEnd() !== expectedLines[i].trimEnd()) {
errors.push(`Error on line ${i + 1}: `);
errors.push(`+ ${actualLines[i]}`);
errors.push(`- ${expectedLines[i]}`);
}
}
if (errors.length > 0) {
await TestPlatform.saveFileAsString('test-data/exporter/notation-legend-formatted.atex.new', data);
assert.fail(errors.join('\n'));
} else {
await TestPlatform.deleteFile('test-data/exporter/notation-legend-formatted.atex.new');
}
});
// Note: we just test all our importer and visual tests to cover all features
it('importer', async () => {
await testRoundTripFolderEqual('guitarpro7');
});
it('visual-effects-and-annotations', async () => {
await testRoundTripFolderEqual('visual-tests/effects-and-annotations', ['hidden-dots.mxml']);
});
it('visual-general', async () => {
await testRoundTripFolderEqual('visual-tests/general');
});
it('visual-guitar-tabs', async () => {
await testRoundTripFolderEqual('visual-tests/guitar-tabs');
});
it('visual-layout', async () => {
await testRoundTripFolderEqual('visual-tests/layout');
});
it('visual-music-notation', async () => {
await testRoundTripFolderEqual('visual-tests/music-notation');
});
it('visual-notation-legend', async () => {
await testRoundTripFolderEqual('visual-tests/notation-legend');
});
it('visual-special-notes', async () => {
await testRoundTripFolderEqual('visual-tests/special-notes');
});
it('visual-special-tracks', async () => {
await testRoundTripFolderEqual('visual-tests/special-tracks');
});
it('gp5-to-alphaTex', async () => {
await testRoundTripEqual(`conversion/full-song.gp5`);
});
it('gp5-articulation', async () => {
await testRoundTripEqual(`guitarpro5/percussion-all.gp5`);
});
it('gp6-to-alphaTex', async () => {
await testRoundTripEqual(`conversion/full-song.gpx`);
});
it('gp7-to-alphaTex', async () => {
await testRoundTripEqual(`conversion/full-song.gp`);
});
});