//! Tests for the formula renderer. //! //! They live here rather than inside the module so that `src/` holds no test //! code, as it does in `apsis-rs`. An integration test is a separate crate and //! reaches only what the library makes public, so the few internal items the //! tests need are opened through `apsis_book::testkit`. use apsis_book::docs::mathml::*; use apsis_book::testkit::mathml; #[test] fn a_variable_is_an_identifier() { assert_eq!(mathml("x"), "x"); } #[test] fn a_power_becomes_a_superscript() { assert_eq!( mathml("x^2"), "x2" ); } #[test] fn a_group_holds_several_tokens() { assert_eq!( mathml("x^{12}"), "x12" ); } #[test] fn a_fraction_has_a_numerator_and_a_denominator() { assert_eq!( mathml("\\frac{a}{b}"), "ab" ); } #[test] fn a_greek_letter_is_translated() { assert_eq!(mathml("\\theta"), "θ"); } #[test] fn an_unknown_command_is_kept_as_written() { // Losing part of a formula would be worse than showing it plainly. // Text is given an element even when it is not understood, because text // without one is not drawn at all. assert_eq!(mathml("\\notacommand"), "\\notacommand"); } #[test] fn a_comma_is_kept() { // The command is the spacing one, so dropping it would run a list of // symbols together. It is written as an operator so that it keeps its // space, which is what a comma is for. assert_eq!( mathml("\\alpha, \\beta"), "α,β" ); } #[test] fn a_root_can_have_an_index() { let html = mathml("\\sqrt[3]{x}"); assert!(html.contains(""), "the index was lost: {html}"); } #[test] fn text_is_escaped() { assert!(mathml("a") || html.contains(""), "{latex} kept its script marker: {html}" ); assert!( !html.contains("^") && !html.contains("_"), "{latex} left a script marker in the output: {html}" ); } } /// The MathML of a formula written in LaTeX. fn render_plain_of(latex: &str) -> String { apsis_book::docs::mathml::render(latex, false) .split("display=\"inline\">") .nth(1) .unwrap_or("") .split("") .next() .unwrap_or("") .to_string() }