aboutsummaryrefslogtreecommitdiff
path: root/structopt/tests/skip.rs
blob: 47682d8d7e762b159cb0845291b7731e6d415228 (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
// 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 skip_1() {
    #[derive(StructOpt, Debug, PartialEq)]
    struct Opt {
        #[structopt(short)]
        x: u32,
        #[structopt(skip)]
        s: u32,
    }

    assert!(Opt::from_iter_safe(&["test", "-x", "10", "20"]).is_err());
    assert_eq!(
        Opt::from_iter(&["test", "-x", "10"]),
        Opt {
            x: 10,
            s: 0, // default
        }
    );
}

#[test]
fn skip_2() {
    #[derive(StructOpt, Debug, PartialEq)]
    struct Opt {
        #[structopt(short)]
        x: u32,
        #[structopt(skip)]
        ss: String,
        #[structopt(skip)]
        sn: u8,
        y: u32,
        #[structopt(skip)]
        sz: u16,
        t: u32,
    }

    assert_eq!(
        Opt::from_iter(&["test", "-x", "10", "20", "30"]),
        Opt {
            x: 10,
            ss: String::from(""),
            sn: 0,
            y: 20,
            sz: 0,
            t: 30,
        }
    );
}

#[test]
fn skip_enum() {
    #[derive(Debug, PartialEq)]
    #[allow(unused)]
    enum Kind {
        A,
        B,
    }

    impl Default for Kind {
        fn default() -> Self {
            return Kind::B;
        }
    }

    #[derive(StructOpt, Debug, PartialEq)]
    pub struct Opt {
        #[structopt(long, short)]
        number: u32,
        #[structopt(skip)]
        k: Kind,
        #[structopt(skip)]
        v: Vec<u32>,
    }

    assert_eq!(
        Opt::from_iter(&["test", "-n", "10"]),
        Opt {
            number: 10,
            k: Kind::B,
            v: vec![],
        }
    );
}

#[test]
fn skip_help_doc_comments() {
    #[derive(StructOpt, Debug, PartialEq)]
    pub struct Opt {
        #[structopt(skip, help = "internal_stuff")]
        a: u32,

        #[structopt(skip, long_help = "internal_stuff\ndo not touch")]
        b: u32,

        /// Not meant to be used by clap.
        ///
        /// I want a default here.
        #[structopt(skip)]
        c: u32,

        #[structopt(short, parse(try_from_str))]
        n: u32,
    }

    assert_eq!(
        Opt::from_iter(&["test", "-n", "10"]),
        Opt {
            n: 10,
            a: 0,
            b: 0,
            c: 0,
        }
    );
}

#[test]
fn skip_val() {
    #[derive(StructOpt, Debug, PartialEq)]
    pub struct Opt {
        #[structopt(long, short)]
        number: u32,

        #[structopt(skip = "key")]
        k: String,

        #[structopt(skip = vec![1, 2, 3])]
        v: Vec<u32>,
    }

    assert_eq!(
        Opt::from_iter(&["test", "-n", "10"]),
        Opt {
            number: 10,
            k: "key".into(),
            v: vec![1, 2, 3]
        }
    );
}