1 // Copyright 2018 Guillaume Pinot (@TeXitoi) <texitoi@texitoi.eu>,
2 // Kevin Knapp (@kbknapp) <kbknapp@gmail.com>, and
3 // Ana Hobden (@hoverbear) <operator@hoverbear.org>
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10 //
11 // This work was derived from Structopt (https://github.com/TeXitoi/structopt)
12 // commit#ea76fa1b1b273e65e3b0b1046643715b49bec51f which is licensed under the
13 // MIT/Apache 2.0 license.
14
15 use clap::Parser;
16
17 use crate::utils::get_help;
18
19 #[test]
required_argument()20 fn required_argument() {
21 #[derive(Parser, PartialEq, Debug)]
22 struct Opt {
23 arg: i32,
24 }
25 assert_eq!(
26 Opt { arg: 42 },
27 Opt::try_parse_from(["test", "42"]).unwrap()
28 );
29 assert!(Opt::try_parse_from(["test"]).is_err());
30 assert!(Opt::try_parse_from(["test", "42", "24"]).is_err());
31 }
32
33 #[test]
argument_with_default()34 fn argument_with_default() {
35 #[derive(Parser, PartialEq, Debug)]
36 struct Opt {
37 #[arg(default_value = "42")]
38 arg: i32,
39 }
40 assert_eq!(
41 Opt { arg: 24 },
42 Opt::try_parse_from(["test", "24"]).unwrap()
43 );
44 assert_eq!(Opt { arg: 42 }, Opt::try_parse_from(["test"]).unwrap());
45 assert!(Opt::try_parse_from(["test", "42", "24"]).is_err());
46 }
47
48 #[test]
auto_value_name()49 fn auto_value_name() {
50 #[derive(Parser, PartialEq, Debug)]
51 struct Opt {
52 my_special_arg: i32,
53 }
54
55 let help = get_help::<Opt>();
56
57 assert!(help.contains("MY_SPECIAL_ARG"));
58 // Ensure the implicit `num_vals` is just 1
59 assert_eq!(
60 Opt { my_special_arg: 10 },
61 Opt::try_parse_from(["test", "10"]).unwrap()
62 );
63 }
64
65 #[test]
explicit_value_name()66 fn explicit_value_name() {
67 #[derive(Parser, PartialEq, Debug)]
68 struct Opt {
69 #[arg(value_name = "BROWNIE_POINTS")]
70 my_special_arg: i32,
71 }
72
73 let help = get_help::<Opt>();
74
75 assert!(help.contains("BROWNIE_POINTS"));
76 assert!(!help.contains("MY_SPECIAL_ARG"));
77 // Ensure the implicit `num_vals` is just 1
78 assert_eq!(
79 Opt { my_special_arg: 10 },
80 Opt::try_parse_from(["test", "10"]).unwrap()
81 );
82 }
83
84 #[test]
option_type_is_optional()85 fn option_type_is_optional() {
86 #[derive(Parser, PartialEq, Debug)]
87 struct Opt {
88 arg: Option<i32>,
89 }
90 assert_eq!(
91 Opt { arg: Some(42) },
92 Opt::try_parse_from(["test", "42"]).unwrap()
93 );
94 assert_eq!(Opt { arg: None }, Opt::try_parse_from(["test"]).unwrap());
95 assert!(Opt::try_parse_from(["test", "42", "24"]).is_err());
96 }
97
98 #[test]
vec_type_is_multiple_values()99 fn vec_type_is_multiple_values() {
100 #[derive(Parser, PartialEq, Debug)]
101 struct Opt {
102 arg: Vec<i32>,
103 }
104 assert_eq!(
105 Opt { arg: vec![24] },
106 Opt::try_parse_from(["test", "24"]).unwrap()
107 );
108 assert_eq!(Opt { arg: vec![] }, Opt::try_parse_from(["test"]).unwrap());
109 assert_eq!(
110 Opt { arg: vec![24, 42] },
111 Opt::try_parse_from(["test", "24", "42"]).unwrap()
112 );
113 assert_eq!(
114 clap::error::ErrorKind::ValueValidation,
115 Opt::try_parse_from(["test", "NOPE"]).err().unwrap().kind()
116 );
117 }
118