gamelog events unit tests

This commit is contained in:
Llywelwyn 2023-09-20 23:21:38 +01:00
parent 58e4742f12
commit 66013667d8
3 changed files with 48 additions and 3 deletions

View file

@ -5,7 +5,7 @@ use crate::data::names::*;
lazy_static! { lazy_static! {
/// A count of each event that has happened over the run. i.e. "turns", "descended", "ascended" /// A count of each event that has happened over the run. i.e. "turns", "descended", "ascended"
static ref EVENT_COUNTER: Mutex<HashMap<String, i32>> = Mutex::new(HashMap::new()); pub static ref EVENT_COUNTER: Mutex<HashMap<String, i32>> = Mutex::new(HashMap::new());
// A record of events that happened on a given turn. i.e. "Advanced to level 2". // A record of events that happened on a given turn. i.e. "Advanced to level 2".
pub static ref EVENTS: Mutex<HashMap<u32, Vec<String>>> = Mutex::new(HashMap::new()); pub static ref EVENTS: Mutex<HashMap<u32, Vec<String>>> = Mutex::new(HashMap::new());
// A record of floors visited, and monsters killed. Used to determine if an event is significant. // A record of floors visited, and monsters killed. Used to determine if an event is significant.
@ -41,8 +41,9 @@ pub fn restore_events(events: HashMap<u32, Vec<String>>) {
} }
/// Wipes all events - for starting a new game. /// Wipes all events - for starting a new game.
pub fn clear_events() { pub fn clear_events() {
EVENT_COUNTER.lock().unwrap().clear(); let (mut events, mut event_counts) = (EVENTS.lock().unwrap(), EVENT_COUNTER.lock().unwrap());
EVENTS.lock().unwrap().clear(); events.clear();
event_counts.clear();
} }
#[allow(unused_mut)] #[allow(unused_mut)]

43
tests/gamelog_test.rs Normal file
View file

@ -0,0 +1,43 @@
// tests/gamelog_test.rs
use rust_rl::gamelog::*;
use rust_rl::data::events::*;
use lazy_static::lazy_static;
use std::sync::Mutex;
// To ensure this test module uses a single thread.
lazy_static! {
static ref SINGLE_THREAD: Mutex<()> = Mutex::new(());
}
#[test]
fn recording_event() {
let _lock = SINGLE_THREAD.lock();
clear_events();
record_event(EVENT::Turn(1));
record_event(EVENT::Turn(0));
record_event(EVENT::Turn(-1));
record_event(EVENT::Killed("mob".to_string()));
}
#[test]
fn getting_event_count() {
let _lock = SINGLE_THREAD.lock();
clear_events();
record_event(EVENT::Turn(1));
assert_eq!(get_event_count(EVENT::COUNT_TURN), 1);
record_event(EVENT::Turn(3));
assert_eq!(get_event_count(EVENT::COUNT_TURN), 4);
clear_events();
assert_eq!(get_event_count(EVENT::COUNT_TURN), 0);
}
#[test]
fn cloning_events() {
let _lock = SINGLE_THREAD.lock();
clear_events();
record_event(EVENT::Level(1));
record_event(EVENT::Turn(5));
record_event(EVENT::Identified("item".to_string()));
let cloned_events = clone_events();
assert_eq!(EVENTS.lock().unwrap().clone(), cloned_events);
}

View file

@ -1,2 +1,3 @@
// tests/mod.rs // tests/mod.rs
mod map_test; mod map_test;
mod gamelog_test;