Share code between validation and step

This commit is contained in:
Richard Feldman 2022-10-27 03:32:04 -04:00
parent 0a80b543b4
commit 6fbafad3ff
No known key found for this signature in database
GPG key ID: F1F21AA5B1D9E43B
2 changed files with 108 additions and 62 deletions

View file

@ -6,10 +6,30 @@ fn one_plus_one() {
let mut state = ReplState::new();
let input = "1 + 1";
assert_validates(input);
assert_step(input, &mut state, Ok("2 : Num * # TODOval1"));
assert_done(&state);
}
#[test]
fn incomplete_annotation() {
let mut state = ReplState::new();
let input = "x : Str";
assert_incomplete(input);
assert_step(input, &mut state, Ok(""));
assert_done(&state);
}
fn assert_validates(input: &str) {
assert!(matches!(validate(input), Ok(ValidationResult::Valid(None))));
}
fn assert_incomplete(input: &str) {
assert!(matches!(validate(input), Ok(ValidationResult::Incomplete)));
}
/// step the given input, then check the Result vs the trimmed input with ANSI escape codes stripped.
fn assert_step(input: &str, state: &mut ReplState, expected_step_result: Result<&str, i32>) {
match state.step(input) {
Ok(string) => {
@ -25,3 +45,13 @@ fn assert_step(input: &str, state: &mut ReplState, expected_step_result: Result<
}
}
}
fn assert_done(state: &ReplState) {
assert_eq!(
state.pending_src,
String::new(),
"pending_src was not empty; it was {:?}",
state.pending_src
);
assert!(!state.prev_line_blank, "prev_line_blank was true");
}