compmd 0.12.0

The CompMD processor: parses, validates and expands CompMD (Component Markdown) documents. In development.
Documentation
//! Patterns match as RE2's do, and are compiled once.

use super::{KEPT, compiled};
use std::rc::Rc;

fn is_match(pattern: &str, text: &str) -> bool {
    compiled(pattern, false)
        .unwrap_or_else(|reason| panic!("{pattern}: {reason}"))
        .is_match(text)
}

#[test]
fn a_pattern_matches_as_re2_s_does() {
    for (pattern, text, matched) in [
        // ASCII classes and boundaries, where the engine's own are Unicode.
        (r"^\d+$", "٣", false),
        (r"^\d+$", "2026", true),
        (r"^\w+$", "café", false),
        (r"^\s$", "\u{a0}", false),
        (r"^\s$", "\u{b}", false),
        (r"a\b", "aé", true),
        (r"\bcat\b", "a cat!", true),
        (r"^\D$", "٣", true),
        // Characters that start nothing.
        (r"^a{,3}$", "a{,3}", true),
        (r"^a{,3}$", "aa", false),
        (r"^{$", "{", true),
        (r"^[a&&b]+$", "&ab", true),
        (r"^[--/]+$", ".", true),
        (r"^\Q1+1\E$", "1+1", true),
        (r"^\101$", "A", true),
        (r"^\p{^Greek}$", "a", true),
        (r"^\p{^Greek}$", "λ", false),
        // Unicode where RE2 reads it so.
        (r"^\pL+$", "café", true),
        (r"^.$", "é", true),
        (r"(?i)^straße$", "STRAßE", true),
    ] {
        assert_eq!(is_match(pattern, text), matched, "{pattern} on {text:?}");
    }
}

#[test]
fn a_pattern_for_the_whole_text_is_anchored_around_its_alternatives() {
    let whole = compiled("a|b", true).expect("a pattern");
    assert!(whole.is_match("b"));
    assert!(!whole.is_match("ab"));
}

#[test]
fn a_pattern_is_compiled_once_and_so_many_are_kept() {
    let first = compiled("kept", false).expect("a pattern");
    assert!(Rc::ptr_eq(
        &first,
        &compiled("kept", false).expect("a pattern")
    ));
    assert!(!Rc::ptr_eq(
        &first,
        &compiled("kept", true).expect("a pattern")
    ));
    for n in 0..=KEPT {
        compiled(&format!("p{n}"), false).expect("a pattern");
    }
    assert!(
        !Rc::ptr_eq(&first, &compiled("kept", false).expect("a pattern")),
        "compiled again"
    );
}

#[test]
fn a_pattern_that_isnt_re2_s_says_why() {
    assert_eq!(
        compiled("(?x)a", false).expect_err("refused"),
        "'(?x)a' isn't an RE2 pattern: the flag 'x' isn't RE2's"
    );
    assert_eq!(
        compiled(r"\p{Nothing}", false).expect_err("refused"),
        r"'\p{Nothing}' isn't an RE2 pattern: '\p{Nothing}' is neither a general category, written as its abbreviation, nor a script"
    );
    assert_eq!(
        compiled("[z-a]", false).expect_err("refused"),
        "'[z-a]' isn't an RE2 pattern: invalid character class range, the start must be <= the end"
    );
    // The engine's own limit, as RE2 has one.
    assert_eq!(
        compiled("((a{1000}){1000}){1000}", false).expect_err("refused"),
        "'((a{1000}){1000}){1000}' isn't an RE2 pattern: Compiled regex exceeds size limit of 10485760 bytes."
    );
}