File: maybe_bin.rs

package info (click to toggle)
rust-clap-num 1.2.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 152 kB
  • sloc: makefile: 2
file content (42 lines) | stat: -rw-r--r-- 1,005 bytes parent folder | download
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
use clap_num::maybe_bin;

#[cfg(test)]
mod basic {
    use super::*;

    // positive path
    macro_rules! pos {
        ($NAME:ident, $VAL:expr, $RESULT:expr) => {
            #[test]
            fn $NAME() {
                assert_eq!(maybe_bin($VAL), Ok($RESULT));
            }
        };
    }

    // negative path
    macro_rules! neg {
        ($NAME:ident, $VAL:expr, $RESULT:expr) => {
            #[test]
            fn $NAME() {
                let val: Result<u64, String> = maybe_bin($VAL);
                assert_eq!(val, Err(String::from($RESULT)));
            }
        };
    }

    pos!(simple, "123", 123u8);
    pos!(zero_dec, "0", 0u16);
    pos!(zero_bin, "0b0", 0u16);
    pos!(one_dec, "1", 1u64);
    pos!(one_bin, "0b1", 1u64);
    pos!(aa, "0b10101010", 0xaau64);
    pos!(leading_zero, "001", 1u64);

    neg!(
        missing_suffix,
        "0b",
        "cannot parse integer from empty string"
    );
    neg!(non_bin_digit, "0b12G", "invalid digit found in string");
}