aboutsummaryrefslogtreecommitdiff
path: root/argparse/src/test_optional.rs
blob: 5f4c35eb06e5c1d5c30e0fe7df5468909f545ce7 (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
use parser::ArgumentParser;
use super::StoreOption;
use test_parser::{check_ok};

fn opt(args: &[&str]) -> Option<isize> {
    let mut val = None;
    {
        let mut ap = ArgumentParser::new();
        ap.refer(&mut val)
          .add_option(&["-s", "--set"], StoreOption,
            "Set int value");
        check_ok(&ap, args);
    }
    return val;
}

#[test]
fn test_opt() {
    assert_eq!(opt(&["./argparse_test"]), None);
    assert_eq!(opt(&["./argparse_test", "-s", "10"]), Some(10));
    assert_eq!(opt(&["./argparse_test", "--set", "11"]), Some(11));
}

#[test]
#[should_panic]
fn test_opt_no_arg() {
    opt(&["./argparse_test", "--set"]);
}

fn optstr(args: &[&str]) -> Option<String> {
    let mut val = None;
    {
        let mut ap = ArgumentParser::new();
        ap.refer(&mut val)
          .add_option(&["-s", "--set"], StoreOption,
            "Set string value");
        check_ok(&ap, args);
    }
    return val;
}

#[test]
fn test_str() {
    assert_eq!(optstr(&["./argparse_test"]), None);
    assert_eq!(optstr(&["./argparse_test", "-s", "10"]), Some(10.to_string()));
    assert_eq!(optstr(&["./argparse_test", "--set", "11"]),
        Some(11.to_string()));
}

#[test]
#[should_panic]
fn test_str_no_art() {
    optstr(&["./argparse_test", "--set"]);
}