aboutsummaryrefslogtreecommitdiff
path: root/structopt/tests/flags.rs
blob: 39a5dc3970000cebaa501335d2ef75364e10367f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
// Copyright 2018 Guillaume Pinot (@TeXitoi) <texitoi@texitoi.eu>
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use structopt::StructOpt;

#[test]
fn unique_flag() {
    #[derive(StructOpt, PartialEq, Debug)]
    struct Opt {
        #[structopt(short, long)]
        alice: bool,
    }

    assert_eq!(
        Opt { alice: false },
        Opt::from_clap(&Opt::clap().get_matches_from(&["test"]))
    );
    assert_eq!(
        Opt { alice: true },
        Opt::from_clap(&Opt::clap().get_matches_from(&["test", "-a"]))
    );
    assert_eq!(
        Opt { alice: true },
        Opt::from_clap(&Opt::clap().get_matches_from(&["test", "--alice"]))
    );
    assert!(Opt::clap().get_matches_from_safe(&["test", "-i"]).is_err());
    assert!(Opt::clap()
        .get_matches_from_safe(&["test", "-a", "foo"])
        .is_err());
    assert!(Opt::clap()
        .get_matches_from_safe(&["test", "-a", "-a"])
        .is_err());
    assert!(Opt::clap()
        .get_matches_from_safe(&["test", "-a", "--alice"])
        .is_err());
}

#[test]
fn multiple_flag() {
    #[derive(StructOpt, PartialEq, Debug)]
    struct Opt {
        #[structopt(short, long, parse(from_occurrences))]
        alice: u64,
        #[structopt(short, long, parse(from_occurrences))]
        bob: u8,
    }

    assert_eq!(
        Opt { alice: 0, bob: 0 },
        Opt::from_clap(&Opt::clap().get_matches_from(&["test"]))
    );
    assert_eq!(
        Opt { alice: 1, bob: 0 },
        Opt::from_clap(&Opt::clap().get_matches_from(&["test", "-a"]))
    );
    assert_eq!(
        Opt { alice: 2, bob: 0 },
        Opt::from_clap(&Opt::clap().get_matches_from(&["test", "-a", "-a"]))
    );
    assert_eq!(
        Opt { alice: 2, bob: 2 },
        Opt::from_clap(&Opt::clap().get_matches_from(&["test", "-a", "--alice", "-bb"]))
    );
    assert_eq!(
        Opt { alice: 3, bob: 1 },
        Opt::from_clap(&Opt::clap().get_matches_from(&["test", "-aaa", "--bob"]))
    );
    assert!(Opt::clap().get_matches_from_safe(&["test", "-i"]).is_err());
    assert!(Opt::clap()
        .get_matches_from_safe(&["test", "-a", "foo"])
        .is_err());
}

fn parse_from_flag(b: bool) -> std::sync::atomic::AtomicBool {
    std::sync::atomic::AtomicBool::new(b)
}

#[test]
fn non_bool_flags() {
    #[derive(StructOpt, Debug)]
    struct Opt {
        #[structopt(short, long, parse(from_flag = parse_from_flag))]
        alice: std::sync::atomic::AtomicBool,
        #[structopt(short, long, parse(from_flag))]
        bob: std::sync::atomic::AtomicBool,
    }

    let falsey = Opt::from_clap(&Opt::clap().get_matches_from(&["test"]));
    assert!(!falsey.alice.load(std::sync::atomic::Ordering::Relaxed));
    assert!(!falsey.bob.load(std::sync::atomic::Ordering::Relaxed));

    let alice = Opt::from_clap(&Opt::clap().get_matches_from(&["test", "-a"]));
    assert!(alice.alice.load(std::sync::atomic::Ordering::Relaxed));
    assert!(!alice.bob.load(std::sync::atomic::Ordering::Relaxed));

    let bob = Opt::from_clap(&Opt::clap().get_matches_from(&["test", "-b"]));
    assert!(!bob.alice.load(std::sync::atomic::Ordering::Relaxed));
    assert!(bob.bob.load(std::sync::atomic::Ordering::Relaxed));

    let both = Opt::from_clap(&Opt::clap().get_matches_from(&["test", "-b", "-a"]));
    assert!(both.alice.load(std::sync::atomic::Ordering::Relaxed));
    assert!(both.bob.load(std::sync::atomic::Ordering::Relaxed));
}

#[test]
fn combined_flags() {
    #[derive(StructOpt, PartialEq, Debug)]
    struct Opt {
        #[structopt(short, long)]
        alice: bool,
        #[structopt(short, long, parse(from_occurrences))]
        bob: u64,
    }

    assert_eq!(
        Opt {
            alice: false,
            bob: 0
        },
        Opt::from_clap(&Opt::clap().get_matches_from(&["test"]))
    );
    assert_eq!(
        Opt {
            alice: true,
            bob: 0
        },
        Opt::from_clap(&Opt::clap().get_matches_from(&["test", "-a"]))
    );
    assert_eq!(
        Opt {
            alice: true,
            bob: 0
        },
        Opt::from_clap(&Opt::clap().get_matches_from(&["test", "-a"]))
    );
    assert_eq!(
        Opt {
            alice: false,
            bob: 1
        },
        Opt::from_clap(&Opt::clap().get_matches_from(&["test", "-b"]))
    );
    assert_eq!(
        Opt {
            alice: true,
            bob: 1
        },
        Opt::from_clap(&Opt::clap().get_matches_from(&["test", "--alice", "--bob"]))
    );
    assert_eq!(
        Opt {
            alice: true,
            bob: 4
        },
        Opt::from_clap(&Opt::clap().get_matches_from(&["test", "-bb", "-a", "-bb"]))
    );
}