| 1 | use claprust-analyzer cargo clap 4.5.27 crate/::Parserrust-analyzer cargo clap_builder 4.5.27 derive/Parser#; |
| 2 | use clap_numrust-analyzer cargo clap-num 1.2.0 crate/::number_rangerust-analyzer cargo clap-num 1.2.0 number_range().; |
| 3 | |
| 4 | // standalone basic tests |
| 5 | #[cfg(test)] |
| 6 | mod basicrust-analyzer cargo clap-num 1.2.0 basic/ { |
| 7 | use superrust-analyzer cargo clap-num 1.2.0 crate/::*; |
| 8 | |
| 9 | macro_rules! posrust-analyzer cargo clap-num 1.2.0 basic/pos! { |
| 10 | ($NAME:ident, $VAL:expr, $MIN:expr, $MAX:expr, $RESULT:expr) => { |
| 11 | #[test] |
| 12 | fn $NAME() { |
| 13 | assert_eq!(number_range($VAL, $MIN, $MAX), Ok($RESULT)); |
| 14 | } |
| 15 | }; |
| 16 | } |
| 17 | |
| 18 | macro_rules! negrust-analyzer cargo clap-num 1.2.0 basic/neg! { |
| 19 | ($NAME:ident, $VAL:expr, $MIN:expr, $MAX:expr, $RESULT:expr) => { |
| 20 | #[test] |
| 21 | fn $NAME() { |
| 22 | assert_eq!(number_range($VAL, $MIN, $MAX), Err(String::from($RESULT))); |
| 23 | } |
| 24 | }; |
| 25 | } |
| 26 | |
| 27 | posrust-analyzer cargo clap-num 1.2.0 basic/pos!!(simplerust-analyzer cargo clap-num 1.2.0 basic/simple()., "123", 12u8, 200u8, 123u8); |
| 28 | posrust-analyzer cargo clap-num 1.2.0 basic/pos!!(zerorust-analyzer cargo clap-num 1.2.0 basic/zero()., "0", 0u8, 0u8, 0u8); |
| 29 | posrust-analyzer cargo clap-num 1.2.0 basic/pos!!(negrust-analyzer cargo clap-num 1.2.0 basic/neg()., "-1", -rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i8][Neg]neg().10i8, 10i8, -rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i8][Neg]neg().1); |
| 30 | posrust-analyzer cargo clap-num 1.2.0 basic/pos!!(min_limitrust-analyzer cargo clap-num 1.2.0 basic/min_limit()., "-5", -rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i8][Neg]neg().5i8, -rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i8][Neg]neg().5i8, -rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i8][Neg]neg().5i8); |
| 31 | posrust-analyzer cargo clap-num 1.2.0 basic/pos!!(max_limitrust-analyzer cargo clap-num 1.2.0 basic/max_limit()., "65535", 0, u16::MAXrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[u16]MAX., u16::MAXrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[u16]MAX.); |
| 32 | |
| 33 | negrust-analyzer cargo clap-num 1.2.0 basic/neg!!(decimalrust-analyzer cargo clap-num 1.2.0 basic/decimal()., "1.1", -rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i8][Neg]neg().10i8, 10i8, "invalid digit found in string"); |
| 34 | negrust-analyzer cargo clap-num 1.2.0 basic/neg!!(minrust-analyzer cargo clap-num 1.2.0 basic/min()., "-1", 0i8, 0i8, "less than minimum of 0"); |
| 35 | negrust-analyzer cargo clap-num 1.2.0 basic/neg!!(maxrust-analyzer cargo clap-num 1.2.0 basic/max()., "1", 0i8, 0i8, "exceeds maximum of 0"); |
| 36 | negrust-analyzer cargo clap-num 1.2.0 basic/neg!!( |
| 37 | overflowrust-analyzer cargo clap-num 1.2.0 basic/overflow()., |
| 38 | "256", |
| 39 | 0, |
| 40 | stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::u8rust-analyzer cargo core https://github.com/rust-lang/rust/library/core legacy_int_modules/u8/::MAXrust-analyzer cargo core https://github.com/rust-lang/rust/library/core legacy_int_modules/u8/MAX., |
| 41 | "number too large to fit in target type" |
| 42 | ); |
| 43 | negrust-analyzer cargo clap-num 1.2.0 basic/neg!!(nanrust-analyzer cargo clap-num 1.2.0 basic/nan()., "nan", 0, 0, "invalid digit found in string"); |
| 44 | |
| 45 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 46 | #[should_panic] |
| 47 | fn min_max_debug_assertrust-analyzer cargo clap-num 1.2.0 basic/min_max_debug_assert().() { |
| 48 | let _ = number_rangerust-analyzer cargo clap-num 1.2.0 number_range().("", 2, 1); |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | // integration tests with clap |
| 53 | #[cfg(test)] |
| 54 | mod integrationrust-analyzer cargo clap-num 1.2.0 integration/ { |
| 55 | use superrust-analyzer cargo clap-num 1.2.0 crate/::*; |
| 56 | |
| 57 | fn human_livable_temperaturerust-analyzer cargo clap-num 1.2.0 integration/human_livable_temperature().(s: &str) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<i8, Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#> { |
| 58 | number_rangerust-analyzer cargo clap-num 1.2.0 number_range().(s, -rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i8][Neg]neg().40, 60) |
| 59 | } |
| 60 | |
| 61 | #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Parserrust-analyzer cargo clap_derive 4.5.24 Parser:, Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:)] |
| 62 | struct Thermostatrust-analyzer cargo clap-num 1.2.0 integration/Thermostat# { |
| 63 | #[claprust-analyzer cargo clap_derive 4.5.24 clap:( |
| 64 | longrust-analyzer cargo clap_builder 4.5.27 builder/arg/impl#[Arg]long()., |
| 65 | value_parserrust-analyzer cargo clap_builder 4.5.27 builder/arg/impl#[Arg]value_parser().=human_livable_temperaturerust-analyzer cargo clap-num 1.2.0 integration/human_livable_temperature()., |
| 66 | allow_hyphen_valuesrust-analyzer cargo clap_builder 4.5.27 builder/arg/impl#[Arg]allow_hyphen_values().=true |
| 67 | )] |
| 68 | temperaturerust-analyzer cargo clap-num 1.2.0 integration/Thermostat#temperature.: i8, |
| 69 | } |
| 70 | |
| 71 | // positive path |
| 72 | macro_rules! posrust-analyzer cargo clap-num 1.2.0 integration/pos! { |
| 73 | ($NAME:ident, $VAL:expr, $RESULT:expr) => { |
| 74 | #[test] |
| 75 | fn $NAME() { |
| 76 | let opt = Thermostat::parse_from(&["", "--temperature", $VAL]); |
| 77 | assert_eq!(opt.temperature, $RESULT); |
| 78 | } |
| 79 | }; |
| 80 | } |
| 81 | |
| 82 | // negative path |
| 83 | macro_rules! negrust-analyzer cargo clap-num 1.2.0 integration/neg! { |
| 84 | ($NAME:ident, $VAL:expr, $RESULT:expr) => { |
| 85 | #[test] |
| 86 | fn $NAME() { |
| 87 | let opt = Thermostat::try_parse_from(&["", "--temperature", $VAL]); |
| 88 | match opt { |
| 89 | Err(e) => { |
| 90 | assert!(format!("{:?}", e).contains($RESULT)); |
| 91 | } |
| 92 | _ => unreachable!(), |
| 93 | }; |
| 94 | } |
| 95 | }; |
| 96 | } |
| 97 | |
| 98 | posrust-analyzer cargo clap-num 1.2.0 integration/pos!!(simplerust-analyzer cargo clap-num 1.2.0 integration/simple()., "50", 50); |
| 99 | posrust-analyzer cargo clap-num 1.2.0 integration/pos!!(zerorust-analyzer cargo clap-num 1.2.0 integration/zero()., "0", 0); |
| 100 | posrust-analyzer cargo clap-num 1.2.0 integration/pos!!(negativerust-analyzer cargo clap-num 1.2.0 integration/negative()., "-30", -rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i8][Neg]neg().30); |
| 101 | posrust-analyzer cargo clap-num 1.2.0 integration/pos!!(positive_limitrust-analyzer cargo clap-num 1.2.0 integration/positive_limit()., "60", 60); |
| 102 | posrust-analyzer cargo clap-num 1.2.0 integration/pos!!(negative_limitrust-analyzer cargo clap-num 1.2.0 integration/negative_limit()., "-40", -rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i8][Neg]neg().40); |
| 103 | |
| 104 | negrust-analyzer cargo clap-num 1.2.0 integration/neg!!(too_smallrust-analyzer cargo clap-num 1.2.0 integration/too_small()., "-41", "less than minimum of -40"); |
| 105 | negrust-analyzer cargo clap-num 1.2.0 integration/neg!!(too_largerust-analyzer cargo clap-num 1.2.0 integration/too_large()., "61", "exceeds maximum of 60"); |
| 106 | } |