More tests and fix tests

This commit is contained in:
Alex 2022-11-18 17:57:21 +01:00
parent bcef147633
commit 790aac7f75
Signed by: lx
GPG Key ID: 0E496D15096376BE
3 changed files with 131 additions and 5 deletions

View File

@ -336,7 +336,8 @@ impl<'de, 'a> SeqAccess<'de> for &'a mut Seq<'de, 'a> {
let first = Term(self.0[0].0.mkref());
self.0 = &self.0[1..];
seed.deserialize(&mut Deserializer(first)).map(Some)
seed.deserialize(&mut Deserializer::from_term(&first))
.map(Some)
}
}
@ -399,7 +400,8 @@ impl<'de, 'a> EnumAccess<'de> for Enum<'de, 'a> {
where
V: DeserializeSeed<'de>,
{
let value = seed.deserialize(&mut Deserializer::from_term(&self.0.list()[0]))?;
let term = &self.0.list()[0];
let value = seed.deserialize(&mut Deserializer(Term(term.0.mkref())))?;
Ok((value, self))
}
}
@ -420,7 +422,7 @@ impl<'de, 'a> VariantAccess<'de> for Enum<'de, 'a> {
T: DeserializeSeed<'de>,
{
let [_, rest] = self.0.list_of_first()?;
seed.deserialize(&mut Deserializer::from_term(&rest))
seed.deserialize(&mut Deserializer(rest))
}
fn tuple_variant<V>(self, _len: usize, visitor: V) -> Result<V::Value>
@ -440,7 +442,7 @@ impl<'de, 'a> VariantAccess<'de> for Enum<'de, 'a> {
}
}
////////////////////////////////////////////////////////////////////////////////
// ----
#[cfg(test)]
mod tests {

View File

@ -5,3 +5,127 @@ mod ser;
pub use de::{from_bytes, Deserializer};
pub use error::{Error, Result};
pub use ser::{to_bytes, to_term, Serializer};
#[cfg(test)]
mod tests {
use super::*;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
fn test_bidir<T: Serialize + for<'de> Deserialize<'de> + PartialEq + std::fmt::Debug>(
input: T,
expected: &[u8],
) {
let ser = to_bytes(&input).unwrap();
eprintln!("Serialized: {}", std::str::from_utf8(&ser).unwrap());
assert_eq!(&ser, expected);
assert_eq!(&from_bytes::<T>(&ser).unwrap(), &input);
}
#[test]
fn test_struct() {
#[derive(Serialize, Deserialize, PartialEq, Debug)]
struct Test {
int: u32,
seq: Vec<String>,
}
let input = Test {
int: 1,
seq: vec!["a".to_string(), "b".to_string()],
};
let expected = br#"Test {
int = 1,
seq = YQ Yg,
}"#;
test_bidir(input, expected);
}
#[test]
fn test_enum() {
#[derive(Serialize, Deserialize, PartialEq, Debug)]
enum E {
Unit,
Newtype(u32),
Tuple(u32, u32),
Struct { a: u32 },
}
let input = E::Unit;
let expected = br#"E.Unit"#;
test_bidir(input, expected);
let input = E::Newtype(1);
let expected = br#"E.Newtype 1"#;
test_bidir(input, expected);
let input = E::Tuple(1, 2);
let expected = br#"E.Tuple 1 2"#;
test_bidir(input, expected);
let input = E::Struct { a: 1 };
let expected = br#"E.Struct { a = 1 }"#;
test_bidir(input, expected);
let input = vec![
E::Unit,
E::Unit,
E::Newtype(1),
E::Tuple(1, 2),
E::Struct { a: 1 },
E::Tuple(3, 2),
];
let expected = br#"E.Unit E.Unit { . = E.Newtype 1 } { . = E.Tuple 1 2 } { . = E.Struct { a = 1 } } { . =
E.Tuple 3 2 }"#;
test_bidir(input, expected);
}
#[test]
fn test_seq1() {
let input = (1, 2, 3, 4);
let expected = br#"1 2 3 4"#;
test_bidir(input, expected);
}
#[test]
fn test_seq2() {
let input = (1, 2, (2, 3, 4), 5, 6);
let expected = br#"1 2 { . = 2 3 4 } 5 6"#;
test_bidir(input, expected);
}
#[test]
fn test_seq3() {
let input = [1, 2, 3, 4];
let expected = br#"1 2 3 4"#;
test_bidir(input, expected);
}
#[test]
fn test_seq4() {
let input = [[1, 2], [2, 3], [3, 4]];
let expected = br#"{ . = 1 2 } { . = 2 3 } { . = 3 4 }"#;
test_bidir(input, expected);
}
#[test]
fn test_dict() {
let mut input = HashMap::new();
input.insert("hello".to_string(), "world".to_string());
input.insert("dont".to_string(), "panic".to_string());
let expected = br#"{
ZG9udA = cGFuaWM,
aGVsbG8 = d29ybGQ,
}"#;
test_bidir(input, expected);
let mut input = HashMap::new();
input.insert(12, vec![42, 125]);
input.insert(33, vec![19, 22, 21]);
let expected = br#"{
12 = 42 125,
33 = 19 22 21,
}"#;
test_bidir(input, expected);
}
}

View File

@ -361,7 +361,7 @@ impl ser::SerializeStructVariant for StructVariantSerializer {
}
}
////////////////////////////////////////////////////////////////////////////////
// ----
#[cfg(test)]
mod tests {