croma-core 1.1.4

Library-first ABC to MusicXML exporter
Documentation
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
use super::*;
use crate::parse::parse_document;

#[test]
fn missing_l_defaults_from_meter() {
    let report = parse_document("X:1\nM:2/4\nK:C\nC\n", ParseOptions::default());
    let tune = report.value.fields.tune(0).expect("expected tune fields");
    let unit = tune
        .header
        .unit_note_length
        .as_ref()
        .expect("expected default unit note length");

    assert_eq!(unit.value.fraction, NoteLengthFraction::new(1, 16));
    assert_eq!(unit.value.origin, UnitNoteLengthOrigin::DefaultFromMeter);
    assert_eq!(report.value.source.slice(unit.span), Some("2/4"));
}

#[test]
fn parses_common_cut_and_free_meter() {
    for (source, expected) in [
        ("X:1\nM:C\nK:C\nC\n", MeterKind::CommonTime),
        ("X:1\nM:C|\nK:C\nC\n", MeterKind::CutTime),
        ("X:1\nM:none\nK:C\nC\n", MeterKind::None),
    ] {
        let report = parse_document(source, ParseOptions::default());
        let tune = report.value.fields.tune(0).expect("expected tune fields");
        let meter = tune.header.meter.as_ref().expect("expected meter");
        assert_eq!(meter.value.kind, expected);
        assert_eq!(
            tune.header
                .unit_note_length
                .as_ref()
                .expect("expected unit")
                .value
                .fraction,
            NoteLengthFraction::new(1, 8)
        );
    }
}

#[test]
fn parses_key_modes_and_explicit_accidentals() {
    let report = parse_document("X:1\nK:D Phr ^f _B\nC\n", ParseOptions::default());
    let tune = report.value.fields.tune(0).expect("expected tune fields");
    let key = tune.header.key.as_ref().expect("expected key");

    assert_eq!(
        key.value.tonic,
        Some(KeyTonic {
            step: 'D',
            accidental: None
        })
    );
    assert_eq!(key.value.mode, KeyMode::Phrygian);
    assert_eq!(key.value.accidentals.len(), 2);
    assert_eq!(key.value.accidentals[0].sign, AccidentalSign::Sharp);
    assert_eq!(key.value.accidentals[0].note.value, 'f');
    assert_eq!(key.value.accidentals[1].sign, AccidentalSign::Flat);
    assert_eq!(key.value.accidentals[1].note.value, 'B');

    let explicit = parse_document("X:1\nK:D exp _b _e ^f\nC\n", ParseOptions::default());
    let key = explicit
        .value
        .fields
        .tune(0)
        .and_then(|tune| tune.header.key.as_ref())
        .expect("expected explicit key");
    assert_eq!(key.value.mode, KeyMode::Explicit);
    assert!(key.value.explicit);
    assert_eq!(key.value.accidentals.len(), 3);
}

#[test]
fn key_tonic_recovers_from_trailing_comma_junk() {
    // `K:Bb, F` — a stray comma directly after a valid tonic must not reject the
    // whole field. The leading tonic Bb (2 flats) is recovered and the trailing
    // `, F` ignored (ABC 2.1 §3.1.14); `K:Bb F` already parses, so the attached
    // comma is the only trigger. (tune_004340 mid-tune key change)
    let report = parse_document("X:1\nK:Bb, F\nC\n", ParseOptions::default());
    let tune = report.value.fields.tune(0).expect("expected tune fields");
    let key = tune.header.key.as_ref().expect("expected key");

    assert_eq!(
        key.value.tonic,
        Some(KeyTonic {
            step: 'B',
            accidental: Some(KeyTonicAccidental::Flat),
        })
    );
    assert_eq!(key.value.mode, KeyMode::Major);
}

#[test]
fn key_tonic_rejects_alphabetic_nonmode_remainder() {
    // The complement of `key_tonic_recovers_from_trailing_comma_junk`: a tonic
    // letter followed by an *alphabetic* non-mode remainder is NOT recovered as a
    // tonic. `K:Bass` is `B` + `ass`, but `ass` is neither a mode nor an
    // accidental — it may be a clef word (`bass`, `alto`) or a property token, so
    // the whole token is treated as not-a-tonic. Only non-alphabetic trailing
    // junk (the comma case) recovers the leading tonic. This is the boundary of
    // the single §3.1.14 tonic-recognition rule in `parse_tonic_token`.
    let report = parse_document("X:1\nK:Bass\nC\n", ParseOptions::default());
    let tune = report.value.fields.tune(0).expect("expected tune fields");
    let key = tune.header.key.as_ref().expect("expected key");

    assert_eq!(key.value.tonic, None, "`Bass` must not parse as tonic B");
}

#[test]
fn parses_explicit_accidental_list_written_without_spaces() {
    // `K:D exp _B^g` — the explicit (exp) accidental list is written space-less,
    // so it arrives as a single token. Every accidental in the token must be
    // parsed (ABC 2.1 §3.1.14), not just the first; dropping `^g` left G natural.
    let report = parse_document("X:1\nK:D exp _B^g\nC\n", ParseOptions::default());
    let key = report
        .value
        .fields
        .tune(0)
        .and_then(|tune| tune.header.key.as_ref())
        .expect("expected explicit key");
    assert_eq!(key.value.mode, KeyMode::Explicit);
    assert!(key.value.explicit);
    assert_eq!(key.value.accidentals.len(), 2);
    assert_eq!(key.value.accidentals[0].sign, AccidentalSign::Flat);
    assert_eq!(key.value.accidentals[0].note.value, 'B');
    assert_eq!(key.value.accidentals[1].sign, AccidentalSign::Sharp);
    assert_eq!(key.value.accidentals[1].note.value, 'g');
}

#[test]
fn parses_nospace_key_global_accidentals_after_tonic() {
    let report = parse_document("X:1\nK:D_B^g\nC\n", ParseOptions::default());
    let tune = report.value.fields.tune(0).expect("expected tune fields");
    let key = tune.header.key.as_ref().expect("expected key");

    assert_eq!(
        key.value.tonic,
        Some(KeyTonic {
            step: 'D',
            accidental: None
        })
    );
    assert_eq!(key.value.mode, KeyMode::Major);
    assert!(
        key.value.accidentals.is_empty(),
        "compact accidental tail is nonstandard and ignored"
    );
    assert!(key.value.compact_accidentals_ignored);
}

#[test]
fn parses_nospace_key_global_accidentals_with_sharp_first_as_base_key() {
    let report = parse_document("X:1\nK:D^f_B_e\nC\n", ParseOptions::default());
    let tune = report.value.fields.tune(0).expect("expected tune fields");
    let key = tune.header.key.as_ref().expect("expected key");

    assert_eq!(
        key.value.tonic,
        Some(KeyTonic {
            step: 'D',
            accidental: None
        })
    );
    assert_eq!(key.value.mode, KeyMode::Major);
    assert!(
        key.value.accidentals.is_empty(),
        "compact accidental tail is nonstandard and ignored"
    );
    assert!(key.value.compact_accidentals_ignored);
}

#[test]
fn key_field_captures_octave_property() {
    let report = parse_document("X:1\nK: Dm octave=1\nC\n", ParseOptions::default());
    let tune = report.value.fields.tune(0).expect("expected tune fields");
    let key = tune.header.key.as_ref().expect("expected key");

    assert_eq!(
        key.value.tonic,
        Some(KeyTonic {
            step: 'D',
            accidental: None
        })
    );
    assert_eq!(key.value.mode, KeyMode::Minor);
    assert_eq!(
        key.value
            .properties
            .octave
            .as_ref()
            .map(|value| value.value.as_str()),
        Some("1")
    );
    // The tonic/mode token (`Dm`) must not be misread as a clef shorthand.
    assert!(key.value.properties.clef.is_none());
}

#[test]
fn key_field_captures_bare_clef_shorthand() {
    let report = parse_document("X:1\nK:C treble+8\nC\n", ParseOptions::default());
    let tune = report.value.fields.tune(0).expect("expected tune fields");
    let key = tune.header.key.as_ref().expect("expected key");

    assert_eq!(
        key.value.tonic,
        Some(KeyTonic {
            step: 'C',
            accidental: None
        })
    );
    assert_eq!(
        key.value
            .properties
            .clef
            .as_ref()
            .map(|value| value.value.as_str()),
        Some("treble+8")
    );
}

#[test]
fn unknown_fields_warn_and_stay_out_of_music() {
    let report = parse_document("X:1\nK:C\nY:ABC\nC\n", ParseOptions::default());

    assert!(
        report
            .diagnostics
            .iter()
            .any(|diagnostic| diagnostic.code == "abc.field.unknown")
    );
    assert_eq!(
        report
            .value
            .surface
            .tokens_of_kind(crate::syntax::tune::SurfaceKind::Note)
            .count(),
        1
    );
}

#[test]
fn version_lines_and_version_instructions_update_interpretation_mode() {
    let strict = parse_document("%abc-2.1\nX:1\nK:C\nC\n", ParseOptions::default());
    assert_eq!(
        strict.value.fields.file_header.dialect.mode,
        ParseMode::Strict
    );

    let loose = parse_document("%abc\nX:1\nK:C\nC\n", ParseOptions::default());
    assert_eq!(
        loose.value.fields.file_header.dialect.mode,
        ParseMode::Loose
    );

    let tune_loose = parse_document(
        "%abc-2.1\nX:1\nI:abc-version 2.0\nK:C\nC\n",
        ParseOptions::default(),
    );
    let tune = tune_loose.value.fields.tune(0).expect("expected tune");
    assert_eq!(tune.header.dialect.mode, ParseMode::Loose);
}

#[test]
fn interpretation_fields_update_decoration_and_linebreak_state() {
    let decoration = parse_document("I:decoration +\n\nX:1\nK:C\nC\n", ParseOptions::default());
    let tune = decoration.value.fields.tune(0).expect("expected tune");
    assert_eq!(
        tune.inherited_file_header.dialect.decoration_delimiter,
        DecorationDelimiter::Plus
    );

    let linebreak = parse_document("X:1\nI:linebreak !\nK:C\nC\n", ParseOptions::default());
    let tune = linebreak.value.fields.tune(0).expect("expected tune");
    assert!(tune.header.dialect.line_break.uses_bang());
    assert_eq!(
        tune.header.dialect.decoration_delimiter,
        DecorationDelimiter::Plus
    );
}

#[test]
fn parses_voice_properties_with_source_spans() {
    let report = parse_document(
        "X:1\nV:T1 name=\"Tenor 1\" nm=T subname=\"Line A\" snm=TA clef=treble stem=up octave=-1 transpose=_B\nK:C\nC\n",
        ParseOptions::default(),
    );
    let tune = report.value.fields.tune(0).expect("expected tune fields");
    let voice = tune
        .header
        .voices
        .first()
        .expect("expected voice definition");
    let properties = &voice.value.parsed_properties;

    assert_eq!(voice.value.id.value, "T1");
    assert_eq!(
        properties.name.as_ref().map(|value| value.value.as_str()),
        Some("Tenor 1")
    );
    assert_eq!(
        properties.nm.as_ref().map(|value| value.value.as_str()),
        Some("T")
    );
    assert_eq!(
        properties
            .subname
            .as_ref()
            .map(|value| value.value.as_str()),
        Some("Line A")
    );
    assert_eq!(
        properties.snm.as_ref().map(|value| value.value.as_str()),
        Some("TA")
    );
    assert_eq!(
        properties.clef.as_ref().map(|value| value.value.as_str()),
        Some("treble")
    );
    assert_eq!(
        properties.stem.as_ref().map(|value| value.value),
        Some(StemDirection::Up)
    );
    assert_eq!(
        report
            .value
            .source
            .slice(properties.name.as_ref().expect("expected name").span),
        Some("Tenor 1")
    );
    assert_eq!(
        properties.octave.as_ref().map(|value| value.value.as_str()),
        Some("-1")
    );
    assert_eq!(
        properties
            .transpose
            .as_ref()
            .map(|value| value.value.as_str()),
        Some("_B")
    );
}

#[test]
fn parses_i_score_as_structured_directive() {
    let report = parse_document("X:1\nK:C\nI:score (T1 T2)\nC\n", ParseOptions::default());
    let score = report
        .value
        .fields
        .fields
        .iter()
        .find_map(|field| match &field.kind {
            ParsedFieldKind::Interpretation(InterpretationField::Score { directive }) => {
                Some(directive)
            }
            _ => None,
        })
        .expect("expected I:score directive");

    assert_eq!(score.value.value, "(T1 T2)");
    assert_eq!(score.tokens.len(), 4);
}

#[test]
fn croma_instruction_warnings_can_be_suppressed() {
    let source = "X:1\nI:croma-future-carrier value\nI:unknown value\nK:C\nC\n";

    let default = parse_document(source, ParseOptions::default());
    assert!(
        default
            .diagnostics
            .iter()
            .any(|diagnostic| diagnostic.code == "abc.field.unknown_instruction"),
        "default parsing should warn for unknown I: instructions"
    );

    let suppressed = parse_document(
        source,
        ParseOptions::default().suppress_croma_carrier_warnings(),
    );
    let messages = suppressed
        .diagnostics
        .iter()
        .map(|diagnostic| diagnostic.message.as_str())
        .collect::<Vec<_>>();

    assert!(
        messages
            .iter()
            .all(|message| !message.contains("croma-future-carrier")),
        "croma-private I: warnings should be suppressed: {messages:?}"
    );
    assert!(
        messages.iter().any(|message| message.contains("`unknown`")),
        "ordinary unknown I: warnings must stay visible: {messages:?}"
    );
}

#[test]
fn invalid_voice_stem_is_preserved_as_other_property_with_span() {
    let report = parse_document(
        "X:1\nV:bad stem=sideways clef=bass\nK:C\nC\n",
        ParseOptions::default(),
    );
    let voice = report
        .value
        .fields
        .tune(0)
        .and_then(|tune| tune.header.voices.first())
        .expect("expected voice definition");
    let properties = &voice.value.parsed_properties;

    assert!(properties.stem.is_none());
    let stem = properties
        .other
        .iter()
        .find(|property| property.key.value == "stem")
        .expect("expected preserved invalid stem property");
    assert_eq!(stem.value.value, "sideways");
    assert_eq!(report.value.source.slice(stem.span), Some("stem=sideways"));
}