Skip to content

Commit

Permalink
add tests for barewords
Browse files Browse the repository at this point in the history
- case sensitivity
- confirm they're not handled as regexes
- ending delimiters

In service of #67
  • Loading branch information
yshavit committed Jun 30, 2024
1 parent d8957ff commit 530dc9c
Showing 1 changed file with 40 additions and 2 deletions.
42 changes: 40 additions & 2 deletions src/matcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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)
}

Expand Down

0 comments on commit 530dc9c

Please sign in to comment.