1 use std::path::PathBuf;
2
3 use clap::{CommandFactory, Parser};
4
5 use crate::utils;
6
7 #[test]
default_value()8 fn default_value() {
9 #[derive(Parser, PartialEq, Debug)]
10 struct Opt {
11 #[arg(default_value = "3")]
12 arg: i32,
13 }
14 assert_eq!(Opt { arg: 3 }, Opt::try_parse_from(["test"]).unwrap());
15 assert_eq!(Opt { arg: 1 }, Opt::try_parse_from(["test", "1"]).unwrap());
16
17 let help = utils::get_long_help::<Opt>();
18 assert!(help.contains("[default: 3]"));
19 }
20
21 #[test]
default_value_t()22 fn default_value_t() {
23 #[derive(Parser, PartialEq, Debug)]
24 struct Opt {
25 #[arg(default_value_t = 3)]
26 arg: i32,
27 }
28 assert_eq!(Opt { arg: 3 }, Opt::try_parse_from(["test"]).unwrap());
29 assert_eq!(Opt { arg: 1 }, Opt::try_parse_from(["test", "1"]).unwrap());
30
31 let help = utils::get_long_help::<Opt>();
32 assert!(help.contains("[default: 3]"));
33 }
34
35 #[test]
auto_default_value_t()36 fn auto_default_value_t() {
37 #[derive(Parser, PartialEq, Debug)]
38 struct Opt {
39 #[arg(default_value_t)]
40 arg: i32,
41 }
42 assert_eq!(Opt { arg: 0 }, Opt::try_parse_from(["test"]).unwrap());
43 assert_eq!(Opt { arg: 1 }, Opt::try_parse_from(["test", "1"]).unwrap());
44
45 let help = utils::get_long_help::<Opt>();
46 assert!(help.contains("[default: 0]"));
47 }
48
49 #[test]
default_values_t()50 fn default_values_t() {
51 #[derive(Parser, PartialEq, Debug)]
52 struct Opt {
53 #[arg(default_values_t = vec![1, 2, 3])]
54 arg1: Vec<i32>,
55
56 #[arg(long, default_values_t = [4, 5, 6])]
57 arg2: Vec<i32>,
58
59 #[arg(long, default_values_t = [7, 8, 9])]
60 arg3: Vec<i32>,
61
62 #[arg(long, default_values_t = 10..=12)]
63 arg4: Vec<i32>,
64
65 #[arg(long, default_values_t = vec!["hello".to_string(), "world".to_string()])]
66 arg5: Vec<String>,
67
68 #[arg(long, default_values_t = &vec!["foo".to_string(), "bar".to_string()])]
69 arg6: Vec<String>,
70 }
71 assert_eq!(
72 Opt {
73 arg1: vec![1, 2, 3],
74 arg2: vec![4, 5, 6],
75 arg3: vec![7, 8, 9],
76 arg4: vec![10, 11, 12],
77 arg5: vec!["hello".to_string(), "world".to_string()],
78 arg6: vec!["foo".to_string(), "bar".to_string()],
79 },
80 Opt::try_parse_from(["test"]).unwrap()
81 );
82 assert_eq!(
83 Opt {
84 arg1: vec![1],
85 arg2: vec![4, 5, 6],
86 arg3: vec![7, 8, 9],
87 arg4: vec![10, 11, 12],
88 arg5: vec!["hello".to_string(), "world".to_string()],
89 arg6: vec!["foo".to_string(), "bar".to_string()],
90 },
91 Opt::try_parse_from(["test", "1"]).unwrap()
92 );
93 assert_eq!(
94 Opt {
95 arg1: vec![1, 2, 3],
96 arg2: vec![4, 5, 6],
97 arg3: vec![7, 8, 9],
98 arg4: vec![42, 15],
99 arg5: vec!["baz".to_string()],
100 arg6: vec!["foo".to_string(), "bar".to_string()],
101 },
102 Opt::try_parse_from(["test", "--arg4", "42", "--arg4", "15", "--arg5", "baz"]).unwrap()
103 );
104
105 let help = utils::get_long_help::<Opt>();
106 assert!(help.contains("[default: 1 2 3]"));
107 }
108
109 #[test]
default_value_os_t()110 fn default_value_os_t() {
111 #[derive(Parser, PartialEq, Debug)]
112 struct Opt {
113 #[arg(default_value_os_t = PathBuf::from("abc.def"))]
114 arg: PathBuf,
115 }
116 assert_eq!(
117 Opt {
118 arg: PathBuf::from("abc.def")
119 },
120 Opt::try_parse_from(["test"]).unwrap()
121 );
122 assert_eq!(
123 Opt {
124 arg: PathBuf::from("ghi")
125 },
126 Opt::try_parse_from(["test", "ghi"]).unwrap()
127 );
128
129 let help = utils::get_long_help::<Opt>();
130 assert!(help.contains("[default: abc.def]"));
131 }
132
133 #[test]
default_values_os_t()134 fn default_values_os_t() {
135 #[derive(Parser, PartialEq, Debug)]
136 struct Opt {
137 #[arg(
138 default_values_os_t = vec![PathBuf::from("abc.def"), PathBuf::from("123.foo")]
139 )]
140 arg1: Vec<PathBuf>,
141
142 #[arg(
143 long,
144 default_values_os_t = [PathBuf::from("bar.baz")]
145 )]
146 arg2: Vec<PathBuf>,
147 }
148 assert_eq!(
149 Opt {
150 arg1: vec![PathBuf::from("abc.def"), PathBuf::from("123.foo")],
151 arg2: vec![PathBuf::from("bar.baz")]
152 },
153 Opt::try_parse_from(["test"]).unwrap()
154 );
155 assert_eq!(
156 Opt {
157 arg1: vec![PathBuf::from("ghi")],
158 arg2: vec![PathBuf::from("baz.bar"), PathBuf::from("foo.bar")]
159 },
160 Opt::try_parse_from(["test", "ghi", "--arg2", "baz.bar", "--arg2", "foo.bar"]).unwrap()
161 );
162
163 let help = utils::get_long_help::<Opt>();
164 assert!(help.contains("[default: abc.def 123.foo]"));
165 }
166
167 #[test]
detect_os_variant()168 fn detect_os_variant() {
169 #![allow(deprecated)]
170
171 #[derive(clap::Parser)]
172 pub struct Options {
173 #[arg(default_value_os = "123")]
174 x: String,
175 }
176 Options::command().debug_assert();
177 }
178