Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 0a79780

Browse files
committedAug 1, 2024
Test parse_args()
1 parent 3ec2959 commit 0a79780

File tree

1 file changed

+106
-1
lines changed

1 file changed

+106
-1
lines changed
 

‎src/main.rs

+106-1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ struct Args {
3131
file: String,
3232
}
3333

34+
const META_FILE: &str = "META.json";
35+
3436
fn parse_args<I>(out: &mut impl Write, args: I) -> Result<Args, Box<dyn Error>>
3537
where
3638
I: IntoIterator,
@@ -39,7 +41,7 @@ where
3941
use lexopt::prelude::*;
4042
let mut res = Args {
4143
exit: false,
42-
file: String::from("META.json"),
44+
file: String::from(META_FILE),
4345
};
4446
let mut parser = lexopt::Parser::from_iter(args);
4547

@@ -101,3 +103,106 @@ fn docs(out: &mut impl Write) -> Result<(), Box<dyn Error>> {
101103
writeln!(out, "Docs")?;
102104
Ok(())
103105
}
106+
107+
#[cfg(test)]
108+
mod tests {
109+
use super::*;
110+
use core::panic;
111+
use std::str;
112+
113+
struct TC<'a> {
114+
name: &'a str,
115+
args: &'a [&'a str],
116+
exit: bool,
117+
file: &'a str,
118+
out: &'a str,
119+
}
120+
121+
#[test]
122+
fn test_parse_args() -> Result<(), Box<dyn Error>> {
123+
for tc in [
124+
TC {
125+
name: "no args",
126+
args: &["meta"],
127+
exit: false,
128+
file: META_FILE,
129+
out: "",
130+
},
131+
TC {
132+
name: "short help",
133+
args: &["meta", "-h"],
134+
exit: true,
135+
file: META_FILE,
136+
out: "Usage: meta [--help | h] [--version | -v] [<path>]\n\n\
137+
Options:\n\
138+
\x20 -h --help Print this usage statement and exit\n\
139+
\x20 -v --version Print the version number and exit\n",
140+
},
141+
TC {
142+
name: "long help",
143+
args: &["meta", "--help"],
144+
exit: true,
145+
file: META_FILE,
146+
out: "Usage: meta [--help | h] [--version | -v] [<path>]\n\n\
147+
Options:\n\
148+
\x20 -h --help Print this usage statement and exit\n\
149+
\x20 -v --version Print the version number and exit\n",
150+
},
151+
TC {
152+
name: "short version",
153+
args: &["meta", "-v"],
154+
exit: true,
155+
file: META_FILE,
156+
out: concat!("meta ", env!("CARGO_PKG_VERSION"), "\n"),
157+
},
158+
TC {
159+
name: "long version",
160+
args: &["meta", "--version"],
161+
exit: true,
162+
file: META_FILE,
163+
out: concat!("meta ", env!("CARGO_PKG_VERSION"), "\n"),
164+
},
165+
TC {
166+
name: "short man",
167+
args: &["meta", "-m"],
168+
exit: true,
169+
file: META_FILE,
170+
out: "Docs\n",
171+
},
172+
TC {
173+
name: "long man",
174+
args: &["meta", "--man"],
175+
exit: true,
176+
file: META_FILE,
177+
out: "Docs\n",
178+
},
179+
TC {
180+
name: "file name",
181+
args: &["meta", "hello.json"],
182+
exit: false,
183+
file: "hello.json",
184+
out: "",
185+
},
186+
] {
187+
let mut file: Vec<u8> = Vec::new();
188+
match parse_args(&mut file, tc.args) {
189+
Err(e) => panic!("test {} failed: {e}", tc.name),
190+
Ok(res) => {
191+
assert_eq!(res.exit, tc.exit);
192+
assert_eq!(res.file, tc.file);
193+
assert_eq!(str::from_utf8(&file)?, tc.out);
194+
}
195+
}
196+
}
197+
198+
let mut file: Vec<u8> = Vec::new();
199+
match parse_args(&mut file, ["hi", "-x"]) {
200+
Ok(_) => panic!("Should have failed on -x but did not"),
201+
Err(e) => {
202+
assert_eq!(e.to_string(), "invalid option '-x'");
203+
}
204+
}
205+
206+
Ok(())
207+
}
208+
}

0 commit comments

Comments
 (0)
Please sign in to comment.