Skip to content

Commit

Permalink
tests: FlagSubCommand
Browse files Browse the repository at this point in the history
Test all methods of `FlagSubCommand` and interactions with aliases
  • Loading branch information
NickHackman committed Jun 13, 2020
1 parent 8145717 commit c89718f
Showing 1 changed file with 151 additions and 0 deletions.
151 changes: 151 additions & 0 deletions tests/flag_subcommands.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
use clap::{App, Arg, FlagSubCommand};

#[test]
fn flag_subcommand_normal() {
let matches = App::new("test")
.subcommand(
FlagSubCommand::new('S', "some").arg(
Arg::new("test")
.short('t')
.long("test")
.about("testing testing"),
),
)
.get_matches_from(vec!["myprog", "some", "--test"]);
assert_eq!(matches.subcommand_name().unwrap(), "some");
let sub_matches = matches.subcommand_matches("some").unwrap();
assert!(sub_matches.is_present("test"));
}

#[test]
fn flag_subcommand_normal_with_alias() {
let matches = App::new("test")
.subcommand(
FlagSubCommand::new('S', "some")
.arg(
Arg::new("test")
.short('t')
.long("test")
.about("testing testing"),
)
.alias("result"),
)
.get_matches_from(vec!["myprog", "result", "--test"]);
assert_eq!(matches.subcommand_name().unwrap(), "some");
let sub_matches = matches.subcommand_matches("some").unwrap();
assert!(sub_matches.is_present("test"));
}

#[test]
fn flag_subcommand_short() {
let matches = App::new("test")
.subcommand(
FlagSubCommand::new_short("some", 'S').arg(
Arg::new("test")
.short('t')
.long("test")
.about("testing testing"),
),
)
.get_matches_from(vec!["myprog", "-S", "--test"]);
assert_eq!(matches.subcommand_name().unwrap(), "some");
let sub_matches = matches.subcommand_matches("some").unwrap();
assert!(sub_matches.is_present("test"));
}

#[test]
fn flag_subcommand_short_with_args() {
let matches = App::new("test")
.subcommand(
FlagSubCommand::new_short("some", 'S').arg(
Arg::new("test")
.short('t')
.long("test")
.about("testing testing"),
),
)
.get_matches_from(vec!["myprog", "-St"]);
assert_eq!(matches.subcommand_name().unwrap(), "some");
let sub_matches = matches.subcommand_matches("some").unwrap();
assert!(sub_matches.is_present("test"));
}

#[test]
fn flag_subcommand_short_with_alias() {
let matches = App::new("test")
.subcommand(
FlagSubCommand::new_short("some", 'S')
.arg(
Arg::new("test")
.short('t')
.long("test")
.about("testing testing"),
)
.alias("M")
.alias("B"),
)
.get_matches_from(vec!["myprog", "-Bt"]);
assert_eq!(matches.subcommand_name().unwrap(), "some");
let sub_matches = matches.subcommand_matches("some").unwrap();
assert!(sub_matches.is_present("test"));
}

#[test]
fn flag_subcommand_long() {
let matches = App::new("test")
.subcommand(
FlagSubCommand::new_long("some", "some").arg(
Arg::new("test")
.short('t')
.long("test")
.about("testing testing"),
),
)
.get_matches_from(vec!["myprog", "--some", "--test"]);
assert_eq!(matches.subcommand_name().unwrap(), "some");
let sub_matches = matches.subcommand_matches("some").unwrap();
assert!(sub_matches.is_present("test"));
}

#[test]
fn flag_subcommand_long_with_alias() {
let matches = App::new("test")
.subcommand(
FlagSubCommand::new_long("some", "some")
.arg(
Arg::new("test")
.short('t')
.long("test")
.about("testing testing"),
)
.alias("result"),
)
.get_matches_from(vec!["myprog", "--result", "--test"]);
assert_eq!(matches.subcommand_name().unwrap(), "some");
let sub_matches = matches.subcommand_matches("some").unwrap();
assert!(sub_matches.is_present("test"));
}

#[test]
fn flag_subcommand_multiple() {
let matches = App::new("test")
.subcommand(
FlagSubCommand::new('S', "some")
.arg(Arg::from("-f, --flag 'some flag'"))
.arg(Arg::from("-p, --print 'print something'"))
.subcommand(
FlagSubCommand::new('R', "result")
.arg(Arg::from("-f, --flag 'some flag'"))
.arg(Arg::from("-p, --print 'print something'")),
),
)
.get_matches_from(vec!["myprog", "-SfpRfp"]);
assert_eq!(matches.subcommand_name().unwrap(), "some");
let sub_matches = matches.subcommand_matches("some").unwrap();
assert!(sub_matches.is_present("flag"));
assert!(sub_matches.is_present("print"));
assert_eq!(sub_matches.subcommand_name().unwrap(), "result");
let result_matches = sub_matches.subcommand_matches("result").unwrap();
assert!(result_matches.is_present("flag"));
assert!(result_matches.is_present("print"));
}

0 comments on commit c89718f

Please sign in to comment.