epoch_forge/lib_efo/tests/entity_index.rs

51 lines
1.6 KiB
Rust

use lib_efo::{
entity::Entity,
epoch::{EntityIndex, EpochInfo},
stats::EntityType,
};
use uuid::Uuid;
fn entity(id: u128, name: &str, entity_type: EntityType) -> Entity {
let mut entity = Entity::default();
entity.id = Uuid::from_u128(id);
entity.name = name.into();
entity.entity_type = entity_type;
entity
}
#[test]
fn index_resolves_ids_and_case_insensitive_names_without_serialized_state() {
let mut world = EpochInfo::default();
world
.characters
.push(entity(1, " Aria ", EntityType::Character));
world.towns.push(entity(2, "Haven", EntityType::Town));
let index = EntityIndex::build(&world);
let aria = index.location_by_id(Uuid::from_u128(1)).expect("UUID");
assert_eq!(index.resolve(&world, aria).expect("entity").name, " Aria ");
assert_eq!(index.locations_by_name("ARIA"), &[aria]);
assert_eq!(index.len(), 2);
assert!(index.duplicate_ids().is_empty());
let serialized = serde_json::to_value(&world).expect("serialize world");
assert!(serialized.get("by_id").is_none());
assert!(serialized.get("by_name").is_none());
}
#[test]
fn index_reports_duplicate_ids_and_ambiguous_names() {
let mut world = EpochInfo::default();
world.events.push(entity(9, "The Fall", EntityType::Event));
world
.calamities
.push(entity(9, "the fall", EntityType::Calamity));
let index = EntityIndex::build(&world);
assert_eq!(index.len(), 1);
assert_eq!(index.locations_by_name("the fall").len(), 2);
assert_eq!(index.duplicate_ids(), &HashSet::from([Uuid::from_u128(9)]));
}
use std::collections::HashSet;