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 flatten() { |
| 13 | #[derive(StructOpt, PartialEq, Debug)] |
| 14 | struct Common { |
| 15 | arg: i32, |
| 16 | } |
| 17 | |
| 18 | #[derive(StructOpt, PartialEq, Debug)] |
| 19 | struct Opt { |
| 20 | #[structopt(flatten)] |
| 21 | common: Common, |
| 22 | } |
| 23 | assert_eq!( |
| 24 | Opt { |
| 25 | common: Common { arg: 42 } |
| 26 | }, |
| 27 | Opt::from_iter(&["test", "42"]) |
| 28 | ); |
| 29 | assert!(Opt::clap().get_matches_from_safe(&["test"]).is_err()); |
| 30 | assert!(Opt::clap() |
| 31 | .get_matches_from_safe(&["test", "42", "24"]) |
| 32 | .is_err()); |
| 33 | } |
| 34 | |
| 35 | #[test] |
| 36 | #[should_panic] |
| 37 | fn flatten_twice() { |
| 38 | #[derive(StructOpt, PartialEq, Debug)] |
| 39 | struct Common { |
| 40 | arg: i32, |
| 41 | } |
| 42 | |
| 43 | #[derive(StructOpt, PartialEq, Debug)] |
| 44 | struct Opt { |
| 45 | #[structopt(flatten)] |
| 46 | c1: Common, |
| 47 | // Defines "arg" twice, so this should not work. |
| 48 | #[structopt(flatten)] |
| 49 | c2: Common, |
| 50 | } |
| 51 | Opt::from_iter(&["test", "42", "43"]); |
| 52 | } |
| 53 | |
| 54 | #[test] |
| 55 | fn flatten_in_subcommand() { |
| 56 | #[derive(StructOpt, PartialEq, Debug)] |
| 57 | struct Common { |
| 58 | arg: i32, |
| 59 | } |
| 60 | |
| 61 | #[derive(StructOpt, PartialEq, Debug)] |
| 62 | struct Add { |
| 63 | #[structopt(short)] |
| 64 | interactive: bool, |
| 65 | #[structopt(flatten)] |
| 66 | common: Common, |
| 67 | } |
| 68 | |
| 69 | #[derive(StructOpt, PartialEq, Debug)] |
| 70 | enum Opt { |
| 71 | Fetch { |
| 72 | #[structopt(short)] |
| 73 | all: bool, |
| 74 | #[structopt(flatten)] |
| 75 | common: Common, |
| 76 | }, |
| 77 | |
| 78 | Add(Add), |
| 79 | } |
| 80 | |
| 81 | assert_eq!( |
| 82 | Opt::Fetch { |
| 83 | all: false, |
| 84 | common: Common { arg: 42 } |
| 85 | }, |
| 86 | Opt::from_iter(&["test", "fetch", "42"]) |
| 87 | ); |
| 88 | assert_eq!( |
| 89 | Opt::Add(Add { |
| 90 | interactive: true, |
| 91 | common: Common { arg: 43 } |
| 92 | }), |
| 93 | Opt::from_iter(&["test", "add", "-i", "43"]) |
| 94 | ); |
| 95 | } |
| 96 | |
| 97 | #[test] |
| 98 | fn merge_subcommands_with_flatten() { |
| 99 | #[derive(StructOpt, PartialEq, Debug)] |
| 100 | enum BaseCli { |
| 101 | Command1(Command1), |
| 102 | } |
| 103 | |
| 104 | #[derive(StructOpt, PartialEq, Debug)] |
| 105 | struct Command1 { |
| 106 | arg1: i32, |
| 107 | } |
| 108 | |
| 109 | #[derive(StructOpt, PartialEq, Debug)] |
| 110 | struct Command2 { |
| 111 | arg2: i32, |
| 112 | } |
| 113 | |
| 114 | #[derive(StructOpt, PartialEq, Debug)] |
| 115 | enum Opt { |
| 116 | #[structopt(flatten)] |
| 117 | BaseCli(BaseCli), |
| 118 | Command2(Command2), |
| 119 | } |
| 120 | |
| 121 | assert_eq!( |
| 122 | Opt::BaseCli(BaseCli::Command1(Command1 { arg1: 42 })), |
| 123 | Opt::from_iter(&["test", "command1", "42"]) |
| 124 | ); |
| 125 | assert_eq!( |
| 126 | Opt::Command2(Command2 { arg2: 43 }), |
| 127 | Opt::from_iter(&["test", "command2", "43"]) |
| 128 | ); |
| 129 | } |