From 530dc9cf123e0b47e252fd1368913738d2d72804 Mon Sep 17 00:00:00 2001 From: Yuval Shavit Date: Sun, 30 Jun 2024 00:50:30 -0400 Subject: [PATCH] add tests for barewords - case sensitivity - confirm they're not handled as regexes - ending delimiters In service of #67 --- src/matcher.rs | 42 ++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 40 insertions(+), 2 deletions(-) diff --git a/src/matcher.rs b/src/matcher.rs index 87b594c..1a38eaa 100644 --- a/src/matcher.rs +++ b/src/matcher.rs @@ -178,6 +178,38 @@ mod test { parse_and_check_with(']', "foo] rest", StringMatcher::Substring("foo".to_string()), "] rest"); } + #[test] + fn bareword_case_sensitivity() { + let m = parse_and_check("hello", StringMatcher::Substring("hello".to_string()), ""); + assert_eq!(true, m.matches("hello")); + assert_eq!(true, m.matches("HELLO")); + } + + #[test] + fn bareword_regex_char() { + let m = parse_and_check("hello.world", StringMatcher::Substring("hello.world".to_string()), ""); + assert_eq!(true, m.matches("hello.world")); + assert_eq!(false, m.matches("hello world")); // the period is _not_ a regex any + } + + #[test] + fn bareword_end_delimiters() { + parse_and_check_with( + '@', + "hello@world", + StringMatcher::Substring("hello".to_string()), + "@world", + ); + + // "$" is always an end delimiter + parse_and_check_with( + '@', + "hello$world", + StringMatcher::Substring("hello".to_string()), + "$world", + ); + } + //noinspection RegExpSingleCharAlternation (for the "(a|b)" case) #[test] fn regex() { @@ -228,15 +260,21 @@ mod test { parse_and_check_with(']', "] rest", StringMatcher::Any, "] rest"); } - fn parse_and_check_with(bareword_end: char, text: &str, expect: StringMatcher, expect_remaining: &str) { + fn parse_and_check_with( + bareword_end: char, + text: &str, + expect: StringMatcher, + expect_remaining: &str, + ) -> StringMatcher { let mut iter = ParsingIterator::new(text); let matcher = StringMatcher::read(&mut iter, bareword_end).unwrap(); assert_eq!(matcher, expect); let remaining: String = iter.collect(); assert_eq!(&remaining, expect_remaining); + expect } - fn parse_and_check(text: &str, expect: StringMatcher, expect_remaining: &str) { + fn parse_and_check(text: &str, expect: StringMatcher, expect_remaining: &str) -> StringMatcher { parse_and_check_with(SELECTOR_SEPARATOR, text, expect, expect_remaining) }