diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-17 12:02:58 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-17 12:02:58 +0000 |
commit | 698f8c2f01ea549d77d7dc3338a12e04c11057b9 (patch) | |
tree | 173a775858bd501c378080a10dca74132f05bc50 /vendor/pulldown-cmark/tests/serde.rs | |
parent | Initial commit. (diff) | |
download | rustc-698f8c2f01ea549d77d7dc3338a12e04c11057b9.tar.xz rustc-698f8c2f01ea549d77d7dc3338a12e04c11057b9.zip |
Adding upstream version 1.64.0+dfsg1.upstream/1.64.0+dfsg1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'vendor/pulldown-cmark/tests/serde.rs')
-rw-r--r-- | vendor/pulldown-cmark/tests/serde.rs | 78 |
1 files changed, 78 insertions, 0 deletions
diff --git a/vendor/pulldown-cmark/tests/serde.rs b/vendor/pulldown-cmark/tests/serde.rs new file mode 100644 index 000000000..7098bfbe8 --- /dev/null +++ b/vendor/pulldown-cmark/tests/serde.rs @@ -0,0 +1,78 @@ +#[cfg(feature = "serde")] +mod tests { + use std::convert::TryInto; + + use pulldown_cmark::CowStr; + + #[test] + fn cow_str_to_str_round_trip_bincode() { + for i in &[ + CowStr::Borrowed("dssa"), + CowStr::Borrowed(""), + CowStr::Boxed("hello".to_owned().into_boxed_str()), + CowStr::Boxed("".to_owned().into_boxed_str()), + CowStr::Inlined("inline".try_into().unwrap()), + CowStr::Inlined("".try_into().unwrap()), + ] { + let encoded = bincode::serialize(i).unwrap(); + let decoded1: CowStr = bincode::deserialize(&encoded).unwrap(); + let decoded2: String = bincode::deserialize(&encoded).unwrap(); + let decoded3: &str = bincode::deserialize(&encoded).unwrap(); + + assert_eq!(&decoded1, i); + assert_eq!(decoded2, i.as_ref()); + assert_eq!(decoded3, i.as_ref()); + } + } + + #[test] + fn cow_str_to_str_round_trip_json() { + for i in &[ + CowStr::Borrowed("dssa"), + CowStr::Borrowed(""), + CowStr::Boxed("hello".to_owned().into_boxed_str()), + CowStr::Boxed("".to_owned().into_boxed_str()), + CowStr::Inlined("inline".try_into().unwrap()), + CowStr::Inlined("".try_into().unwrap()), + ] { + let encoded = serde_json::to_string(i).unwrap(); + let decoded1: CowStr = serde_json::from_str(&encoded).unwrap(); + let decoded2: String = serde_json::from_str(&encoded).unwrap(); + let decoded3: &str = serde_json::from_str(&encoded).unwrap(); + + assert_eq!(&decoded1, i); + assert_eq!(decoded2, i.as_ref()); + assert_eq!(decoded3, i.as_ref()); + } + } + + #[test] + fn str_to_cow_str_json() { + let str = "a borrowed str"; + let string = "a owned str".to_owned(); + + let encoded_str = serde_json::to_string(&str).unwrap(); + let encoded_string = serde_json::to_string(&string).unwrap(); + + let decoded_str: CowStr = serde_json::from_str(&encoded_str).unwrap(); + let decoded_string: CowStr = serde_json::from_str(&encoded_string).unwrap(); + + assert_eq!(decoded_str.as_ref(), str); + assert_eq!(decoded_string.as_ref(), string); + } + + #[test] + fn str_to_cow_str_bincode() { + let str = "a borrowed str"; + let string = "a owned str".to_owned(); + + let encoded_str = bincode::serialize(&str).unwrap(); + let encoded_string = bincode::serialize(&string).unwrap(); + + let decoded_str: CowStr = bincode::deserialize(&encoded_str).unwrap(); + let decoded_string: CowStr = bincode::deserialize(&encoded_string).unwrap(); + + assert_eq!(decoded_str.as_ref(), str); + assert_eq!(decoded_string.as_ref(), string); + } +} |