Lines
100 %
Functions
44.83 %
Branches
use std::collections::HashMap;
use ron::{
de::from_str,
error::Error,
extensions::Extensions,
ser::{to_string_pretty, PrettyConfig},
};
use serde::{Deserialize, Serialize};
#[derive(Debug, Deserialize, Serialize, Eq, PartialEq)]
enum TestEnum {
Unit,
PrimitiveNewtype(String),
Tuple(u32, bool),
Struct { a: u32, b: bool },
TupleNewtypeUnit(Unit),
TupleNewtypeNewtype(Newtype),
TupleNewtypeTuple((u32, bool)),
TupleNewtypeTupleStruct(TupleStruct),
TupleNewtypeStruct(Struct),
TupleNewtypeEnum(Enum),
TupleNewtypeOption(Option<Struct>),
TupleNewtypeSeq(Vec<Struct>),
TupleNewtypeMap(HashMap<u32, Struct>),
}
struct Unit;
struct Newtype(i32);
struct TupleStruct(u32, bool);
struct Struct {
a: u32,
b: bool,
enum Enum {
A,
B(Struct),
C(u32, bool),
D { a: u32, b: bool },
#[test]
fn test_deserialise_non_newtypes() {
assert_eq!(
from_str::<TestEnum>(r#"#![enable(unwrap_variant_newtypes)] Unit"#).unwrap(),
TestEnum::Unit,
);
from_str::<TestEnum>(r#"#![enable(unwrap_variant_newtypes)] PrimitiveNewtype("hi")"#)
.unwrap(),
TestEnum::PrimitiveNewtype(String::from("hi")),
from_str::<TestEnum>(r#"#![enable(unwrap_variant_newtypes)] Tuple(4, false)"#).unwrap(),
TestEnum::Tuple(4, false),
from_str::<TestEnum>(r#"#![enable(unwrap_variant_newtypes)] Struct(a: 4, b: false)"#)
TestEnum::Struct { a: 4, b: false },
fn test_deserialise_tuple_newtypes() {
from_str::<TestEnum>(r#"#![enable(unwrap_variant_newtypes)] TupleNewtypeUnit(Unit)"#)
.unwrap_err()
.code,
Error::ExpectedStructLikeEnd,
from_str::<TestEnum>(r#"#![enable(unwrap_variant_newtypes)] TupleNewtypeUnit(())"#)
from_str::<TestEnum>(r#"#![enable(unwrap_variant_newtypes)] TupleNewtypeUnit()"#).unwrap(),
TestEnum::TupleNewtypeUnit(Unit),
from_str::<TestEnum>(
r#"#![enable(unwrap_variant_newtypes)] TupleNewtypeNewtype(Newtype(4))"#
)
Error::ExpectedInteger,
from_str::<TestEnum>(r#"#![enable(unwrap_variant_newtypes)] TupleNewtypeNewtype((4))"#)
from_str::<TestEnum>(r#"#![enable(unwrap_variant_newtypes)] TupleNewtypeNewtype(4)"#)
TestEnum::TupleNewtypeNewtype(Newtype(4)),
from_str::<TestEnum>(r#"#![enable(unwrap_newtypes)] TupleNewtypeNewtype(4)"#).unwrap(),
from_str::<TestEnum>(r#"#![enable(unwrap_newtypes)] #![enable(unwrap_variant_newtypes)] TupleNewtypeNewtype(4)"#).unwrap(),
r#"#![enable(unwrap_variant_newtypes)] TupleNewtypeTuple((4, false))"#
from_str::<TestEnum>(r#"#![enable(unwrap_variant_newtypes)] TupleNewtypeTuple(4, false)"#)
TestEnum::TupleNewtypeTuple((4, false)),
r#"#![enable(unwrap_variant_newtypes)] TupleNewtypeTupleStruct(TupleStruct(4, false))"#
r#"#![enable(unwrap_variant_newtypes)] TupleNewtypeTupleStruct((4, false))"#
r#"#![enable(unwrap_variant_newtypes)] TupleNewtypeTupleStruct(4, false)"#
TestEnum::TupleNewtypeTupleStruct(TupleStruct(4, false)),
r#"#![enable(unwrap_variant_newtypes)] TupleNewtypeStruct(Struct(a: 4, b: false))"#
Error::ExpectedMapColon,
r#"#![enable(unwrap_variant_newtypes)] TupleNewtypeStruct((a: 4, b: false))"#
Error::ExpectedIdentifier,
r#"#![enable(unwrap_variant_newtypes)] TupleNewtypeStruct(a: 4, b: false)"#
TestEnum::TupleNewtypeStruct(Struct { a: 4, b: false }),
from_str::<TestEnum>(r#"#![enable(unwrap_variant_newtypes)] TupleNewtypeEnum(A)"#).unwrap(),
TestEnum::TupleNewtypeEnum(Enum::A),
r#"#![enable(unwrap_variant_newtypes)] TupleNewtypeEnum(B(a: 4, b: false))"#
TestEnum::TupleNewtypeEnum(Enum::B(Struct { a: 4, b: false })),
from_str::<TestEnum>(r#"#![enable(unwrap_variant_newtypes)] TupleNewtypeEnum(C 4, false)"#)
Error::ExpectedStructLike,
r#"#![enable(unwrap_variant_newtypes)] TupleNewtypeEnum(C(4, false))"#
TestEnum::TupleNewtypeEnum(Enum::C(4, false)),
r#"#![enable(unwrap_variant_newtypes)] TupleNewtypeEnum(D a: 4, b: false)"#
r#"#![enable(unwrap_variant_newtypes)] TupleNewtypeEnum(D(a: 4, b: false))"#
TestEnum::TupleNewtypeEnum(Enum::D { a: 4, b: false }),
from_str::<TestEnum>(r#"#![enable(unwrap_variant_newtypes)] TupleNewtypeOption(None)"#)
TestEnum::TupleNewtypeOption(None),
r#"#![enable(unwrap_variant_newtypes)] TupleNewtypeOption(Some(a: 4, b: false))"#
TestEnum::TupleNewtypeOption(Some(Struct { a: 4, b: false })),
r#"#![enable(unwrap_variant_newtypes)] TupleNewtypeOption(a: 4, b: false)"#
Error::ExpectedOption,
from_str::<TestEnum>(r#"#![enable(unwrap_variant_newtypes, implicit_some)] TupleNewtypeOption(a: 4, b: false)"#).unwrap(),
from_str::<TestEnum>(r#"#![enable(unwrap_variant_newtypes)] TupleNewtypeSeq([])"#).unwrap(),
TestEnum::TupleNewtypeSeq(vec![]),
r#"#![enable(unwrap_variant_newtypes)] TupleNewtypeSeq([(a: 4, b: false)])"#
TestEnum::TupleNewtypeSeq(vec![Struct { a: 4, b: false }]),
r#"#![enable(unwrap_variant_newtypes)] TupleNewtypeSeq([Struct(a: 4, b: false)])"#
from_str::<TestEnum>(r#"#![enable(unwrap_variant_newtypes)] TupleNewtypeMap({})"#).unwrap(),
TestEnum::TupleNewtypeMap(vec![].into_iter().collect()),
r#"#![enable(unwrap_variant_newtypes)] TupleNewtypeMap({2: (a: 4, b: false)})"#
TestEnum::TupleNewtypeMap(vec![(2, Struct { a: 4, b: false })].into_iter().collect()),
r#"#![enable(unwrap_variant_newtypes)] TupleNewtypeMap({8: Struct(a: 4, b: false)})"#
TestEnum::TupleNewtypeMap(vec![(8, Struct { a: 4, b: false })].into_iter().collect()),
fn test_newtype_some() {
from_str::<Option<Struct>>(r#"Some((a: 4, b: false))"#).unwrap(),
Some(Struct { a: 4, b: false }),
from_str::<Option<Struct>>(r#"Some(a: 4, b: false)"#)
Error::ExpectedDifferentStructName {
expected: "Struct",
found: String::from("a"),
},
from_str::<Option<Struct>>(r#"#![enable(unwrap_variant_newtypes)] Some(a: 4, b: false)"#)
fn test_serialise_non_newtypes() {
assert_eq_serialize_roundtrip(TestEnum::Unit, Extensions::UNWRAP_VARIANT_NEWTYPES);
assert_eq_serialize_roundtrip(
Extensions::UNWRAP_VARIANT_NEWTYPES,
fn test_serialise_tuple_newtypes() {
Extensions::UNWRAP_NEWTYPES,
Extensions::UNWRAP_VARIANT_NEWTYPES | Extensions::UNWRAP_NEWTYPES,
Extensions::IMPLICIT_SOME,
Extensions::UNWRAP_VARIANT_NEWTYPES | Extensions::IMPLICIT_SOME,
fn assert_eq_serialize_roundtrip<
S: Serialize + serde::de::DeserializeOwned + PartialEq + std::fmt::Debug,
>(
value: S,
extensions: Extensions,
) {
let ron = to_string_pretty(&value, PrettyConfig::default().extensions(extensions)).unwrap();
let result = from_str::<S>(&ron);
assert_eq!(result.as_ref(), Ok(&value), "{}", ron,);