diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 09:22:09 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 09:22:09 +0000 |
commit | 43a97878ce14b72f0981164f87f2e35e14151312 (patch) | |
tree | 620249daf56c0258faa40cbdcf9cfba06de2a846 /third_party/rust/ron/tests/roundtrip.rs | |
parent | Initial commit. (diff) | |
download | firefox-43a97878ce14b72f0981164f87f2e35e14151312.tar.xz firefox-43a97878ce14b72f0981164f87f2e35e14151312.zip |
Adding upstream version 110.0.1.upstream/110.0.1upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'third_party/rust/ron/tests/roundtrip.rs')
-rw-r--r-- | third_party/rust/ron/tests/roundtrip.rs | 121 |
1 files changed, 121 insertions, 0 deletions
diff --git a/third_party/rust/ron/tests/roundtrip.rs b/third_party/rust/ron/tests/roundtrip.rs new file mode 100644 index 0000000000..7ca5bd8ad7 --- /dev/null +++ b/third_party/rust/ron/tests/roundtrip.rs @@ -0,0 +1,121 @@ +use serde::{Deserialize, Serialize}; +use std::collections::HashMap; + +use ron::extensions::Extensions; + +#[derive(Debug, PartialEq, Deserialize, Serialize)] +struct UnitStruct; + +#[derive(Debug, PartialEq, Deserialize, Serialize)] +struct NewType(f32); + +#[derive(Debug, PartialEq, Deserialize, Serialize)] +struct TupleStruct(UnitStruct, i8); + +#[derive(Debug, PartialEq, Eq, Hash, Deserialize, Serialize)] +struct Key(u32); + +#[derive(Debug, PartialEq, Eq, Hash, Deserialize, Serialize)] +enum Enum { + Unit, + Bool(bool), + Chars(char, String), +} + +#[derive(Debug, PartialEq, Deserialize, Serialize)] +struct Struct { + tuple: ((), NewType, TupleStruct), + vec: Vec<Option<UnitStruct>>, + map: HashMap<Key, Enum>, +} + +#[test] +fn roundtrip() { + let value = Struct { + tuple: ((), NewType(0.5), TupleStruct(UnitStruct, -5)), + vec: vec![None, Some(UnitStruct)], + map: vec![ + (Key(5), Enum::Unit), + (Key(6), Enum::Bool(false)), + (Key(7), Enum::Bool(true)), + (Key(9), Enum::Chars('x', "".to_string())), + ] + .into_iter() + .collect(), + }; + + let serial = ron::ser::to_string(&value).unwrap(); + + println!("Serialized: {}", serial); + + let deserial = ron::de::from_str(&serial); + + assert_eq!(Ok(value), deserial); +} + +#[test] +fn roundtrip_pretty() { + let value = Struct { + tuple: ((), NewType(0.5), TupleStruct(UnitStruct, -5)), + vec: vec![None, Some(UnitStruct)], + map: vec![ + (Key(5), Enum::Unit), + (Key(6), Enum::Bool(false)), + (Key(7), Enum::Bool(true)), + (Key(9), Enum::Chars('x', "".to_string())), + ] + .into_iter() + .collect(), + }; + + let pretty = ron::ser::PrettyConfig::new() + .enumerate_arrays(true) + .extensions(Extensions::IMPLICIT_SOME); + let serial = ron::ser::to_string_pretty(&value, pretty).unwrap(); + + println!("Serialized: {}", serial); + + let deserial = ron::de::from_str(&serial); + + assert_eq!(Ok(value), deserial); +} + +#[test] +fn roundtrip_sep_tuple_members() { + #[derive(Debug, Deserialize, PartialEq, Eq, Serialize)] + pub enum FileOrMem { + File(String), + Memory, + } + + #[derive(Debug, Deserialize, PartialEq, Serialize)] + struct Both { + a: Struct, + b: FileOrMem, + } + + let a = Struct { + tuple: ((), NewType(0.5), TupleStruct(UnitStruct, -5)), + vec: vec![None, Some(UnitStruct)], + map: vec![ + (Key(5), Enum::Unit), + (Key(6), Enum::Bool(false)), + (Key(7), Enum::Bool(true)), + (Key(9), Enum::Chars('x', "".to_string())), + ] + .into_iter() + .collect(), + }; + let b = FileOrMem::File("foo".to_owned()); + + let value = Both { a, b }; + + let pretty = ron::ser::PrettyConfig::new().separate_tuple_members(true); + let serial = ron::ser::to_string_pretty(&value, pretty).unwrap(); + + println!("Serialized: {}", serial); + + let deserial = ron::de::from_str(&serial); + + assert_eq!(Ok(value), deserial); +} |