From 047b552e3be7c14de989cf9fcbe0bd639c5e63ac Mon Sep 17 00:00:00 2001 From: "Shane F. Carr" Date: Thu, 7 Mar 2024 18:14:54 +0100 Subject: [PATCH] Add tests --- Cargo.lock | 2 ++ utils/pattern/Cargo.toml | 2 ++ utils/pattern/src/frontend/serde.rs | 36 +++++++++++++++++++++++++++++ utils/pattern/src/single.rs | 1 + 4 files changed, 41 insertions(+) diff --git a/Cargo.lock b/Cargo.lock index e179a401424..94483fd05ab 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1604,7 +1604,9 @@ version = "0.1.5" dependencies = [ "databake", "displaydoc", + "postcard", "serde", + "serde_json", "writeable", "yoke", "zerofrom", diff --git a/utils/pattern/Cargo.toml b/utils/pattern/Cargo.toml index 39c90ffd54c..6f783d8ea37 100644 --- a/utils/pattern/Cargo.toml +++ b/utils/pattern/Cargo.toml @@ -31,6 +31,8 @@ zerofrom = { workspace = true, features = ["derive"], optional = true } [dev-dependencies] zerofrom = { workspace = true, features = ["alloc"] } zerovec = { workspace = true, features = ["databake", "serde"] } +serde_json = { version = "1.0" } +postcard = { version = "1.0", features = ["use-std"] } [features] alloc = [] diff --git a/utils/pattern/src/frontend/serde.rs b/utils/pattern/src/frontend/serde.rs index 1222e634a86..ebd55bc9923 100644 --- a/utils/pattern/src/frontend/serde.rs +++ b/utils/pattern/src/frontend/serde.rs @@ -62,3 +62,39 @@ where } } } + +#[cfg(test)] +mod tests { + use super::*; + use crate::SinglePlaceholderPattern; + + #[test] + fn test_json() { + let pattern_owned = SinglePlaceholderPattern::try_from_str("Hello, {0}!").unwrap(); + let pattern_cow: SinglePlaceholderPattern> = + SinglePlaceholderPattern::from_store_unchecked(Cow::Owned(pattern_owned.take_store())); + let pattern_json = serde_json::to_string(&pattern_cow).unwrap(); + assert_eq!( + pattern_json, + r#"[{"Literal":"Hello, "},{"Placeholder":"Singleton"},{"Literal":"!"}]"# + ); + let pattern_deserialized: SinglePlaceholderPattern> = + serde_json::from_str(&pattern_json).unwrap(); + assert_eq!(pattern_cow, pattern_deserialized); + } + + #[test] + fn test_postcard() { + let pattern_owned = SinglePlaceholderPattern::try_from_str("Hello, {0}!").unwrap(); + let pattern_cow: SinglePlaceholderPattern> = + SinglePlaceholderPattern::from_store_unchecked(Cow::Owned(pattern_owned.take_store())); + let pattern_postcard = postcard::to_stdvec(&pattern_cow).unwrap(); + assert_eq!( + pattern_postcard, + b"\x09\x08Hello, !" + ); + let pattern_deserialized: SinglePlaceholderPattern> = + postcard::from_bytes(&pattern_postcard).unwrap(); + assert_eq!(pattern_cow, pattern_deserialized); + } +} diff --git a/utils/pattern/src/single.rs b/utils/pattern/src/single.rs index 0dd57565101..b26b6362942 100644 --- a/utils/pattern/src/single.rs +++ b/utils/pattern/src/single.rs @@ -41,6 +41,7 @@ use alloc::string::String; /// ); /// ``` #[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)] +#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))] #[allow(clippy::exhaustive_enums)] // Singleton pub enum SinglePlaceholderKey { Singleton,