diff --git a/src/lib.rs b/src/lib.rs index c5af9c5..04733b0 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -11,7 +11,7 @@ pub const PIXI_PACK_METADATA_PATH: &str = "pixi-pack.json"; pub const DEFAULT_PIXI_PACK_VERSION: &str = "1"; /// The metadata for a "pixi-pack". -#[derive(Serialize, Deserialize, Debug, Clone)] +#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)] pub struct PixiPackMetadata { /// The pack format version. pub version: String, @@ -27,3 +27,36 @@ impl Default for PixiPackMetadata { } } } + +/* --------------------------------------------------------------------------------------------- */ +/* TESTS */ +/* --------------------------------------------------------------------------------------------- */ + +#[cfg(test)] +mod tests { + use super::*; + use rstest::*; + use serde_json::{json, Value}; + + #[rstest] + fn test_metadata_serialization() { + let metadata = PixiPackMetadata { + version: DEFAULT_PIXI_PACK_VERSION.to_string(), + platform: Platform::Linux64, + }; + let result = json!(metadata).to_string(); + assert_eq!(result, "{\"version\":\"1\",\"platform\":\"linux-64\"}"); + assert_eq!( + serde_json::from_str::(&result).unwrap(), + metadata + ); + } + + #[rstest] + #[case(json!({"version": "1", "platform": "linux64"}))] + #[case(json!({"version": 1.0, "platform": "linux-64"}))] + #[case(json!({"version": 1, "platform": "linux-64"}))] + fn test_metadata_serialization_failure(#[case] invalid: Value) { + assert!(serde_json::from_str::(&invalid.to_string()).is_err()); + } +}