aboutsummaryrefslogtreecommitdiff
path: root/clap/tests/multiple_occurrences.rs
blob: 2f92fb1d941069da4fc4180e239c501b6fc63b9c (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
extern crate clap;

use clap::{App, Arg};

#[test]
fn multiple_occurrences_of_flags_long() {
    let m = App::new("mo_flags_long")
                .arg(Arg::from_usage("--multflag 'allowed multiple flag'")
                    .multiple(true))
                .arg(Arg::from_usage("--flag 'disallowed multiple flag'"))
                .get_matches_from(vec![
                    "",
                    "--multflag",
                    "--flag",
                    "--multflag"
                    ]);
    assert!(m.is_present("multflag"));
    assert_eq!(m.occurrences_of("multflag"), 2);
    assert!(m.is_present("flag"));
    assert_eq!(m.occurrences_of("flag"), 1)
}

#[test]
fn multiple_occurrences_of_flags_short() {
    let m = App::new("mo_flags_short")
                .arg(Arg::from_usage("-m --multflag 'allowed multiple flag'")
                    .multiple(true))
                .arg(Arg::from_usage("-f --flag 'disallowed multiple flag'"))
                .get_matches_from(vec![
                    "",
                    "-m",
                    "-f",
                    "-m"
                    ]);
    assert!(m.is_present("multflag"));
    assert_eq!(m.occurrences_of("multflag"), 2);
    assert!(m.is_present("flag"));
    assert_eq!(m.occurrences_of("flag"), 1);
}

#[test]
fn multiple_occurrences_of_flags_mixed() {
    let m = App::new("mo_flags_mixed")
                .arg(Arg::from_usage("-m, --multflag1 'allowed multiple flag'")
                    .multiple(true))
                .arg(Arg::from_usage("-n, --multflag2 'another allowed multiple flag'")
                    .multiple(true))
                .arg(Arg::from_usage("-f, --flag 'disallowed multiple flag'"))
                .get_matches_from(vec![
                    "",
                    "-m",
                    "-f",
                    "-n",
                    "--multflag1",
                    "-m",
                    "--multflag2"
                    ]);
    assert!(m.is_present("multflag1"));
    assert_eq!(m.occurrences_of("multflag1"), 3);
    assert!(m.is_present("multflag2"));
    assert_eq!(m.occurrences_of("multflag2"), 2);
    assert!(m.is_present("flag"));
    assert_eq!(m.occurrences_of("flag"), 1);
}

#[test]
fn multiple_occurrences_of_flags_large_quantity() {
    let args : Vec<&str> = vec![""].into_iter().chain(vec!["-m"; 1024].into_iter()).collect();
    let m = App::new("mo_flags_larg_qty")
                .arg(Arg::from_usage("-m --multflag 'allowed multiple flag'")
                    .multiple(true))
                .get_matches_from(args);
    assert!(m.is_present("multflag"));
    assert_eq!(m.occurrences_of("multflag"), 1024);
}