Matthew Maurer | 32e7669 | 2020-06-02 11:15:15 -0700 | [diff] [blame] | 1 | use structopt::StructOpt; |
| 2 | |
| 3 | #[test] |
| 4 | fn test_single_word_enum_variant_is_default_renamed_into_kebab_case() { |
| 5 | #[derive(StructOpt, Debug, PartialEq)] |
| 6 | enum Opt { |
| 7 | Command { foo: u32 }, |
| 8 | } |
| 9 | |
| 10 | assert_eq!( |
| 11 | Opt::Command { foo: 0 }, |
| 12 | Opt::from_clap(&Opt::clap().get_matches_from(&["test", "command", "0"])) |
| 13 | ); |
| 14 | } |
| 15 | |
| 16 | #[test] |
| 17 | fn test_multi_word_enum_variant_is_renamed() { |
| 18 | #[derive(StructOpt, Debug, PartialEq)] |
| 19 | enum Opt { |
| 20 | FirstCommand { foo: u32 }, |
| 21 | } |
| 22 | |
| 23 | assert_eq!( |
| 24 | Opt::FirstCommand { foo: 0 }, |
| 25 | Opt::from_clap(&Opt::clap().get_matches_from(&["test", "first-command", "0"])) |
| 26 | ); |
| 27 | } |
| 28 | |
| 29 | #[test] |
| 30 | fn test_standalone_long_generates_kebab_case() { |
| 31 | #[derive(StructOpt, Debug, PartialEq)] |
| 32 | #[allow(non_snake_case)] |
| 33 | struct Opt { |
| 34 | #[structopt(long)] |
| 35 | FOO_OPTION: bool, |
| 36 | } |
| 37 | |
| 38 | assert_eq!( |
| 39 | Opt { FOO_OPTION: true }, |
| 40 | Opt::from_clap(&Opt::clap().get_matches_from(&["test", "--foo-option"])) |
| 41 | ); |
| 42 | } |
| 43 | |
| 44 | #[test] |
| 45 | fn test_custom_long_overwrites_default_name() { |
| 46 | #[derive(StructOpt, Debug, PartialEq)] |
| 47 | struct Opt { |
| 48 | #[structopt(long = "foo")] |
| 49 | foo_option: bool, |
| 50 | } |
| 51 | |
| 52 | assert_eq!( |
| 53 | Opt { foo_option: true }, |
| 54 | Opt::from_clap(&Opt::clap().get_matches_from(&["test", "--foo"])) |
| 55 | ); |
| 56 | } |
| 57 | |
| 58 | #[test] |
| 59 | fn test_standalone_long_uses_previous_defined_custom_name() { |
| 60 | #[derive(StructOpt, Debug, PartialEq)] |
| 61 | struct Opt { |
| 62 | #[structopt(name = "foo", long)] |
| 63 | foo_option: bool, |
| 64 | } |
| 65 | |
| 66 | assert_eq!( |
| 67 | Opt { foo_option: true }, |
| 68 | Opt::from_clap(&Opt::clap().get_matches_from(&["test", "--foo"])) |
| 69 | ); |
| 70 | } |
| 71 | |
| 72 | #[test] |
| 73 | fn test_standalone_long_ignores_afterwards_defined_custom_name() { |
| 74 | #[derive(StructOpt, Debug, PartialEq)] |
| 75 | struct Opt { |
| 76 | #[structopt(long, name = "foo")] |
| 77 | foo_option: bool, |
| 78 | } |
| 79 | |
| 80 | assert_eq!( |
| 81 | Opt { foo_option: true }, |
| 82 | Opt::from_clap(&Opt::clap().get_matches_from(&["test", "--foo-option"])) |
| 83 | ); |
| 84 | } |
| 85 | |
| 86 | #[test] |
| 87 | fn test_standalone_short_generates_kebab_case() { |
| 88 | #[derive(StructOpt, Debug, PartialEq)] |
| 89 | #[allow(non_snake_case)] |
| 90 | struct Opt { |
| 91 | #[structopt(short)] |
| 92 | FOO_OPTION: bool, |
| 93 | } |
| 94 | |
| 95 | assert_eq!( |
| 96 | Opt { FOO_OPTION: true }, |
| 97 | Opt::from_clap(&Opt::clap().get_matches_from(&["test", "-f"])) |
| 98 | ); |
| 99 | } |
| 100 | |
| 101 | #[test] |
| 102 | fn test_custom_short_overwrites_default_name() { |
| 103 | #[derive(StructOpt, Debug, PartialEq)] |
| 104 | struct Opt { |
| 105 | #[structopt(short = "o")] |
| 106 | foo_option: bool, |
| 107 | } |
| 108 | |
| 109 | assert_eq!( |
| 110 | Opt { foo_option: true }, |
| 111 | Opt::from_clap(&Opt::clap().get_matches_from(&["test", "-o"])) |
| 112 | ); |
| 113 | } |
| 114 | |
| 115 | #[test] |
| 116 | fn test_standalone_short_uses_previous_defined_custom_name() { |
| 117 | #[derive(StructOpt, Debug, PartialEq)] |
| 118 | struct Opt { |
| 119 | #[structopt(name = "option", short)] |
| 120 | foo_option: bool, |
| 121 | } |
| 122 | |
| 123 | assert_eq!( |
| 124 | Opt { foo_option: true }, |
| 125 | Opt::from_clap(&Opt::clap().get_matches_from(&["test", "-o"])) |
| 126 | ); |
| 127 | } |
| 128 | |
| 129 | #[test] |
| 130 | fn test_standalone_short_ignores_afterwards_defined_custom_name() { |
| 131 | #[derive(StructOpt, Debug, PartialEq)] |
| 132 | struct Opt { |
| 133 | #[structopt(short, name = "option")] |
| 134 | foo_option: bool, |
| 135 | } |
| 136 | |
| 137 | assert_eq!( |
| 138 | Opt { foo_option: true }, |
| 139 | Opt::from_clap(&Opt::clap().get_matches_from(&["test", "-f"])) |
| 140 | ); |
| 141 | } |
| 142 | |
| 143 | #[test] |
| 144 | fn test_standalone_long_uses_previous_defined_casing() { |
| 145 | #[derive(StructOpt, Debug, PartialEq)] |
| 146 | struct Opt { |
| 147 | #[structopt(rename_all = "screaming_snake", long)] |
| 148 | foo_option: bool, |
| 149 | } |
| 150 | |
| 151 | assert_eq!( |
| 152 | Opt { foo_option: true }, |
| 153 | Opt::from_clap(&Opt::clap().get_matches_from(&["test", "--FOO_OPTION"])) |
| 154 | ); |
| 155 | } |
| 156 | |
| 157 | #[test] |
| 158 | fn test_standalone_short_uses_previous_defined_casing() { |
| 159 | #[derive(StructOpt, Debug, PartialEq)] |
| 160 | struct Opt { |
| 161 | #[structopt(rename_all = "screaming_snake", short)] |
| 162 | foo_option: bool, |
| 163 | } |
| 164 | |
| 165 | assert_eq!( |
| 166 | Opt { foo_option: true }, |
| 167 | Opt::from_clap(&Opt::clap().get_matches_from(&["test", "-F"])) |
| 168 | ); |
| 169 | } |
| 170 | |
| 171 | #[test] |
| 172 | fn test_standalone_long_works_with_verbatim_casing() { |
| 173 | #[derive(StructOpt, Debug, PartialEq)] |
| 174 | #[allow(non_snake_case)] |
| 175 | struct Opt { |
| 176 | #[structopt(rename_all = "verbatim", long)] |
| 177 | _fOO_oPtiON: bool, |
| 178 | } |
| 179 | |
| 180 | assert_eq!( |
| 181 | Opt { _fOO_oPtiON: true }, |
| 182 | Opt::from_clap(&Opt::clap().get_matches_from(&["test", "--_fOO_oPtiON"])) |
| 183 | ); |
| 184 | } |
| 185 | |
| 186 | #[test] |
| 187 | fn test_standalone_short_works_with_verbatim_casing() { |
| 188 | #[derive(StructOpt, Debug, PartialEq)] |
| 189 | struct Opt { |
| 190 | #[structopt(rename_all = "verbatim", short)] |
| 191 | _foo: bool, |
| 192 | } |
| 193 | |
| 194 | assert_eq!( |
| 195 | Opt { _foo: true }, |
| 196 | Opt::from_clap(&Opt::clap().get_matches_from(&["test", "-_"])) |
| 197 | ); |
| 198 | } |
| 199 | |
| 200 | #[test] |
| 201 | fn test_rename_all_is_propagated_from_struct_to_fields() { |
| 202 | #[derive(StructOpt, Debug, PartialEq)] |
| 203 | #[structopt(rename_all = "screaming_snake")] |
| 204 | struct Opt { |
| 205 | #[structopt(long)] |
| 206 | foo: bool, |
| 207 | } |
| 208 | |
| 209 | assert_eq!( |
| 210 | Opt { foo: true }, |
| 211 | Opt::from_clap(&Opt::clap().get_matches_from(&["test", "--FOO"])) |
| 212 | ); |
| 213 | } |
| 214 | |
| 215 | #[test] |
| 216 | fn test_rename_all_is_not_propagated_from_struct_into_flattened() { |
| 217 | #[derive(StructOpt, Debug, PartialEq)] |
| 218 | #[structopt(rename_all = "screaming_snake")] |
| 219 | struct Opt { |
| 220 | #[structopt(flatten)] |
| 221 | foo: Foo, |
| 222 | } |
| 223 | |
| 224 | #[derive(StructOpt, Debug, PartialEq)] |
| 225 | struct Foo { |
| 226 | #[structopt(long)] |
| 227 | foo: bool, |
| 228 | } |
| 229 | |
| 230 | assert_eq!( |
| 231 | Opt { |
| 232 | foo: Foo { foo: true } |
| 233 | }, |
| 234 | Opt::from_clap(&Opt::clap().get_matches_from(&["test", "--foo"])) |
| 235 | ); |
| 236 | } |
| 237 | |
| 238 | #[test] |
| 239 | fn test_rename_all_is_not_propagated_from_struct_into_subcommand() { |
| 240 | #[derive(StructOpt, Debug, PartialEq)] |
| 241 | #[structopt(rename_all = "screaming_snake")] |
| 242 | struct Opt { |
| 243 | #[structopt(subcommand)] |
| 244 | foo: Foo, |
| 245 | } |
| 246 | |
| 247 | #[derive(StructOpt, Debug, PartialEq)] |
| 248 | enum Foo { |
| 249 | Command { |
| 250 | #[structopt(long)] |
| 251 | foo: bool, |
| 252 | }, |
| 253 | } |
| 254 | |
| 255 | assert_eq!( |
| 256 | Opt { |
| 257 | foo: Foo::Command { foo: true } |
| 258 | }, |
| 259 | Opt::from_clap(&Opt::clap().get_matches_from(&["test", "command", "--foo"])) |
| 260 | ); |
| 261 | } |
| 262 | |
| 263 | #[test] |
| 264 | fn test_rename_all_is_propagated_from_enum_to_variants_and_their_fields() { |
| 265 | #[derive(StructOpt, Debug, PartialEq)] |
| 266 | #[structopt(rename_all = "screaming_snake")] |
| 267 | enum Opt { |
| 268 | FirstVariant, |
| 269 | SecondVariant { |
| 270 | #[structopt(long)] |
| 271 | foo: bool, |
| 272 | }, |
| 273 | } |
| 274 | |
| 275 | assert_eq!( |
| 276 | Opt::FirstVariant, |
| 277 | Opt::from_clap(&Opt::clap().get_matches_from(&["test", "FIRST_VARIANT"])) |
| 278 | ); |
| 279 | |
| 280 | assert_eq!( |
| 281 | Opt::SecondVariant { foo: true }, |
| 282 | Opt::from_clap(&Opt::clap().get_matches_from(&["test", "SECOND_VARIANT", "--FOO"])) |
| 283 | ); |
| 284 | } |
| 285 | |
| 286 | #[test] |
| 287 | fn test_rename_all_is_propagation_can_be_overridden() { |
| 288 | #[derive(StructOpt, Debug, PartialEq)] |
| 289 | #[structopt(rename_all = "screaming_snake")] |
| 290 | enum Opt { |
| 291 | #[structopt(rename_all = "kebab_case")] |
| 292 | FirstVariant { |
| 293 | #[structopt(long)] |
| 294 | foo_option: bool, |
| 295 | }, |
| 296 | SecondVariant { |
| 297 | #[structopt(rename_all = "kebab_case", long)] |
| 298 | foo_option: bool, |
| 299 | }, |
| 300 | } |
| 301 | |
| 302 | assert_eq!( |
| 303 | Opt::FirstVariant { foo_option: true }, |
| 304 | Opt::from_clap(&Opt::clap().get_matches_from(&["test", "first-variant", "--foo-option"])) |
| 305 | ); |
| 306 | |
| 307 | assert_eq!( |
| 308 | Opt::SecondVariant { foo_option: true }, |
| 309 | Opt::from_clap(&Opt::clap().get_matches_from(&["test", "SECOND_VARIANT", "--foo-option"])) |
| 310 | ); |
| 311 | } |