fix expression parser, make tests more verbose
This commit is contained in:
parent
92a4dd02eb
commit
d9eab53858
1 changed files with 36 additions and 22 deletions
|
@ -242,17 +242,16 @@ where
|
|||
F: Fn(I) -> Option<O>,
|
||||
{
|
||||
move |input: I| {
|
||||
let mut len = 1usize;
|
||||
let mut result = Err(nom::Err::Incomplete(nom::Needed::Unknown));
|
||||
loop {
|
||||
let new_result = take(len).map_opt(&cond).parse(input);
|
||||
if new_result.is_err() {
|
||||
let mut len = input.input_len();
|
||||
while len > 0 {
|
||||
let result = take(len).map_opt(&cond).parse(input);
|
||||
if result.is_ok() {
|
||||
return result;
|
||||
} else {
|
||||
result = new_result;
|
||||
len += 1;
|
||||
len -= 1;
|
||||
}
|
||||
}
|
||||
Err(nom::Err::Incomplete(nom::Needed::Unknown))
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -270,11 +269,11 @@ mod tests {
|
|||
let parser = expression_parser(&['x']);
|
||||
let mut working_test_cases = vec![
|
||||
("1", ("", "1")),
|
||||
("1x", ("", "1x")),
|
||||
("1*x", ("", "1*x")),
|
||||
("56coucou", ("coucou", "56")), // should stop after 56 because c is not a known variable
|
||||
(
|
||||
"8x + 1 heille salut ça va ou quoi 46 - 5x",
|
||||
("heille salut ça va ou quoi 46 - 5x", "8x + 1"),
|
||||
"8*x + 1 heille salut ça va ou quoi 46 - 5*x",
|
||||
("heille salut ça va ou quoi 46 - 5*x", "8*x + 1"),
|
||||
),
|
||||
];
|
||||
let mut not_working_test_cases = vec![
|
||||
|
@ -282,9 +281,9 @@ mod tests {
|
|||
"(",
|
||||
"y",
|
||||
"abcdexx489",
|
||||
" ", // spaces are not expressions
|
||||
" ", // spaces are not expressions
|
||||
" h", // because of previous, this fails
|
||||
" 1", // this too but the parser should remain dumb and not expect spaces before / after
|
||||
// " 1", // the current impl of the parser means that this is valid
|
||||
];
|
||||
|
||||
for (test, expected) in working_test_cases.drain(..) {
|
||||
|
@ -296,13 +295,16 @@ mod tests {
|
|||
"case \"{test}\""
|
||||
)
|
||||
} else {
|
||||
panic!("result was not Ok: {output:?}");
|
||||
panic!("result of \"{test}\" was not Ok: {output:?}");
|
||||
}
|
||||
}
|
||||
|
||||
for test in not_working_test_cases.drain(..) {
|
||||
let output = parser(test);
|
||||
assert!(output.is_err(), "result was not Err: {output:?}");
|
||||
assert!(
|
||||
output.is_err(),
|
||||
"result of \"{test}\" was not Err: {output:?}"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -320,12 +322,15 @@ mod tests {
|
|||
if let Ok(result) = output {
|
||||
assert_eq!(expected, result);
|
||||
} else {
|
||||
panic!("result was not Ok: {output:?}");
|
||||
panic!("result of \"{test}\" was not Ok: {output:?}");
|
||||
}
|
||||
}
|
||||
for test in not_working_cases.drain(..) {
|
||||
let output = parser(test);
|
||||
assert!(output.is_err(), "result was not Err: {output:?}");
|
||||
assert!(
|
||||
output.is_err(),
|
||||
"result of \"{test}\" was not Err: {output:?}"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -343,12 +348,15 @@ mod tests {
|
|||
if let Ok(result) = output {
|
||||
assert_eq!(expected, result);
|
||||
} else {
|
||||
panic!("result was not Ok: {output:?}");
|
||||
panic!("result of \"{test}\" was not Ok: {output:?}");
|
||||
}
|
||||
}
|
||||
for test in not_working_cases.drain(..) {
|
||||
let output = parser(test);
|
||||
assert!(output.is_err(), "result was not Err: {output:?}");
|
||||
assert!(
|
||||
output.is_err(),
|
||||
"result of \"{test}\" was not Err: {output:?}"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -371,12 +379,15 @@ mod tests {
|
|||
if let Ok(result) = output {
|
||||
assert_eq!(expected, result, "{test}");
|
||||
} else {
|
||||
panic!("result was not Ok: {output:?}");
|
||||
panic!("result of \"{test}\" was not Ok: {output:?}");
|
||||
}
|
||||
}
|
||||
for test in not_working_cases.drain(..) {
|
||||
let output = parser(test);
|
||||
assert!(output.is_err(), "result was not Err: {output:?}");
|
||||
assert!(
|
||||
output.is_err(),
|
||||
"result of \"{test}\" was not Err: {output:?}"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -397,12 +408,15 @@ mod tests {
|
|||
"case \"{test}\""
|
||||
);
|
||||
} else {
|
||||
panic!("result was not Ok: {output:?}");
|
||||
panic!("result of \"{test}\" was not Ok: {output:?}");
|
||||
}
|
||||
}
|
||||
for test in not_working_cases.drain(..) {
|
||||
let output = parser(test);
|
||||
assert!(output.is_err(), "result was not Err: {output:?}");
|
||||
assert!(
|
||||
output.is_err(),
|
||||
"result of \"{test}\" was not Err: {output:?}"
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue