Skip to content

Commit

Permalink
test(cmn): ✅ added the following additional tests: test_get_value, …
Browse files Browse the repository at this point in the history
…`test_is_valid`
  • Loading branch information
sebastienrousseau committed May 9, 2024
1 parent f63ff26 commit e371f8a
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 16 deletions.
40 changes: 25 additions & 15 deletions src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,20 +57,30 @@ impl Constants {
/// ```
pub fn get_value(&self, name: &str) -> Option<ConstantValue> {
if let Some(constant) = self.constant(name) {
if let Ok(float_value) = constant.value.parse::<f64>() {
Some(ConstantValue::Float(float_value))
} else if let Ok(u32_value) = constant.value.parse::<u32>()
{
Some(ConstantValue::U32(u32_value))
} else if let Ok(usize_value) =
constant.value.parse::<usize>()
{
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::<f64>()
{
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
Expand Down Expand Up @@ -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",
Expand Down
65 changes: 64 additions & 1 deletion tests/test_constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

#[cfg(test)]
mod tests {
use cmn::constants::Constants;
use cmn::constants::{ConstantValue, Constants};

#[test]
fn test_constant() {
Expand Down Expand Up @@ -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());
}
}

0 comments on commit e371f8a

Please sign in to comment.