proptest_arbitrary/_std/
char.rs

1//! Arbitrary implementations for `std::char`.
2
3use super::*;
4use std::char::*;
5use std::iter::once;
6use std::ops::Range;
7
8macro_rules! impl_wrap_char {
9    ($type: ty, $mapper: expr) => {
10        arbitrary!($type, SMapped<'a, char, Self>, ParamsType<'a, char>;
11            args => any_with_smap(args, $mapper));
12    };
13}
14
15impl_wrap_char!(EscapeDebug,   char::escape_debug);
16impl_wrap_char!(EscapeDefault, char::escape_default);
17impl_wrap_char!(EscapeUnicode, char::escape_unicode);
18#[cfg(MIN_VER_1_24_0)]
19impl_wrap_char!(ToLowercase,   char::to_lowercase);
20#[cfg(MIN_VER_1_24_0)]
21impl_wrap_char!(ToUppercase,   char::to_uppercase);
22
23#[cfg(feature = "unstable")]
24// TODO: Make generic over all Iterator<Item = u8>?
25arbitrary!(DecodeUtf8<<Vec<u8> as IntoIterator>::IntoIter>,
26    Flatten<Mapped<'a, u16, SMapped<'a, Vec<u8>, Self>>>;
27    any::<u16>().prop_flat_map(|size| any_with_smap(
28        product_pack![size_bounds(..size as usize), default()],
29        decode_utf8
30    ))
31);
32
33#[cfg(MIN_VER_1_24_0)]
34// TODO: Make generic over all Iterator<Item = u16>?
35arbitrary!(DecodeUtf16<<Vec<u16> as IntoIterator>::IntoIter>,
36    Flatten<Mapped<'a, u16, SMapped<'a, Vec<u16>, Self>>>;
37    any::<u16>().prop_flat_map(|size| any_with_smap(
38        product_pack![size_bounds(..size as usize), default()],
39        decode_utf16
40    ))
41);
42
43arbitrary!(ParseCharError, IndFlatten<Mapped<'a, bool, Just<Self>>>;
44    any::<bool>().prop_ind_flat_map(|is_two|
45        Just((if is_two { "__" } else { "" }).parse::<char>().unwrap_err()))
46);
47
48#[cfg(feature = "unstable")]
49arbitrary!(CharTryFromError; {
50    use std::convert::TryFrom;
51    char::try_from(0xD800 as u32).unwrap_err()
52});
53
54arbitrary!(DecodeUtf16Error, SFnPtrMap<Range<u16>, Self>;
55    static_map(0xD800..0xE000, |x|
56        decode_utf16(once(x)).next().unwrap().unwrap_err())
57);
58
59#[cfg(test)]
60mod test {
61    no_panic_test!(
62        escape_debug => EscapeDebug,
63        escape_default => EscapeDefault,
64        escape_unicode => EscapeUnicode,
65        parse_char_error => ParseCharError,
66        decode_utf16_error => DecodeUtf16Error
67    );
68
69    #[cfg(MIN_VER_1_24_0)]
70    no_panic_test!(
71        to_lowercase => ToLowercase,
72        to_uppercase => ToUppercase,
73        decode_utf16 => DecodeUtf16<<Vec<u16> as IntoIterator>::IntoIter>
74    );
75
76    #[cfg(feature = "unstable")]
77    no_panic_test!(
78        decode_utf8 => DecodeUtf8<<Vec<u8> as IntoIterator>::IntoIter>,
79        char_try_from_error => CharTryFromError
80    );
81}