Add tests for runner

This commit is contained in:
Nicolas 2022-04-09 17:49:21 +02:00
parent f860e190f6
commit 1dcc712983
2 changed files with 41 additions and 1 deletions

View File

@ -3,6 +3,9 @@ use crate::sampler::{Sample, Sampler};
use crate::surface::Surface;
use crate::Vector;
use mockall::automock;
#[automock]
pub trait Renderer {
fn render(&mut self, step: f32);
fn present(&mut self) -> Result<(), Error>;

View File

@ -19,7 +19,7 @@ impl<T1: Timer, T2: Renderer> Runner<T1, T2> {
}
pub fn run(mut self) -> Result<(), Error> {
for i in 0..self.ticks {
for i in 0..=self.ticks {
let step = i as f32 / self.ticks as f32;
self.renderer.render(step);
@ -28,4 +28,41 @@ impl<T1: Timer, T2: Renderer> Runner<T1, T2> {
}
self.renderer.finish()
}
}
#[cfg(test)]
mod test {
use std::time::Duration;
use mockall::predicate::*;
use mockall::Sequence;
use crate::timer::MockTimer;
use crate::render::MockRenderer;
use super::*;
#[test]
fn run() {
let mut timer = MockTimer::new();
let mut renderer = MockRenderer::new();
let seq = &mut Sequence::new();
timer.expect_delay().return_const(Duration::from_secs(2));
renderer.expect_render().once().with(eq(0.0)).in_sequence(seq).return_const(());
renderer.expect_present().once().in_sequence(seq).returning(|| Ok(()));
timer.expect_sleep().once().in_sequence(seq).return_const(());
renderer.expect_render().once().with(eq(0.5)).in_sequence(seq).return_const(());
renderer.expect_present().once().in_sequence(seq).returning(|| Ok(()));
timer.expect_sleep().once().in_sequence(seq).return_const(());
renderer.expect_render().once().with(eq(1.0)).in_sequence(seq).return_const(());
renderer.expect_present().once().in_sequence(seq).returning(|| Ok(()));
timer.expect_sleep().once().in_sequence(seq).return_const(());
renderer.expect_finish().once().in_sequence(seq).returning(|| Ok(()));
let runner = Runner::new(Duration::from_secs(4), timer, renderer);
runner.run().unwrap();
}
}