Skip to content

Commit

Permalink
Add serialization tests
Browse files Browse the repository at this point in the history
  • Loading branch information
delsner committed Jun 4, 2024
1 parent c80be3e commit f87e442
Showing 1 changed file with 34 additions and 1 deletion.
35 changes: 34 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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::<PixiPackMetadata>(&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::<PixiPackMetadata>(&invalid.to_string()).is_err());
}
}

0 comments on commit f87e442

Please sign in to comment.