// tests/mod.rs - or put this inside your crate's tests module #![allow(unused_imports)] use std::str::FromStr; use crate::entity::{EffectApplication, EffectBounds, Entity, EntityDuration, EntityIdentifier}; use crate::epoch::epoch_traits::Diffable; use crate::error::EpochError; use crate::stats::{self, EntityType, MetaStats, Position, StatPair, TownId}; use std::io::Write; use super::*; use crate::Displayable; use insta::assert_snapshot; use uuid::Uuid; #[test] fn entity_duration_parses_permanent_and_done() { assert_eq!( EntityDuration::from_str("permanent").unwrap(), EntityDuration::Permanent ); assert_eq!( EntityDuration::from_str("PERMANENT").unwrap(), EntityDuration::Permanent ); assert_eq!( EntityDuration::from_str("perm").unwrap(), EntityDuration::Permanent ); assert_eq!( EntityDuration::from_str("done").unwrap(), EntityDuration::Done ); } #[test] fn entity_duration_parses_numeric_forms() { // bare number assert_eq!( EntityDuration::from_str("5").unwrap(), EntityDuration::Duration(5) ); // with leading zeros assert_eq!( EntityDuration::from_str("0005").unwrap(), EntityDuration::Duration(5) ); // duration: prefix assert_eq!( EntityDuration::from_str("duration:10").unwrap(), EntityDuration::Duration(10) ); // case-insensitive prefix assert_eq!( EntityDuration::from_str("DurAtIoN:20").unwrap(), EntityDuration::Duration(20) ); // zero value assert_eq!( EntityDuration::from_str("duration:0").unwrap(), EntityDuration::Duration(0) ); } #[test] fn entity_duration_parses_with_surrounding_whitespace() { assert_eq!( EntityDuration::from_str(" 7 ").unwrap(), EntityDuration::Duration(7) ); assert_eq!( EntityDuration::from_str(" PERMANENT ").unwrap(), EntityDuration::Permanent ); } #[test] fn entity_duration_returns_error_on_invalid_inputs() { // non-numeric bare string let err = EntityDuration::from_str("10a").unwrap_err(); match err { EpochError::ParsingDuration(msg, val) => { assert!(!msg.is_empty()); assert_eq!(val, "10a"); } _ => panic!("Expected ParsingDuration error"), } // malformed duration: form let err = EntityDuration::from_str("duration:invalid").unwrap_err(); match err { EpochError::ParsingDuration(msg, val) => { assert!(!msg.is_empty()); assert_eq!(val, "invalid"); } _ => panic!("Expected ParsingDuration error"), } // trailing spaces after the duration prefix - the numeric parser fails let err = EntityDuration::from_str("duration: 5").unwrap_err(); match err { EpochError::ParsingDuration(msg, val) => { assert_eq!(msg, "Invalid duration value"); assert_eq!(val, " 5"); } _ => panic!("Expected ParsingDuration error"), } // empty string let err = EntityDuration::from_str("").unwrap_err(); match err { EpochError::ParsingDuration(msg, val) => { assert!(!msg.is_empty()); assert_eq!(val, ""); } _ => panic!("Expected ParsingDuration error"), } } #[test] fn effect_bounds_parses_colon_and_dash_separated_values() { // colon let eb = EffectBounds::from_str("1:5").unwrap(); assert_eq!(eb.min, 1); assert_eq!(eb.max, 5); // dash let eb = EffectBounds::from_str("2-10").unwrap(); assert_eq!(eb.min, 2); assert_eq!(eb.max, 10); } #[test] fn effect_bounds_parses_with_whitespace_and_leading_zeros() { let eb = EffectBounds::from_str(" 01 : 05 ").unwrap(); assert_eq!(eb.min, 1); assert_eq!(eb.max, 5); } #[test] fn effect_bounds_returns_error_on_invalid_inputs() { // too many parts let err = EffectBounds::from_str("1:5:7").unwrap_err(); match err { EpochError::ParsingBounds(msg, val) => { assert_eq!(msg, "Invalid EffectBounds format"); assert_eq!(val, "1:5:7"); } _ => panic!("Expected ParsingBounds error"), } // missing one side let err = EffectBounds::from_str("1:").unwrap_err(); match err { EpochError::ParsingBounds(msg, val) => { assert_eq!(msg, "Could not parse number"); assert_eq!(val, "1:"); } _ => panic!("Expected ParsingBounds error"), } // non-numeric part let err = EffectBounds::from_str("a:5").unwrap_err(); match err { EpochError::ParsingBounds(msg, val) => { assert_eq!(msg, "Could not parse number"); assert_eq!(val, "a:5"); } _ => panic!("Expected ParsingBounds error"), } // no delimiter let err = EffectBounds::from_str("5").unwrap_err(); match err { EpochError::ParsingBounds(msg, val) => { assert_eq!(msg, "Invalid EffectBounds"); assert_eq!(val, "5"); } _ => panic!("Expected ParsingBounds error"), } } #[test] fn entity_identifier_parses_id_and_name() { // id let id = EntityIdentifier::from_str("@1234-5678-9012-3456-7890").unwrap(); match id { EntityIdentifier::Id(uuid) => assert_eq!(uuid, "1234-5678-9012-3456-7890"), _ => panic!("Expected Id variant"), } // name let name = EntityIdentifier::from_str("my_entity").unwrap(); match name { EntityIdentifier::Name(n) => assert_eq!(n, "my_entity"), _ => panic!("Expected Name variant"), } // empty name is still a valid Name let empty = EntityIdentifier::from_str("").unwrap(); match empty { EntityIdentifier::Name(n) => assert_eq!(n, ""), _ => panic!("Expected Name variant"), } } #[test] fn entity_identifier_errors_on_invalid_id() { // prefix with nothing after @ let err = EntityIdentifier::from_str("@").unwrap_err(); match err { EpochError::EmptyIdentifier => {} _ => panic!("Expected EmptyIdentifier error"), } // // prefix followed by whitespace only // let err = EntityIdentifier::from_str("@ ").unwrap_err(); // match err { // EpochError::EmptyIdentifier => {} // _ => panic!("Expected EmptyIdentifier error"), // } } fn init_snapshot_env() { colored::control::set_override(false); } fn full_entity() -> Entity { Entity { id: Uuid::from_u128_le(12), name: "Hero".into(), entity_type: EntityType::Character, metadata: MetaStats { capital: Some(stats::TownId(Uuid::from_u128(121))), governor: Some(stats::CharacterId(11)), prestige: stats::GenericStat(3), position: Position::from((2, 4)), faction: Some(crate::stats::FactionId(2)), links: vec![TownId(Uuid::from_u128(121))], }, duration: EntityDuration::Duration(10), effect_nature: EffectApplication::Recurring, effect_bounds: EffectBounds::from((1, 5)), stats: stats::OverallStats::default(), temp_stats: Some(stats::OverallStats::default()), description: String::from("Bob"), } } #[test] fn effect_bound_testing() { let EffectBounds { min, max } = EffectBounds::from(0..1); let EffectBounds { min: min2, max: max2, } = EffectBounds::from((0, 1)); assert_eq!(min, min2); assert_eq!(max, max2); } #[test] fn snapshot_full_oneline_display() { init_snapshot_env(); let e = full_entity(); assert_snapshot!(e.oneline_display()); assert_snapshot!(e.to_pretty_tree()); } #[test] fn snapshot_minimal_oneline_display() { init_snapshot_env(); let e = Entity::default(); assert_snapshot!(e.oneline_display()); assert_snapshot!(e.to_pretty_tree()); } #[test] fn snapshot_print_full_output() { init_snapshot_env(); let e = full_entity(); // Capture stdout into a buffer let buffer = Vec::new(); { // let stdout = std::io::stdout(); // let mut handle = stdout.lock(); // // Temporarily swap the real stdout for our buffer. // let original = std::mem::replace(&mut handle, &mut buffer); // Run the method we want to snapshot. e.print_full(); // Restore the original stdout. // std::mem::replace(&mut handle, original); } // Convert buffer → UTF-8 string and snapshot it. let output = String::from_utf8_lossy(&buffer); assert_snapshot!(output); } #[test] fn snapshot_full_stats_no_temp() { init_snapshot_env(); let mut e = full_entity(); e.temp_stats = None; let table = e.full_stats_to_table().expect("table generation failed"); assert_snapshot!(format!("{}", table)); let table = e.stats_to_table().expect("table generation failed"); assert_snapshot!(format!("{}", table)); } #[test] fn snapshot_full_stats_with_temp() { init_snapshot_env(); let mut e = full_entity(); e.temp_stats = Some(OverallStats::default()); let table = e.full_stats_to_table().expect("table generation failed"); assert_snapshot!(format!("{}", table)); let table = e.stats_to_table().expect("table generation failed"); assert_snapshot!(format!("{}", table)); } /// ------------------------------------------------------------------ /// 1. Helpers - `overwrite_stat` & `add_to_stat` /// ------------------------------------------------------------------ #[test] fn snapshot_overwrite_stat() { init_snapshot_env(); let mut entity = Entity::default(); let stats = OverallStats::linear(0); entity.stats = stats; entity.change_stat(StatPair { stat: stats::ListStats::Population, value: stats::GenericStat(33), }); assert_snapshot!(format!("{:#?}", entity.stats)); } // #[test] // fn snapshot_add_to_stat_saturating_add() { // init_snapshot_env(); // let mut stats = stats_from_values(u32::MAX as i32 - 1, 20, 30); // let pair = pair("health", 5); // add_to_stat(&mut stats, pair); // // The saturated addition should clamp to `u32::MAX`. // assert_snapshot!(format!("{}\n", stats)); // } /// ------------------------------------------------------------------ /// 2. `Diffable` - `Entity::delta` /// ------------------------------------------------------------------ #[test] fn snapshot_entity_delta() { init_snapshot_env(); // Build two slightly different entities let e1 = Entity { id: uuid::Uuid::nil(), name: "Alice".into(), stats: OverallStats::linear(2), temp_stats: None, metadata: MetaStats { position: Position::from((44, 666)), capital: Some(stats::TownId(Uuid::from_u128(124))), governor: Some(stats::CharacterId(2000)), faction: Some(stats::FactionId(44)), links: vec![TownId(Uuid::from_u128(141))], prestige: stats::GenericStat(3), }, entity_type: EntityType::Monster, duration: EntityDuration::Permanent, effect_nature: EffectApplication::Recurring, effect_bounds: EffectBounds::from(0..33), description: "Ello".to_string(), }; let e2 = Entity { id: uuid::Uuid::nil(), name: "Bob".into(), stats: OverallStats::linear(44), temp_stats: Some(OverallStats::linear(4)), metadata: MetaStats { position: Position::from_str("33,4").unwrap(), capital: Some(stats::TownId(Uuid::from_u128(121))), governor: Some(stats::CharacterId(1500)), faction: Some(stats::FactionId(5)), links: vec![TownId(Uuid::from_u128(121))], prestige: stats::GenericStat(5), }, entity_type: EntityType::Town, duration: EntityDuration::Permanent, effect_nature: EffectApplication::Recurring, effect_bounds: EffectBounds::from(0..3), description: "Ilo".to_string(), }; // Compute the delta let mut delta = e1.delta(&e2); let mut delta2 = e2.delta(&e2); let mut delta3 = e2.delta(&e1); delta.id = Uuid::nil(); delta2.id = Uuid::nil(); delta3.id = Uuid::nil(); assert_snapshot!(format!( "{:#?}{}", &delta, delta.full_stats_to_table().unwrap() )); assert_snapshot!(format!( "{:#?}{}", delta2, delta2.full_stats_to_table().unwrap() )); assert_snapshot!(format!( "{:#?}{}", delta3, delta3.full_stats_to_table().unwrap() )); } #[test] fn snapshot_option_stats_delta() { init_snapshot_env(); let a = OverallStats::linear(32); let b = OverallStats::linear(22); let opt_a = Some(a.clone()); let opt_b = Some(b.clone()); // `delta` is commutative for the purpose of the snapshot let delta = opt_a.delta(&opt_b); let delta2 = None.delta(&opt_b); let delta3 = opt_a.delta(&None); let delta4 = None::.delta(&None); assert_snapshot!(format!("{:#?}", delta)); assert_snapshot!(format!("{:#?}", delta2)); assert_snapshot!(format!("{:#?}", delta3)); assert_snapshot!(format!("{:#?}", delta4)); } #[test] fn snapshot_meta_stats_delta() { init_snapshot_env(); let meta1 = MetaStats { position: Position::from((1, 11)), capital: Some(stats::TownId(Uuid::from_u128(1000))), governor: Some(stats::CharacterId(500)), faction: Some(stats::FactionId(1)), links: vec![TownId(Uuid::from_u128(12))], prestige: stats::GenericStat(3), }; let meta2 = MetaStats { position: Position::from((44, 555)), capital: Some(stats::TownId(Uuid::from_u128(12))), governor: Some(stats::CharacterId(400)), faction: Some(stats::FactionId(33)), links: vec![TownId::from(Uuid::from_u128(42))], prestige: stats::GenericStat(1), }; let meta3 = MetaStats { position: Position::from((44, 555)), capital: None, governor: Some(stats::CharacterId(400)), faction: Some(stats::FactionId(33)), links: vec![TownId::from(Uuid::from_u128(2))], prestige: stats::GenericStat(1), }; let delta = meta1.delta(&meta2); let delta2 = meta2.delta(&meta3); let delta3 = meta3.delta(&meta3); assert_snapshot!(format!("{:#?}", delta)); assert_snapshot!(format!("{:#?}", delta2)); assert_snapshot!(format!("{:#?}", delta3)); } #[test] fn snapshot_delta_stat_positive() { init_snapshot_env(); let mut e = Entity::default(); e.delta_stat(StatPair::from_str("health:4").unwrap()); e.id = Uuid::nil(); assert_snapshot!(format!("{:#?}", e)); } #[test] fn snapshot_modify_duration() { init_snapshot_env(); let mut e = Entity::default(); e.modify_duration(EntityDuration::Duration(33)); e.id = Uuid::nil(); assert_snapshot!(format!("{:#?}", e)); } #[test] fn snapshot_modify_effect_application() { init_snapshot_env(); let mut e = Entity::default(); e.modify_effect_application(EffectApplication::PermanentEffectApplied); e.id = Uuid::nil(); assert_snapshot!(format!("{:#?}", e)); } #[test] fn snapshot_modify_effect_bounds() { init_snapshot_env(); let mut e = Entity::default(); e.modify_effect_bounds(EffectBounds::from(33..33)); e.id = Uuid::nil(); assert_snapshot!(format!("{:#?}", e)); } #[test] fn test_rng() { Entity::create_rng(&EntityType::Character, &crate::utils::RngLevel::Zero).unwrap(); Entity::create_rng(&EntityType::Player, &crate::utils::RngLevel::Low).unwrap(); Entity::create_rng(&EntityType::Town, &crate::utils::RngLevel::Med).unwrap(); Entity::create_rng(&EntityType::Monster, &crate::utils::RngLevel::High).unwrap(); Entity::create_rng(&EntityType::Blocker, &crate::utils::RngLevel::Legendary).unwrap(); Entity::create_rng(&EntityType::Event, &crate::utils::RngLevel::Mixed).unwrap(); Entity::create_rng(&EntityType::Event, &crate::utils::RngLevel::Flawed).unwrap(); }