Skip to content

Commit

Permalink
Test run
Browse files Browse the repository at this point in the history
  • Loading branch information
theory committed Aug 1, 2024
1 parent 0a79780 commit 910a7c8
Showing 1 changed file with 49 additions and 1 deletion.
50 changes: 49 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ where
docs(out)?;
res.exit = true
}
// Last one wins. Raise an error instead?
Value(val) => res.file = val.string()?,
_ => return Err(Box::new(arg.unexpected())),
}
Expand Down Expand Up @@ -108,7 +109,7 @@ fn docs(out: &mut impl Write) -> Result<(), Box<dyn Error>> {
mod tests {
use super::*;
use core::panic;
use std::str;
use std::{ffi::OsStr, path::Path, str};

struct TC<'a> {
name: &'a str,
Expand Down Expand Up @@ -183,6 +184,13 @@ mod tests {
file: "hello.json",
out: "",
},
TC {
name: "multiple values",
args: &["meta", "hello.json", "hi.json"],
exit: false,
file: "hi.json",
out: "",
},
] {
let mut file: Vec<u8> = Vec::new();
match parse_args(&mut file, tc.args) {
Expand All @@ -195,6 +203,7 @@ mod tests {
}
}

// Make sure we get an error for an unknown option.
let mut file: Vec<u8> = Vec::new();
match parse_args(&mut file, ["hi", "-x"]) {
Ok(_) => panic!("Should have failed on -x but did not"),
Expand All @@ -205,4 +214,43 @@ mod tests {

Ok(())
}

#[test]
fn test_run() -> Result<(), Box<dyn Error>> {
let meta = Path::new(env!("CARGO_MANIFEST_DIR"))
.join("corpus")
.join("v2")
.join("minimal.json");
println!("{}", meta.display());

struct TC<'a> {
name: &'a str,
args: &'a [&'a OsStr],
out: &'a str,
// code: u8,
}

for tc in [
TC {
name: "version",
args: &[OsStr::new("xyz"), OsStr::new("-v")],
out: concat!("xyz ", env!("CARGO_PKG_VERSION"), "\n"),
},
TC {
name: "pass file",
args: &[OsStr::new("xyz"), meta.as_os_str()],
out: "",
},
] {
let mut file: Vec<u8> = Vec::new();
match run(&mut file, tc.args) {
Err(e) => panic!("test {:} failed: {e}", tc.name),
Ok(_) => {
assert_eq!(str::from_utf8(&file)?, tc.out);
}
}
}

Ok(())
}
}

0 comments on commit 910a7c8

Please sign in to comment.