Add more serialization tests

Signed-off-by: trivernis <trivernis@protonmail.com>
pull/29/head
trivernis 3 years ago
parent 5ab864006a
commit 2a9426badc
Signed by: Trivernis
GPG Key ID: DFFFCC2C7A02DB45

@ -1,48 +1,61 @@
#[cfg(feature = "serialize")]
use bromine::prelude::*;
#[cfg(feature = "serialize")]
use serde::{de::DeserializeOwned, Serialize};
use std::fmt::Debug;
#[cfg(feature = "serialize_rmp")]
#[test]
fn it_serializes_messagepack() {
test_serialization(DynamicSerializer::Messagepack)
test_serialization::<BigTestPayload>(DynamicSerializer::Messagepack).unwrap();
test_serialization::<AdjacentlyTaggedEnum>(DynamicSerializer::Messagepack).unwrap();
test_serialization::<u128>(DynamicSerializer::Messagepack).unwrap_err();
}
#[cfg(feature = "serialize_bincode")]
#[test]
fn it_serializes_bincode() {
test_serialization(DynamicSerializer::Bincode)
test_serialization::<BigTestPayload>(DynamicSerializer::Bincode).unwrap();
test_serialization::<AdjacentlyTaggedEnum>(DynamicSerializer::Bincode).unwrap_err();
test_serialization::<u128>(DynamicSerializer::Bincode).unwrap();
}
#[cfg(feature = "serialize_postcard")]
#[test]
fn it_serializes_postcard() {
test_serialization(DynamicSerializer::Postcard)
test_serialization::<BigTestPayload>(DynamicSerializer::Postcard).unwrap();
test_serialization::<AdjacentlyTaggedEnum>(DynamicSerializer::Postcard).unwrap_err();
test_serialization::<u128>(DynamicSerializer::Postcard).unwrap();
}
#[cfg(feature = "serialize_json")]
#[test]
fn it_serializes_json() {
test_serialization(DynamicSerializer::Json)
test_serialization::<BigTestPayload>(DynamicSerializer::Json).unwrap();
test_serialization::<AdjacentlyTaggedEnum>(DynamicSerializer::Json).unwrap();
test_serialization::<u128>(DynamicSerializer::Json).unwrap();
}
#[cfg(feature = "serialize")]
fn test_serialization(serializer: DynamicSerializer) {
let test_payload = get_test_payload(serializer);
let payload_bytes = test_payload.clone().try_into_bytes().unwrap();
let payload = TestSerdePayload::from_payload(&payload_bytes[..]).unwrap();
assert_eq!(payload.data(), test_payload.data())
fn test_serialization<D: Default + Serialize + DeserializeOwned + Clone + Eq + Debug>(
serializer: DynamicSerializer,
) -> IPCResult<()> {
let test_payload = SerdePayload::new(serializer, D::default());
let payload_bytes = test_payload.clone().try_into_bytes()?;
let payload = SerdePayload::<D>::from_payload(&payload_bytes[..])?;
assert_eq!(payload.data(), test_payload.data());
Ok(())
}
#[cfg(feature = "serialize")]
pub mod payload {
use bromine::payload::{DynamicSerializer, SerdePayload};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
pub type TestSerdePayload = SerdePayload<TestPayload>;
#[derive(Clone, Serialize, Deserialize, Eq, PartialEq, Debug)]
pub struct TestPayload {
pub struct BigTestPayload {
items: Vec<u64>,
variant: TestPayloadEnum,
string: String,
@ -57,20 +70,34 @@ pub mod payload {
Third(usize),
}
pub fn get_test_payload(serializer: DynamicSerializer) -> SerdePayload<TestPayload> {
let mut maps = HashMap::new();
maps.insert("Hello".to_string(), 12);
#[derive(Clone, Serialize, Deserialize, Eq, PartialEq, Debug)]
#[serde(tag = "variant", content = "data")]
pub enum AdjacentlyTaggedEnum {
Variant1(u64),
Variant2(String),
Variant3(Vec<u8>),
}
impl Default for AdjacentlyTaggedEnum {
fn default() -> Self {
Self::Variant3(vec![0, 1, 2])
}
}
maps.insert("Wäüörld".to_string(), -12380);
let inner_payload = TestPayload {
items: vec![0u64, 12452u64, u64::MAX],
variant: TestPayloadEnum::Third(12),
string: String::from("Hello World ſð"),
signed: -12,
maps,
};
impl Default for BigTestPayload {
fn default() -> Self {
let mut maps = HashMap::new();
maps.insert("Hello".to_string(), 12);
maps.insert("Wäüörld".to_string(), -12380);
SerdePayload::new(serializer, inner_payload)
BigTestPayload {
items: vec![0u64, 12452u64, u64::MAX],
variant: TestPayloadEnum::Third(12),
string: String::from("Hello World ſð"),
signed: -12,
maps,
}
}
}
}
#[cfg(feature = "serialize")]

Loading…
Cancel
Save