Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sffc committed Mar 7, 2024
1 parent 5985730 commit 047b552
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 0 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions utils/pattern/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []
Expand Down
36 changes: 36 additions & 0 deletions utils/pattern/src/frontend/serde.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Cow<str>> =
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<Cow<str>> =
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<Cow<str>> =
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<Cow<str>> =
postcard::from_bytes(&pattern_postcard).unwrap();
assert_eq!(pattern_cow, pattern_deserialized);
}
}
1 change: 1 addition & 0 deletions utils/pattern/src/single.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 047b552

Please sign in to comment.