From e371f8a183bde6740663987316a5737f50609ec3 Mon Sep 17 00:00:00 2001 From: Sebastien Rousseau Date: Thu, 9 May 2024 23:25:44 +0100 Subject: [PATCH] test(cmn): :white_check_mark: added the following additional tests: `test_get_value`, `test_is_valid` --- src/constants.rs | 40 +++++++++++++++---------- tests/test_constants.rs | 65 ++++++++++++++++++++++++++++++++++++++++- 2 files changed, 89 insertions(+), 16 deletions(-) diff --git a/src/constants.rs b/src/constants.rs index 224bbcd..34e8fee 100644 --- a/src/constants.rs +++ b/src/constants.rs @@ -57,20 +57,30 @@ impl Constants { /// ``` pub fn get_value(&self, name: &str) -> Option { if let Some(constant) = self.constant(name) { - if let Ok(float_value) = constant.value.parse::() { - Some(ConstantValue::Float(float_value)) - } else if let Ok(u32_value) = constant.value.parse::() - { - Some(ConstantValue::U32(u32_value)) - } else if let Ok(usize_value) = - constant.value.parse::() - { - Some(ConstantValue::Usize(usize_value)) - } else if let Some(char_array) = Self::get_char_array(name) - { - Some(ConstantValue::CharArray(char_array)) - } else { - Some(ConstantValue::String(constant.value.clone())) + match name { + "HASH_COST" => { + constant.value.parse().map(ConstantValue::U32).ok() + } + "HASH_LENGTH" => constant + .value + .parse() + .map(ConstantValue::Usize) + .ok(), + _ => { + if let Ok(float_value) = + constant.value.parse::() + { + Some(ConstantValue::Float(float_value)) + } else if let Some(char_array) = + Self::get_char_array(name) + { + Some(ConstantValue::CharArray(char_array)) + } else { + Some(ConstantValue::String( + constant.value.clone(), + )) + } + } } } else { None @@ -190,7 +200,7 @@ impl Constants { }, Constant { name: "HASH_COST", - value: HASH_COST.to_string(), + value: HASH_COST.to_string(), // Remove this line }, Constant { name: "HASH_LENGTH", diff --git a/tests/test_constants.rs b/tests/test_constants.rs index 94664d0..e6cd632 100644 --- a/tests/test_constants.rs +++ b/tests/test_constants.rs @@ -3,7 +3,7 @@ #[cfg(test)] mod tests { - use cmn::constants::Constants; + use cmn::constants::{ConstantValue, Constants}; #[test] fn test_constant() { @@ -53,16 +53,79 @@ mod tests { assert!(names.contains(&"VACUUM_PERMEABILITY")); assert!(names.contains(&"VACUUM_PERMITTIVITY")); } + #[test] fn test_new() { let new_constant = Constants::new(); let constants = new_constant.constants(); assert!(!constants.is_empty()); } + #[test] fn test_default() { let default_constant = Constants::default(); let constants = default_constant.constants(); assert!(!constants.is_empty()); } + + #[test] + fn test_get_value() { + let constants = Constants::new(); + + // Test getting a float value + let value = constants.get_value("EULER"); + assert!(value.is_some()); + if let Some(ConstantValue::Float(float_value)) = value { + assert!((float_value - std::f64::consts::E).abs() < 1e-10); + } else { + panic!("Expected a float value"); + } + + // Test getting a string value + let value = constants.get_value("HASH_ALGORITHM"); + assert!(value.is_some()); + if let Some(ConstantValue::String(string_value)) = value { + assert_eq!(string_value, "Blake3"); + } else { + panic!("Expected a string value"); + } + + // Test getting a u32 value + let value = constants.get_value("HASH_COST"); + assert!(value.is_some()); + if let Some(ConstantValue::U32(u32_value)) = value { + assert_eq!(u32_value, 8); + } else { + panic!("Expected a u32 value"); + } + + // Test getting a usize value + let value = constants.get_value("HASH_LENGTH"); + assert!(value.is_some()); + if let Some(ConstantValue::Usize(usize_value)) = value { + assert_eq!(usize_value, 32); + } else { + panic!("Expected a usize value"); + } + + // Test getting a char array value + let value = constants.get_value("SPECIAL_CHARS"); + assert!(value.is_some()); + if let Some(ConstantValue::CharArray(char_array)) = value { + assert_eq!( + char_array, + &[ + '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', + '_', '+', '=', '[', ']', '{', '}', '|', ';', ':', + '"', '<', '>', ',', '.', '?', '/', '~', '`' + ] + ); + } else { + panic!("Expected a char array value"); + } + + // Test getting a non-existent constant value + let value = constants.get_value("NON_EXISTENT"); + assert!(value.is_none()); + } }