Skip to content

Commit

Permalink
Add test for the Serialize identifier validation
Browse files Browse the repository at this point in the history
  • Loading branch information
juntyr committed Aug 17, 2023
1 parent 182a131 commit 4af5152
Showing 1 changed file with 89 additions and 0 deletions.
89 changes: 89 additions & 0 deletions tests/465_validate_ser_identifiers.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
use serde::ser::{SerializeStruct, SerializeStructVariant, Serializer};

#[test]
fn invalid_struct_name() {
let mut ser = ron::Serializer::new(Vec::new(), None).unwrap();

assert_eq!(
ser.serialize_newtype_struct("", &true).err(),
Some(ron::Error::InvalidIdentifier(String::from(""))),
);

assert_eq!(
ser.serialize_tuple_struct("", 0).err(),
Some(ron::Error::InvalidIdentifier(String::from(""))),
);

assert_eq!(
ser.serialize_struct("", 0).err(),
Some(ron::Error::InvalidIdentifier(String::from(""))),
);
}

#[test]
fn invalid_enum_variant_name() {
let mut ser = ron::Serializer::new(Vec::new(), None).unwrap();

assert_eq!(
ser.serialize_unit_variant("", 0, "A").err(),
Some(ron::Error::InvalidIdentifier(String::from(""))),
);

assert_eq!(
ser.serialize_unit_variant("A", 0, "").err(),
Some(ron::Error::InvalidIdentifier(String::from(""))),
);

assert_eq!(
ser.serialize_newtype_variant("", 0, "A", &true).err(),
Some(ron::Error::InvalidIdentifier(String::from(""))),
);

assert_eq!(
ser.serialize_newtype_variant("A", 0, "", &true).err(),
Some(ron::Error::InvalidIdentifier(String::from(""))),
);

assert_eq!(
ser.serialize_tuple_variant("", 0, "A", 0).err(),
Some(ron::Error::InvalidIdentifier(String::from(""))),
);

assert_eq!(
ser.serialize_tuple_variant("A", 0, "", 0).err(),
Some(ron::Error::InvalidIdentifier(String::from(""))),
);

assert_eq!(
ser.serialize_struct_variant("", 0, "A", 0).err(),
Some(ron::Error::InvalidIdentifier(String::from(""))),
);

assert_eq!(
ser.serialize_struct_variant("A", 0, "", 0).err(),
Some(ron::Error::InvalidIdentifier(String::from(""))),
);
}

#[test]
fn invalid_struct_field_name() {
let mut ser = ron::Serializer::new(Vec::new(), None).unwrap();

let mut r#struct = ser.serialize_struct("A", 2).unwrap();
SerializeStruct::serialize_field(&mut r#struct, "A", &true).unwrap();

assert_eq!(
SerializeStruct::serialize_field(&mut r#struct, "", &true).err(),
Some(ron::Error::InvalidIdentifier(String::from(""))),
);

std::mem::drop(r#struct);

let mut r#struct = ser.serialize_struct_variant("A", 0, "A", 2).unwrap();
SerializeStructVariant::serialize_field(&mut r#struct, "A", &true).unwrap();

assert_eq!(
SerializeStructVariant::serialize_field(&mut r#struct, "", &true).err(),
Some(ron::Error::InvalidIdentifier(String::from(""))),
);
}

0 comments on commit 4af5152

Please sign in to comment.