1 //-
2 // Copyright 2017, 2018 The proptest developers
3 //
4 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
5 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
7 // option. This file may not be copied, modified, or distributed
8 // except according to those terms.
9
10 //! Arbitrary implementations for `std::str`.
11
12 use crate::std_facade::Vec;
13 use core::iter::repeat;
14 use core::str::{from_utf8, ParseBoolError, Utf8Error};
15
16 use crate::arbitrary::*;
17 use crate::strategy::statics::static_map;
18 use crate::strategy::*;
19
20 arbitrary!(ParseBoolError; "".parse::<bool>().unwrap_err());
21
22 type ELSeq = WA<Just<&'static [u8]>>;
23 type ELSeqs = TupleUnion<(ELSeq, ELSeq, ELSeq, ELSeq)>;
24
gen_el_seqs() -> ELSeqs25 fn gen_el_seqs() -> ELSeqs {
26 prop_oneof![
27 Just(&[0xC2]), // None
28 Just(&[0x80]), // Some(1)
29 Just(&[0xE0, 0xA0, 0x00]), // Some(2)
30 Just(&[0xF0, 0x90, 0x80, 0x00]) // Some(3)
31 ]
32 }
33
34 arbitrary!(Utf8Error, SFnPtrMap<(StrategyFor<u16>, ELSeqs), Utf8Error>;
35 static_map((any::<u16>(), gen_el_seqs()), |(vut, elseq)| {
36 let v = repeat(b'_').take(vut as usize)
37 .chain(elseq.iter().cloned())
38 .collect::<Vec<u8>>();
39 from_utf8(&v).unwrap_err()
40 })
41 );
42
43 #[cfg(test)]
44 mod test {
45 no_panic_test!(
46 parse_bool_errror => ParseBoolError,
47 utf8_error => Utf8Error
48 );
49 }
50