use std::process::Command;
fn ilo() -> Command {
Command::new(env!("CARGO_BIN_EXE_ilo"))
}
fn run_ok(engine: &str, src: &str, entry: &str) -> String {
let out = ilo()
.args([src, engine, entry])
.output()
.expect("failed to run ilo");
assert!(
out.status.success(),
"ilo {engine} failed for `{src}`: stderr={}",
String::from_utf8_lossy(&out.stderr)
);
String::from_utf8_lossy(&out.stdout).trim().to_string()
}
fn engines() -> &'static [&'static str] {
&["--vm"]
}
#[test]
fn wr_csv_list_of_lists_no_header() {
for (i, engine) in engines().iter().enumerate() {
let path = format!("/tmp/ilo_wr_csv_ll_{i}.csv");
let _ = std::fs::remove_file(&path);
let src = format!(r#"f>R t t;wr "{path}" [["a","b"],["c","d"]] "csv""#);
let _ = run_ok(engine, &src, "f");
let body = std::fs::read_to_string(&path).expect("missing output file");
assert_eq!(body, "a,b\nc,d\n", "engine={engine}");
let _ = std::fs::remove_file(&path);
}
}
#[test]
fn wr_tsv_list_of_lists() {
for (i, engine) in engines().iter().enumerate() {
let path = format!("/tmp/ilo_wr_tsv_ll_{i}.tsv");
let _ = std::fs::remove_file(&path);
let src = format!(r#"f>R t t;wr "{path}" [["a","b"],["c","d"]] "tsv""#);
let _ = run_ok(engine, &src, "f");
let body = std::fs::read_to_string(&path).expect("missing output file");
assert_eq!(body, "a\tb\nc\td\n", "engine={engine}");
let _ = std::fs::remove_file(&path);
}
}
#[test]
fn wr_csv_field_with_comma_is_quoted() {
for (i, engine) in engines().iter().enumerate() {
let path = format!("/tmp/ilo_wr_csv_comma_{i}.csv");
let _ = std::fs::remove_file(&path);
let src = format!(r#"f>R t t;wr "{path}" [["a,b","plain"]] "csv""#);
let _ = run_ok(engine, &src, "f");
let body = std::fs::read_to_string(&path).expect("missing output file");
assert_eq!(body, "\"a,b\",plain\n", "engine={engine}");
let _ = std::fs::remove_file(&path);
}
}
#[test]
fn wr_csv_field_with_quote_is_escaped() {
for (i, engine) in engines().iter().enumerate() {
let path = format!("/tmp/ilo_wr_csv_quote_{i}.csv");
let _ = std::fs::remove_file(&path);
let src = format!(r#"f>R t t;wr "{path}" [["he said \"hi\"","x"]] "csv""#);
let _ = run_ok(engine, &src, "f");
let body = std::fs::read_to_string(&path).expect("missing output file");
assert_eq!(body, "\"he said \"\"hi\"\"\",x\n", "engine={engine}");
let _ = std::fs::remove_file(&path);
}
}
#[test]
fn wr_csv_field_with_newline_is_quoted() {
for (i, engine) in engines().iter().enumerate() {
let path = format!("/tmp/ilo_wr_csv_nl_{i}.csv");
let _ = std::fs::remove_file(&path);
let src = format!(r#"f>R t t;wr "{path}" [["a\nb","c"]] "csv""#);
let _ = run_ok(engine, &src, "f");
let body = std::fs::read_to_string(&path).expect("missing output file");
assert_eq!(body, "\"a\nb\",c\n", "engine={engine}");
let _ = std::fs::remove_file(&path);
}
}
#[test]
fn wr_csv_roundtrip_via_rdl_spl() {
for (i, engine) in engines().iter().enumerate() {
let path = format!("/tmp/ilo_wr_csv_rt_{i}.csv");
let _ = std::fs::remove_file(&path);
let write_src = format!(r#"f>R t t;wr "{path}" [["a","b"],["c","d"]] "csv""#);
let _ = run_ok(engine, &write_src, "f");
let read_src = format!(r#"g>t;p=rdl "{path}";?p{{~v:hd (spl (hd v) ",");^_:"err"}}"#);
let first = run_ok(engine, &read_src, "g");
assert_eq!(first, "a", "engine={engine}: rdl/spl roundtrip mismatch");
let _ = std::fs::remove_file(&path);
}
}