1 // Copyright 2018 Guillaume Pinot (@TeXitoi) <texitoi@texitoi.eu>
2 //
3 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
5 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
6 // option. This file may not be copied, modified, or distributed
7 // except according to those terms.
8
9 use structopt::clap;
10 use structopt::StructOpt;
11
12 #[test]
required_argument()13 fn required_argument() {
14 #[derive(StructOpt, PartialEq, Debug)]
15 struct Opt {
16 arg: i32,
17 }
18 assert_eq!(Opt { arg: 42 }, Opt::from_iter(&["test", "42"]));
19 assert!(Opt::clap().get_matches_from_safe(&["test"]).is_err());
20 assert!(Opt::clap()
21 .get_matches_from_safe(&["test", "42", "24"])
22 .is_err());
23 }
24
25 #[test]
optional_argument()26 fn optional_argument() {
27 #[derive(StructOpt, PartialEq, Debug)]
28 struct Opt {
29 arg: Option<i32>,
30 }
31 assert_eq!(Opt { arg: Some(42) }, Opt::from_iter(&["test", "42"]));
32 assert_eq!(Opt { arg: None }, Opt::from_iter(&["test"]));
33 assert!(Opt::clap()
34 .get_matches_from_safe(&["test", "42", "24"])
35 .is_err());
36 }
37
38 #[test]
argument_with_default()39 fn argument_with_default() {
40 #[derive(StructOpt, PartialEq, Debug)]
41 struct Opt {
42 #[structopt(default_value = "42")]
43 arg: i32,
44 }
45 assert_eq!(Opt { arg: 24 }, Opt::from_iter(&["test", "24"]));
46 assert_eq!(Opt { arg: 42 }, Opt::from_iter(&["test"]));
47 assert!(Opt::clap()
48 .get_matches_from_safe(&["test", "42", "24"])
49 .is_err());
50 }
51
52 #[test]
arguments()53 fn arguments() {
54 #[derive(StructOpt, PartialEq, Debug)]
55 struct Opt {
56 arg: Vec<i32>,
57 }
58 assert_eq!(Opt { arg: vec![24] }, Opt::from_iter(&["test", "24"]));
59 assert_eq!(Opt { arg: vec![] }, Opt::from_iter(&["test"]));
60 assert_eq!(
61 Opt { arg: vec![24, 42] },
62 Opt::from_iter(&["test", "24", "42"])
63 );
64 }
65
66 #[test]
arguments_safe()67 fn arguments_safe() {
68 #[derive(StructOpt, PartialEq, Debug)]
69 struct Opt {
70 arg: Vec<i32>,
71 }
72 assert_eq!(
73 Opt { arg: vec![24] },
74 Opt::from_iter_safe(&["test", "24"]).unwrap()
75 );
76 assert_eq!(Opt { arg: vec![] }, Opt::from_iter_safe(&["test"]).unwrap());
77 assert_eq!(
78 Opt { arg: vec![24, 42] },
79 Opt::from_iter_safe(&["test", "24", "42"]).unwrap()
80 );
81
82 assert_eq!(
83 clap::ErrorKind::ValueValidation,
84 Opt::from_iter_safe(&["test", "NOPE"]).err().unwrap().kind
85 );
86 }
87