Skip to content

Commit

Permalink
add tests for timer, add comments
Browse files Browse the repository at this point in the history
  • Loading branch information
cdsupina committed Jul 10, 2021
1 parent e6e7740 commit d0614f8
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 4 deletions.
2 changes: 1 addition & 1 deletion thetawave_lib/src/spawn/systems/despawn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ mod test {
world.insert(EffectReturn(entity));
})
.with_assertion(|world| {
let entity = world.read_resource::<EffectReturn<Entity>>().0.clone();
let entity = world.read_resource::<EffectReturn<Entity>>().0;
world.maintain();
assert!(!world.is_alive(entity));
})
Expand Down
95 changes: 92 additions & 3 deletions thetawave_lib/src/tools/timer.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,30 @@
use serde::{Deserialize, Serialize};

/// Counts down a set amount of time then resets
/// Counts down timer and resets
#[derive(Serialize, Deserialize, Debug, Clone, Default)]
pub struct Timer {
/// Amount of time between resets
period: f32,
/// Remaining time until reset
countdown: f32,
}

impl Timer {
/// Creates a new instance of Timer
/// Sets both the period and countdown to given value
/// Create a new instance of `Timer`
///
/// # Parameters
///
/// `period` - An `f32` length of the period
///
/// # Returns
///
/// A new instance of `Timer` with `period` and `countdown` set to the given period
///
/// # Panics
///
/// Panics when given period is less than or equal to zero.
pub fn new(period: f32) -> Self {
Self::check_period(period);
Timer {
period,
countdown: period,
Expand Down Expand Up @@ -41,11 +55,86 @@ impl Timer {
}

pub fn set_period(&mut self, period: f32) {
Self::check_period(period);
self.period = period;
if self.countdown > self.period {
self.reset()
}
}

/// Returns the difference between the period and countdown
pub fn get_time_left(&self) -> f32 {
self.period - self.countdown
}

fn check_period(period: f32) {
if period <= 0.0 {
panic!("Period of timer must be greater than zero.")
}
}
}

#[cfg(test)]
mod test {
use super::*;

#[test]
fn false_on_update_greater_than_zero() {
let mut timer = Timer::new(2.0);
assert!(!timer.update(1.0))
}

#[test]
fn true_on_update_equal_to_zero() {
let mut timer = Timer::new(1.0);
assert!(timer.update(1.0))
}

#[test]
fn true_on_update_less_than_zero() {
let mut timer = Timer::new(0.5);
assert!(timer.update(1.0))
}

#[test]
fn zero_time_left_on_reset() {
let mut timer = Timer::new(5.0);
timer.update(1.0);
timer.reset();
assert!(timer.get_time_left() == 0.0)
}

#[test]
fn zero_time_left_on_set_period_when_less_than_countdown() {
let mut timer = Timer::new(5.0);
timer.update(1.0);
timer.set_period(3.0);
assert!(timer.get_time_left() == 0.0);
}

#[test]
#[should_panic]
fn panic_on_new_with_zero_period() {
Timer::new(0.0);
}

#[test]
#[should_panic]
fn panic_on_new_with_negative_period() {
Timer::new(-1.0);
}

#[test]
#[should_panic]
fn panic_on_set_period_to_zero() {
let mut timer = Timer::new(1.0);
timer.set_period(0.0);
}

#[test]
#[should_panic]
fn panic_on_set_period_to_negative() {
let mut timer = Timer::new(1.0);
timer.set_period(-1.0);
}
}

0 comments on commit d0614f8

Please sign in to comment.