more unit tests, better highlighting

This commit is contained in:
2025-05-13 20:22:25 -04:00
parent de6e0166c5
commit 6006244739
10 changed files with 223 additions and 28 deletions

27
src/tests/highlight.rs Normal file
View File

@@ -0,0 +1,27 @@
use insta::assert_snapshot;
use crate::prompt::highlight::FernHighlighter;
use super::super::*;
#[test]
fn highlight_simple() {
let line = "echo foo bar";
let styled = FernHighlighter::new(line.to_string()).hl_input();
assert_snapshot!(styled)
}
#[test]
fn highlight_cmd_sub() {
let line = "echo foo $(echo bar)";
let styled = FernHighlighter::new(line.to_string()).hl_input();
assert_snapshot!(styled)
}
#[test]
fn highlight_cmd_sub_in_dquotes() {
let line = "echo \"foo $(echo bar) biz\"";
let styled = FernHighlighter::new(line.to_string()).hl_input();
assert_snapshot!(styled)
}

View File

@@ -24,6 +24,8 @@ pub mod expand;
pub mod term;
pub mod error;
pub mod getopt;
pub mod script;
pub mod highlight;
/// Unsafe to use outside of tests
pub fn get_nodes<F1>(input: &str, filter: F1) -> Vec<Node>

52
src/tests/script.rs Normal file
View File

@@ -0,0 +1,52 @@
use std::process::{self, Output};
use pretty_assertions::assert_eq;
use super::super::*;
fn get_script_output(name: &str, args: &[&str]) -> Output {
// Resolve the path to the fern binary.
// Do not question me.
let mut fern_path = env::current_exe()
.expect("Failed to get test executable"); // The path to the test executable
fern_path.pop(); // Hocus pocus
fern_path.pop();
fern_path.push("fern"); // Abra Kadabra
if !fern_path.is_file() {
fern_path.pop();
fern_path.pop();
fern_path.push("release");
fern_path.push("fern");
}
if !fern_path.is_file() {
panic!("where the hell is the binary")
}
process::Command::new(fern_path) // Alakazam
.arg(name)
.args(args)
.output()
.expect("Failed to run script")
}
#[test]
fn script_hello_world() {
let output = get_script_output("./test_scripts/hello.sh", &[]);
assert!(output.status.success());
let stdout = String::from_utf8_lossy(&output.stdout);
assert_eq!(stdout.trim(), "Hello, World!")
}
#[test]
fn script_cmdsub() {
let output = get_script_output("./test_scripts/cmdsub.sh", &[]);
assert!(output.status.success());
let stdout = String::from_utf8_lossy(&output.stdout);
assert_eq!(stdout.trim(), "foo Hello bar")
}
#[test]
fn script_multiline() {
let output = get_script_output("./test_scripts/multiline.sh", &[]);
assert!(output.status.success());
let stdout = String::from_utf8_lossy(&output.stdout);
assert_eq!(stdout.trim(), "foo\nbar\nbiz\nbuzz")
}

View File

@@ -3,8 +3,8 @@ source: src/tests/error.rs
expression: err_fmt
---
Parse Error - Unclosed subshell
-> [1;1]
-> [1;2]
 |
1 | (foo
 | ^
 | ^
 |