-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbasic_usage.rs
152 lines (137 loc) · 4.61 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
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_to_zero())
.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_to_zero())
.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_to_zero())
.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);
});
}
/// Assert for equality prints out expected and actual values when failing.
/// Things to note:
/// 1. The first argument in `[monkey_test::ConfAndGen::assert_eq]` is the
/// expected value and the second argument is the actual value tested.
/// This may not be the usual rust `assert_eq!` macro convention, but is
/// common in other test frameworks, like
/// [in JUnit](https://junit.org/junit5/docs/5.0.1/api/org/junit/jupiter/api/Assertions.html).
/// 2. When property is failing, all shrinked values are kept in the same range
/// as given in the generator, namely 10 or larger. That is why 11 is the
/// smallest failure.
#[test]
#[should_panic(expected = "Monkey test property failed!\n\
Failure: 11\n\
Reason: Actual value should equal expected 11, but got 10.")]
fn can_assert_eq() {
monkey_test()
.with_generator(gen::u32::ranged(10..))
.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));
}
/// Adding filter to generator, also reuses the filtering in shrinking.
#[test]
fn filtering_of_generated_values() {
monkey_test()
.with_generator(
gen::u8::any()
// only odd numbers
.filter(|e| e % 2 == 1)
// only numbers equal or greater to 100
.filter(|&e| e >= 100u8),
)
.test_true(|_example| false)
.assert_minimum_failure(101);
}