# SPDX-License-Identifier: Apache-2.0 # Copyright 2026 Scienting # # Source Code: https://git.scient.ing/papers/paperspec # See the LICENSE.md file for full license terms. """Simple notation and its canonical linear form (spec/05 §5.4).""" import pytest from hypothesis import given, settings, strategies as st from helpers_lib1 import vectors from paperspec import notation as nt PROPERTIES = settings(derandomize=True, max_examples=300, database=None, deadline=None) MINUS = "\N{MINUS SIGN}" PRIME = "\N{PRIME}" DOT = "⋅" @pytest.mark.parametrize("case", vectors("notation")) def test_notation_vector(case): assert nt.fold_math(case["notation"], case["source"]) == case["canonical"] @pytest.mark.parametrize( ("source", "canonical"), [ ("", ""), (" ", ""), ("a b", "ab"), ("a~b", "ab"), ("a\\,b\\;c\\:d\\!e\\ f", "abcdef"), ("a\\quad b\\qquad c", "abc"), ("\\rm x", "x"), ("\\mathrm x", "x"), ("\\text{ab}", "ab"), ("\\textrm{a}_1", "a_{1}"), ("\\mathup{c}", "c"), ("x'", "x" + PRIME), ("x''", "x" + PRIME * 2), ("x^{\\prime\\prime}", "x" + PRIME * 2), ("x^{\\prime}_1", "x" + PRIME + "_{1}"), ("x'_1", "x" + PRIME + "_{1}"), ("'", PRIME), ("x_{i}^{2}", "x_{i}^{2}"), ("x^2_i", "x_{i}^{2}"), ("{a}_2", "a_{2}"), ("{a}{b}", "ab"), ("{}", ""), ("x{}_2", "x_{2}"), ("\\alpha\\beta", "αβ"), ("\\varepsilon\\epsilon", "εϵ"), ("\\Omega", "Ω"), ("\\AA", "Å"), ("a \\le b \\geq c", "a≤b≥c"), ("a\\lt b\\gt c", "ac"), ("a \\cdot b \\times c \\pm d", "a" + DOT + "b\N{MULTIPLICATION SIGN}c±d"), ("a-b\N{EN DASH}c", "a" + MINUS + "b" + MINUS + "c"), ("a·b∙c", "a" + DOT + "b" + DOT + "c"), ("\\left(x\\right)", "(x)"), ("(a,b)/", "(a,b)/"), ("3.14", "3.14"), ("٣", "٣"), ("\N{MICRO SIGN}", "\N{MICRO SIGN}"), ("é", "é"), ("x_{{a}}", "x_{a}"), ], ) def test_tex_folds_to_the_canonical_linear_form(source, canonical): assert nt.fold_math("tex", source) == canonical @pytest.mark.parametrize( "source", [ "a\\", "a}", "{a", "x_{a", "\\{", "\\}", "\\é", "\\foo", "\\frac12", "\\left[x\\right]", "\\left(x\\right]", "\\right(", "\\left", "x^", "x_", "x^}", "x^^2", "x_^2", "x^'", "\\mathrm", "\\text", "x_1_2", "x^1^2", "{ab}_2", "{ab}^2", "∫", "a=b#", "$", "&", "\N{CYRILLIC SMALL LETTER A}", "\N{FULLWIDTH LATIN CAPITAL LETTER A}", "ʰ", ], ) def test_tex_outside_the_simple_forms_is_complex(source): assert nt.fold_math("tex", source) is None @pytest.mark.parametrize( ("source", "canonical"), [ ("", ""), ("a b", "ab"), ("x_1", "x_{1}"), ("x_(12)", "x_{12}"), ("x^12", "x^{12}"), ("x^(a+b)", "x^{a+b}"), ("x'", "x" + PRIME), ("x''_1", "x" + PRIME * 2 + "_{1}"), ("alpha beta", "αβ"), ("Omega", "Ω"), ("epsilon epsilon.alt", "εϵ"), ("theta.alt sigma.alt", "ϑς"), ("a plus b minus c", "a+b" + MINUS + "c"), ("a times b", "a\N{MULTIPLICATION SIGN}b"), ("a dot.op b dot.c c", "a" + DOT + "b" + DOT + "c"), ("a plus.minus b", "a±b"), ("a lt b gt c eq d", "ac=d"), ("a lt.eq b gt.eq c", "a≤b≥c"), ("a <= b >= c", "a≤b≥c"), ("a prime", "a" + PRIME), ("a slash b", "a/b"), ("a thin b med c thick d quad e wide f space g", "abcdefg"), ('"cm"^(-1)', "cm^{" + MINUS + "1}"), ('"a b"', "ab"), ("upright(H)_2", "H_{2}"), ("1.5", "1.5"), ("x1.5", "x1.5"), ("12", "12"), ("(a)", "(a)"), ("((a))", "((a))"), ("a)", "a)"), ("a-b", "a" + MINUS + "b"), ("\N{MICRO SIGN}", "\N{MICRO SIGN}"), ], ) def test_typst_folds_to_the_canonical_linear_form(source, canonical): assert nt.fold_math("typst", source) == canonical @pytest.mark.parametrize( "source", [ "(a", "((a)", "x^", "x_", "x^)", "x^_2", "x^'", "x_1_2", "x^1^2", "a / b", "a * b", "a # b", "a & b", "a [b]", "a {b}", "a \\ b", "a $ b", "a ! b", "a : b", "a ; b", "a | b", "a ~ b", "a @ b", "a -> b", "a => b", "a != b", "a <- b", "a == b", "a := b", '"unclosed', '"a\\"b"', "foo", "cos x", "a.b", "upright", "upright x", "frac(a, b)", '"∫"', "∫", ], ) def test_typst_outside_the_simple_forms_is_complex(source): assert nt.fold_math("typst", source) is None def test_a_notation_other_than_tex_is_read_as_typst(): assert nt.fold_math("typst", "x_1") == nt.fold_math("other", "x_1") == "x_{1}" assert nt.fold_math("other", "\\frac{a}{b}") is None def test_the_source_is_composed_before_it_is_read(): assert nt.fold_math("tex", "é") == nt.fold_math("tex", "é") == "é" @pytest.mark.parametrize( ("notation", "source", "key"), [ ("tex", "\\frac{a}{b}", "tex:\\frac{a}{b}"), ("tex", "\\frac { a } { b }", "tex:\\frac{a}{b}"), ("typst", "a / b", "typst:a/b"), ("tex", "é \n x", "tex:éx"), ("typst", "", "typst:"), ], ) def test_complex_key_is_the_notation_and_the_source_without_white_space( notation, source, key ): assert nt.complex_key(notation, source) == key @pytest.mark.parametrize( ("ch", "expected"), [ ("a", True), ("Z", True), ("é", True), ("ɐ", False), ("\N{GREEK CAPITAL LETTER ALPHA}", True), ("ω", True), ("Ϣ", False), ("ϵ", True), ("ẞ", True), ("ἀ", True), ("\N{MICRO SIGN}", True), ("\N{CYRILLIC SMALL LETTER A}", False), ("中", False), ("1", False), ("+", False), ("ª", True), ("ʰ", False), ], ) def test_is_letter_is_latin_or_greek_letters_and_the_micro_sign(ch, expected): assert nt.is_letter(ch) is expected @pytest.mark.parametrize( ("ch", "expected"), [ ("x", True), ("7", True), ("٣", True), ("-", True), ("\N{EN DASH}", True), ("'", True), ("+", True), ("(", True), (".", True), ("\N{MULTIPLICATION SIGN}", True), ("±", True), ("*", False), ("{", False), ("\\", False), ("∫", False), ("²", False), (" ", False), ], ) def test_is_simple_char(ch, expected): assert nt.is_simple_char(ch) is expected @pytest.mark.parametrize( ("ch", "folded"), [ ("-", MINUS), ("\N{EN DASH}", MINUS), ("\N{MINUS SIGN}", MINUS), ("·", DOT), ("∙", DOT), ("⋅", DOT), ("'", PRIME), (PRIME, PRIME), ("a", "a"), ("—", "—"), ], ) def test_fold_char(ch, folded): assert nt.fold_char(ch) == folded @pytest.mark.parametrize( ("ch", "expected"), [ ("²", ("^", "2")), ("³", ("^", "3")), ("⁴", ("^", "4")), ("⁺", ("^", "+")), ("₂", ("_", "2")), ("ₙ", ("_", "n")), ("℠", ("^", "SM")), ("2", (None, None)), ("a", (None, None)), ("½", (None, None)), ], ) def test_script_type(ch, expected): assert nt.script_type(ch) == expected @pytest.mark.parametrize( ("text", "folded"), [ ("10³ per s", "10^{3} per s"), ("H₂O", "H_{2}O"), ("cm⁻¹", "cm^{\N{MINUS SIGN}1}"), ("x²⁺", "x^{2+}"), ("x²₂", "x^{2}_{2}"), ("ᴬᴮ", "^{AB}"), ("aʴ", "aʴ"), ("ʴ²", "ʴ²"), ("ʰ", "^{h}"), ("10-3", "10" + MINUS + "3"), ("10\N{EN DASH}3", "10" + MINUS + "3"), ("a-b", "a-b"), ("a - 3", "a - 3"), ("1-", "1-"), ("-", "-"), ("é", "é"), ("plain text", "plain text"), ("", ""), ], ) def test_fold_plain_text(text, folded): assert nt.fold_plain_text(text) == folded @pytest.mark.parametrize( ("op", "text", "folded"), [ ("^", "2", "^{2}"), ("_", "2", "_{2}"), ("_", " i j ", "_{ij}"), ("^", "-1", "^{" + MINUS + "1}"), ("^", "\N{PRIME}", "\N{PRIME}"), ("^", "\N{PRIME}\N{PRIME}", "\N{PRIME}\N{PRIME}"), ("_", "\N{PRIME}", "_{\N{PRIME}}"), ("^", "", "^{}"), ("^", "a b c?", "^{a b c?}"), ("^", " a b ? ", "^{a b ?}"), ("_", "x²", "_{x^{2}}"), ("^", "\N{GREEK SMALL LETTER ALPHA}", "^{\N{GREEK SMALL LETTER ALPHA}}"), ("^", "\N{CYRILLIC SMALL LETTER A}", "^{\N{CYRILLIC SMALL LETTER A}}"), ], ) def test_fold_styled_script(op, text, folded): assert nt.fold_styled_script(op, text) == folded def test_a_styled_script_and_a_script_character_fold_alike(): assert nt.fold_styled_script("^", "3") == nt.fold_plain_text("³") assert nt.fold_styled_script("_", "2") == nt.fold_plain_text("₂") TEX_PIECES = [ "a", "b", "x", "1", "2", "_", "^", "{", "}", "'", "-", "+", " ", "\\alpha", "\\mathrm", "\\left(", "\\right)", "\\,", "\\frac", "\\prime", "(", ")", "=", ] TYPST_PIECES = [ "a", "b", "1", "2", "_", "^", "(", ")", "'", "-", "+", " ", "alpha", "upright", '"', '"cm"', "<=", "->", ".", "/", "dot.op", ] @given(st.text()) @PROPERTIES def test_any_source_gives_a_form_or_none_never_an_error(source): for notation in ("tex", "typst"): result = nt.fold_math(notation, source) assert result is None or isinstance(result, str) @given(st.lists(st.sampled_from(TEX_PIECES), max_size=14).map("".join)) @PROPERTIES def test_a_tex_form_is_stable_under_folding_again(source): form = nt.fold_math("tex", source) if form is not None: assert nt.fold_math("tex", form) == form @given(st.lists(st.sampled_from(TYPST_PIECES), max_size=14).map("".join)) @PROPERTIES def test_a_typst_form_contains_no_white_space(source): form = nt.fold_math("typst", source) if form is not None: assert not any(c.isspace() for c in form) @given(st.lists(st.sampled_from(TEX_PIECES), max_size=14).map("".join)) @PROPERTIES def test_white_space_does_not_change_a_tex_form(source): spaced = " ".join(source) plain = nt.fold_math("tex", source.replace(" ", "")) if plain is not None and "\\" not in source: assert nt.fold_math("tex", spaced) == plain