|
| 1 | +// Copyright 2018 Guillaume Pinot (@TeXitoi) <[email protected]>, |
| 2 | +// Kevin Knapp (@kbknapp) <[email protected]>, and |
| 3 | +// Ana Hobden (@hoverbear) <[email protected]> |
| 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::CommandFactory; |
| 16 | +use clap::Parser; |
| 17 | + |
| 18 | +#[test] |
| 19 | +fn required_argument() { |
| 20 | + #[derive(Parser, PartialEq, Debug)] |
| 21 | + struct Opt { |
| 22 | + #[clap(value_parser)] |
| 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] |
| 34 | +fn argument_with_default() { |
| 35 | + #[derive(Parser, PartialEq, Debug)] |
| 36 | + struct Opt { |
| 37 | + #[clap(value_parser, 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] |
| 49 | +fn auto_value_name() { |
| 50 | + #[derive(Parser, PartialEq, Debug)] |
| 51 | + struct Opt { |
| 52 | + #[clap(value_parser)] |
| 53 | + my_special_arg: i32, |
| 54 | + } |
| 55 | + |
| 56 | + let mut help = Vec::new(); |
| 57 | + Opt::command().write_help(&mut help).unwrap(); |
| 58 | + let help = String::from_utf8(help).unwrap(); |
| 59 | + |
| 60 | + assert!(help.contains("MY_SPECIAL_ARG")); |
| 61 | + // Ensure the implicit `num_vals` is just 1 |
| 62 | + assert_eq!( |
| 63 | + Opt { my_special_arg: 10 }, |
| 64 | + Opt::try_parse_from(&["test", "10"]).unwrap() |
| 65 | + ); |
| 66 | +} |
| 67 | + |
| 68 | +#[test] |
| 69 | +fn explicit_value_name() { |
| 70 | + #[derive(Parser, PartialEq, Debug)] |
| 71 | + struct Opt { |
| 72 | + #[clap(value_parser, value_name = "BROWNIE_POINTS")] |
| 73 | + my_special_arg: i32, |
| 74 | + } |
| 75 | + |
| 76 | + let mut help = Vec::new(); |
| 77 | + Opt::command().write_help(&mut help).unwrap(); |
| 78 | + let help = String::from_utf8(help).unwrap(); |
| 79 | + |
| 80 | + assert!(help.contains("BROWNIE_POINTS")); |
| 81 | + assert!(!help.contains("MY_SPECIAL_ARG")); |
| 82 | + // Ensure the implicit `num_vals` is just 1 |
| 83 | + assert_eq!( |
| 84 | + Opt { my_special_arg: 10 }, |
| 85 | + Opt::try_parse_from(&["test", "10"]).unwrap() |
| 86 | + ); |
| 87 | +} |
| 88 | + |
| 89 | +#[test] |
| 90 | +fn option_type_is_optional() { |
| 91 | + #[derive(Parser, PartialEq, Debug)] |
| 92 | + struct Opt { |
| 93 | + #[clap(value_parser)] |
| 94 | + arg: Option<i32>, |
| 95 | + } |
| 96 | + assert_eq!( |
| 97 | + Opt { arg: Some(42) }, |
| 98 | + Opt::try_parse_from(&["test", "42"]).unwrap() |
| 99 | + ); |
| 100 | + assert_eq!(Opt { arg: None }, Opt::try_parse_from(&["test"]).unwrap()); |
| 101 | + assert!(Opt::try_parse_from(&["test", "42", "24"]).is_err()); |
| 102 | +} |
| 103 | + |
| 104 | +#[test] |
| 105 | +fn vec_type_is_multiple_values() { |
| 106 | + #[derive(Parser, PartialEq, Debug)] |
| 107 | + struct Opt { |
| 108 | + #[clap(value_parser)] |
| 109 | + arg: Vec<i32>, |
| 110 | + } |
| 111 | + assert_eq!( |
| 112 | + Opt { arg: vec![24] }, |
| 113 | + Opt::try_parse_from(&["test", "24"]).unwrap() |
| 114 | + ); |
| 115 | + assert_eq!(Opt { arg: vec![] }, Opt::try_parse_from(&["test"]).unwrap()); |
| 116 | + assert_eq!( |
| 117 | + Opt { arg: vec![24, 42] }, |
| 118 | + Opt::try_parse_from(&["test", "24", "42"]).unwrap() |
| 119 | + ); |
| 120 | + assert_eq!( |
| 121 | + clap::ErrorKind::ValueValidation, |
| 122 | + Opt::try_parse_from(&["test", "NOPE"]).err().unwrap().kind() |
| 123 | + ); |
| 124 | +} |
0 commit comments