Crates.io | clocks |
lib.rs | clocks |
version | 0.0.1 |
source | src |
created_at | 2022-09-18 20:30:14.716938 |
updated_at | 2022-09-18 20:30:14.716938 |
description | A testable source of time |
homepage | |
repository | https://github.com/eds-collabora/clocks |
max_upload_size | |
id | 668792 |
size | 33,343 |
This crate provides a
Clock
type that can be used as a source of time. By using a clock in your
code, you make it possible to replace the time source during
testing. This can allow you to more simply test things like expiry
times.
Example usage:
use chrono::{DateTime, Utc};
use clocks::Clock;
struct Prod {
clock: Clock<Utc>,
}
impl Prod {
pub fn new() -> Self {
Self::new_with_clock(Clock::new())
}
pub fn new_with_clock(clock: Clock<Utc>) -> Self {
Self { clock }
}
pub fn get_time(&self) -> String {
self.clock.now().to_rfc2822()
}
}
fn main() {
let p = Prod::new();
println!("{}", p.get_time());
}
#[test]
fn test_basic() {
let start = DateTime::parse_from_rfc2822("Sun, 18 Sep 2022 20:53:00 GMT")
.unwrap()
.with_timezone(&Utc);
let c = Clock::new_fake(start.clone());
let p = Prod::new_with_clock(c);
assert_eq!(p.get_time(), "Sun, 18 Sep 2022 20:53:00 +0000");
}
This code is made available under either an Apache-2.0 or an MIT license.