mirror of
https://github.com/trussed-dev/serde-indexed.git
synced 2026-06-20 04:16:32 -07:00
Add tests from #1
This commit is contained in:
committed by
sosthene-nitrokey
parent
b1c9c6e92c
commit
2eefedf15a
+2
-2
@@ -21,7 +21,7 @@ syn = "1.0"
|
||||
[dev-dependencies]
|
||||
heapless = { version = "0.7.16", default-features = false, features = ["serde"] }
|
||||
hex-literal = "0.4.1"
|
||||
serde = { version = "1", default-features = false }
|
||||
serde = { version = "1" }
|
||||
serde-byte-array = "0.1.2"
|
||||
serde_bytes = { version = "0.11.12", default-features = false }
|
||||
serde_cbor = { version = "0.11.0", default-features = false }
|
||||
serde_cbor = { version = "0.11.0" }
|
||||
|
||||
@@ -220,3 +220,48 @@ mod some_keys {
|
||||
assert_eq!(maybe_example, example);
|
||||
}
|
||||
}
|
||||
|
||||
mod cow {
|
||||
use super::*;
|
||||
use std::borrow::Cow;
|
||||
|
||||
#[derive(PartialEq, Debug, SerializeIndexed, DeserializeIndexed)]
|
||||
#[serde_indexed(offset = 1)]
|
||||
struct WithLifetimes<'a> {
|
||||
data: Cow<'a, [u8]>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
option: Option<u8>,
|
||||
}
|
||||
|
||||
fn lifetime_example<'a>() -> WithLifetimes<'a> {
|
||||
WithLifetimes {
|
||||
data: Cow::Borrowed(&[1, 2, 3]),
|
||||
option: None,
|
||||
}
|
||||
}
|
||||
|
||||
const SERIALIZED_LIFETIME_EXAMPLE: &'static [u8] = b"\xa1\x01\x83\x01\x02\x03";
|
||||
|
||||
#[test]
|
||||
fn serialize() {
|
||||
let data = lifetime_example();
|
||||
let mut buf = [0u8; 64];
|
||||
|
||||
let size = cbor_serialize(&data, &mut buf).unwrap();
|
||||
|
||||
assert_eq!(&buf[..size], SERIALIZED_LIFETIME_EXAMPLE);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn deserialize() {
|
||||
let example = lifetime_example();
|
||||
|
||||
let deserialized: WithLifetimes<'_> =
|
||||
cbor_deserialize_with_scratch(SERIALIZED_LIFETIME_EXAMPLE, &mut []).unwrap();
|
||||
|
||||
assert_eq!(deserialized, example);
|
||||
let Cow::Owned(_) = deserialized.data else {
|
||||
panic!("Expected deserialized data Cow::Owned");
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user