Skip to content

Commit 6701c6e

Browse files
committed
expr: Rework branches and add tests for check_posix_regex_errors
1 parent cfb539d commit 6701c6e

File tree

1 file changed

+26
-13
lines changed

1 file changed

+26
-13
lines changed

src/uu/expr/src/syntax_tree.rs

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ fn check_posix_regex_errors(pattern: &str) -> ExprResult<()> {
193193
let mut escaped_braces: u64 = 0;
194194
let mut escaped = false;
195195

196-
let mut repeating_pattern_text = String::with_capacity(13);
196+
let mut repeating_pattern_text = String::new();
197197
let mut invalid_content_error = false;
198198

199199
for c in pattern.chars() {
@@ -212,22 +212,27 @@ fn check_posix_regex_errors(pattern: &str) -> ExprResult<()> {
212212
.ok_or(ExprError::UnmatchedClosingBrace)?;
213213
let mut repetition = repeating_pattern_text[..repeating_pattern_text.len() - 1]
214214
.splitn(2, |x| x == ',');
215-
match (repetition.next(), repetition.next()) {
216-
(None, None) => {
215+
match (
216+
repetition
217+
.next()
218+
.expect("splitn always returns at least one string"),
219+
repetition.next(),
220+
) {
221+
("", None) => {
217222
// Empty repeating pattern
218223
invalid_content_error = true;
219224
}
220-
(Some(x), None) | (Some(x), Some("")) => {
225+
(x, None) | (x, Some("")) => {
221226
if x.parse::<i16>().is_err() {
222227
invalid_content_error = true;
223228
}
224229
}
225-
(None, Some(x)) | (Some(""), Some(x)) => {
230+
("", Some(x)) => {
226231
if x.parse::<i16>().is_err() {
227232
invalid_content_error = true;
228233
}
229234
}
230-
(Some(f), Some(l)) => {
235+
(f, Some(l)) => {
231236
if let (Ok(f), Ok(l)) = (f.parse::<i16>(), l.parse::<i16>()) {
232237
invalid_content_error = invalid_content_error || f > l;
233238
} else {
@@ -736,17 +741,17 @@ mod test {
736741
}
737742

738743
#[test]
739-
fn validate_regex_valid() {
744+
fn check_regex_valid() {
740745
assert!(check_posix_regex_errors(r"(a+b) \(a* b\)").is_ok());
741746
}
742747

743748
#[test]
744-
fn validate_regex_simple_repeating_pattern() {
745-
assert!(check_posix_regex_errors(r"(a+b){4}").is_ok());
749+
fn check_regex_simple_repeating_pattern() {
750+
assert!(check_posix_regex_errors(r"\(a+b\)\{4\}").is_ok());
746751
}
747752

748753
#[test]
749-
fn validate_regex_missing_closing() {
754+
fn check_regex_missing_closing() {
750755
assert_eq!(
751756
check_posix_regex_errors(r"\(abc"),
752757
Err(ExprError::UnmatchedOpeningParenthesis)
@@ -759,7 +764,7 @@ mod test {
759764
}
760765

761766
#[test]
762-
fn validate_regex_missing_opening() {
767+
fn check_regex_missing_opening() {
763768
assert_eq!(
764769
check_posix_regex_errors(r"abc\)"),
765770
Err(ExprError::UnmatchedClosingParenthesis)
@@ -772,15 +777,15 @@ mod test {
772777
}
773778

774779
#[test]
775-
fn validate_regex_empty_repeating_pattern() {
780+
fn check_regex_empty_repeating_pattern() {
776781
assert_eq!(
777782
check_posix_regex_errors("ab\\{\\}"),
778783
Err(InvalidContent(r"\{\}".to_string()))
779784
)
780785
}
781786

782787
#[test]
783-
fn validate_regex_intervals_two_numbers() {
788+
fn check_regex_intervals_two_numbers() {
784789
assert_eq!(
785790
// out of order
786791
check_posix_regex_errors("ab\\{1,0\\}"),
@@ -798,5 +803,13 @@ mod test {
798803
check_posix_regex_errors("ab\\{a,b\\}"),
799804
Err(InvalidContent(r"\{\}".to_string()))
800805
);
806+
assert_eq!(
807+
check_posix_regex_errors("ab\\{a,\\}"),
808+
Err(InvalidContent(r"\{\}".to_string()))
809+
);
810+
assert_eq!(
811+
check_posix_regex_errors("ab\\{,b\\}"),
812+
Err(InvalidContent(r"\{\}".to_string()))
813+
);
801814
}
802815
}

0 commit comments

Comments
 (0)