Day 25: Test the halls
To keep a streak going on what was otherewise a busy day, lets add a basic test to Seance! I will be adding a test that ensures the program exits with a failure status code when the config file is misisng. I wrote the code to work entirely on pure operations, so testing should be straightforward, without the need for any sort of mocking as you’d find in a dynamic language.
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn exits_with_failure_when_config_is_missing() {
let mut world = VecWorld::new();
let mut agent = Agent::new(&mut world);
assert_eq!(world.actions.len(), 1);
let Action::FileRead(file_read_id, _path) = world.actions.as_slice()[0].clone() else {
panic!("unexpected actions: {:?}", &world.actions)
};
world.actions.clear();
agent.receive(Message::FileReadComplete(file_read_id, Err(io::ErrorKind::NotFound)), &mut world);
assert_eq!(world.actions.len(), 1);
assert!(matches!(world.actions[0], Action::ExitFailure(_)));
}
}
And this test passes! This was more involved than I would have liked. Notably, the error messages will not show the actual actions that were present if there was more than one, and I had to do a lot of manual slicing. I expect I can make this much nicer by building a new world implementation intended for testing.
And that’s all for now. Best to you and yours!