mirror of
https://github.com/TECHNOFAB11/pay-respects.git
synced 2025-12-11 22:10:09 +01:00
test: make handle_args() testable & add test_handle_args() (codeberg #5)
This commit is contained in:
parent
7cdfa3a239
commit
1227fc88ee
2 changed files with 63 additions and 9 deletions
|
|
@ -7,23 +7,23 @@ pub enum Status {
|
|||
Error,
|
||||
}
|
||||
|
||||
pub fn handle_args() -> Status {
|
||||
use Status::*;
|
||||
let args = std::env::args().collect::<Vec<String>>();
|
||||
pub fn handle_args(args: impl IntoIterator<Item = String>) -> Status {
|
||||
let args: Vec<_> = args.into_iter().collect();
|
||||
if args.len() <= 1 {
|
||||
return Continue;
|
||||
return Status::Continue;
|
||||
}
|
||||
|
||||
let mut init = Init::new();
|
||||
let mut index = 1;
|
||||
while index < args.len() {
|
||||
match args[index].as_str() {
|
||||
"-h" | "--help" => {
|
||||
print_help();
|
||||
return Exit;
|
||||
return Status::Exit;
|
||||
}
|
||||
"-v" | "--version" => {
|
||||
print_version();
|
||||
return Exit;
|
||||
return Status::Exit;
|
||||
}
|
||||
"-a" | "--alias" => {
|
||||
if args.len() > index + 1 {
|
||||
|
|
@ -52,13 +52,13 @@ pub fn handle_args() -> Status {
|
|||
|
||||
if init.shell.is_empty() {
|
||||
eprintln!("{}", t!("no-shell"));
|
||||
return Error;
|
||||
return Status::Error;
|
||||
}
|
||||
|
||||
init.binary_path = args[0].clone();
|
||||
|
||||
initialization(&mut init);
|
||||
Exit
|
||||
Status::Exit
|
||||
}
|
||||
|
||||
fn print_help() {
|
||||
|
|
@ -92,3 +92,56 @@ fn print_version() {
|
|||
println!("Default lib directory: {}", lib.unwrap());
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::{handle_args, Status};
|
||||
|
||||
#[test]
|
||||
fn test_handle_args() {
|
||||
assert!(matches!(
|
||||
handle_args([String::from("pay-respects")]),
|
||||
Status::Continue
|
||||
));
|
||||
|
||||
for args in [
|
||||
[String::new(), String::from("-h")],
|
||||
[String::new(), String::from("--help")],
|
||||
[String::new(), String::from("-v")],
|
||||
[String::new(), String::from("--version")],
|
||||
[String::new(), String::from("zsh")],
|
||||
] {
|
||||
println!("Arguments {:?} should return Exit", args);
|
||||
assert!(matches!(handle_args(args), Status::Exit));
|
||||
}
|
||||
|
||||
for args in [
|
||||
[String::new(), String::from("fish"), String::from("--alias")],
|
||||
[String::new(), String::from("bash"), String::from("--nocnf")],
|
||||
] {
|
||||
println!("Arguments {:?} should return Exit", args);
|
||||
assert!(matches!(handle_args(args), Status::Exit));
|
||||
}
|
||||
|
||||
for args in [
|
||||
[String::new(), String::from("-a")],
|
||||
[String::new(), String::from("--alias")],
|
||||
[String::new(), String::from("--nocnf")],
|
||||
] {
|
||||
println!("Arguments {:?} should return Error", args);
|
||||
assert!(matches!(handle_args(args), Status::Error));
|
||||
}
|
||||
|
||||
for args in [
|
||||
[String::new(), String::from("-a"), String::from("--nocnf")],
|
||||
[
|
||||
String::new(),
|
||||
String::from("--alias"),
|
||||
String::from("--nocnf"),
|
||||
],
|
||||
] {
|
||||
println!("Argument {:?} should return Error", args);
|
||||
assert!(matches!(handle_args(args), Status::Error));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -14,6 +14,7 @@
|
|||
// You should have received a copy of the GNU Affero General Public License
|
||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
use std::env;
|
||||
use sys_locale::get_locale;
|
||||
|
||||
mod args;
|
||||
|
|
@ -71,7 +72,7 @@ fn init() -> Result<shell::Data, args::Status> {
|
|||
};
|
||||
rust_i18n::set_locale(&locale[0..2]);
|
||||
|
||||
let status = args::handle_args();
|
||||
let status = args::handle_args(env::args());
|
||||
match status {
|
||||
args::Status::Exit => {
|
||||
return Err(status);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue