Debian Codegraph @

tests/si_number.rs

raw symbols SCIP
1 use clap::Parser;
2 use clap_num::si_number;
3
4 #[test]
5 fn utf8_byte_index_not_char_boundry() {
6 let _ = si_number::<u64>("˲TP");
7 }
8
9 #[test]
10 fn utf8_byte_index_not_char_boundry_with_decimal() {
11 let _ = si_number::<u64>("˲.E");
12 }
13
14 // standalone basic tests
15 #[cfg(test)]
16 mod basic {
17 use super::*;
18
19 macro_rules! pos {
20 ($NAME:ident, $VAL:expr, $RESULT:expr) => {
21 #[test]
22 fn $NAME() {
23 assert_eq!(si_number($VAL), Ok($RESULT));
24 }
25 };
26 }
27
28 macro_rules! neg {
29 ($NAME:ident, $VAL:expr, $TYPE:ident, $RESULT:expr) => {
30 #[test]
31 fn $NAME() {
32 let num: Result<$TYPE, String> = si_number($VAL);
33 assert_eq!(num, Err(String::from($RESULT)));
34 }
35 };
36 }
37
38 // basic positive path
39 pos!(zero, "0", 0u8);
40 pos!(one, "1", 1u8);
41 pos!(neg_one, "-1", -1i8);
42 pos!(limit, "255", 255u8);
43 pos!(underscores, "1_000_000", 1_000_000u32);
44
45 // basic positive path with Si suffix
46 pos!(kilo, "1k", 1_000u16);
47 pos!(kilo_caps, "1K", 1_000u16);
48 pos!(mega, "1M", 1_000_000u32);
49 pos!(giga, "1G", 1_000_000_000u64);
50 pos!(tera, "1T", 1_000_000_000_000u64);
51 pos!(peta, "1P", 1_000_000_000_000_000u64);
52 pos!(exa, "1E", 1_000_000_000_000_000_000u64);
53 pos!(zetta, "1Z", 1_000_000_000_000_000_000_000u128);
54 pos!(yotta, "1Y", 1_000_000_000_000_000_000_000_000u128);
55
56 pos!(trailing_1, "1k2", 1_200u16);
57 pos!(trailing_2, "1k23", 1_230u16);
58 pos!(trailing_3, "1k234", 1_234u16);
59 neg!(trailing_4, "1k2345", u16, "not an integer");
60 pos!(trailing_do_nothing, "1k000", 1_000u16);
61 pos!(negative_trailing, "-1k234", -1_234i16);
62
63 pos!(leading_2, "12k123", 12_123u16);
64 pos!(leading_3, "123k123", 123_123u32);
65 pos!(leading_4, "1234k123", 1_234_123i32);
66 pos!(negative_leading, "-123k123", -123_123i32);
67
68 pos!(dec_1, "1.2k", 1_200u16);
69 pos!(dec_2, "1.23k", 1_230u16);
70 pos!(dec_3, "1.234k", 1_234u16);
71 neg!(dec_4, "1.2345k", u16, "not an integer");
72 pos!(dec_do_nothing, "1.000k", 1_000u16);
73
74 pos!(dec_ending_si, "1.k", 1_000u16);
75
76 neg!(mixed_1, "1K23.45", u16, "not an integer");
77 neg!(mixed_2, "1.23k45", u16, "invalid digit found in string");
78
79 neg!(trailing_dec, "1.", u8, "invalid digit found in string");
80
81 pos!(
82 big,
83 "1Y123456789987654321",
84 1_123_456_789_987_654_321_000_000u128
85 );
86
87 neg!(leading_si, "k1", u16, "no value found before SI symbol");
88
89 neg!(overflow, "1k", u8, "number too large to fit in target type");
90 neg!(
91 normal_overflow,
92 "300",
93 u8,
94 "number too large to fit in target type"
95 );
96
97 neg!(multiple_suffix, "1kk", u16, "invalid digit found in string");
98 }
99
100 // integration tests with clap
101 #[cfg(test)]
102 mod integration {
103 use super::*;
104
105 #[derive(Parser)]
106 struct Args {
107 #[clap(long, value_parser=si_number::<u128>)]
108 resistance: u128,
109 }
110
111 // positive path
112 macro_rules! pos {
113 ($NAME:ident, $VAL:expr, $RESULT:expr) => {
114 #[test]
115 fn $NAME() {
116 let opt = Args::parse_from(&["", "--resistance", $VAL]);
117 assert_eq!(opt.resistance, $RESULT);
118 }
119 };
120 }
121
122 // negative path
123 macro_rules! neg {
124 ($NAME:ident, $VAL:expr, $RESULT:expr) => {
125 #[test]
126 fn $NAME() {
127 let opt = Args::try_parse_from(&["", "--resistance", $VAL]);
128 match opt {
129 Err(e) => {
130 assert!(format!("{:?}", e).contains($RESULT));
131 }
132 _ => unreachable!(),
133 };
134 }
135 };
136 }
137
138 pos!(simple_0, "1k123", 1123);
139 pos!(simple_1, "456789k123", 456789123);
140 pos!(simple_2, "1M1", 1_100_000);
141
142 neg!(big, "999999999999999999999Y", "too large");
143 neg!(invalid, "1k1k", "invalid digit");
144 neg!(precise, "1k1111", "not an integer");
145 neg!(leading_prefix, "k123", "no value found before SI symbol");
146 }