-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: add unit tests for rolling file (#36)
Co-authored-by: tison <[email protected]>
- Loading branch information
1 parent
d2db4c7
commit ef83ed2
Showing
6 changed files
with
408 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
// Copyright 2024 tison <[email protected]> | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
use time::OffsetDateTime; | ||
|
||
#[derive(Debug)] | ||
pub enum Clock { | ||
DefaultClock, | ||
#[cfg(test)] | ||
ManualClock(ManualClock), | ||
} | ||
|
||
impl Clock { | ||
pub fn now(&self) -> OffsetDateTime { | ||
match self { | ||
Clock::DefaultClock => OffsetDateTime::now_utc(), | ||
#[cfg(test)] | ||
Clock::ManualClock(clock) => clock.now(), | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
pub fn set_now(&mut self, new_time: OffsetDateTime) { | ||
if let Clock::ManualClock(clock) = self { | ||
clock.set_now(new_time); | ||
} | ||
} | ||
} | ||
|
||
/// The time could be reset. | ||
#[derive(Debug)] | ||
#[cfg(test)] | ||
pub struct ManualClock { | ||
fixed_time: OffsetDateTime, | ||
} | ||
|
||
#[cfg(test)] | ||
impl ManualClock { | ||
pub fn new(fixed_time: OffsetDateTime) -> ManualClock { | ||
ManualClock { fixed_time } | ||
} | ||
|
||
fn now(&self) -> OffsetDateTime { | ||
self.fixed_time | ||
} | ||
|
||
pub fn set_now(&mut self, new_time: OffsetDateTime) { | ||
self.fixed_time = new_time; | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use time::macros::datetime; | ||
|
||
use super::*; | ||
|
||
#[test] | ||
fn test_manual_clock_adjusting() { | ||
let mut clock = ManualClock { | ||
fixed_time: datetime!(2023-01-01 12:00:00 UTC), | ||
}; | ||
assert_eq!(clock.now(), datetime!(2023-01-01 12:00:00 UTC)); | ||
|
||
clock.set_now(datetime!(2024-01-01 12:00:00 UTC)); | ||
assert_eq!(clock.now(), datetime!(2024-01-01 12:00:00 UTC)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.