|
| 1 | +"""Tests for the lexers.""" |
| 2 | +import ly.document |
| 3 | +import ly.lex._token |
| 4 | +import ly.lex.html |
| 5 | +import ly.lex.lilypond |
| 6 | + |
| 7 | + |
| 8 | +DIGIT_TOKENS = (ly.lex._token.Numeric, |
| 9 | + ly.lex.lilypond.Fingering, |
| 10 | + ly.lex.lilypond.StringNumber, |
| 11 | + ly.lex.lilypond.ChordStepNumber, |
| 12 | + ly.lex.lilypond.FigureStep, |
| 13 | + ly.lex.lilypond.TempoSeparator, |
| 14 | + ly.lex.lilypond.Scaling) |
| 15 | + |
| 16 | +TO_FULLWIDTH = str.maketrans('0123456789', '0123456789') |
| 17 | + |
| 18 | +SOURCES = [ |
| 19 | + '#(display 42)', |
| 20 | + '#(x 1/2)', |
| 21 | + '#(x 1.5)', |
| 22 | + '\\tempo 4 = 60 - 80', |
| 23 | + '\\time 3/4', |
| 24 | + 'c4', |
| 25 | + 'c-4', |
| 26 | + '\\override NoteHead.font-size = 2', |
| 27 | + '\\chords { c1:5 }', |
| 28 | + '\\figures { <1> }', |
| 29 | + '\\score { { c1*2 } }', |
| 30 | + '\\score { \\new TabStaff { c4\\4 } }', |
| 31 | +] |
| 32 | + |
| 33 | + |
| 34 | +def digit_tokens(text, mode=None): |
| 35 | + """Return the text of every token the lexer only reads as such because of |
| 36 | + the digits in it.""" |
| 37 | + doc = ly.document.Document(text, mode) |
| 38 | + return [str(t) for block in doc |
| 39 | + for t in doc.tokens(block) if isinstance(t, DIGIT_TOKENS)] |
| 40 | + |
| 41 | + |
| 42 | +def test_ascii_digits_are_read(): |
| 43 | + for source in SOURCES: |
| 44 | + assert digit_tokens(source), source |
| 45 | + |
| 46 | + |
| 47 | +def test_fullwidth_digits_are_not_read(): |
| 48 | + for source in SOURCES: |
| 49 | + fullwidth = source.translate(TO_FULLWIDTH) |
| 50 | + assert digit_tokens(fullwidth) == [], fullwidth |
| 51 | + |
| 52 | + |
| 53 | +def entity_refs(text): |
| 54 | + doc = ly.document.Document(text, 'html') |
| 55 | + return [str(t) for block in doc |
| 56 | + for t in doc.tokens(block) if isinstance(t, ly.lex.html.EntityRef)] |
| 57 | + |
| 58 | + |
| 59 | +def test_only_ascii_digits_form_a_numeric_entity(): |
| 60 | + assert entity_refs('A') == ['A'] |
| 61 | + assert entity_refs('A'.translate(TO_FULLWIDTH)) == [] |
0 commit comments