Skip to content

Commit 298ec89

Browse files
committed
Feat: Add generators for char type
1 parent c2d950f commit 298ec89

File tree

3 files changed

+72
-0
lines changed

3 files changed

+72
-0
lines changed

DOCUMENTATION.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,16 @@ let some_longs = gens::i64::ranged(10..=20);
214214
let mostly_true = gens::bool::with_ratio(1,20);
215215
```
216216

217+
Various generators for `char` type:
218+
219+
```rust
220+
use monkey_test::*;
221+
let same_as_unicode = gens::char::any();
222+
let unicode = gens::char::unicode();
223+
let alpha_lower = gens::char::alpha_lower();
224+
let alpha_numeric = gens::char::alpha_numeric();
225+
```
226+
217227
There are also specialized generators:
218228

219229
* In `gens::fixed`, generators that produce fixed values without randomness.

src/gens.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
33
pub mod bool;
44
mod chain;
5+
pub mod char;
56
mod filter;
67
pub mod fixed;
78
mod float;

src/gens/char.rs

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
//! Generators for char type.
2+
3+
use crate::BoxGen;
4+
use crate::FilterWithGen;
5+
use crate::MapWithGen;
6+
7+
/// Any possible Rust `char` value, that is any valid unicode scalar value.
8+
/// For reference, see
9+
/// [Rust official documentation on `char` type](https://doc.rust-lang.org/std/primitive.char.html).
10+
pub fn unicode() -> BoxGen<char> {
11+
crate::gens::u32::ranged(0..=0x10ffff)
12+
.filter(|&num| !(0xD800..=0xDFFF).contains(&num))
13+
.map(|num| char::from_u32(num).unwrap(), |ch| ch as u32)
14+
}
15+
16+
/// Shorthand for [unicode].
17+
pub fn any() -> BoxGen<char> {
18+
unicode()
19+
}
20+
21+
/// Build char generator from u32 range (inclusive)
22+
fn chars_from_u32_range(min: u32, max_inclusive: u32) -> BoxGen<char> {
23+
crate::gens::u32::ranged(min..=max_inclusive)
24+
.map(|num| char::from_u32(num).unwrap(), |ch| ch as u32)
25+
}
26+
27+
/// Any arabic numeral 0..9, unicode values 48..=57.
28+
pub fn number() -> BoxGen<char> {
29+
chars_from_u32_range(48, 57)
30+
}
31+
32+
/// Any alpha upper char, unicode values 65..=90.
33+
pub fn alpha_upper() -> BoxGen<char> {
34+
chars_from_u32_range(65, 90)
35+
}
36+
37+
/// Any alpha lower char, unicode values 97..=122.
38+
pub fn alpha_lower() -> BoxGen<char> {
39+
chars_from_u32_range(97, 122)
40+
}
41+
42+
/// Any alpha char, both lower and upper case.
43+
pub fn alpha() -> BoxGen<char> {
44+
crate::gens::mix_evenly(&[alpha_upper(), alpha_lower()])
45+
}
46+
47+
/// Any alpha numeric char, see [alpha] and [number].
48+
pub fn alpha_numeric() -> BoxGen<char> {
49+
crate::gens::mix_with_ratio(&[(9, alpha()), (1, number())])
50+
}
51+
52+
/// Any ASCII printable character, unicode values 32..=126.
53+
pub fn ascii_printable() -> BoxGen<char> {
54+
chars_from_u32_range(32, 126)
55+
}
56+
57+
/// Any ASCII character, including both printable and non-printable characters,
58+
/// unicode values 0..=127.
59+
pub fn ascii() -> BoxGen<char> {
60+
chars_from_u32_range(0, 127)
61+
}

0 commit comments

Comments
 (0)