Skip to content

Commit 8313f29

Browse files
committed
remove instants generator
1 parent c0da2e6 commit 8313f29

File tree

4 files changed

+4
-80
lines changed

4 files changed

+4
-80
lines changed

RELEASE.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
RELEASE_TYPE: patch
22

3-
Add generators for Duration and Instant
3+
Add generator for Duration

src/generators/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ pub use strings::{
5353
IpAddressGenerator, RegexGenerator, TextGenerator, TimeGenerator, UrlGenerator, binary, dates,
5454
datetimes, domains, emails, from_regex, ip_addresses, text, times, urls,
5555
};
56-
pub use time::{DurationGenerator, InstantGenerator, durations, instants};
56+
pub use time::{DurationGenerator, durations};
5757
#[doc(hidden)]
5858
pub use tuples::{
5959
tuples0, tuples1, tuples2, tuples3, tuples4, tuples5, tuples6, tuples7, tuples8, tuples9,

src/generators/time.rs

Lines changed: 1 addition & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use super::{BasicGenerator, Generator, TestCase};
22
use crate::cbor_utils::cbor_map;
3-
use std::time::{Duration, Instant};
3+
use std::time::Duration;
44

55
/// Generator for [`Duration`] values. Created by [`durations()`].
66
///
@@ -77,64 +77,6 @@ pub fn durations() -> DurationGenerator {
7777
}
7878
}
7979

80-
/// Generator for [`Instant`] values. Created by [`instants()`].
81-
///
82-
/// Generates instants by adding a random [`Duration`] offset to a fixed base
83-
/// instant captured when the generator is created. The offsets are deterministic
84-
/// (controlled by the test engine), while the base varies between runs.
85-
pub struct InstantGenerator {
86-
base: Instant,
87-
max_offset_nanos: u64,
88-
}
89-
90-
impl InstantGenerator {
91-
/// Set the maximum offset from the base instant (inclusive).
92-
pub fn max_offset(mut self, max: Duration) -> Self {
93-
self.max_offset_nanos = duration_to_nanos(max);
94-
self
95-
}
96-
}
97-
98-
impl Generator<Instant> for InstantGenerator {
99-
fn do_draw(&self, tc: &TestCase) -> Instant {
100-
let schema = cbor_map! {
101-
"type" => "integer",
102-
"min_value" => 0u64,
103-
"max_value" => self.max_offset_nanos
104-
};
105-
let nanos: u64 = super::generate_from_schema(tc, &schema);
106-
self.base + Duration::from_nanos(nanos)
107-
}
108-
}
109-
110-
/// Generate [`Instant`] values.
111-
///
112-
/// Produces instants offset from a fixed base (`Instant::now()` at call time)
113-
/// by a random duration. The default maximum offset is one hour. Use
114-
/// `max_offset` to change it.
115-
///
116-
/// The base is captured once when `instants()` is called, so all generated
117-
/// values within a test share the same reference point. The offsets are
118-
/// deterministic and shrinkable.
119-
///
120-
/// # Example
121-
///
122-
/// ```no_run
123-
/// use std::time::Duration;
124-
///
125-
/// #[hegel::test]
126-
/// fn my_test(tc: hegel::TestCase) {
127-
/// let i = tc.draw(hegel::generators::instants()
128-
/// .max_offset(Duration::from_secs(3600)));
129-
/// }
130-
/// ```
131-
pub fn instants() -> InstantGenerator {
132-
InstantGenerator {
133-
base: Instant::now(),
134-
max_offset_nanos: 3_600_000_000_000,
135-
}
136-
}
137-
13880
fn duration_to_nanos(d: Duration) -> u64 {
13981
d.as_nanos().try_into().unwrap_or(u64::MAX)
14082
}

tests/test_time.rs

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ mod common;
22

33
use common::utils::assert_all_examples;
44
use hegel::generators;
5-
use std::time::{Duration, Instant};
5+
use std::time::Duration;
66

77
#[test]
88
fn test_durations_default() {
@@ -18,21 +18,3 @@ fn test_durations_bounded() {
1818
move |d| *d >= min && *d <= max,
1919
);
2020
}
21-
22-
#[test]
23-
fn test_instants_default() {
24-
let before = Instant::now();
25-
let max_offset = Duration::from_secs(3600);
26-
assert_all_examples(generators::instants(), move |i| {
27-
*i >= before && *i <= Instant::now() + max_offset
28-
});
29-
}
30-
31-
#[test]
32-
fn test_instants_bounded() {
33-
let max_offset = Duration::from_secs(10);
34-
let before = Instant::now();
35-
assert_all_examples(generators::instants().max_offset(max_offset), move |i| {
36-
*i >= before && *i <= Instant::now() + max_offset
37-
});
38-
}

0 commit comments

Comments
 (0)