From c5e16ccf4d4a254b3dc19525fb10b86085fd3a94 Mon Sep 17 00:00:00 2001 From: Sebastien Rousseau Date: Mon, 27 May 2024 20:46:28 +0100 Subject: [PATCH] test(serde_yml): :white_check_mark: add unit tests for `macro_get_field` --- .../{macro_utility.rs => macro_get_field.rs} | 33 ++++--- src/macros/mod.rs | 5 +- tests/data/test.json | 5 ++ tests/macros/mod.rs | 3 + tests/macros/test_macro_get_field.rs | 86 +++++++++++++++++++ 5 files changed, 111 insertions(+), 21 deletions(-) rename src/macros/{macro_utility.rs => macro_get_field.rs} (59%) create mode 100644 tests/data/test.json create mode 100644 tests/macros/test_macro_get_field.rs diff --git a/src/macros/macro_utility.rs b/src/macros/macro_get_field.rs similarity index 59% rename from src/macros/macro_utility.rs rename to src/macros/macro_get_field.rs index 42c86575..0196dc16 100644 --- a/src/macros/macro_utility.rs +++ b/src/macros/macro_get_field.rs @@ -27,24 +27,21 @@ macro_rules! macro_get_field { let current_dir = env::current_dir()?; let file_path = Path::new(¤t_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) }, ) } diff --git a/src/macros/mod.rs b/src/macros/mod.rs index 760cd6b7..53618cf5 100644 --- a/src/macros/mod.rs +++ b/src/macros/mod.rs @@ -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; diff --git a/tests/data/test.json b/tests/data/test.json new file mode 100644 index 00000000..4c24d75e --- /dev/null +++ b/tests/data/test.json @@ -0,0 +1,5 @@ +{ + "name": "John Doe", + "age": 30, + "city": "New York" +} diff --git a/tests/macros/mod.rs b/tests/macros/mod.rs index 2cfd3b8a..de6a6cd0 100644 --- a/tests/macros/mod.rs +++ b/tests/macros/mod.rs @@ -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; diff --git a/tests/macros/test_macro_get_field.rs b/tests/macros/test_macro_get_field.rs new file mode 100644 index 00000000..d9a425d6 --- /dev/null +++ b/tests/macros/test_macro_get_field.rs @@ -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> { + // 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> { + // 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> { + // 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(()) + } +}