use bson::{Bson, Document, Regex, doc};
use super::{MongoTextComparison, text_regex};
use crate::{
At, ComplexComparison, FieldName, Mongo, RecordAddress, TextFilter, ToDatabaseComparison,
TryFromFilter,
comparison::{
TextMatch,
mongo::{MongoConjunction, MongoOperator},
},
};
fn pattern(operand: &str, kind: TextMatch) -> String {
text_regex(operand, kind, false).pattern.into_string()
}
fn unescaped(operand: &str, kind: TextMatch) -> String {
match kind {
TextMatch::Contains => operand.to_owned(),
TextMatch::Prefix => format!("\\A{operand}"),
TextMatch::Suffix => format!("{operand}\\z"),
}
}
enum Token {
Literal(char),
AnyCharacter,
StartOfSubject,
EndOfSubject,
EndOrBeforeFinalNewline,
}
fn tokens(pattern: &str) -> Option<Vec<Token>> {
let mut characters = pattern.chars();
let mut read = Vec::new();
while let Some(ch) = characters.next() {
read.push(match ch {
'\\' => match characters.next()? {
'A' => Token::StartOfSubject,
'z' => Token::EndOfSubject,
'x' => {
let digits: String = characters
.by_ref()
.skip_while(|ch| *ch == '{')
.take_while(|ch| *ch != '}')
.collect();
Token::Literal(char::from_u32(u32::from_str_radix(&digits, 16).ok()?)?)
}
quoted if !quoted.is_ascii_alphanumeric() => Token::Literal(quoted),
_ => return None,
},
'.' => Token::AnyCharacter,
'^' => Token::StartOfSubject,
'$' => Token::EndOrBeforeFinalNewline,
meta if "[]|(){}?*+".contains(meta) => return None,
literal => Token::Literal(literal),
});
}
Some(read)
}
fn matches_at(read: &[Token], subject: &[char], from: usize) -> bool {
let mut at = from;
for token in read {
match token {
Token::StartOfSubject => {
if at != 0 {
return false;
}
}
Token::EndOfSubject => {
if at != subject.len() {
return false;
}
}
Token::EndOrBeforeFinalNewline => {
if at != subject.len() && !(at + 1 == subject.len() && subject[at] == '\n') {
return false;
}
}
Token::Literal(expected) => {
if subject.get(at) != Some(expected) {
return false;
}
at += 1;
}
Token::AnyCharacter => {
if !matches!(subject.get(at), Some(ch) if *ch != '\n') {
return false;
}
at += 1;
}
}
}
true
}
fn admitted<'a>(pattern: &str, subjects: &[&'a str]) -> Vec<&'a str> {
let read = tokens(pattern).expect("this evaluator reads the pattern under test");
subjects
.iter()
.copied()
.filter(|subject| {
let characters: Vec<char> = subject.chars().collect();
(0..=characters.len()).any(|from| matches_at(&read, &characters, from))
})
.collect()
}
#[test]
fn the_three_matches_differ_only_in_their_anchors() {
assert_eq!(pattern("cam", TextMatch::Contains), "cam");
assert_eq!(pattern("cam", TextMatch::Prefix), "\\Acam");
assert_eq!(pattern("cam", TextMatch::Suffix), "cam\\z");
}
#[test]
fn dropping_the_escape_changes_which_subjects_match() {
const SUBJECTS: [&str; 4] = ["a.b", "axb", "ab", "zza.bzz"];
let escaped = pattern("a.b", TextMatch::Contains);
let dropped = unescaped("a.b", TextMatch::Contains);
assert_eq!(escaped, "a\\.b");
assert_eq!(dropped, "a.b");
assert_eq!(admitted(&escaped, &SUBJECTS), ["a.b", "zza.bzz"]);
assert_eq!(admitted(&dropped, &SUBJECTS), ["a.b", "axb", "zza.bzz"]);
}
#[test]
fn an_operand_that_looks_like_an_anchor_does_not_anchor() {
const SUBJECTS: [&str; 3] = ["^abc", "abc", "x^abc"];
let escaped = pattern("^a", TextMatch::Prefix);
let dropped = unescaped("^a", TextMatch::Prefix);
assert_eq!(escaped, "\\A\\^a");
assert_eq!(dropped, "\\A^a");
assert_eq!(admitted(&escaped, &SUBJECTS), ["^abc"]);
assert_eq!(admitted(&dropped, &SUBJECTS), ["abc"]);
}
#[test]
fn a_suffix_ends_at_the_subject_and_not_before_its_last_newline() {
const SUBJECTS: [&str; 3] = ["acme corp", "acme corp\n", "corporate"];
let exact = pattern("corp", TextMatch::Suffix);
assert_eq!(exact, "corp\\z");
assert_eq!(admitted(&exact, &SUBJECTS), ["acme corp"]);
assert_eq!(admitted("corp$", &SUBJECTS), ["acme corp", "acme corp\n"]);
}
#[test]
fn every_ascii_character_is_a_pattern_matching_only_itself() {
let alphabet: Vec<String> = (0u8..=127)
.map(|byte| char::from(byte).to_string())
.collect();
let subjects: Vec<&str> = alphabet.iter().map(String::as_str).collect();
for one in &subjects {
let escaped = pattern(one, TextMatch::Contains);
assert_eq!(
admitted(&escaped, &subjects),
[*one],
"{one:?} escaped to {escaped:?}"
);
}
}
#[test]
fn a_nul_operand_is_carried_as_a_braced_hex_escape() {
let escaped = pattern("a\0b", TextMatch::Contains);
assert_eq!(escaped, "a\\x{0}b");
assert!(!escaped.contains('\0'));
assert_eq!(
admitted(&escaped, &["a\u{0}b", "ab", "a0b", "ax0b"]),
["a\u{0}b"]
);
}
#[test]
fn a_non_ascii_operand_is_carried_unchanged() {
let escaped = pattern("café-中", TextMatch::Contains);
assert_eq!(escaped, "café\\-中");
assert_eq!(
admitted(&escaped, &["a café-中 shop", "cafe-中"]),
["a café-中 shop"]
);
}
#[test]
fn a_bracket_operand_is_a_character_and_not_a_group() {
let escaped = pattern("(", TextMatch::Contains);
assert_eq!(escaped, "\\(");
assert_eq!(admitted(&escaped, &["f(x)", "fx"]), ["f(x)"]);
assert!(
tokens("(").is_none(),
"an unescaped bracket leaves the language this family writes"
);
}
#[test]
fn the_case_insensitive_variant_moves_the_option_and_not_the_pattern() {
let plain = text_regex("Cam", TextMatch::Contains, false);
let folding = text_regex("Cam", TextMatch::Contains, true);
assert_eq!(plain.pattern, folding.pattern);
assert_eq!(plain.options.as_str(), "");
assert_eq!(folding.options.as_str(), "i");
}
const ROW: RecordAddress = RecordAddress::new(FieldName::new("media"), &[FieldName::new("id")]);
fn rendered<C: ComplexComparison<Mongo>>(comparison: &C, field: &str) -> Document {
let frame = At::new(&ROW).frame(FieldName::new(field));
comparison
.apply(frame.at(), MongoConjunction::new())
.expect("a MongoDB lowering cannot refuse")
.into_document()
}
fn written<'a>(filter: &'a Document, field: &str, operator: MongoOperator) -> &'a Regex {
match filter.get(field) {
Some(Bson::Document(operators)) => match operators.get(operator.as_str()) {
Some(Bson::RegularExpression(regex)) => regex,
other => panic!("{field}'s {} holds {other:?}", operator.as_str()),
},
other => panic!("{field} holds {other:?}"),
}
}
#[test]
fn the_value_operators_lower_as_the_scalar_kind_s_do() {
let comparison = MongoTextComparison::<String>::try_from_filter(
TextFilter::<String>::new()
.with_eq("a".to_owned())
.with_ne("b".to_owned())
.with_gt("c".to_owned())
.with_gte("d".to_owned())
.with_lt("e".to_owned())
.with_lte("f".to_owned())
.with_in_(["g".to_owned(), "h".to_owned()])
.with_not_in(["i".to_owned()]),
)
.expect("the identity crossing cannot fail");
assert_eq!(comparison.conjuncts(), 8);
assert_eq!(
rendered(&comparison, "n"),
doc! {
"n": {
"$eq": "a",
"$ne": "b",
"$gt": "c",
"$gte": "d",
"$lt": "e",
"$lte": "f",
"$in": ["g", "h"],
"$nin": ["i"],
}
}
);
}
#[test]
fn the_three_matches_reach_the_document_under_one_key() {
for (filter, expected) in [
(
TextFilter::<String>::new().with_contains("cam".to_owned()),
"cam",
),
(
TextFilter::<String>::new().with_starts_with("cam".to_owned()),
"\\Acam",
),
(
TextFilter::<String>::new().with_ends_with("cam".to_owned()),
"cam\\z",
),
] {
let comparison = MongoTextComparison::<String>::try_from_filter(filter)
.expect("the identity crossing cannot fail");
let document = rendered(&comparison, "n");
assert_eq!(
written(&document, "n", MongoOperator::Regex)
.pattern
.as_str(),
expected
);
}
}
#[test]
fn a_negated_match_is_the_same_regex_under_not() {
let plain = MongoTextComparison::<String>::try_from_filter(
TextFilter::<String>::new().with_contains("cam".to_owned()),
)
.expect("the identity crossing cannot fail");
let negated = MongoTextComparison::<String>::try_from_filter(
TextFilter::<String>::new().with_not_contains("cam".to_owned()),
)
.expect("the identity crossing cannot fail");
let positive = rendered(&plain, "n");
let refused = rendered(&negated, "n");
assert_eq!(
written(&positive, "n", MongoOperator::Regex),
written(&refused, "n", MongoOperator::Not)
);
}
#[test]
fn the_lowering_carries_the_escaped_pattern_into_the_document() {
const SUBJECTS: [&str; 3] = ["a.b", "axb", "ab"];
let comparison = MongoTextComparison::<String>::try_from_filter(
TextFilter::<String>::new().with_contains("a.b".to_owned()),
)
.expect("the identity crossing cannot fail");
let document = rendered(&comparison, "n");
let carried = written(&document, "n", MongoOperator::Regex)
.pattern
.as_str();
assert_eq!(carried, "a\\.b");
assert_eq!(admitted(carried, &SUBJECTS), ["a.b"]);
}
#[test]
fn a_case_insensitive_slot_carries_its_option_into_the_document() {
let comparison = MongoTextComparison::<String>::try_from_filter(
TextFilter::<String>::new()
.with_contains("Cam".to_owned())
.with_starts_with_nocase("Cam".to_owned()),
)
.expect("the identity crossing cannot fail");
let document = rendered(&comparison, "n");
let plain = written(&document, "n", MongoOperator::Regex);
let folding = match document.get_array("$and").expect("the second match folds")[0] {
Bson::Document(ref branch) => written(branch, "n", MongoOperator::Regex).clone(),
ref other => panic!("an `$and` branch holds {other:?}"),
};
assert_eq!(plain.options.as_str(), "");
assert_eq!(folding.options.as_str(), "i");
assert_eq!(folding.pattern.as_str(), "\\ACam");
}
#[test]
fn twelve_affix_slots_reach_two_keys_and_the_rest_fold() {
let comparison = MongoTextComparison::<String>::try_from_filter(
TextFilter::<String>::new()
.with_eq("a".to_owned())
.with_ne("b".to_owned())
.with_gt("c".to_owned())
.with_gte("d".to_owned())
.with_lt("e".to_owned())
.with_lte("f".to_owned())
.with_contains("g".to_owned())
.with_contains_nocase("h".to_owned())
.with_not_contains("i".to_owned())
.with_not_contains_nocase("j".to_owned())
.with_starts_with("k".to_owned())
.with_starts_with_nocase("l".to_owned())
.with_not_starts_with("m".to_owned())
.with_not_starts_with_nocase("n".to_owned())
.with_ends_with("o".to_owned())
.with_ends_with_nocase("p".to_owned())
.with_not_ends_with("q".to_owned())
.with_not_ends_with_nocase("r".to_owned())
.with_in_(["s".to_owned(), "t".to_owned()])
.with_not_in(["u".to_owned()]),
)
.expect("the identity crossing cannot fail");
assert_eq!(comparison.conjuncts(), 20);
let document = rendered(&comparison, "n");
let branches = document.get_array("$and").expect("ten operators fold");
assert_eq!(branches.len(), 10);
assert_eq!(
written(&document, "n", MongoOperator::Regex)
.pattern
.as_str(),
"g"
);
assert_eq!(
written(&document, "n", MongoOperator::Not).pattern.as_str(),
"i"
);
}
#[test]
fn a_crossing_replaces_every_operand_with_its_stored_form() {
let crossed = MongoTextComparison::<String>::try_from_filter_with(
TextFilter::<String>::new()
.with_eq("Camera".to_owned())
.with_starts_with("Cam".to_owned())
.with_not_ends_with_nocase("RAW".to_owned())
.with_in_(["A".to_owned(), "B".to_owned()]),
|text: String| Ok::<_, core::convert::Infallible>(text.to_lowercase()),
)
.expect("a folding conversion cannot fail");
assert_eq!(crossed.eq().map(String::as_str), Some("camera"));
assert_eq!(crossed.starts_with().map(String::as_str), Some("cam"));
assert_eq!(
crossed.not_ends_with_nocase().map(String::as_str),
Some("raw")
);
assert_eq!(crossed.in_().unwrap(), ["a".to_owned(), "b".to_owned()]);
}
#[test]
fn a_borrowed_operand_lowers_as_the_owned_one_does() {
let owned = MongoTextComparison::<String>::try_from_filter(
TextFilter::<String>::new().with_starts_with("cam".to_owned()),
)
.expect("the identity crossing cannot fail");
let text = "cam".to_owned();
let borrowed = MongoTextComparison::<&String>::try_from_filter(
TextFilter::<&String>::new().with_starts_with(&text),
)
.expect("the identity crossing cannot fail");
assert_eq!(rendered(&owned, "n"), rendered(&borrowed, "n"));
}
#[test]
fn the_codegen_projection_resolves_for_a_text_field() {
type Crossed = <TextFilter<String> as ToDatabaseComparison<String, Mongo>>::Comparison;
let comparison: Crossed = MongoTextComparison::<String>::new();
assert!(comparison.is_empty());
}
#[test]
fn the_identity_crossing_cannot_fail() {
fn unreachable_reason(
error: <MongoTextComparison<String> as TryFromFilter<TextFilter<String>>>::Error,
) -> ! {
match error {}
}
match MongoTextComparison::<String>::try_from_filter(
TextFilter::<String>::new().with_contains("cam".to_owned()),
) {
Ok(comparison) => assert_eq!(comparison.conjuncts(), 1),
Err(error) => unreachable_reason(error),
}
}