Remove duplicated code from roc_repl_cli and get tests compiling

This commit is contained in:
Brian Carroll 2023-09-09 13:01:31 +01:00
parent 8f59ee9492
commit 3923dad203
No known key found for this signature in database
GPG key ID: 5C7B2EC4101703C0
11 changed files with 107 additions and 561 deletions

View file

@ -1,5 +1,12 @@
use bumpalo::Bump;
use indoc::indoc;
use roc_repl_cli::repl_state::{is_incomplete, ReplState, TIPS};
use roc_repl_cli::{evaluate, ReplHelper};
use roc_repl_ui::is_incomplete;
use roc_repl_ui::repl_state::{ReplAction, ReplState};
use roc_reporting::report::DEFAULT_PALETTE;
use roc_target::TargetInfo;
use rustyline::Editor;
use target_lexicon::Triple;
// These are tests of the REPL state machine. They work without actually
// running the CLI, and without using rustyline, and instead verify
@ -7,27 +14,27 @@ use roc_repl_cli::repl_state::{is_incomplete, ReplState, TIPS};
#[test]
fn one_plus_one() {
complete("1 + 1", &mut ReplState::new(), Ok(("2 : Num *", "val1")));
complete("1 + 1", &mut ReplState::new(), "2 : Num *", "val1");
}
#[test]
fn generated_expr_names() {
let mut state = ReplState::new();
complete("2 * 3", &mut state, Ok(("6 : Num *", "val1")));
complete("4 - 1", &mut state, Ok(("3 : Num *", "val2")));
complete("val1 + val2", &mut state, Ok(("9 : Num *", "val3")));
complete("1 + (val2 * val3)", &mut state, Ok(("28 : Num *", "val4")));
complete("2 * 3", &mut state, "6 : Num *", "val1");
complete("4 - 1", &mut state, "3 : Num *", "val2");
complete("val1 + val2", &mut state, "9 : Num *", "val3");
complete("1 + (val2 * val3)", &mut state, "28 : Num *", "val4");
}
#[test]
fn persisted_defs() {
let mut state = ReplState::new();
complete("x = 5", &mut state, Ok(("5 : Num *", "x")));
complete("7 - 3", &mut state, Ok(("4 : Num *", "val1")));
complete("y = 6", &mut state, Ok(("6 : Num *", "y")));
complete("val1 + x + y", &mut state, Ok(("15 : Num *", "val2")));
complete("x = 5", &mut state, "5 : Num *", "x");
complete("7 - 3", &mut state, "4 : Num *", "val1");
complete("y = 6", &mut state, "6 : Num *", "y");
complete("val1 + x + y", &mut state, "15 : Num *", "val2");
}
#[test]
@ -38,7 +45,7 @@ fn annotated_body() {
input.push_str("t = A");
complete(&input, &mut ReplState::new(), Ok(("A : [A, B, C]", "t")));
complete(&input, &mut ReplState::new(), "A : [A, B, C]", "t");
}
#[test]
@ -53,7 +60,7 @@ fn exhaustiveness_problem() {
input.push_str("t = A");
complete(&input, &mut state, Ok(("A : [A, B, C]", "t")));
complete(&input, &mut state, "A : [A, B, C]", "t");
}
// Run a `when` on it that isn't exhaustive
@ -88,7 +95,11 @@ fn exhaustiveness_problem() {
#[test]
fn tips() {
assert!(!is_incomplete(""));
assert_eq!(ReplState::new().step("", None), Ok(TIPS.to_string()));
let arena = Bump::new();
let target = Triple::host();
let target_info = TargetInfo::from(&target);
let action = ReplState::default().step(&arena, "", target_info, DEFAULT_PALETTE);
assert!(matches!(action, ReplAction::Help));
}
#[test]
@ -98,35 +109,49 @@ fn standalone_annotation() {
incomplete(&mut input);
assert!(!is_incomplete(&input));
assert_eq!(state.step(&input, None), Ok(String::new()));
let arena = Bump::new();
let target = Triple::host();
let target_info = TargetInfo::from(&target);
let action = state.step(&arena, &input, target_info, DEFAULT_PALETTE);
assert!(matches!(action, ReplAction::Nothing));
}
/// validate and step the given input, then check the Result vs the output
/// with ANSI escape codes stripped.
fn complete(input: &str, state: &mut ReplState, expected_step_result: Result<(&str, &str), i32>) {
fn complete(input: &str, state: &mut ReplState, expected_start: &str, expected_end: &str) {
assert!(!is_incomplete(input));
let arena = Bump::new();
let target = Triple::host();
let target_info = TargetInfo::from(&target);
let action = state.step(&arena, input, target_info, DEFAULT_PALETTE);
let repl_helper = ReplHelper::default();
let mut editor = Editor::<ReplHelper>::new();
editor.set_helper(Some(repl_helper));
let dimensions = editor.dimensions();
match state.step(input, None) {
Ok(string) => {
match action {
ReplAction::Eval {
opt_mono,
problems,
opt_var_name,
} => {
let string = evaluate(opt_mono, problems, opt_var_name, &target, dimensions);
let escaped =
std::string::String::from_utf8(strip_ansi_escapes::strip(string.trim()).unwrap())
.unwrap();
let comment_index = escaped.rfind('#').unwrap_or(escaped.len());
assert_eq!(
expected_step_result.map(|(starts_with, _)| starts_with),
Ok(escaped[0..comment_index].trim())
);
assert_eq!(expected_start, (escaped[0..comment_index].trim()));
assert_eq!(
expected_step_result.map(|(_, ends_with)| ends_with),
expected_end,
// +1 because we want to skip over the '#' itself
Ok(escaped[comment_index + 1..].trim())
(escaped[comment_index + 1..].trim())
);
}
Err(err) => {
assert_eq!(expected_step_result, Err(err));
_ => {
assert!(false, "Unexpected action: {:?}", action);
}
}
}
@ -143,10 +168,29 @@ fn incomplete(input: &mut String) {
/// with ANSI escape codes stripped.
fn error(input: &str, state: &mut ReplState, expected_step_result: String) {
assert!(!is_incomplete(input));
let arena = Bump::new();
let target = Triple::host();
let target_info = TargetInfo::from(&target);
let action = state.step(&arena, input, target_info, DEFAULT_PALETTE);
let repl_helper = ReplHelper::default();
let mut editor = Editor::<ReplHelper>::new();
editor.set_helper(Some(repl_helper));
let dimensions = editor.dimensions();
let escaped = state.step(input, None).map(|string| {
std::string::String::from_utf8(strip_ansi_escapes::strip(string.trim()).unwrap()).unwrap()
});
assert_eq!(Ok(expected_step_result), escaped);
match action {
ReplAction::Eval {
opt_mono,
problems,
opt_var_name,
} => {
let string = evaluate(opt_mono, problems, opt_var_name, &target, dimensions);
let escaped =
std::string::String::from_utf8(strip_ansi_escapes::strip(string.trim()).unwrap())
.unwrap();
assert_eq!(expected_step_result, escaped);
}
_ => {
assert!(false, "Unexpected action: {:?}", action);
}
}
}