/
basic_usage.rs
127 lines (113 loc) · 3.46 KB
/
basic_usage.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
use monkey_test::*;
#[test]
fn add_up_to_overflow() {
monkey_test()
.with_generator(gen::u8::any())
.assert_true(|x| x == 255 || x + 1 > x);
}
#[test]
fn can_fail_with_details_when_using_check() {
let actual_result: MonkeyResult<u8> = monkey_test()
.with_seed(123456)
.with_generator(gen::fixed::sequence(&[1, 2, 3, 10, 20, 30]))
.with_shrinker(shrink::int())
.title("Less than thirteen")
.test_true(|x| x < 13);
assert_eq!(
actual_result,
MonkeyResult::MonkeyErr {
minimum_failure: 13,
original_failure: 20,
some_other_failures: vec!(15, 14),
success_count: 4,
shrink_count: 3,
seed: 123456,
title: Some("Less than thirteen".into()),
reason: "Expecting 'true' but got 'false'.".into(),
}
);
}
#[test]
#[should_panic(expected = "Monkey test property failed!\nFailure: 13")]
fn can_fail_with_panic_when_using_assert() {
monkey_test()
.with_seed(123456)
.with_generator(gen::fixed::sequence(&[1, 2, 3, 10, 20, 30]))
.with_shrinker(shrink::int())
.assert_true(|x| x < 13);
}
/// Can do the same as above by asserting minimum failing example
#[test]
fn can_assert_minimumfail_with_panic_when_using_assert() {
monkey_test()
.with_seed(123456)
.with_generator(gen::fixed::sequence(&[1, 2, 3, 10, 20, 30]))
.with_shrinker(shrink::int())
.test_true(|x| x < 15)
.assert_minimum_failure(15);
}
#[test]
#[should_panic(
expected = "Reason: Expecting no panic, but got panic \"attempt to divide by zero\""
)]
fn can_assert_that_there_is_no_panic_thrown() {
monkey_test()
.with_example_count(1_000)
.with_generator(gen::u32::any())
.assert_no_panic(|n| {
let _ = 1 / (n / u32::MAX);
});
}
#[test]
#[should_panic(
expected = "Reason: Actual value should equal expected 1, but got 0."
)]
fn can_assert_eq() {
monkey_test()
.with_generator(gen::u32::any())
.assert_eq(|n| n, |n| n / 2 * 2);
}
#[test]
#[should_panic(
expected = "Reason: Actual value should not equal expected 1, but got 1."
)]
fn can_assert_ne() {
monkey_test()
.with_example_count(1_000)
.with_generator(gen::u32::ranged(0..10_000))
.assert_ne(|n| (n + 1) / 2, |n| (n + 2) / 2);
}
#[test]
fn use_all_settings_available() {
monkey_test()
.with_example_count(1_000)
.with_seed(1234567890)
.with_generator(gen::u8::any())
.with_shrinker(shrink::none())
.assert_true(|x| x as u16 * x as u16 >= x as u16);
}
#[test]
fn pick_from_alternatives_evenly() {
monkey_test()
.with_generator(gen::pick_evenly(&["Apple", "Orange", "Banana"]))
.assert_true(|fruit| fruit.ends_with('e') || fruit.ends_with('a'));
}
#[test]
fn pick_from_alternatives_with_ratio() {
monkey_test()
.with_generator(gen::pick_with_ratio(&[
(1, "Apple"),
(2, "Orange"),
(55, "Banana"),
]))
.assert_true(|fruit| fruit.ends_with('e') || fruit.ends_with('a'));
}
#[test]
fn mix_from_alternative_generators_with_ratio() {
let evens = gen::pick_evenly(&[0, 2, 4, 6, 8]);
let odds = gen::pick_evenly(&[1, 3, 5, 7, 9]);
let mostly_evens = gen::mix_with_ratio(&[(93, evens), (7, odds)]);
monkey_test()
.with_generator(mostly_evens)
.assert_true(|number| (0..10).contains(&number));
}