get_count && tests

This commit is contained in:
Lewis Wynne 2025-03-11 13:35:43 +00:00
parent 3f679f2d54
commit 222e779da4
2 changed files with 32 additions and 6 deletions

View file

@ -2471,14 +2471,14 @@ fn def_classical() -> HashMap<String, bool> {
.collect(); .collect();
} }
fn all_classical() -> HashMap<String, bool> { pub fn all_classical() -> HashMap<String, bool> {
return def_classical() return def_classical()
.iter() .iter()
.map(|(k, _)| (k.to_string(), true)) .map(|(k, _)| (k.to_string(), true))
.collect(); .collect();
} }
fn no_classical() -> HashMap<String, bool> { pub fn no_classical() -> HashMap<String, bool> {
return def_classical() return def_classical()
.iter() .iter()
.map(|(k, _)| (k.to_string(), false)) .map(|(k, _)| (k.to_string(), false))
@ -2533,8 +2533,8 @@ impl Word {
} }
pub struct Engine { pub struct Engine {
classical_dict: HashMap<String, bool>, pub classical_dict: HashMap<String, bool>,
persistent_count: Option<i32>, pub persistent_count: Option<i32>,
mill_count: i32, mill_count: i32,
pl_sb_user_defined: Vec<Option<Word>>, pl_sb_user_defined: Vec<Option<Word>>,
pl_v_user_defined: Vec<Option<Word>>, pl_v_user_defined: Vec<Option<Word>>,
@ -2573,7 +2573,7 @@ impl Engine {
pub fn check_gender(&self) -> &String { &self.the_gender } pub fn check_gender(&self) -> &String { &self.the_gender }
fn get_count<T: Into<IntOrString>>(&self, count: Option<T>) -> i32 { pub fn get_count<T: Into<IntOrString>>(&self, count: Option<T>) -> i32 {
if count.is_none() { if count.is_none() {
if self.persistent_count.is_some() { if self.persistent_count.is_some() {
return self.persistent_count.unwrap(); return self.persistent_count.unwrap();

View file

@ -74,7 +74,7 @@ fn test_words() {
} }
#[test] #[test]
fn test_engine() { fn test_engine_gender() {
let mut e = Engine::new(); let mut e = Engine::new();
assert_eq!(e.check_gender(), "neuter"); assert_eq!(e.check_gender(), "neuter");
e.gender("masculine"); e.gender("masculine");
@ -83,3 +83,29 @@ fn test_engine() {
assert_ne!(e.check_gender(), "fff"); assert_ne!(e.check_gender(), "fff");
assert_eq!(e.check_gender(), "masculine"); assert_eq!(e.check_gender(), "masculine");
} }
#[test]
fn test_engine_get_count() {
let mut e = Engine::new();
// i32 Into<IntOrString>>
for i in -1e3 as i32..1e3 as i32 {
assert_eq!(e.get_count(Some(i)), i);
}
// None cases
assert_eq!(e.get_count::<i32>(None), 0);
assert_eq!(e.get_count::<String>(None), 0);
assert_eq!(e.get_count::<&str>(None), 0);
// Strings
assert_eq!(e.get_count(Some("a".to_string())), 1);
assert_eq!(e.get_count(Some("nil")), 2);
assert_eq!(e.get_count(Some("some")), 2);
e.classical_dict = all_classical();
assert_eq!(e.get_count(Some("nil")), 1);
e.persistent_count = Some(3);
assert_eq!(e.get_count::<i32>(None), 3);
}