From 629a2627fad8f8526342a1b319a9736da74608d3 Mon Sep 17 00:00:00 2001 From: Sebastien Rousseau Date: Sat, 13 May 2023 22:35:50 +0100 Subject: [PATCH] fix(cmn): unit tests --- src/lib.rs | 10 ++++++++-- tests/test_lib.rs | 23 ++++++++++++++++++++++- 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 9438603..94580bf 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -119,6 +119,7 @@ /// that are used to serialize and deserialize the data. extern crate serde; use serde::{Deserialize, Serialize}; +use serde_json; /// The `macros` module contains functions for generating macros. pub mod macros; @@ -144,12 +145,17 @@ pub use words::Words; /// * `constants`: A reference to the `Constants` structure. /// * `words`: A reference to the `Words` structure. #[derive(Clone, Serialize, Deserialize, Debug)] -pub struct Common; +pub struct Common { + #[serde(flatten)] + fields: serde_json::Value, +} impl Common { /// Creates a new instance of the `Common` structure. pub fn new() -> Self { - Self + Self { + fields: serde_json::Value::Null, + } } /// Returns the `Constants` instance. pub fn constants(&self) -> Constants { diff --git a/tests/test_lib.rs b/tests/test_lib.rs index c2d931e..de8de7d 100644 --- a/tests/test_lib.rs +++ b/tests/test_lib.rs @@ -43,7 +43,10 @@ mod tests { let constants = common.constants(); assert_eq!(constants.constants().len(), 16); - assert_eq!(constants.constants(), Constants::default().constants()); + assert_eq!( + constants.constants(), + Constants::default().constants() + ); } #[test] @@ -83,4 +86,22 @@ mod tests { assert_eq!(default_constants.len(), 16); assert_eq!(default_constants, constants.constants()); } + + #[test] + fn test_parse_valid_input() { + let input = r#"{"field1": "value1", "field2": "value2"}"#; + let result = Common::parse(input); + assert!( + result.is_ok(), + "Parsing should succeed: {:?}", + result.unwrap_err() + ); + } + + #[test] + fn test_parse_invalid_input() { + let input = "invalid input"; + let result = Common::parse(input); + assert!(result.is_err(), "Parsing should fail"); + } }