#![cfg(test)]
use crate::opts::{Op, Opts};
use crate::parser;
use std::path::PathBuf;
#[test]
fn test_get_opts() {
fn ex(c: Option<&str>, x: Option<&str>, op: Op) -> Opts {
let mut opts = Opts::new();
(opts.xargs_cmd, opts.op) = (x.map(PathBuf::from), op);
opts.cwd =
c.map(PathBuf::from).unwrap_or(std::env::current_dir().unwrap());
opts
}
fn rc(args: &[&str]) -> Opts {
let args = [&["gitnu"], args].concat();
parser::parse(&args.iter().map(|v| String::from(*v)).collect()).1
}
fn assert_eq((rec, exp): &(&[&str], Opts)) {
let rec = rc(rec);
assert_eq!(rec.cwd, exp.cwd);
assert_eq!(rec.xargs_cmd, exp.xargs_cmd);
assert_eq!(rec.op, exp.op);
}
let tests: &[(&[&str], Opts)] = &[
(&["-C", "/dev/null"], ex(Some("/dev/null"), None, Op::Bypass)),
(&["-c", "nvim"], ex(None, Some("nvim"), Op::Xargs)),
(
&["-C", "/etc", "-c", "nvim"],
ex(Some("/etc"), Some("nvim"), Op::Xargs),
),
(
&["-c", "nvim", "-C", "/etc"],
ex(Some("/etc"), Some("nvim"), Op::Xargs),
),
(&["status", "--short"], ex(None, None, Op::Status)),
(&["add", "2-4"], ex(None, None, Op::Read)),
(&["-C", "/tmp", "add", "2-4"], ex(Some("/tmp"), None, Op::Read)),
(&["-C"], ex(None, None, Op::Bypass)),
(&["-C", "status"], ex(Some("status"), None, Op::Bypass)),
(&["-c"], ex(None, None, Op::Bypass)),
(&["-c", "status"], ex(None, Some("status"), Op::Xargs)),
];
for i in tests {
assert_eq(i);
}
}