oysh/crates/oyster_runtime/tests/it/builtins/mod.rs
Charlotte Meyer 4a9d49fbd8 fix(runtime tests): treat write's better
- unwrap on writeln!()
- make sure "all" [0] is written in capture

Signed-off-by: Charlotte Meyer <dev@buffet.sh>
2022-11-05 16:21:34 +00:00

75 lines
1.6 KiB
Rust

mod math;
use std::{
borrow::Cow,
ffi::OsStr,
io::{self, Write},
};
use expect_test::{expect, Expect};
use oyster_builtin_proc::builtin;
use oyster_parser::ast;
use oyster_runtime::Shell;
use crate::collect_output;
#[builtin(description = "test builtin")]
fn test_builtin(_: &mut Shell, _: &[Cow<OsStr>]) {
writeln!(io::stdout(), "this is a test").unwrap();
}
fn check(ast: &ast::Code, expect: Expect) {
let actual = collect_output(|| {
let mut shell = Shell::new().unwrap();
shell.builtins_mut().add(test_builtin);
shell.run(ast).unwrap();
});
expect.assert_debug_eq(&actual);
}
#[test]
fn simple_builtin() {
let ast = {
use oyster_parser::ast::*;
Code(vec![Statement::Pipeline(Pipeline(vec![Command(
vec![Word(vec![WordPart::Text("test_builtin")])],
Redirect::None,
)]))])
};
check(
&ast,
expect![[r#"
"this is a test\r\n"
"#]],
);
}
#[test]
fn builtin_redirection() {
let ast = {
use oyster_parser::ast::*;
Code(vec![Statement::Pipeline(Pipeline(vec![
Command(
vec![Word(vec![WordPart::Text("test_builtin")])],
Redirect::Stdout,
),
Command(
vec![
Word(vec![WordPart::Text("wc")]),
Word(vec![WordPart::Text("-c")]),
],
Redirect::None,
),
]))])
};
check(
&ast,
expect![[r#"
"15\r\n"
"#]],
);
}