• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 use clap::{arg, value_parser, Command};
2 #[cfg(debug_assertions)]
3 use clap::{Arg, ArgAction};
4 
5 #[test]
ids()6 fn ids() {
7     let m = Command::new("test")
8         .arg(arg!(--color <when>).value_parser(["auto", "always", "never"]))
9         .arg(arg!(--config <path>).value_parser(value_parser!(std::path::PathBuf)))
10         .try_get_matches_from(["test", "--config=config.toml", "--color=auto"])
11         .unwrap();
12     assert_eq!(
13         m.ids().map(|id| id.as_str()).collect::<Vec<_>>(),
14         ["config", "color"]
15     );
16     assert_eq!(m.ids().len(), 2);
17 }
18 
19 #[test]
ids_ignore_unused()20 fn ids_ignore_unused() {
21     let m = Command::new("test")
22         .arg(arg!(--color <when>).value_parser(["auto", "always", "never"]))
23         .arg(arg!(--config <path>).value_parser(value_parser!(std::path::PathBuf)))
24         .try_get_matches_from(["test", "--config=config.toml"])
25         .unwrap();
26     assert_eq!(
27         m.ids().map(|id| id.as_str()).collect::<Vec<_>>(),
28         ["config"]
29     );
30     assert_eq!(m.ids().len(), 1);
31 }
32 
33 #[test]
ids_ignore_overridden()34 fn ids_ignore_overridden() {
35     let m = Command::new("test")
36         .arg(arg!(--color <when>).value_parser(["auto", "always", "never"]))
37         .arg(
38             arg!(--config <path>)
39                 .value_parser(value_parser!(std::path::PathBuf))
40                 .overrides_with("color"),
41         )
42         .try_get_matches_from(["test", "--config=config.toml", "--color=auto"])
43         .unwrap();
44     assert_eq!(m.ids().map(|id| id.as_str()).collect::<Vec<_>>(), ["color"]);
45     assert_eq!(m.ids().len(), 1);
46 }
47 
48 #[test]
49 #[cfg(debug_assertions)]
50 #[should_panic = "Unknown argument or group id.  Make sure you are using the argument id and not the short or long flags"]
arg_matches_if_present_wrong_arg()51 fn arg_matches_if_present_wrong_arg() {
52     let m = Command::new("test")
53         .arg(Arg::new("flag").short('f').action(ArgAction::SetTrue))
54         .try_get_matches_from(["test", "-f"])
55         .unwrap();
56 
57     assert!(*m.get_one::<bool>("flag").expect("defaulted by clap"));
58     m.contains_id("f");
59 }
60 
61 #[test]
62 #[cfg(debug_assertions)]
63 #[should_panic = "Mismatch between definition and access of `o`. Unknown argument or group id.  Make sure you are using the argument id and not the short or long flags"]
arg_matches_value_of_wrong_arg()64 fn arg_matches_value_of_wrong_arg() {
65     let m = Command::new("test")
66         .arg(Arg::new("opt").short('o').action(ArgAction::Set))
67         .try_get_matches_from(["test", "-o", "val"])
68         .unwrap();
69 
70     assert_eq!(m.get_one::<String>("opt").map(|v| v.as_str()), Some("val"));
71     m.get_one::<String>("o").map(|v| v.as_str());
72 }
73 
74 #[test]
75 #[cfg(debug_assertions)]
76 #[should_panic = "`seed` is not a name of a subcommand."]
arg_matches_subcommand_matches_wrong_sub()77 fn arg_matches_subcommand_matches_wrong_sub() {
78     let m = Command::new("test")
79         .subcommand(Command::new("speed"))
80         .try_get_matches_from(["test", "speed"])
81         .unwrap();
82 
83     assert!(m.subcommand_matches("speed").is_some());
84     m.subcommand_matches("seed");
85 }
86