#![allow(clippy::unwrap_used, clippy::expect_used)]
use kaish_kernel::{Kernel, KernelConfig};
fn kernel() -> Kernel {
Kernel::new(KernelConfig::isolated()).expect("kernel")
}
#[tokio::test]
async fn only_lowercase_literals_are_booleans() {
let k = kernel();
for (source, expected) in [
("x=true; typeof $x", "bool"),
("x=false; typeof $x", "bool"),
("x=yes; typeof $x", "string"),
("x=no; typeof $x", "string"),
("x=TRUE; typeof $x", "string"),
("x=FALSE; typeof $x", "string"),
("x=True; typeof $x", "string"),
("x=False; typeof $x", "string"),
("x=Yes; typeof $x", "string"),
("x=YES; typeof $x", "string"),
("x=NO; typeof $x", "string"),
("x=on; typeof $x", "string"),
("x=off; typeof $x", "string"),
] {
let r = k.execute(source).await.expect("kernel execute");
assert_eq!(r.code, 0, "{source} failed: {r:?}");
assert_eq!(r.text_out().trim(), expected, "for: {source}");
}
}
#[tokio::test]
async fn lookalikes_compare_as_the_strings_they_are() {
let k = kernel();
for (source, expected) in [
("if [[ true == \"true\" ]]; then echo same; else echo differ; fi", "same"),
("if [[ true == TRUE ]]; then echo same; else echo differ; fi", "differ"),
("if [[ -n TRUE ]]; then echo nonempty; else echo empty; fi", "nonempty"),
("if [[ true == on ]]; then echo same; else echo differ; fi", "differ"),
] {
let r = k.execute(source).await.expect("kernel execute");
assert_eq!(r.text_out().trim(), expected, "for: {source}");
}
}
#[tokio::test]
async fn lookalike_words_run_as_ordinary_commands() {
let k = kernel();
for source in [
"echo yes",
"echo no",
"echo Yes",
"echo TRUE",
"echo yes | head -1",
"echo -- no",
"echo TRUE data.csv",
] {
let r = k.execute(source).await.expect("kernel execute");
assert_eq!(r.code, 0, "{source} failed: {r:?}");
}
}
#[tokio::test]
async fn yes_is_reachable_as_a_command_name() {
let plans = kaish_kernel::plan_program("yes | head -3").expect("parses");
let names: Vec<&str> = plans[0]
.plan
.commands
.iter()
.map(|c| c.name.as_str())
.collect();
assert_eq!(names, vec!["yes", "head"]);
}