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 unique_flag() { |
| 13 | #[derive(StructOpt, PartialEq, Debug)] |
| 14 | struct Opt { |
| 15 | #[structopt(short, long)] |
| 16 | alice: bool, |
| 17 | } |
| 18 | |
| 19 | assert_eq!( |
| 20 | Opt { alice: false }, |
| 21 | Opt::from_clap(&Opt::clap().get_matches_from(&["test"])) |
| 22 | ); |
| 23 | assert_eq!( |
| 24 | Opt { alice: true }, |
| 25 | Opt::from_clap(&Opt::clap().get_matches_from(&["test", "-a"])) |
| 26 | ); |
| 27 | assert_eq!( |
| 28 | Opt { alice: true }, |
| 29 | Opt::from_clap(&Opt::clap().get_matches_from(&["test", "--alice"])) |
| 30 | ); |
| 31 | assert!(Opt::clap().get_matches_from_safe(&["test", "-i"]).is_err()); |
| 32 | assert!(Opt::clap() |
| 33 | .get_matches_from_safe(&["test", "-a", "foo"]) |
| 34 | .is_err()); |
| 35 | assert!(Opt::clap() |
| 36 | .get_matches_from_safe(&["test", "-a", "-a"]) |
| 37 | .is_err()); |
| 38 | assert!(Opt::clap() |
| 39 | .get_matches_from_safe(&["test", "-a", "--alice"]) |
| 40 | .is_err()); |
| 41 | } |
| 42 | |
| 43 | #[test] |
| 44 | fn multiple_flag() { |
| 45 | #[derive(StructOpt, PartialEq, Debug)] |
| 46 | struct Opt { |
| 47 | #[structopt(short, long, parse(from_occurrences))] |
| 48 | alice: u64, |
| 49 | #[structopt(short, long, parse(from_occurrences))] |
| 50 | bob: u8, |
| 51 | } |
| 52 | |
| 53 | assert_eq!( |
| 54 | Opt { alice: 0, bob: 0 }, |
| 55 | Opt::from_clap(&Opt::clap().get_matches_from(&["test"])) |
| 56 | ); |
| 57 | assert_eq!( |
| 58 | Opt { alice: 1, bob: 0 }, |
| 59 | Opt::from_clap(&Opt::clap().get_matches_from(&["test", "-a"])) |
| 60 | ); |
| 61 | assert_eq!( |
| 62 | Opt { alice: 2, bob: 0 }, |
| 63 | Opt::from_clap(&Opt::clap().get_matches_from(&["test", "-a", "-a"])) |
| 64 | ); |
| 65 | assert_eq!( |
| 66 | Opt { alice: 2, bob: 2 }, |
| 67 | Opt::from_clap(&Opt::clap().get_matches_from(&["test", "-a", "--alice", "-bb"])) |
| 68 | ); |
| 69 | assert_eq!( |
| 70 | Opt { alice: 3, bob: 1 }, |
| 71 | Opt::from_clap(&Opt::clap().get_matches_from(&["test", "-aaa", "--bob"])) |
| 72 | ); |
| 73 | assert!(Opt::clap().get_matches_from_safe(&["test", "-i"]).is_err()); |
| 74 | assert!(Opt::clap() |
| 75 | .get_matches_from_safe(&["test", "-a", "foo"]) |
| 76 | .is_err()); |
| 77 | } |
| 78 | |
| 79 | fn parse_from_flag(b: bool) -> std::sync::atomic::AtomicBool { |
| 80 | std::sync::atomic::AtomicBool::new(b) |
| 81 | } |
| 82 | |
| 83 | #[test] |
| 84 | fn non_bool_flags() { |
| 85 | #[derive(StructOpt, Debug)] |
| 86 | struct Opt { |
| 87 | #[structopt(short, long, parse(from_flag = parse_from_flag))] |
| 88 | alice: std::sync::atomic::AtomicBool, |
| 89 | #[structopt(short, long, parse(from_flag))] |
| 90 | bob: std::sync::atomic::AtomicBool, |
| 91 | } |
| 92 | |
| 93 | let falsey = Opt::from_clap(&Opt::clap().get_matches_from(&["test"])); |
| 94 | assert!(!falsey.alice.load(std::sync::atomic::Ordering::Relaxed)); |
| 95 | assert!(!falsey.bob.load(std::sync::atomic::Ordering::Relaxed)); |
| 96 | |
| 97 | let alice = Opt::from_clap(&Opt::clap().get_matches_from(&["test", "-a"])); |
| 98 | assert!(alice.alice.load(std::sync::atomic::Ordering::Relaxed)); |
| 99 | assert!(!alice.bob.load(std::sync::atomic::Ordering::Relaxed)); |
| 100 | |
| 101 | let bob = Opt::from_clap(&Opt::clap().get_matches_from(&["test", "-b"])); |
| 102 | assert!(!bob.alice.load(std::sync::atomic::Ordering::Relaxed)); |
| 103 | assert!(bob.bob.load(std::sync::atomic::Ordering::Relaxed)); |
| 104 | |
| 105 | let both = Opt::from_clap(&Opt::clap().get_matches_from(&["test", "-b", "-a"])); |
| 106 | assert!(both.alice.load(std::sync::atomic::Ordering::Relaxed)); |
| 107 | assert!(both.bob.load(std::sync::atomic::Ordering::Relaxed)); |
| 108 | } |
| 109 | |
| 110 | #[test] |
| 111 | fn combined_flags() { |
| 112 | #[derive(StructOpt, PartialEq, Debug)] |
| 113 | struct Opt { |
| 114 | #[structopt(short, long)] |
| 115 | alice: bool, |
| 116 | #[structopt(short, long, parse(from_occurrences))] |
| 117 | bob: u64, |
| 118 | } |
| 119 | |
| 120 | assert_eq!( |
| 121 | Opt { |
| 122 | alice: false, |
| 123 | bob: 0 |
| 124 | }, |
| 125 | Opt::from_clap(&Opt::clap().get_matches_from(&["test"])) |
| 126 | ); |
| 127 | assert_eq!( |
| 128 | Opt { |
| 129 | alice: true, |
| 130 | bob: 0 |
| 131 | }, |
| 132 | Opt::from_clap(&Opt::clap().get_matches_from(&["test", "-a"])) |
| 133 | ); |
| 134 | assert_eq!( |
| 135 | Opt { |
| 136 | alice: true, |
| 137 | bob: 0 |
| 138 | }, |
| 139 | Opt::from_clap(&Opt::clap().get_matches_from(&["test", "-a"])) |
| 140 | ); |
| 141 | assert_eq!( |
| 142 | Opt { |
| 143 | alice: false, |
| 144 | bob: 1 |
| 145 | }, |
| 146 | Opt::from_clap(&Opt::clap().get_matches_from(&["test", "-b"])) |
| 147 | ); |
| 148 | assert_eq!( |
| 149 | Opt { |
| 150 | alice: true, |
| 151 | bob: 1 |
| 152 | }, |
| 153 | Opt::from_clap(&Opt::clap().get_matches_from(&["test", "--alice", "--bob"])) |
| 154 | ); |
| 155 | assert_eq!( |
| 156 | Opt { |
| 157 | alice: true, |
| 158 | bob: 4 |
| 159 | }, |
| 160 | Opt::from_clap(&Opt::clap().get_matches_from(&["test", "-bb", "-a", "-bb"])) |
| 161 | ); |
| 162 | } |