Skip to content

Commit

Permalink
test(serde_yml): ✅ add unit tests for macro_get_field
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastienrousseau committed May 27, 2024
1 parent ab30997 commit c5e16cc
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 21 deletions.
33 changes: 15 additions & 18 deletions src/macros/macro_utility.rs → src/macros/macro_get_field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,24 +27,21 @@ macro_rules! macro_get_field {
let current_dir = env::current_dir()?;
let file_path =
Path::new(&current_dir).join(file_path);
read_file(&file_path, |file| {
let value: serde_json::Value =
$deserializer(file)?;
let field_value = value
.get(field_name)
.ok_or_else(|| {
format!(
"Field '{}' not found",
field_name
)
})?
.as_str()
.map(|s| s.to_string())
.unwrap_or_else(|| {
value[field_name].to_string()
});
Ok(field_value)
})
let file_contents =
std::fs::read_to_string(&file_path)?;
let value: serde_json::Value =
$deserializer(file_contents.as_bytes())?;
let field_value = value
.get(field_name)
.ok_or_else(|| {
format!("Field '{}' not found", field_name)
})?
.as_str()
.map(|s| s.to_string())
.unwrap_or_else(|| {
value[field_name].to_string()
});
Ok(field_value)
},
)
}
Expand Down
5 changes: 2 additions & 3 deletions src/macros/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,8 @@ pub mod macro_from_number;
/// The `partialeq_numeric_macros` module contains macros related to the `PartialEq` trait for primitive numeric types and `Value`.
pub mod macro_partialeq_numeric;

/// The `utility_macros` module contains utility macros for common tasks such as
/// replacing placeholders in a line with values from parameters.
pub mod macro_utility;
/// The `macro_get_field` module contains macros related to retrieving field values from a JSON file.
pub mod macro_get_field;

/// The `nested_enum_serde` module contains macros related to serializing and deserializing nested enums.
pub mod macro_nested_enum_serde;
Expand Down
5 changes: 5 additions & 0 deletions tests/data/test.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"name": "John Doe",
"age": 30,
"city": "New York"
}
3 changes: 3 additions & 0 deletions tests/macros/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,6 @@ pub mod test_macro_partialeq_numeric;

/// The `test_macro_replace_placeholder` module contains tests for the utility module.
pub mod test_macro_replace_placeholder;

/// The `test_macro_get_field` module contains tests for the get_field module.
pub mod test_macro_get_field;
86 changes: 86 additions & 0 deletions tests/macros/test_macro_get_field.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/// Tests for the `macro_get_field` macro.
mod tests {
use serde_yml::macro_get_field;
use std::env;
use std::error::Error;
use std::path::Path;

/// Test reading a JSON file and retrieving a field value.
#[test]
fn test_macro_get_field_success() -> Result<(), Box<dyn Error>> {
// Define the generated function name.
macro_get_field!(get_field, serde_json::from_reader);

// Define the path to the JSON file.
let file_path = Some("tests/data/test.json");

// Define the field names to retrieve.
let field_name = "name";
let field_age = "age";
let field_city = "city";

// Retrieve the field values.
let field_value_name = get_field(file_path, field_name)?;
let field_value_age = get_field(file_path, field_age)?;
let field_value_city = get_field(file_path, field_city)?;

// Check if the field values are correct.
assert_eq!(field_value_name, "John Doe");
assert_eq!(field_value_age, "30");
assert_eq!(field_value_city, "New York");

Ok(())
}

/// Test retrieving a non-existent field from a JSON file.
#[test]
fn test_macro_get_field_non_existent_field(
) -> Result<(), Box<dyn Error>> {
// Define the generated function name.
macro_get_field!(get_field, serde_json::from_reader);

// Define the path to the JSON file.
let file_path = Some("tests/data/test.json");

// Define a non-existent field name.
let field_name = "non_existent_field";

// Attempt to retrieve the non-existent field value.
let result = get_field(file_path, field_name);

// Check if the expected error is returned.
assert!(result.is_err());
assert_eq!(
result.unwrap_err().to_string(),
format!("Field '{}' not found", field_name)
);

Ok(())
}

/// Test retrieving a field value from a non-existent JSON file.
#[test]
fn test_macro_get_field_non_existent_file(
) -> Result<(), Box<dyn Error>> {
// Define the generated function name.
macro_get_field!(get_field, serde_json::from_reader);

// Define the path to a non-existent JSON file.
let file_path = Some("tests/data/non_existent.json");

// Define a field name to retrieve.
let field_name = "name";

// Attempt to retrieve the field value from the non-existent file.
let result = get_field(file_path, field_name);

// Check if the expected error is returned.
assert!(result.is_err());
assert!(result
.unwrap_err()
.to_string()
.contains("No such file or directory"));

Ok(())
}
}

0 comments on commit c5e16cc

Please sign in to comment.