Matthew Maurer | 32e7669 | 2020-06-02 11:15:15 -0700 | [diff] [blame] | 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::StructOpt; |
| 10 | |
| 11 | #[test] |
| 12 | fn skip_1() { |
| 13 | #[derive(StructOpt, Debug, PartialEq)] |
| 14 | struct Opt { |
| 15 | #[structopt(short)] |
| 16 | x: u32, |
| 17 | #[structopt(skip)] |
| 18 | s: u32, |
| 19 | } |
| 20 | |
| 21 | assert!(Opt::from_iter_safe(&["test", "-x", "10", "20"]).is_err()); |
| 22 | assert_eq!( |
| 23 | Opt::from_iter(&["test", "-x", "10"]), |
| 24 | Opt { |
| 25 | x: 10, |
| 26 | s: 0, // default |
| 27 | } |
| 28 | ); |
| 29 | } |
| 30 | |
| 31 | #[test] |
| 32 | fn skip_2() { |
| 33 | #[derive(StructOpt, Debug, PartialEq)] |
| 34 | struct Opt { |
| 35 | #[structopt(short)] |
| 36 | x: u32, |
| 37 | #[structopt(skip)] |
| 38 | ss: String, |
| 39 | #[structopt(skip)] |
| 40 | sn: u8, |
| 41 | y: u32, |
| 42 | #[structopt(skip)] |
| 43 | sz: u16, |
| 44 | t: u32, |
| 45 | } |
| 46 | |
| 47 | assert_eq!( |
| 48 | Opt::from_iter(&["test", "-x", "10", "20", "30"]), |
| 49 | Opt { |
| 50 | x: 10, |
| 51 | ss: String::from(""), |
| 52 | sn: 0, |
| 53 | y: 20, |
| 54 | sz: 0, |
| 55 | t: 30, |
| 56 | } |
| 57 | ); |
| 58 | } |
| 59 | |
| 60 | #[test] |
| 61 | fn skip_enum() { |
| 62 | #[derive(Debug, PartialEq)] |
| 63 | #[allow(unused)] |
| 64 | enum Kind { |
| 65 | A, |
| 66 | B, |
| 67 | } |
| 68 | |
| 69 | impl Default for Kind { |
| 70 | fn default() -> Self { |
| 71 | return Kind::B; |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | #[derive(StructOpt, Debug, PartialEq)] |
| 76 | pub struct Opt { |
| 77 | #[structopt(long, short)] |
| 78 | number: u32, |
| 79 | #[structopt(skip)] |
| 80 | k: Kind, |
| 81 | #[structopt(skip)] |
| 82 | v: Vec<u32>, |
| 83 | } |
| 84 | |
| 85 | assert_eq!( |
| 86 | Opt::from_iter(&["test", "-n", "10"]), |
| 87 | Opt { |
| 88 | number: 10, |
| 89 | k: Kind::B, |
| 90 | v: vec![], |
| 91 | } |
| 92 | ); |
| 93 | } |
| 94 | |
| 95 | #[test] |
| 96 | fn skip_help_doc_comments() { |
| 97 | #[derive(StructOpt, Debug, PartialEq)] |
| 98 | pub struct Opt { |
| 99 | #[structopt(skip, help = "internal_stuff")] |
| 100 | a: u32, |
| 101 | |
| 102 | #[structopt(skip, long_help = "internal_stuff\ndo not touch")] |
| 103 | b: u32, |
| 104 | |
| 105 | /// Not meant to be used by clap. |
| 106 | /// |
| 107 | /// I want a default here. |
| 108 | #[structopt(skip)] |
| 109 | c: u32, |
| 110 | |
| 111 | #[structopt(short, parse(try_from_str))] |
| 112 | n: u32, |
| 113 | } |
| 114 | |
| 115 | assert_eq!( |
| 116 | Opt::from_iter(&["test", "-n", "10"]), |
| 117 | Opt { |
| 118 | n: 10, |
| 119 | a: 0, |
| 120 | b: 0, |
| 121 | c: 0, |
| 122 | } |
| 123 | ); |
| 124 | } |
| 125 | |
| 126 | #[test] |
| 127 | fn skip_val() { |
| 128 | #[derive(StructOpt, Debug, PartialEq)] |
| 129 | pub struct Opt { |
| 130 | #[structopt(long, short)] |
| 131 | number: u32, |
| 132 | |
| 133 | #[structopt(skip = "key")] |
| 134 | k: String, |
| 135 | |
| 136 | #[structopt(skip = vec![1, 2, 3])] |
| 137 | v: Vec<u32>, |
| 138 | } |
| 139 | |
| 140 | assert_eq!( |
| 141 | Opt::from_iter(&["test", "-n", "10"]), |
| 142 | Opt { |
| 143 | number: 10, |
| 144 | k: "key".into(), |
| 145 | v: vec![1, 2, 3] |
| 146 | } |
| 147 | ); |
| 148 | } |