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 | |
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')
37 files changed, 2959 insertions, 0 deletions
diff --git a/third_party/rust/ron/tests/117_untagged_tuple_variant.rs b/third_party/rust/ron/tests/117_untagged_tuple_variant.rs new file mode 100644 index 0000000000..6d98deb65c --- /dev/null +++ b/third_party/rust/ron/tests/117_untagged_tuple_variant.rs @@ -0,0 +1,59 @@ +use std::borrow::Cow; + +use ron::{de::from_str, ser::to_string}; +use serde::{Deserialize, Serialize}; + +#[derive(Debug, Deserialize, Eq, PartialEq, Serialize)] +pub struct BuildSystem<'m> { + version: Cow<'m, str>, + flags: Vec<Flag<'m>>, +} + +#[derive(Debug, Deserialize, Eq, PartialEq, Serialize)] +#[serde(untagged)] +pub enum Flag<'m> { + Value(Cow<'m, str>), + If(Cow<'m, str>, Vec<Cow<'m, str>>), +} + +#[test] +fn test_ebkalderon_case() { + let file = r#"BuildSystem( + version: "1.0.0", + flags: [ + "--enable-thing", + "--enable-other-thing", + If("some-conditional", ["--enable-third-thing"]), + ] +) +"#; + + assert_eq!( + from_str::<BuildSystem>(file).unwrap(), + BuildSystem { + version: "1.0.0".into(), + flags: vec![ + Flag::Value("--enable-thing".into()), + Flag::Value("--enable-other-thing".into()), + Flag::If( + "some-conditional".into(), + vec!["--enable-third-thing".into()] + ) + ] + }, + ); +} + +#[derive(Debug, Clone, Deserialize, PartialEq, Serialize)] +#[serde(untagged)] +enum Foo { + Bar(usize), +} + +#[test] +fn test_vessd_case() { + let foo_vec = vec![Foo::Bar(0); 5]; + let foo_str = to_string(&foo_vec).unwrap(); + assert_eq!(foo_str.as_str(), "[0,0,0,0,0]"); + assert_eq!(from_str::<Vec<Foo>>(&foo_str).unwrap(), foo_vec); +} diff --git a/third_party/rust/ron/tests/123_enum_representation.rs b/third_party/rust/ron/tests/123_enum_representation.rs new file mode 100644 index 0000000000..3f82b08cd5 --- /dev/null +++ b/third_party/rust/ron/tests/123_enum_representation.rs @@ -0,0 +1,273 @@ +use ron::{de::from_str, ser::to_string}; +use serde::{Deserialize, Serialize}; +use std::{cmp::PartialEq, fmt::Debug}; + +#[derive(Debug, PartialEq, Eq, Serialize, Deserialize)] +enum Inner { + Foo, + Bar, +} + +#[derive(Debug, PartialEq, Eq, Serialize, Deserialize)] +enum EnumStructExternally { + VariantA { foo: u32, bar: u32, different: u32 }, + VariantB { foo: u32, bar: u32 }, +} + +#[derive(Debug, PartialEq, Eq, Serialize, Deserialize)] +#[serde(tag = "type")] +enum EnumStructInternally { + VariantA { foo: u32, bar: u32, different: u32 }, + VariantB { foo: u32, bar: u32 }, +} + +#[derive(Debug, PartialEq, Eq, Serialize, Deserialize)] +#[serde(tag = "type", content = "content")] +enum EnumStructAdjacently { + VariantA { + foo: u32, + bar: u32, + different: Inner, + }, + VariantB { + foo: u32, + bar: u32, + }, +} + +#[derive(Debug, PartialEq, Eq, Serialize, Deserialize)] +#[serde(untagged)] +enum EnumStructUntagged { + VariantA { foo: u32, bar: u32, different: u32 }, + VariantB { foo: u32, bar: u32 }, +} + +fn test_ser<T: Serialize>(value: &T, expected: &str) { + let actual = to_string(value).expect("Failed to serialize"); + assert_eq!(actual, expected); +} + +fn test_de<T>(s: &str, expected: T) +where + T: for<'a> Deserialize<'a> + Debug + PartialEq, +{ + let actual: Result<T, _> = from_str(s); + assert_eq!(actual, Ok(expected)); +} + +fn test_roundtrip<T>(value: T) +where + T: Serialize + for<'a> Deserialize<'a> + Debug + PartialEq, +{ + let s = to_string(&value).expect("Failed to serialize"); + let actual: Result<T, _> = from_str(&s); + assert_eq!(actual, Ok(value)); +} + +#[test] +fn test_externally_a_ser() { + let v = EnumStructExternally::VariantA { + foo: 1, + bar: 2, + different: 3, + }; + let e = "VariantA(foo:1,bar:2,different:3)"; + test_ser(&v, e); +} + +#[test] +fn test_externally_b_ser() { + let v = EnumStructExternally::VariantB { foo: 1, bar: 2 }; + let e = "VariantB(foo:1,bar:2)"; + test_ser(&v, e); +} + +#[test] +fn test_internally_a_ser() { + let v = EnumStructInternally::VariantA { + foo: 1, + bar: 2, + different: 3, + }; + let e = "(type:\"VariantA\",foo:1,bar:2,different:3)"; + test_ser(&v, e); +} + +#[test] +fn test_internally_b_ser() { + let v = EnumStructInternally::VariantB { foo: 1, bar: 2 }; + let e = "(type:\"VariantB\",foo:1,bar:2)"; + test_ser(&v, e); +} + +#[test] +fn test_adjacently_a_ser() { + let v = EnumStructAdjacently::VariantA { + foo: 1, + bar: 2, + different: Inner::Foo, + }; + let e = "(type:\"VariantA\",content:(foo:1,bar:2,different:Foo))"; + test_ser(&v, e); +} + +#[test] +fn test_adjacently_b_ser() { + let v = EnumStructAdjacently::VariantB { foo: 1, bar: 2 }; + let e = "(type:\"VariantB\",content:(foo:1,bar:2))"; + test_ser(&v, e); +} + +#[test] +fn test_untagged_a_ser() { + let v = EnumStructUntagged::VariantA { + foo: 1, + bar: 2, + different: 3, + }; + let e = "(foo:1,bar:2,different:3)"; + test_ser(&v, e); +} + +#[test] +fn test_untagged_b_ser() { + let v = EnumStructUntagged::VariantB { foo: 1, bar: 2 }; + let e = "(foo:1,bar:2)"; + test_ser(&v, e); +} + +#[test] +fn test_externally_a_de() { + let s = "VariantA(foo:1,bar:2,different:3)"; + let e = EnumStructExternally::VariantA { + foo: 1, + bar: 2, + different: 3, + }; + test_de(s, e); +} + +#[test] +fn test_externally_b_de() { + let s = "VariantB(foo:1,bar:2)"; + let e = EnumStructExternally::VariantB { foo: 1, bar: 2 }; + test_de(s, e); +} + +#[test] +fn test_internally_a_de() { + let s = "(type:\"VariantA\",foo:1,bar:2,different:3)"; + let e = EnumStructInternally::VariantA { + foo: 1, + bar: 2, + different: 3, + }; + test_de(s, e); +} + +#[test] +fn test_internally_b_de() { + let s = "(type:\"VariantB\",foo:1,bar:2)"; + let e = EnumStructInternally::VariantB { foo: 1, bar: 2 }; + test_de(s, e); +} + +#[test] +fn test_adjacently_a_de() { + let s = "(type:\"VariantA\",content:(foo:1,bar:2,different:Foo))"; + let e = EnumStructAdjacently::VariantA { + foo: 1, + bar: 2, + different: Inner::Foo, + }; + test_de(s, e); +} + +#[test] +fn test_adjacently_b_de() { + let s = "(type:\"VariantB\",content:(foo:1,bar:2))"; + let e = EnumStructAdjacently::VariantB { foo: 1, bar: 2 }; + test_de(s, e); +} + +#[test] +fn test_untagged_a_de() { + let s = "(foo:1,bar:2,different:3)"; + let e = EnumStructUntagged::VariantA { + foo: 1, + bar: 2, + different: 3, + }; + test_de(s, e); +} + +#[test] +fn test_untagged_b_de() { + let s = "(foo:1,bar:2)"; + let e = EnumStructUntagged::VariantB { foo: 1, bar: 2 }; + test_de(s, e); +} + +#[test] +fn test_externally_a_roundtrip() { + let v = EnumStructExternally::VariantA { + foo: 1, + bar: 2, + different: 3, + }; + test_roundtrip(v); +} + +#[test] +fn test_externally_b_roundtrip() { + let v = EnumStructExternally::VariantB { foo: 1, bar: 2 }; + test_roundtrip(v); +} + +#[test] +fn test_internally_a_roundtrip() { + let v = EnumStructInternally::VariantA { + foo: 1, + bar: 2, + different: 3, + }; + test_roundtrip(v); +} + +#[test] +fn test_internally_b_roundtrip() { + let v = EnumStructInternally::VariantB { foo: 1, bar: 2 }; + test_roundtrip(v); +} + +#[test] +fn test_adjacently_a_roundtrip() { + let v = EnumStructAdjacently::VariantA { + foo: 1, + bar: 2, + different: Inner::Foo, + }; + test_roundtrip(v); +} + +#[test] +fn test_adjacently_b_roundtrip() { + let v = EnumStructAdjacently::VariantB { foo: 1, bar: 2 }; + test_roundtrip(v); +} + +#[test] +fn test_untagged_a_roundtrip() { + let v = EnumStructUntagged::VariantA { + foo: 1, + bar: 2, + different: 3, + }; + test_roundtrip(v); +} + +#[test] +fn test_untagged_b_roundtrip() { + let v = EnumStructUntagged::VariantB { foo: 1, bar: 2 }; + test_roundtrip(v); +} diff --git a/third_party/rust/ron/tests/129_indexmap.rs b/third_party/rust/ron/tests/129_indexmap.rs new file mode 100644 index 0000000000..7fae993548 --- /dev/null +++ b/third_party/rust/ron/tests/129_indexmap.rs @@ -0,0 +1,76 @@ +#[cfg(feature = "indexmap")] +use ron::{de::from_str, Value}; + +#[test] +#[cfg(feature = "indexmap")] +fn test_order_preserved() { + let file = r#"( +tasks: { + "debug message": Dbg( + msg: "test message. some text after it." + ), + "shell command": Shell( + command: "ls", + args: Some([ + "-l", + "-h", + ]), + ch_dir: Some("/"), + ), +}, +) +"#; + + let value: Value = from_str(file).unwrap(); + match value { + Value::Map(map) => match &map[&Value::String("tasks".to_owned())] { + Value::Map(map) => { + assert_eq!( + *map.keys().next().unwrap(), + Value::String("debug message".to_string()) + ); + assert_eq!( + *map.keys().nth(1).unwrap(), + Value::String("shell command".to_string()) + ); + } + _ => panic!(), // GRCOV_EXCL_LINE + }, + _ => panic!(), // GRCOV_EXCL_LINE + } + + let file = r#"( +tasks: { + "shell command": Shell( + command: "ls", + args: Some([ + "-l", + "-h", + ]), + ch_dir: Some("/") + ), + "debug message": Dbg( + msg: "test message. some text after it." + ), +} +) +"#; + + let value: Value = from_str(file).unwrap(); + match value { + Value::Map(map) => match &map[&Value::String("tasks".to_owned())] { + Value::Map(map) => { + assert_eq!( + *map.keys().next().unwrap(), + Value::String("shell command".to_string()) + ); + assert_eq!( + *map.keys().nth(1).unwrap(), + Value::String("debug message".to_string()) + ); + } + _ => panic!(), // GRCOV_EXCL_LINE + }, + _ => panic!(), // GRCOV_EXCL_LINE + } +} diff --git a/third_party/rust/ron/tests/147_empty_sets_serialisation.rs b/third_party/rust/ron/tests/147_empty_sets_serialisation.rs new file mode 100644 index 0000000000..81cabf64fc --- /dev/null +++ b/third_party/rust/ron/tests/147_empty_sets_serialisation.rs @@ -0,0 +1,62 @@ +use serde::{Deserialize, Serialize}; +use std::collections::HashMap; + +#[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, Deserialize, Serialize)] +struct Struct { + tuple: ((), NewType, TupleStruct), + vec: Vec<Option<UnitStruct>>, + map: HashMap<Key, i32>, + deep_vec: HashMap<Key, Vec<()>>, + deep_map: HashMap<Key, HashMap<Key, i32>>, +} + +#[test] +fn empty_sets_arrays() { + let value = Struct { + tuple: ((), NewType(0.5), TupleStruct(UnitStruct, -5)), + vec: vec![], + map: vec![].into_iter().collect(), + deep_vec: vec![(Key(0), vec![])].into_iter().collect(), + deep_map: vec![(Key(0), vec![].into_iter().collect())] + .into_iter() + .collect(), + }; + + let pretty = ron::ser::PrettyConfig::new() + .enumerate_arrays(true) + .new_line("\n".to_string()); + let serial = ron::ser::to_string_pretty(&value, pretty).unwrap(); + + println!("Serialized: {}", serial); + + assert_eq!( + "( + tuple: ((), (0.5), ((), -5)), + vec: [], + map: {}, + deep_vec: { + (0): [], + }, + deep_map: { + (0): {}, + }, +)", + serial + ); + + let deserial = ron::de::from_str(&serial); + + assert_eq!(Ok(value), deserial); +} diff --git a/third_party/rust/ron/tests/152_bitflags.rs b/third_party/rust/ron/tests/152_bitflags.rs new file mode 100644 index 0000000000..b51fd55102 --- /dev/null +++ b/third_party/rust/ron/tests/152_bitflags.rs @@ -0,0 +1,80 @@ +use bitflags::*; +use option_set::option_set; + +#[macro_use] +extern crate bitflags_serial; + +bitflags! { + #[derive(serde::Serialize, serde::Deserialize)] + struct TestGood: u8 { + const ONE = 1; + const TWO = 1 << 1; + const THREE = 1 << 2; + } +} + +option_set! { + struct TestBad: UpperCamel + u8 { + const ONE = 1; + const TWO = 1 << 1; + const THREE = 1 << 2; + } +} + +bitflags_serial! { + struct TestBadTWO: u8 { + const ONE = 1; + const TWO = 1 << 1; + const THREE = 1 << 2; + } +} + +#[test] +fn test_bitflags() { + // Test case provided by jaynus in + // https://github.com/ron-rs/ron/issues/152#issue-421298302 + + let flag_good = TestGood::ONE | TestGood::TWO; + + let json_ser_good = serde_json::ser::to_string(&flag_good).unwrap(); + let ron_ser_good = ron::ser::to_string(&flag_good).unwrap(); + + assert_eq!(json_ser_good, "{\"bits\":3}"); + assert_eq!(ron_ser_good, "(bits:3)"); + + let json_de_good: TestGood = serde_json::de::from_str(json_ser_good.as_str()).unwrap(); + let ron_de_good: TestGood = ron::de::from_str(ron_ser_good.as_str()).unwrap(); + + assert_eq!(json_de_good, flag_good); + assert_eq!(ron_de_good, flag_good); + + // option_set + let flag_bad = TestBad::ONE | TestBad::TWO; + + let json_ser_bad = serde_json::ser::to_string(&flag_bad).unwrap(); + let ron_ser_bad = ron::ser::to_string(&flag_bad).unwrap(); + + assert_eq!(json_ser_bad, "[\"One\",\"Two\"]"); + assert_eq!(ron_ser_bad, "[\"One\",\"Two\"]"); + + let json_de_bad: TestBad = serde_json::de::from_str(json_ser_bad.as_str()).unwrap(); + let ron_de_bad: TestBad = ron::de::from_str(ron_ser_bad.as_str()).unwrap(); + + assert_eq!(json_de_bad, flag_bad); + assert_eq!(ron_de_bad, flag_bad); + + // bitflags_serial + let flag_bad_two = TestBadTWO::ONE | TestBadTWO::TWO; + + let json_ser_bad_two = serde_json::ser::to_string(&flag_bad_two).unwrap(); + let ron_ser_bad_two = ron::ser::to_string(&flag_bad_two).unwrap(); + + assert_eq!(json_ser_bad_two, "[\"ONE\",\"TWO\"]"); + assert_eq!(ron_ser_bad_two, "[ONE,TWO]"); + + let json_de_bad_two: TestBadTWO = serde_json::de::from_str(json_ser_bad_two.as_str()).unwrap(); + let ron_de_bad_two: TestBadTWO = ron::de::from_str(ron_ser_bad_two.as_str()).unwrap(); + + assert_eq!(json_de_bad_two, flag_bad_two); + assert_eq!(ron_de_bad_two, flag_bad_two); +} diff --git a/third_party/rust/ron/tests/203_error_positions.rs b/third_party/rust/ron/tests/203_error_positions.rs new file mode 100644 index 0000000000..0ef486a179 --- /dev/null +++ b/third_party/rust/ron/tests/203_error_positions.rs @@ -0,0 +1,105 @@ +use std::num::NonZeroU32; + +use ron::error::{Error, Position, SpannedError}; +use serde::{ + de::{Deserialize, Error as DeError, Unexpected}, + Deserializer, +}; + +#[derive(Debug, serde::Deserialize, PartialEq)] +#[serde(deny_unknown_fields)] +enum Test { + TupleVariant(i32, String), + StructVariant { a: bool, b: NonZeroU32, c: i32 }, +} + +#[derive(Debug, PartialEq)] +struct TypeError; + +impl<'de> Deserialize<'de> for TypeError { + fn deserialize<D: Deserializer<'de>>(_deserializer: D) -> Result<Self, D::Error> { + Err(D::Error::invalid_type(Unexpected::Unit, &"impossible")) + } +} + +#[test] +fn test_error_positions() { + assert_eq!( + ron::from_str::<TypeError>(" ()"), + Err(SpannedError { + code: Error::InvalidValueForType { + expected: String::from("impossible"), + found: String::from("a unit value"), + }, + position: Position { line: 1, col: 3 }, + }) + ); + + assert_eq!( + ron::from_str::<Test>("StructVariant(a: true, b: 0, c: -42)"), + Err(SpannedError { + code: Error::InvalidValueForType { + expected: String::from("a nonzero u32"), + found: String::from("the unsigned integer `0`"), + }, + position: Position { line: 1, col: 28 }, + }) + ); + + assert_eq!( + ron::from_str::<Test>("TupleVariant(42)"), + Err(SpannedError { + code: Error::ExpectedDifferentLength { + expected: String::from("tuple variant Test::TupleVariant with 2 elements"), + found: 1, + }, + position: Position { line: 1, col: 16 }, + }) + ); + + assert_eq!( + ron::from_str::<Test>("NotAVariant"), + Err(SpannedError { + code: Error::NoSuchEnumVariant { + expected: &["TupleVariant", "StructVariant"], + found: String::from("NotAVariant"), + outer: Some(String::from("Test")), + }, + position: Position { line: 1, col: 12 }, + }) + ); + + assert_eq!( + ron::from_str::<Test>("StructVariant(a: true, b: 1, c: -42, d: \"gotcha\")"), + Err(SpannedError { + code: Error::NoSuchStructField { + expected: &["a", "b", "c"], + found: String::from("d"), + outer: Some(String::from("StructVariant")), + }, + position: Position { line: 1, col: 39 }, + }) + ); + + assert_eq!( + ron::from_str::<Test>("StructVariant(a: true, c: -42)"), + Err(SpannedError { + code: Error::MissingStructField { + field: "b", + outer: Some(String::from("StructVariant")), + }, + position: Position { line: 1, col: 30 }, + }) + ); + + assert_eq!( + ron::from_str::<Test>("StructVariant(a: true, b: 1, a: false, c: -42)"), + Err(SpannedError { + code: Error::DuplicateStructField { + field: "a", + outer: Some(String::from("StructVariant")), + }, + position: Position { line: 1, col: 31 }, + }) + ); +} diff --git a/third_party/rust/ron/tests/207_adjacently_tagged_enum.rs b/third_party/rust/ron/tests/207_adjacently_tagged_enum.rs new file mode 100644 index 0000000000..46fda5c648 --- /dev/null +++ b/third_party/rust/ron/tests/207_adjacently_tagged_enum.rs @@ -0,0 +1,22 @@ +use ron::{de::from_str, ser::to_string}; +use serde::{Deserialize, Serialize}; + +#[derive(Debug, PartialEq, Serialize, Deserialize)] +#[serde(tag = "type", content = "data")] +enum TestEnum { + Name(String), + Index(u32), +} + +#[test] +fn test_adjacently_tagged() { + let source = TestEnum::Index(1); + + let ron_string = to_string(&source).unwrap(); + + assert_eq!(ron_string, "(type:\"Index\",data:1)"); + + let deserialized = from_str::<TestEnum>(&ron_string).unwrap(); + + assert_eq!(deserialized, source); +} diff --git a/third_party/rust/ron/tests/240_array_pretty.rs b/third_party/rust/ron/tests/240_array_pretty.rs new file mode 100644 index 0000000000..db49f57677 --- /dev/null +++ b/third_party/rust/ron/tests/240_array_pretty.rs @@ -0,0 +1,52 @@ +use ron::ser::{to_string_pretty, PrettyConfig}; + +#[test] +fn small_array() { + let arr = &[(), (), ()][..]; + assert_eq!( + to_string_pretty(&arr, PrettyConfig::new().new_line("\n".to_string())).unwrap(), + "[ + (), + (), + (), +]" + ); + assert_eq!( + to_string_pretty( + &arr, + PrettyConfig::new() + .new_line("\n".to_string()) + .compact_arrays(true) + ) + .unwrap(), + "[(), (), ()]" + ); + assert_eq!( + to_string_pretty( + &arr, + PrettyConfig::new() + .new_line("\n".to_string()) + .compact_arrays(true) + .separator("".to_string()) + ) + .unwrap(), + "[(),(),()]" + ); + assert_eq!( + to_string_pretty( + &vec![(1, 2), (3, 4)], + PrettyConfig::new() + .new_line("\n".to_string()) + .separate_tuple_members(true) + .compact_arrays(true) + ) + .unwrap(), + "[( + 1, + 2, +), ( + 3, + 4, +)]" + ); +} diff --git a/third_party/rust/ron/tests/250_variant_newtypes.rs b/third_party/rust/ron/tests/250_variant_newtypes.rs new file mode 100644 index 0000000000..f4c76606c6 --- /dev/null +++ b/third_party/rust/ron/tests/250_variant_newtypes.rs @@ -0,0 +1,407 @@ +use std::collections::HashMap; + +use ron::{ + de::from_str, error::Error, extensions::Extensions, ser::to_string_pretty, ser::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>), +} + +#[derive(Debug, Deserialize, Serialize, Eq, PartialEq)] +struct Unit; + +#[derive(Debug, Deserialize, Serialize, Eq, PartialEq)] +struct Newtype(i32); + +#[derive(Debug, Deserialize, Serialize, Eq, PartialEq)] +struct TupleStruct(u32, bool); + +#[derive(Debug, Deserialize, Serialize, Eq, PartialEq)] +struct Struct { + a: u32, + b: bool, +} + +#[derive(Debug, Deserialize, Serialize, Eq, PartialEq)] +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, + ); + + assert_eq!( + from_str::<TestEnum>(r#"#![enable(unwrap_variant_newtypes)] PrimitiveNewtype("hi")"#) + .unwrap(), + TestEnum::PrimitiveNewtype(String::from("hi")), + ); + + assert_eq!( + from_str::<TestEnum>(r#"#![enable(unwrap_variant_newtypes)] Tuple(4, false)"#).unwrap(), + TestEnum::Tuple(4, false), + ); + + assert_eq!( + from_str::<TestEnum>(r#"#![enable(unwrap_variant_newtypes)] Struct(a: 4, b: false)"#) + .unwrap(), + TestEnum::Struct { a: 4, b: false }, + ); +} + +#[test] +fn test_deserialise_tuple_newtypes() { + assert_eq!( + from_str::<TestEnum>(r#"#![enable(unwrap_variant_newtypes)] TupleNewtypeUnit(Unit)"#) + .unwrap_err() + .code, + Error::ExpectedStructLikeEnd, + ); + assert_eq!( + from_str::<TestEnum>(r#"#![enable(unwrap_variant_newtypes)] TupleNewtypeUnit(())"#) + .unwrap_err() + .code, + Error::ExpectedStructLikeEnd, + ); + assert_eq!( + from_str::<TestEnum>(r#"#![enable(unwrap_variant_newtypes)] TupleNewtypeUnit()"#).unwrap(), + TestEnum::TupleNewtypeUnit(Unit), + ); + + assert_eq!( + from_str::<TestEnum>( + r#"#![enable(unwrap_variant_newtypes)] TupleNewtypeNewtype(Newtype(4))"# + ) + .unwrap_err() + .code, + Error::ExpectedInteger, + ); + assert_eq!( + from_str::<TestEnum>(r#"#![enable(unwrap_variant_newtypes)] TupleNewtypeNewtype((4))"#) + .unwrap_err() + .code, + Error::ExpectedInteger, + ); + assert_eq!( + from_str::<TestEnum>(r#"#![enable(unwrap_variant_newtypes)] TupleNewtypeNewtype(4)"#) + .unwrap(), + TestEnum::TupleNewtypeNewtype(Newtype(4)), + ); + assert_eq!( + from_str::<TestEnum>(r#"#![enable(unwrap_newtypes)] TupleNewtypeNewtype(4)"#).unwrap(), + TestEnum::TupleNewtypeNewtype(Newtype(4)), + ); + assert_eq!( + from_str::<TestEnum>(r#"#![enable(unwrap_newtypes)] #![enable(unwrap_variant_newtypes)] TupleNewtypeNewtype(4)"#).unwrap(), + TestEnum::TupleNewtypeNewtype(Newtype(4)), + ); + + assert_eq!( + from_str::<TestEnum>( + r#"#![enable(unwrap_variant_newtypes)] TupleNewtypeTuple((4, false))"# + ) + .unwrap_err() + .code, + Error::ExpectedInteger, + ); + assert_eq!( + from_str::<TestEnum>(r#"#![enable(unwrap_variant_newtypes)] TupleNewtypeTuple(4, false)"#) + .unwrap(), + TestEnum::TupleNewtypeTuple((4, false)), + ); + + assert_eq!( + from_str::<TestEnum>( + r#"#![enable(unwrap_variant_newtypes)] TupleNewtypeTupleStruct(TupleStruct(4, false))"# + ) + .unwrap_err() + .code, + Error::ExpectedInteger, + ); + assert_eq!( + from_str::<TestEnum>( + r#"#![enable(unwrap_variant_newtypes)] TupleNewtypeTupleStruct((4, false))"# + ) + .unwrap_err() + .code, + Error::ExpectedInteger, + ); + assert_eq!( + from_str::<TestEnum>( + r#"#![enable(unwrap_variant_newtypes)] TupleNewtypeTupleStruct(4, false)"# + ) + .unwrap(), + TestEnum::TupleNewtypeTupleStruct(TupleStruct(4, false)), + ); + + assert_eq!( + from_str::<TestEnum>( + r#"#![enable(unwrap_variant_newtypes)] TupleNewtypeStruct(Struct(a: 4, b: false))"# + ) + .unwrap_err() + .code, + Error::ExpectedMapColon, + ); + assert_eq!( + from_str::<TestEnum>( + r#"#![enable(unwrap_variant_newtypes)] TupleNewtypeStruct((a: 4, b: false))"# + ) + .unwrap_err() + .code, + Error::ExpectedIdentifier, + ); + assert_eq!( + from_str::<TestEnum>( + r#"#![enable(unwrap_variant_newtypes)] TupleNewtypeStruct(a: 4, b: false)"# + ) + .unwrap(), + TestEnum::TupleNewtypeStruct(Struct { a: 4, b: false }), + ); + + assert_eq!( + from_str::<TestEnum>(r#"#![enable(unwrap_variant_newtypes)] TupleNewtypeEnum(A)"#).unwrap(), + TestEnum::TupleNewtypeEnum(Enum::A), + ); + assert_eq!( + from_str::<TestEnum>( + r#"#![enable(unwrap_variant_newtypes)] TupleNewtypeEnum(B(a: 4, b: false))"# + ) + .unwrap(), + TestEnum::TupleNewtypeEnum(Enum::B(Struct { a: 4, b: false })), + ); + assert_eq!( + from_str::<TestEnum>(r#"#![enable(unwrap_variant_newtypes)] TupleNewtypeEnum(C 4, false)"#) + .unwrap_err() + .code, + Error::ExpectedStructLike, + ); + assert_eq!( + from_str::<TestEnum>( + r#"#![enable(unwrap_variant_newtypes)] TupleNewtypeEnum(C(4, false))"# + ) + .unwrap(), + TestEnum::TupleNewtypeEnum(Enum::C(4, false)), + ); + assert_eq!( + from_str::<TestEnum>( + r#"#![enable(unwrap_variant_newtypes)] TupleNewtypeEnum(D a: 4, b: false)"# + ) + .unwrap_err() + .code, + Error::ExpectedStructLike, + ); + assert_eq!( + from_str::<TestEnum>( + r#"#![enable(unwrap_variant_newtypes)] TupleNewtypeEnum(D(a: 4, b: false))"# + ) + .unwrap(), + TestEnum::TupleNewtypeEnum(Enum::D { a: 4, b: false }), + ); + + assert_eq!( + from_str::<TestEnum>(r#"#![enable(unwrap_variant_newtypes)] TupleNewtypeOption(None)"#) + .unwrap(), + TestEnum::TupleNewtypeOption(None), + ); + assert_eq!( + from_str::<TestEnum>( + r#"#![enable(unwrap_variant_newtypes)] TupleNewtypeOption(Some(a: 4, b: false))"# + ) + .unwrap(), + TestEnum::TupleNewtypeOption(Some(Struct { a: 4, b: false })), + ); + assert_eq!( + from_str::<TestEnum>( + r#"#![enable(unwrap_variant_newtypes)] TupleNewtypeOption(a: 4, b: false)"# + ) + .unwrap_err() + .code, + Error::ExpectedOption, + ); + assert_eq!( + from_str::<TestEnum>(r#"#![enable(unwrap_variant_newtypes, implicit_some)] TupleNewtypeOption(a: 4, b: false)"#).unwrap(), + TestEnum::TupleNewtypeOption(Some(Struct { a: 4, b: false })), + ); + + assert_eq!( + from_str::<TestEnum>(r#"#![enable(unwrap_variant_newtypes)] TupleNewtypeSeq([])"#).unwrap(), + TestEnum::TupleNewtypeSeq(vec![]), + ); + assert_eq!( + from_str::<TestEnum>( + r#"#![enable(unwrap_variant_newtypes)] TupleNewtypeSeq([(a: 4, b: false)])"# + ) + .unwrap(), + TestEnum::TupleNewtypeSeq(vec![Struct { a: 4, b: false }]), + ); + assert_eq!( + from_str::<TestEnum>( + r#"#![enable(unwrap_variant_newtypes)] TupleNewtypeSeq([Struct(a: 4, b: false)])"# + ) + .unwrap(), + TestEnum::TupleNewtypeSeq(vec![Struct { a: 4, b: false }]), + ); + + assert_eq!( + from_str::<TestEnum>(r#"#![enable(unwrap_variant_newtypes)] TupleNewtypeMap({})"#).unwrap(), + TestEnum::TupleNewtypeMap(vec![].into_iter().collect()), + ); + assert_eq!( + from_str::<TestEnum>( + r#"#![enable(unwrap_variant_newtypes)] TupleNewtypeMap({2: (a: 4, b: false)})"# + ) + .unwrap(), + TestEnum::TupleNewtypeMap(vec![(2, Struct { a: 4, b: false })].into_iter().collect()), + ); + assert_eq!( + from_str::<TestEnum>( + r#"#![enable(unwrap_variant_newtypes)] TupleNewtypeMap({8: Struct(a: 4, b: false)})"# + ) + .unwrap(), + TestEnum::TupleNewtypeMap(vec![(8, Struct { a: 4, b: false })].into_iter().collect()), + ); +} + +#[test] +fn test_serialise_non_newtypes() { + assert_eq_serialize_roundtrip(TestEnum::Unit, Extensions::UNWRAP_VARIANT_NEWTYPES); + + assert_eq_serialize_roundtrip( + TestEnum::PrimitiveNewtype(String::from("hi")), + Extensions::UNWRAP_VARIANT_NEWTYPES, + ); + + assert_eq_serialize_roundtrip( + TestEnum::Tuple(4, false), + Extensions::UNWRAP_VARIANT_NEWTYPES, + ); + + assert_eq_serialize_roundtrip( + TestEnum::Struct { a: 4, b: false }, + Extensions::UNWRAP_VARIANT_NEWTYPES, + ); +} + +#[test] +fn test_serialise_tuple_newtypes() { + assert_eq_serialize_roundtrip( + TestEnum::TupleNewtypeUnit(Unit), + Extensions::UNWRAP_VARIANT_NEWTYPES, + ); + + assert_eq_serialize_roundtrip( + TestEnum::TupleNewtypeNewtype(Newtype(4)), + Extensions::UNWRAP_VARIANT_NEWTYPES, + ); + assert_eq_serialize_roundtrip( + TestEnum::TupleNewtypeNewtype(Newtype(4)), + Extensions::UNWRAP_NEWTYPES, + ); + assert_eq_serialize_roundtrip( + TestEnum::TupleNewtypeNewtype(Newtype(4)), + Extensions::UNWRAP_VARIANT_NEWTYPES | Extensions::UNWRAP_NEWTYPES, + ); + + assert_eq_serialize_roundtrip( + TestEnum::TupleNewtypeTuple((4, false)), + Extensions::UNWRAP_VARIANT_NEWTYPES, + ); + + assert_eq_serialize_roundtrip( + TestEnum::TupleNewtypeTupleStruct(TupleStruct(4, false)), + Extensions::UNWRAP_VARIANT_NEWTYPES, + ); + + assert_eq_serialize_roundtrip( + TestEnum::TupleNewtypeStruct(Struct { a: 4, b: false }), + Extensions::UNWRAP_VARIANT_NEWTYPES, + ); + + assert_eq_serialize_roundtrip( + TestEnum::TupleNewtypeEnum(Enum::A), + Extensions::UNWRAP_VARIANT_NEWTYPES, + ); + assert_eq_serialize_roundtrip( + TestEnum::TupleNewtypeEnum(Enum::B(Struct { a: 4, b: false })), + Extensions::UNWRAP_VARIANT_NEWTYPES, + ); + assert_eq_serialize_roundtrip( + TestEnum::TupleNewtypeEnum(Enum::C(4, false)), + Extensions::UNWRAP_VARIANT_NEWTYPES, + ); + assert_eq_serialize_roundtrip( + TestEnum::TupleNewtypeEnum(Enum::D { a: 4, b: false }), + Extensions::UNWRAP_VARIANT_NEWTYPES, + ); + + assert_eq_serialize_roundtrip( + TestEnum::TupleNewtypeOption(None), + Extensions::UNWRAP_VARIANT_NEWTYPES, + ); + assert_eq_serialize_roundtrip( + TestEnum::TupleNewtypeOption(Some(Struct { a: 4, b: false })), + Extensions::UNWRAP_VARIANT_NEWTYPES, + ); + assert_eq_serialize_roundtrip( + TestEnum::TupleNewtypeOption(Some(Struct { a: 4, b: false })), + Extensions::IMPLICIT_SOME, + ); + assert_eq_serialize_roundtrip( + TestEnum::TupleNewtypeOption(Some(Struct { a: 4, b: false })), + Extensions::UNWRAP_VARIANT_NEWTYPES | Extensions::IMPLICIT_SOME, + ); + + assert_eq_serialize_roundtrip( + TestEnum::TupleNewtypeSeq(vec![]), + Extensions::UNWRAP_VARIANT_NEWTYPES, + ); + assert_eq_serialize_roundtrip( + TestEnum::TupleNewtypeSeq(vec![Struct { a: 4, b: false }]), + Extensions::UNWRAP_VARIANT_NEWTYPES, + ); + + assert_eq_serialize_roundtrip( + TestEnum::TupleNewtypeMap(vec![].into_iter().collect()), + Extensions::UNWRAP_VARIANT_NEWTYPES, + ); + assert_eq_serialize_roundtrip( + TestEnum::TupleNewtypeMap(vec![(2, Struct { a: 4, b: false })].into_iter().collect()), + Extensions::UNWRAP_VARIANT_NEWTYPES, + ); +} + +fn assert_eq_serialize_roundtrip< + S: Serialize + serde::de::DeserializeOwned + PartialEq + std::fmt::Debug, +>( + value: S, + extensions: Extensions, +) { + assert_eq!( + from_str::<S>( + &to_string_pretty(&value, PrettyConfig::default().extensions(extensions)).unwrap() + ) + .unwrap(), + value, + ); +} diff --git a/third_party/rust/ron/tests/256_comma_error.rs b/third_party/rust/ron/tests/256_comma_error.rs new file mode 100644 index 0000000000..4a873e4762 --- /dev/null +++ b/third_party/rust/ron/tests/256_comma_error.rs @@ -0,0 +1,72 @@ +#![allow(dead_code)] + +use ron::error::{Error, Position, SpannedError}; + +#[derive(Debug, serde::Deserialize)] +struct Test { + a: i32, + b: i32, +} + +#[test] +fn test_missing_comma_error() { + let tuple_string = r#"( + 1 // <-- forgotten comma here + 2 + )"#; + + assert_eq!( + ron::from_str::<(i32, i32)>(tuple_string).unwrap_err(), + SpannedError { + code: Error::ExpectedComma, + position: Position { line: 3, col: 9 } + } + ); + + let list_string = r#"[ + 0, + 1 // <-- forgotten comma here + 2 + ]"#; + + assert_eq!( + ron::from_str::<Vec<i32>>(list_string).unwrap_err(), + SpannedError { + code: Error::ExpectedComma, + position: Position { line: 4, col: 9 } + } + ); + + let struct_string = r#"Test( + a: 1 // <-- forgotten comma here + b: 2 + )"#; + + assert_eq!( + ron::from_str::<Test>(struct_string).unwrap_err(), + SpannedError { + code: Error::ExpectedComma, + position: Position { line: 3, col: 9 } + } + ); + + let map_string = r#"{ + "a": 1 // <-- forgotten comma here + "b": 2 + }"#; + + assert_eq!( + ron::from_str::<std::collections::HashMap<String, i32>>(map_string).unwrap_err(), + SpannedError { + code: Error::ExpectedComma, + position: Position { line: 3, col: 9 } + } + ); +} + +#[test] +fn test_comma_end() { + assert_eq!(ron::from_str::<(i32, i32)>("(0, 1)").unwrap(), (0, 1)); + assert_eq!(ron::from_str::<(i32, i32)>("(0, 1,)").unwrap(), (0, 1)); + assert_eq!(ron::from_str::<()>("()"), Ok(())); +} diff --git a/third_party/rust/ron/tests/289_enumerate_arrays.rs b/third_party/rust/ron/tests/289_enumerate_arrays.rs new file mode 100644 index 0000000000..6e0d5c3071 --- /dev/null +++ b/third_party/rust/ron/tests/289_enumerate_arrays.rs @@ -0,0 +1,47 @@ +const EXPTECTED: &str = "[ + /*[0]*/ None, + /*[1]*/ Some([]), + /*[2]*/ Some([ + /*[0]*/ 42, + ]), + /*[3]*/ Some([ + /*[0]*/ 4, + /*[1]*/ 2, + ]), + /*[4]*/ None, +]"; + +const EXPTECTED_COMPACT: &str = "[/*[0]*/ None, /*[1]*/ Some([]), /*[2]*/ Some([/*[0]*/ 42]), \ +/*[3]*/ Some([/*[0]*/ 4, /*[1]*/ 2]), /*[4]*/ None]"; + +#[test] +fn enumerate_arrays() { + let v: Vec<Option<Vec<u8>>> = vec![None, Some(vec![]), Some(vec![42]), Some(vec![4, 2]), None]; + + let pretty = ron::ser::PrettyConfig::new().enumerate_arrays(true); + + let ser = ron::ser::to_string_pretty(&v, pretty).unwrap(); + + assert_eq!(ser, EXPTECTED); + + let de: Vec<Option<Vec<u8>>> = ron::from_str(&ser).unwrap(); + + assert_eq!(v, de) +} + +#[test] +fn enumerate_compact_arrays() { + let v: Vec<Option<Vec<u8>>> = vec![None, Some(vec![]), Some(vec![42]), Some(vec![4, 2]), None]; + + let pretty = ron::ser::PrettyConfig::new() + .enumerate_arrays(true) + .compact_arrays(true); + + let ser = ron::ser::to_string_pretty(&v, pretty).unwrap(); + + assert_eq!(ser, EXPTECTED_COMPACT); + + let de: Vec<Option<Vec<u8>>> = ron::from_str(&ser).unwrap(); + + assert_eq!(v, de) +} diff --git a/third_party/rust/ron/tests/301_struct_name_mismatch.rs b/third_party/rust/ron/tests/301_struct_name_mismatch.rs new file mode 100644 index 0000000000..063cdac364 --- /dev/null +++ b/third_party/rust/ron/tests/301_struct_name_mismatch.rs @@ -0,0 +1,130 @@ +use ron::error::{Error, Position, SpannedError}; +use serde::Deserialize; + +#[derive(Debug, Deserialize, PartialEq)] +struct MyUnitStruct; + +#[derive(Debug, Deserialize, PartialEq)] +struct MyTupleStruct(bool, i32); + +#[derive(Debug, Deserialize, PartialEq)] +struct MyNewtypeStruct(MyTupleStruct); + +#[derive(Debug, Deserialize, PartialEq)] +struct MyStruct { + a: bool, + b: i32, +} + +#[test] +fn test_unit_struct_name_mismatch() { + assert_eq!(ron::from_str::<MyUnitStruct>("()"), Ok(MyUnitStruct),); + assert_eq!( + ron::from_str::<MyUnitStruct>("MyUnitStruct"), + Ok(MyUnitStruct), + ); + assert_eq!( + ron::from_str::<MyUnitStruct>("MyUnit Struct"), + Err(SpannedError { + code: Error::ExpectedDifferentStructName { + expected: "MyUnitStruct", + found: String::from("MyUnit") + }, + position: Position { line: 1, col: 7 } + }), + ); + assert_eq!( + ron::from_str::<MyUnitStruct>("42"), + Err(SpannedError { + code: Error::ExpectedNamedStructLike("MyUnitStruct"), + position: Position { line: 1, col: 1 } + }), + ); +} + +#[test] +fn test_tuple_struct_name_mismatch() { + assert_eq!( + ron::from_str::<MyTupleStruct>("(true, 42)"), + Ok(MyTupleStruct(true, 42)), + ); + assert_eq!( + ron::from_str::<MyTupleStruct>("MyTupleStruct(true, 42)"), + Ok(MyTupleStruct(true, 42)), + ); + assert_eq!( + ron::from_str::<MyTupleStruct>("MyTypleStruct(true, 42)"), + Err(SpannedError { + code: Error::ExpectedDifferentStructName { + expected: "MyTupleStruct", + found: String::from("MyTypleStruct") + }, + position: Position { line: 1, col: 14 } + }), + ); + assert_eq!( + ron::from_str::<MyTupleStruct>("42"), + Err(SpannedError { + code: Error::ExpectedNamedStructLike("MyTupleStruct"), + position: Position { line: 1, col: 1 } + }), + ); +} + +#[test] +fn test_newtype_struct_name_mismatch() { + assert_eq!( + ron::from_str::<MyNewtypeStruct>("((true, 42))"), + Ok(MyNewtypeStruct(MyTupleStruct(true, 42))), + ); + assert_eq!( + ron::from_str::<MyNewtypeStruct>("MyNewtypeStruct((true, 42))"), + Ok(MyNewtypeStruct(MyTupleStruct(true, 42))), + ); + assert_eq!( + ron::from_str::<MyNewtypeStruct>("MyNewtypeStrucl((true, 42))"), + Err(SpannedError { + code: Error::ExpectedDifferentStructName { + expected: "MyNewtypeStruct", + found: String::from("MyNewtypeStrucl") + }, + position: Position { line: 1, col: 16 } + }), + ); + assert_eq!( + ron::from_str::<MyNewtypeStruct>("42"), + Err(SpannedError { + code: Error::ExpectedNamedStructLike("MyNewtypeStruct"), + position: Position { line: 1, col: 1 } + }), + ); +} + +#[test] +fn test_struct_name_mismatch() { + assert_eq!( + ron::from_str::<MyStruct>("(a: true, b: 42)"), + Ok(MyStruct { a: true, b: 42 }), + ); + assert_eq!( + ron::from_str::<MyStruct>("MyStruct(a: true, b: 42)"), + Ok(MyStruct { a: true, b: 42 }), + ); + assert_eq!( + ron::from_str::<MyStruct>("MuStryct(a: true, b: 42)"), + Err(SpannedError { + code: Error::ExpectedDifferentStructName { + expected: "MyStruct", + found: String::from("MuStryct") + }, + position: Position { line: 1, col: 9 } + }), + ); + assert_eq!( + ron::from_str::<MyStruct>("42"), + Err(SpannedError { + code: Error::ExpectedNamedStructLike("MyStruct"), + position: Position { line: 1, col: 1 } + }), + ); +} diff --git a/third_party/rust/ron/tests/322_escape_idents.rs b/third_party/rust/ron/tests/322_escape_idents.rs new file mode 100644 index 0000000000..f236369f31 --- /dev/null +++ b/third_party/rust/ron/tests/322_escape_idents.rs @@ -0,0 +1,34 @@ +use serde::{Deserialize, Serialize}; + +#[derive(Debug, Deserialize, PartialEq, Serialize)] +#[serde(rename_all = "kebab-case")] +enum MyEnumWithDashes { + ThisIsMyUnitVariant, + ThisIsMyTupleVariant(bool, i32), +} + +#[derive(Debug, Deserialize, PartialEq, Serialize)] +#[serde(rename_all = "kebab-case")] +struct MyStructWithDashes { + my_enum: MyEnumWithDashes, + #[serde(rename = "2nd")] + my_enum2: MyEnumWithDashes, + will_be_renamed: u32, +} + +#[test] +fn roundtrip_ident_with_dash() { + let value = MyStructWithDashes { + my_enum: MyEnumWithDashes::ThisIsMyUnitVariant, + my_enum2: MyEnumWithDashes::ThisIsMyTupleVariant(false, -3), + will_be_renamed: 32, + }; + + let serial = ron::ser::to_string(&value).unwrap(); + + println!("Serialized: {}", serial); + + let deserial = ron::de::from_str(&serial); + + assert_eq!(Ok(value), deserial); +} diff --git a/third_party/rust/ron/tests/337_value_float_roundtrip.rs b/third_party/rust/ron/tests/337_value_float_roundtrip.rs new file mode 100644 index 0000000000..66873eaeab --- /dev/null +++ b/third_party/rust/ron/tests/337_value_float_roundtrip.rs @@ -0,0 +1,29 @@ +#[test] +fn roundtrip_value_float_with_decimals() { + let v: ron::Value = ron::from_str("1.0").unwrap(); + + assert_eq!(v, ron::Value::Number(1.0_f64.into())); + + let ser = ron::ser::to_string(&v).unwrap(); + + let roundtrip = ron::from_str(&ser).unwrap(); + + assert_eq!(v, roundtrip); +} + +#[test] +#[allow(clippy::float_cmp)] +fn roundtrip_value_float_into() { + let v: ron::Value = ron::from_str("1.0").unwrap(); + assert_eq!(v, ron::Value::Number(1.0_f64.into())); + + let ser = ron::ser::to_string(&v).unwrap(); + + let f1: f64 = ron::from_str(&ser).unwrap(); + assert_eq!(f1, 1.0_f64); + + let roundtrip: ron::Value = ron::from_str(&ser).unwrap(); + + let f2: f64 = roundtrip.into_rust().unwrap(); + assert_eq!(f2, 1.0_f64); +} diff --git a/third_party/rust/ron/tests/359_deserialize_seed.rs b/third_party/rust/ron/tests/359_deserialize_seed.rs new file mode 100644 index 0000000000..4bca6c438b --- /dev/null +++ b/third_party/rust/ron/tests/359_deserialize_seed.rs @@ -0,0 +1,57 @@ +#[test] +fn test_deserialize_seed() { + // Test adapted from David Tolnay's serde-yaml: + // https://github.com/dtolnay/serde-yaml/blob/8a806e316302fd2e6541dccee6d166dd51b689d6/tests/test_de.rs#L357-L392 + + struct Seed(i64); + + impl<'de> serde::de::DeserializeSeed<'de> for Seed { + type Value = i64; + + fn deserialize<D>(self, deserializer: D) -> Result<i64, D::Error> + where + D: serde::de::Deserializer<'de>, + { + struct Visitor(i64); + + impl<'de> serde::de::Visitor<'de> for Visitor { + type Value = i64; + + fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result { + write!(formatter, "an integer") + } + + fn visit_i64<E: serde::de::Error>(self, v: i64) -> Result<i64, E> { + Ok(v * self.0) + } + + fn visit_u64<E: serde::de::Error>(self, v: u64) -> Result<i64, E> { + Ok(v as i64 * self.0) + } + } + + deserializer.deserialize_any(Visitor(self.0)) + } + } + + let cases = [("3", 5, 15), ("6", 7, 42), ("-5", 9, -45)]; + + for &(ron, seed, expected) in &cases { + let deserialized = ron::Options::default() + .from_str_seed(ron, Seed(seed)) + .unwrap(); + + assert_eq!(expected, deserialized); + } + + assert_eq!( + ron::Options::default().from_str_seed("'a'", Seed(42)), + Err(ron::error::SpannedError { + code: ron::Error::InvalidValueForType { + expected: String::from("an integer"), + found: String::from("the string \"a\""), + }, + position: ron::error::Position { line: 1, col: 4 }, + }) + ); +} diff --git a/third_party/rust/ron/tests/367_implicit_some.rs b/third_party/rust/ron/tests/367_implicit_some.rs new file mode 100644 index 0000000000..4eeac4df35 --- /dev/null +++ b/third_party/rust/ron/tests/367_implicit_some.rs @@ -0,0 +1,108 @@ +#[derive(Debug, PartialEq, serde::Deserialize, serde::Serialize)] +struct MaybeFields { + f1: i64, + f2: Option<i64>, + f3: Option<Option<i64>>, +} + +#[test] +fn test_recursive_implicit_some() { + // Test case provided by d86leader in + // https://github.com/ron-rs/ron/issues/367#issue-1147920589 + + let x1: std::result::Result<MaybeFields, _> = + ron::from_str("#![enable(implicit_some)]\n(f1: 1)"); + let x2: std::result::Result<MaybeFields, _> = + ron::from_str("#![enable(implicit_some)]\n(f1: 1, f2: None, f3: None)"); + let x3: std::result::Result<MaybeFields, _> = + ron::from_str("#![enable(implicit_some)]\n(f1: 1, f2: 2, f3: 3)"); + let x4: std::result::Result<MaybeFields, _> = + ron::from_str("#![enable(implicit_some)]\n(f1: 1, f2: 2, f3: Some(3))"); + let x5: std::result::Result<MaybeFields, _> = + ron::from_str("#![enable(implicit_some)]\n(f1: 1, f2: 2, f3: Some(Some(3)))"); + let x6: std::result::Result<MaybeFields, _> = + ron::from_str("#![enable(implicit_some)]\n(f1: 1, f2: 2, f3: Some(None))"); + + assert_eq!( + x1, + Ok(MaybeFields { + f1: 1, + f2: None, + f3: None + }) + ); + assert_eq!( + x2, + Ok(MaybeFields { + f1: 1, + f2: None, + f3: None + }) + ); + assert_eq!( + x3, + Ok(MaybeFields { + f1: 1, + f2: Some(2), + f3: Some(Some(3)) + }) + ); + assert_eq!( + x4, + Ok(MaybeFields { + f1: 1, + f2: Some(2), + f3: Some(Some(3)) + }) + ); + assert_eq!( + x5, + Ok(MaybeFields { + f1: 1, + f2: Some(2), + f3: Some(Some(3)) + }) + ); + assert_eq!( + x6, + Ok(MaybeFields { + f1: 1, + f2: Some(2), + f3: Some(None) + }) + ); +} + +#[test] +fn test_nested_implicit_some() { + assert_eq!( + ron::from_str::<Option<Option<Option<u32>>>>("#![enable(implicit_some)]\n5"), + Ok(Some(Some(Some(5)))) + ); + assert_eq!( + ron::from_str::<Option<Option<Option<u32>>>>("#![enable(implicit_some)]\nNone"), + Ok(None) + ); + assert_eq!( + ron::from_str::<Option<Option<Option<u32>>>>("#![enable(implicit_some)]\nSome(5)"), + Ok(Some(Some(Some(5)))) + ); + assert_eq!( + ron::from_str::<Option<Option<Option<u32>>>>("#![enable(implicit_some)]\nSome(None)"), + Ok(Some(None)) + ); + assert_eq!( + ron::from_str::<Option<Option<Option<u32>>>>("#![enable(implicit_some)]\nSome(Some(5))"), + Ok(Some(Some(Some(5)))) + ); + assert_eq!( + ron::from_str::<Option<Option<Option<u32>>>>("#![enable(implicit_some)]\nSome(Some(None))"), + Ok(Some(Some(None))) + ); + assert_eq!( + ron::from_str::<Option<Option<Option<u32>>>>( + "#![enable(implicit_some)]\nSome(Some(Some(5)))" + ), + Ok(Some(Some(Some(5)))) + ); +} diff --git a/third_party/rust/ron/tests/370_float_parsing.rs b/third_party/rust/ron/tests/370_float_parsing.rs new file mode 100644 index 0000000000..786d5ae331 --- /dev/null +++ b/third_party/rust/ron/tests/370_float_parsing.rs @@ -0,0 +1,66 @@ +use ron::{ + de::{Position, SpannedError}, + Error, +}; + +#[test] +fn test_float_literal_parsing() { + assert_eq!(ron::from_str("inf"), Ok(f64::INFINITY)); + assert_eq!(ron::from_str("+inf"), Ok(f64::INFINITY)); + assert_eq!(ron::from_str("-inf"), Ok(f64::NEG_INFINITY)); + + assert!(ron::from_str::<f64>("NaN").unwrap().is_nan()); + assert!(ron::from_str::<f64>("+NaN").unwrap().is_nan()); + assert!(ron::from_str::<f64>("-NaN").unwrap().is_nan()); + + assert_eq!(ron::from_str("1"), Ok(1.0_f64)); + assert_eq!(ron::from_str("+1"), Ok(1.0_f64)); + assert_eq!(ron::from_str("-1"), Ok(-1.0_f64)); + assert_eq!(ron::from_str("1e3"), Ok(1000.0_f64)); + assert_eq!(ron::from_str("1e+1"), Ok(10.0_f64)); + assert_eq!(ron::from_str("7E-1"), Ok(0.7_f64)); + + assert_eq!(ron::from_str("1."), Ok(1.0_f64)); + assert_eq!(ron::from_str("+1.1"), Ok(1.1_f64)); + assert_eq!(ron::from_str("-1.42"), Ok(-1.42_f64)); + assert_eq!(ron::from_str("-1.5e3"), Ok(-1500.0_f64)); + assert_eq!(ron::from_str("1.e+1"), Ok(10.0_f64)); + assert_eq!(ron::from_str("7.4E-1"), Ok(0.74_f64)); + + assert_eq!(ron::from_str(".1"), Ok(0.1_f64)); + assert_eq!(ron::from_str("+.1"), Ok(0.1_f64)); + assert_eq!(ron::from_str("-.42"), Ok(-0.42_f64)); + assert_eq!(ron::from_str("-.5e3"), Ok(-500.0_f64)); + assert_eq!(ron::from_str(".3e+1"), Ok(3.0_f64)); + assert_eq!(ron::from_str(".4E-1"), Ok(0.04_f64)); + + assert_eq!( + ron::from_str::<f64>("1_0.1_0"), + Err(SpannedError { + code: Error::FloatUnderscore, + position: Position { line: 1, col: 2 }, + }) + ); + assert_eq!( + ron::from_str::<f64>("1_0.10"), + Err(SpannedError { + code: Error::FloatUnderscore, + position: Position { line: 1, col: 2 }, + }) + ); + assert_eq!( + ron::from_str::<f64>("10.1_0"), + Err(SpannedError { + code: Error::FloatUnderscore, + position: Position { line: 1, col: 5 }, + }) + ); + + assert_eq!( + ron::from_str::<f64>("1.0e1.0"), + Err(SpannedError { + code: Error::ExpectedFloat, + position: Position { line: 1, col: 8 }, + }) + ); +} diff --git a/third_party/rust/ron/tests/393_serde_errors.rs b/third_party/rust/ron/tests/393_serde_errors.rs new file mode 100644 index 0000000000..f57532bf67 --- /dev/null +++ b/third_party/rust/ron/tests/393_serde_errors.rs @@ -0,0 +1,215 @@ +use ron::error::{Error, Position, SpannedError}; + +#[derive(Debug, serde::Deserialize, PartialEq)] +#[serde(deny_unknown_fields)] +enum TestEnum { + StructVariant { a: bool, b: char, c: i32 }, + NewtypeVariant(TestStruct), +} + +#[derive(Debug, serde::Deserialize, PartialEq)] +#[serde(tag = "type")] +enum TestEnumInternal { + StructVariant { a: bool }, +} + +#[derive(Debug, serde::Deserialize, PartialEq)] +#[serde(tag = "type", content = "content")] +enum TestEnumAdjacent { + StructVariant { a: bool }, +} + +#[derive(Debug, serde::Deserialize, PartialEq)] +#[serde(untagged)] +enum TestEnumUntagged { + StructVariant { a: bool }, +} + +#[derive(Debug, serde::Deserialize, PartialEq)] +#[serde(deny_unknown_fields)] +struct TestStruct { + a: bool, + b: char, + c: i32, +} + +#[test] +fn test_unknown_enum_variant() { + assert_eq!( + ron::from_str::<TestEnum>("NotAVariant"), + Err(SpannedError { + code: Error::NoSuchEnumVariant { + expected: &["StructVariant", "NewtypeVariant"], + found: String::from("NotAVariant"), + outer: Some(String::from("TestEnum")), + }, + position: Position { line: 1, col: 12 }, + }) + ); +} + +#[test] +fn test_struct_enum_fields() { + assert_eq!( + ron::from_str::<TestEnum>("StructVariant(a: true, b: 'b', c: -42, d: \"gotcha\")"), + Err(SpannedError { + code: Error::NoSuchStructField { + expected: &["a", "b", "c"], + found: String::from("d"), + outer: Some(String::from("StructVariant")), + }, + position: Position { line: 1, col: 41 }, + }) + ); + + assert_eq!( + ron::from_str::<TestEnum>("StructVariant(a: true, c: -42)"), + Err(SpannedError { + code: Error::MissingStructField { + field: "b", + outer: Some(String::from("StructVariant")), + }, + position: Position { line: 1, col: 30 }, + }) + ); + + assert_eq!( + ron::from_str::<TestEnum>("StructVariant(a: true, b: 'b', a: false, c: -42)"), + Err(SpannedError { + code: Error::DuplicateStructField { + field: "a", + outer: Some(String::from("StructVariant")), + }, + position: Position { line: 1, col: 33 }, + }) + ); +} + +#[test] +fn test_newtype_enum_fields() { + assert_eq!( + ron::from_str::<TestEnum>("#![enable(unwrap_variant_newtypes)] NewtypeVariant(a: true, b: 'b', c: -42, d: \"gotcha\")"), + Err(SpannedError { + code: Error::NoSuchStructField { + expected: &["a", "b", "c"], + found: String::from("d"), + outer: Some(String::from("NewtypeVariant")), + }, + position: Position { line: 1, col: 78 }, + }) + ); + + assert_eq!( + ron::from_str::<TestEnum>( + "#![enable(unwrap_variant_newtypes)] NewtypeVariant(a: true, c: -42)" + ), + Err(SpannedError { + code: Error::MissingStructField { + field: "b", + outer: Some(String::from("NewtypeVariant")), + }, + position: Position { line: 1, col: 67 }, + }) + ); + + assert_eq!( + ron::from_str::<TestEnum>( + "#![enable(unwrap_variant_newtypes)] NewtypeVariant(a: true, b: 'b', a: false, c: -42)" + ), + Err(SpannedError { + code: Error::DuplicateStructField { + field: "a", + outer: Some(String::from("NewtypeVariant")), + }, + position: Position { line: 1, col: 70 }, + }) + ); +} + +#[test] +fn test_struct_fields() { + assert_eq!( + ron::from_str::<TestStruct>("TestStruct(a: true, b: 'b', c: -42, d: \"gotcha\")"), + Err(SpannedError { + code: Error::NoSuchStructField { + expected: &["a", "b", "c"], + found: String::from("d"), + outer: Some(String::from("TestStruct")), + }, + position: Position { line: 1, col: 38 }, + }) + ); + + assert_eq!( + ron::from_str::<TestStruct>("TestStruct(a: true, c: -42)"), + Err(SpannedError { + code: Error::MissingStructField { + field: "b", + outer: Some(String::from("TestStruct")), + }, + position: Position { line: 1, col: 27 }, + }) + ); + + assert_eq!( + ron::from_str::<TestStruct>("TestStruct(a: true, b: 'b', a: false, c: -42)"), + Err(SpannedError { + code: Error::DuplicateStructField { + field: "a", + outer: Some(String::from("TestStruct")), + }, + position: Position { line: 1, col: 30 }, + }) + ); +} + +#[test] +fn test_internally_tagged_enum() { + // Note: Not extracting the variant type is not great, + // but at least not wrong either + // Since the error occurs in serde-generated user code, + // after successfully deserialising, we cannot annotate + + assert_eq!( + ron::from_str::<TestEnumInternal>("(type: \"StructVariant\")"), + Err(SpannedError { + code: Error::MissingStructField { + field: "a", + outer: None, + }, + position: Position { line: 1, col: 24 }, + }) + ); +} + +#[test] +fn test_adjacently_tagged_enum() { + // Note: TestEnumAdjacent makes sense here since we are now treating + // the enum as a struct + + assert_eq!( + ron::from_str::<TestEnumAdjacent>("(type: \"StructVariant\", content: (d: 4))"), + Err(SpannedError { + code: Error::MissingStructField { + field: "a", + outer: Some(String::from("TestEnumAdjacent")), + }, + position: Position { line: 1, col: 39 }, + }) + ); +} + +#[test] +fn test_untagged_enum() { + // Note: Errors inside untagged enums are not bubbled up + + assert_eq!( + ron::from_str::<TestEnumUntagged>("(a: true, a: false)"), + Err(SpannedError { + code: Error::Message(String::from( + "data did not match any variant of untagged enum TestEnumUntagged" + )), + position: Position { line: 1, col: 20 }, + }) + ); +} diff --git a/third_party/rust/ron/tests/big_struct.rs b/third_party/rust/ron/tests/big_struct.rs new file mode 100644 index 0000000000..40583606f1 --- /dev/null +++ b/third_party/rust/ron/tests/big_struct.rs @@ -0,0 +1,79 @@ +use serde::{Deserialize, Serialize}; + +#[derive(Serialize, Deserialize)] +pub struct ImVec2 { + pub x: f32, + pub y: f32, +} + +#[derive(Serialize, Deserialize)] +pub struct ImColorsSave { + pub text: f32, +} + +#[derive(Serialize, Deserialize)] +pub struct ImGuiStyleSave { + pub alpha: f32, + pub window_padding: ImVec2, + pub window_min_size: ImVec2, + pub window_rounding: f32, + pub window_title_align: ImVec2, + pub child_window_rounding: f32, + pub frame_padding: ImVec2, + pub frame_rounding: f32, + pub item_spacing: ImVec2, + pub item_inner_spacing: ImVec2, + pub touch_extra_padding: ImVec2, + pub indent_spacing: f32, + pub columns_min_spacing: f32, + pub scrollbar_size: f32, + pub scrollbar_rounding: f32, + pub grab_min_size: f32, + pub grab_rounding: f32, + pub button_text_align: ImVec2, + pub display_window_padding: ImVec2, + pub display_safe_area_padding: ImVec2, + pub anti_aliased_lines: bool, + pub anti_aliased_shapes: bool, + pub curve_tessellation_tol: f32, + pub colors: ImColorsSave, + pub new_type: NewType, +} + +#[derive(Serialize, Deserialize)] +pub struct NewType(i32); + +const CONFIG: &str = "( + alpha: 1.0, + window_padding: (x: 8, y: 8), + window_min_size: (x: 32, y: 32), + window_rounding: 9.0, + window_title_align: (x: 0.0, y: 0.5), + child_window_rounding: 0.0, + frame_padding: (x: 4, y: 3), + frame_rounding: 0.0, + item_spacing: (x: 8, y: 4), + item_inner_spacing: (x: 4, y: 4), + touch_extra_padding: (x: 0, y: 0), + indent_spacing: 21.0, + columns_min_spacing: 6.0, + scrollbar_size: 16, + scrollbar_rounding: 9, + grab_min_size: 10, + grab_rounding: 0, + button_text_align: (x: 0.5, y: 0.5), + display_window_padding: (x: 22, y: 22), + display_safe_area_padding: (x: 4, y: 4), + anti_aliased_lines: true, + anti_aliased_shapes: true, + curve_tessellation_tol: 1.25, + colors: (text: 4), + new_type: NewType( 1 ), + + ignored_field: \"Totally ignored, not causing a panic. Hopefully.\", +)"; + +#[test] +fn deserialize_big_struct() { + ron::de::from_str::<ImGuiStyleSave>(CONFIG).unwrap(); +} diff --git a/third_party/rust/ron/tests/borrowed_str.rs b/third_party/rust/ron/tests/borrowed_str.rs new file mode 100644 index 0000000000..f01eeb141f --- /dev/null +++ b/third_party/rust/ron/tests/borrowed_str.rs @@ -0,0 +1,16 @@ +use serde::{Deserialize, Serialize}; + +#[derive(Debug, Serialize, Deserialize, PartialEq, Eq)] +struct Borrowed<'a> { + value: &'a str, +} + +const BORROWED: &str = "Borrowed(value: \"test\")"; + +#[test] +fn borrowed_str() { + assert_eq!( + ron::de::from_str(BORROWED).ok(), + Some(Borrowed { value: "test" }) + ); +} diff --git a/third_party/rust/ron/tests/comments.rs b/third_party/rust/ron/tests/comments.rs new file mode 100644 index 0000000000..2b77de6187 --- /dev/null +++ b/third_party/rust/ron/tests/comments.rs @@ -0,0 +1,52 @@ +use ron::de::{from_str, Error, Position, SpannedError as RonErr}; + +#[test] +fn test_simple() { + assert_eq!( + from_str( + "/* + * We got a hexadecimal number here! + * + */0x507" + ), + Ok(0x507) + ); +} + +#[test] +fn test_nested() { + assert_eq!( + from_str( + "/* + /* quite * some * nesting * going * on * /* here /* (yeah, maybe a bit too much) */ */ */ + */ + // The actual value comes.. /* + // very soon, these are just checks that */ + // multi-line comments don't trigger in line comments /* +\"THE VALUE\" /* This is the value /* :) */ */ + " + ), + Ok("THE VALUE".to_owned()) + ); +} + +#[test] +fn test_unclosed() { + assert_eq!( + from_str::<String>( + "/* + /* quite * some * nesting * going * on * /* here /* (yeah, maybe a bit too much) */ */ */ + */ + // The actual value comes.. /* + // very soon, these are just checks that */ + // multi-line comments don't trigger in line comments /* +/* Unfortunately, this comment won't get closed :( +\"THE VALUE (which is invalid)\" +" + ), + Err(RonErr { + code: Error::UnclosedBlockComment, + position: Position { col: 1, line: 9 } + }) + ); +} diff --git a/third_party/rust/ron/tests/depth_limit.rs b/third_party/rust/ron/tests/depth_limit.rs new file mode 100644 index 0000000000..6d197bde93 --- /dev/null +++ b/third_party/rust/ron/tests/depth_limit.rs @@ -0,0 +1,59 @@ +use serde::Serialize; +use std::collections::HashMap; + +#[derive(Serialize)] +struct Config { + float: (f32, f64), + tuple: TupleStruct, + map: HashMap<u8, char>, + nested: Nested, + var: Variant, + array: Vec<()>, +} + +#[derive(Serialize)] +struct TupleStruct((), bool); + +#[derive(Serialize)] +enum Variant { + A(u8, &'static str), +} + +#[derive(Serialize)] +struct Nested { + a: String, + b: char, +} + +const EXPECTED: &str = "( + float: (2.18, -1.1), + tuple: ((), false), + map: {8: '1'}, + nested: (a: \"a\", b: 'b'), + var: A(255, \"\"), + array: [(), (), ()], +)"; + +#[test] +fn depth_limit() { + let data = Config { + float: (2.18, -1.1), + tuple: TupleStruct((), false), + map: vec![(8, '1')].into_iter().collect(), + nested: Nested { + a: "a".to_owned(), + b: 'b', + }, + var: Variant::A(!0, ""), + array: vec![(); 3], + }; + + let pretty = ron::ser::PrettyConfig::new() + .depth_limit(1) + .separate_tuple_members(true) + .enumerate_arrays(true) + .new_line("\n".to_string()); + let s = ron::ser::to_string_pretty(&data, pretty); + + assert_eq!(s, Ok(EXPECTED.to_string())); +} diff --git a/third_party/rust/ron/tests/escape.rs b/third_party/rust/ron/tests/escape.rs new file mode 100644 index 0000000000..48f5fdaa1d --- /dev/null +++ b/third_party/rust/ron/tests/escape.rs @@ -0,0 +1,77 @@ +use ron::{de::from_str, ser::to_string}; +use serde::{Deserialize, Serialize}; +use std::{char::from_u32, fmt::Debug}; + +#[test] +fn test_escape_basic() { + assert_eq!(to_string(&"\x07").unwrap(), "\"\\u{7}\""); + + assert_eq!(from_str::<String>("\"\\x07\"").unwrap(), "\x07"); + assert_eq!(from_str::<String>("\"\\u{7}\"").unwrap(), "\x07"); + + assert_eq!(from_str::<char>("\'\\x07\'").unwrap(), '\x07'); + assert_eq!(from_str::<char>("\'\\u{7}\'").unwrap(), '\x07'); +} + +fn check_same<T>(t: T) +where + T: Debug + for<'a> Deserialize<'a> + PartialEq + Serialize, +{ + let s: String = to_string(&t).unwrap(); + + println!("Serialized: \n\n{}\n\n", s); + + assert_eq!(from_str(&s), Ok(t)); +} + +#[test] +fn test_ascii_10() { + check_same("\u{10}".to_owned()); +} + +#[test] +fn test_ascii_chars() { + (1..128).into_iter().flat_map(from_u32).for_each(check_same) +} + +#[test] +fn test_ascii_string() { + let s: String = (1..128).into_iter().flat_map(from_u32).collect(); + + check_same(s); +} + +#[test] +fn test_non_ascii() { + assert_eq!(to_string(&"♠").unwrap(), "\"♠\""); + assert_eq!(to_string(&"ß").unwrap(), "\"ß\""); + assert_eq!(to_string(&"ä").unwrap(), "\"ä\""); + assert_eq!(to_string(&"ö").unwrap(), "\"ö\""); + assert_eq!(to_string(&"ü").unwrap(), "\"ü\""); +} + +#[test] +fn test_chars() { + assert_eq!(to_string(&'♠').unwrap(), "'♠'"); + assert_eq!(to_string(&'ß').unwrap(), "'ß'"); + assert_eq!(to_string(&'ä').unwrap(), "'ä'"); + assert_eq!(to_string(&'ö').unwrap(), "'ö'"); + assert_eq!(to_string(&'ü').unwrap(), "'ü'"); + assert_eq!(to_string(&'\u{715}').unwrap(), "'\u{715}'"); + assert_eq!( + from_str::<char>("'\u{715}'").unwrap(), + from_str("'\\u{715}'").unwrap() + ); +} + +#[test] +fn test_nul_in_string() { + assert_eq!( + from_str("\"Hello\0World!\""), + Ok(String::from("Hello\0World!")) + ); + + check_same("Hello\0World!".to_owned()); + check_same("Hello\x00World!".to_owned()); + check_same("Hello\u{0}World!".to_owned()); +} diff --git a/third_party/rust/ron/tests/extensions.rs b/third_party/rust/ron/tests/extensions.rs new file mode 100644 index 0000000000..6161d90b43 --- /dev/null +++ b/third_party/rust/ron/tests/extensions.rs @@ -0,0 +1,91 @@ +use serde::{Deserialize, Serialize}; +use std::collections::HashMap; + +#[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>, +} + +const CONFIG_U_NT: &str = " +#![enable(unwrap_newtypes)] + +( + tuple: ((), 0.5, ((), -5)), + vec: [ + None, + Some(()), + ], + map: { + 7: Bool(true), + 9: Chars('x', \"\"), + 6: Bool(false), + 5: Unit, + }, +) +"; + +#[test] +fn unwrap_newtypes() { + let d: Struct = ron::de::from_str(CONFIG_U_NT).expect("Failed to deserialize"); + + println!("unwrap_newtypes: {:#?}", d); + + let s = ron::ser::to_string_pretty( + &d, + ron::ser::PrettyConfig::default().extensions(ron::extensions::Extensions::UNWRAP_NEWTYPES), + ) + .expect("Failed to serialize"); + + let d2: Struct = ron::de::from_str(&s).expect("Failed to deserialize"); + + assert_eq!(d, d2); +} + +const CONFIG_I_S: &str = " +#![enable(implicit_some)] + +( + tuple: ((), (0.5), ((), -5)), + vec: [ + None, + (), + UnitStruct, + None, + (), + ], + map: { + (7): Bool(true), + (9): Chars('x', \"\"), + (6): Bool(false), + (5): Unit, + }, +) +"; + +#[test] +fn implicit_some() { + let d: Struct = ron::de::from_str(CONFIG_I_S).expect("Failed to deserialize"); + + println!("implicit_some: {:#?}", d); +} diff --git a/third_party/rust/ron/tests/floats.rs b/third_party/rust/ron/tests/floats.rs new file mode 100644 index 0000000000..9a5474fa68 --- /dev/null +++ b/third_party/rust/ron/tests/floats.rs @@ -0,0 +1,23 @@ +use ron::{ + de::from_str, + ser::{to_string, to_string_pretty, PrettyConfig}, +}; + +#[test] +fn test_inf_and_nan() { + assert_eq!(from_str("inf"), Ok(std::f64::INFINITY)); + assert_eq!(from_str("-inf"), Ok(std::f64::NEG_INFINITY)); + assert_eq!(from_str::<f64>("NaN").map(|n| n.is_nan()), Ok(true)) +} + +#[test] +fn decimal_floats() { + let non_pretty = to_string(&1.0).unwrap(); + assert_eq!(non_pretty, "1.0"); + + let with_pretty = to_string_pretty(&1.0, PrettyConfig::new()).unwrap(); + assert_eq!(with_pretty, "1.0"); + + let tiny_pretty = to_string_pretty(&0.00000000000000005, PrettyConfig::new()).unwrap(); + assert_eq!(tiny_pretty, "0.00000000000000005"); +} diff --git a/third_party/rust/ron/tests/large_number.rs b/third_party/rust/ron/tests/large_number.rs new file mode 100644 index 0000000000..c8ae49f0b4 --- /dev/null +++ b/third_party/rust/ron/tests/large_number.rs @@ -0,0 +1,28 @@ +use ron::value::{Number, Value}; + +#[test] +fn test_large_number() { + let test_var = Value::Number(Number::new(10000000000000000000000.0f64)); + let test_ser = ron::ser::to_string(&test_var).unwrap(); + let test_deser = ron::de::from_str::<Value>(&test_ser); + + assert_eq!( + test_deser.unwrap(), + Value::Number(Number::new(10000000000000000000000.0)) + ); +} + +#[test] +fn test_large_integer_to_float() { + use ron::value::Float; + let test_var = std::i64::MAX as u64 + 1; + let expected = test_var as f64; // Is exactly representable by f64 + let test_ser = ron::ser::to_string(&test_var).unwrap(); + assert_eq!(test_ser, test_var.to_string()); + let test_deser = ron::de::from_str::<Value>(&test_ser); + + assert_eq!( + test_deser.unwrap(), + Value::Number(Number::Float(Float::new(expected))), + ); +} diff --git a/third_party/rust/ron/tests/min_max.rs b/third_party/rust/ron/tests/min_max.rs new file mode 100644 index 0000000000..cbe6c0298b --- /dev/null +++ b/third_party/rust/ron/tests/min_max.rs @@ -0,0 +1,69 @@ +use ron::{de::*, ser::*}; + +#[test] +fn test_i32_min() { + assert_eq!( + std::i32::MIN, + from_str(&to_string(&std::i32::MIN).unwrap()).unwrap() + ); +} + +#[test] +fn test_i32_max() { + assert_eq!( + std::i32::MAX, + from_str(&to_string(&std::i32::MAX).unwrap()).unwrap() + ); +} + +#[test] +fn test_i64_min() { + assert_eq!( + std::i64::MIN, + from_str(&to_string(&std::i64::MIN).unwrap()).unwrap() + ); +} + +#[test] +fn test_i64_max() { + assert_eq!( + std::i64::MAX, + from_str(&to_string(&std::i64::MAX).unwrap()).unwrap() + ); +} + +#[cfg(feature = "integer128")] +#[test] +fn test_i128_min() { + assert_eq!( + std::i128::MIN, + from_str(&to_string(&std::i128::MIN).unwrap()).unwrap() + ); +} + +#[cfg(feature = "integer128")] +#[test] +fn test_i128_max() { + assert_eq!( + std::i128::MAX, + from_str(&to_string(&std::i128::MAX).unwrap()).unwrap() + ); +} + +#[cfg(feature = "integer128")] +#[test] +fn test_u128_min() { + assert_eq!( + std::u128::MIN, + from_str(&to_string(&std::u128::MIN).unwrap()).unwrap() + ); +} + +#[cfg(feature = "integer128")] +#[test] +fn test_u128_max() { + assert_eq!( + std::u128::MAX, + from_str(&to_string(&std::u128::MAX).unwrap()).unwrap() + ); +} diff --git a/third_party/rust/ron/tests/numbers.rs b/third_party/rust/ron/tests/numbers.rs new file mode 100644 index 0000000000..4717ddb9f2 --- /dev/null +++ b/third_party/rust/ron/tests/numbers.rs @@ -0,0 +1,111 @@ +use ron::de::from_str; +use ron::error::{Error, Position, SpannedError}; + +#[test] +fn test_hex() { + assert_eq!(from_str("0x507"), Ok(0x507)); + assert_eq!(from_str("0x1A5"), Ok(0x1A5)); + assert_eq!(from_str("0x53C537"), Ok(0x53C537)); + + assert_eq!( + from_str::<u8>("0x"), + Err(SpannedError { + code: Error::ExpectedInteger, + position: Position { line: 1, col: 3 }, + }) + ); + assert_eq!( + from_str::<u8>("0x_1"), + Err(SpannedError { + code: Error::UnderscoreAtBeginning, + position: Position { line: 1, col: 3 }, + }) + ); + assert_eq!( + from_str::<u8>("0xFFF"), + Err(SpannedError { + code: Error::IntegerOutOfBounds, + position: Position { line: 1, col: 6 }, + }) + ); +} + +#[test] +fn test_bin() { + assert_eq!(from_str("0b101"), Ok(0b101)); + assert_eq!(from_str("0b001"), Ok(0b001)); + assert_eq!(from_str("0b100100"), Ok(0b100100)); + + assert_eq!( + from_str::<u8>("0b"), + Err(SpannedError { + code: Error::ExpectedInteger, + position: Position { line: 1, col: 3 }, + }) + ); + assert_eq!( + from_str::<u8>("0b_1"), + Err(SpannedError { + code: Error::UnderscoreAtBeginning, + position: Position { line: 1, col: 3 }, + }) + ); + assert_eq!( + from_str::<u8>("0b111111111"), + Err(SpannedError { + code: Error::IntegerOutOfBounds, + position: Position { line: 1, col: 12 }, + }) + ); +} + +#[test] +fn test_oct() { + assert_eq!(from_str("0o1461"), Ok(0o1461)); + assert_eq!(from_str("0o051"), Ok(0o051)); + assert_eq!(from_str("0o150700"), Ok(0o150700)); + + assert_eq!( + from_str::<u8>("0o"), + Err(SpannedError { + code: Error::ExpectedInteger, + position: Position { line: 1, col: 3 }, + }) + ); + assert_eq!( + from_str::<u8>("0o_1"), + Err(SpannedError { + code: Error::UnderscoreAtBeginning, + position: Position { line: 1, col: 3 }, + }) + ); + assert_eq!( + from_str::<u8>("0o77777"), + Err(SpannedError { + code: Error::IntegerOutOfBounds, + position: Position { line: 1, col: 8 }, + }) + ); +} + +#[test] +fn test_dec() { + assert_eq!(from_str("1461"), Ok(1461)); + assert_eq!(from_str("51"), Ok(51)); + assert_eq!(from_str("150700"), Ok(150700)); + + assert_eq!( + from_str::<i8>("-_1"), + Err(SpannedError { + code: Error::UnderscoreAtBeginning, + position: Position { line: 1, col: 2 }, + }) + ); + assert_eq!( + from_str::<u8>("256"), + Err(SpannedError { + code: Error::IntegerOutOfBounds, + position: Position { line: 1, col: 4 }, + }) + ); +} diff --git a/third_party/rust/ron/tests/options.rs b/third_party/rust/ron/tests/options.rs new file mode 100644 index 0000000000..89a122af8a --- /dev/null +++ b/third_party/rust/ron/tests/options.rs @@ -0,0 +1,53 @@ +use serde::{Deserialize, Serialize}; + +use ron::{extensions::Extensions, ser::PrettyConfig, Options}; + +#[derive(Serialize, Deserialize)] +struct Newtype(f64); + +#[derive(Serialize, Deserialize)] +struct Struct(Option<u32>, Newtype); + +#[test] +fn default_options() { + let ron = Options::default(); + + let de: Struct = ron.from_str("(Some(42),(4.2))").unwrap(); + let ser = ron.to_string(&de).unwrap(); + + assert_eq!(ser, "(Some(42),(4.2))") +} + +#[test] +fn single_default_extension() { + let ron = Options::default().with_default_extension(Extensions::IMPLICIT_SOME); + + let de: Struct = ron.from_str("(42,(4.2))").unwrap(); + let ser = ron.to_string(&de).unwrap(); + + assert_eq!(ser, "(42,(4.2))"); + + let de: Struct = ron.from_str("#![enable(implicit_some)](42,(4.2))").unwrap(); + let ser = ron.to_string(&de).unwrap(); + + assert_eq!(ser, "(42,(4.2))"); + + let de: Struct = ron + .from_str("#![enable(implicit_some)]#![enable(unwrap_newtypes)](42,4.2)") + .unwrap(); + let ser = ron.to_string(&de).unwrap(); + + assert_eq!(ser, "(42,(4.2))"); + + let de: Struct = ron + .from_str("#![enable(implicit_some)]#![enable(unwrap_newtypes)](42,4.2)") + .unwrap(); + let ser = ron + .to_string_pretty( + &de, + PrettyConfig::default().extensions(Extensions::UNWRAP_NEWTYPES), + ) + .unwrap(); + + assert_eq!(ser, "#![enable(unwrap_newtypes)]\n(42, 4.2)"); +} diff --git a/third_party/rust/ron/tests/preserve_sequence.rs b/third_party/rust/ron/tests/preserve_sequence.rs new file mode 100644 index 0000000000..8ffb20d86f --- /dev/null +++ b/third_party/rust/ron/tests/preserve_sequence.rs @@ -0,0 +1,47 @@ +use ron::{ + de::from_str, + ser::{to_string_pretty, PrettyConfig}, +}; +use serde::{Deserialize, Serialize}; +use std::collections::BTreeMap; + +#[derive(Debug, Deserialize, Serialize)] +struct Config { + boolean: bool, + float: f32, + map: BTreeMap<u8, char>, + nested: Nested, + tuple: (u32, u32), +} + +#[derive(Debug, Deserialize, Serialize)] +struct Nested { + a: String, + b: char, +} + +fn read_original(source: &str) -> String { + source.to_string().replace("\r\n", "\n") +} + +fn make_roundtrip(source: &str) -> String { + let config: Config = from_str(source).unwrap(); + let pretty = PrettyConfig::new() + .depth_limit(3) + .separate_tuple_members(true) + .enumerate_arrays(true) + .new_line("\n".into()); + to_string_pretty(&config, pretty).expect("Serialization failed") +} + +#[test] +fn test_sequence_ex1() { + let file = include_str!("preserve_sequence_ex1.ron"); + assert_eq!(read_original(file), make_roundtrip(file)); +} + +#[test] +fn test_sequence_ex2() { + let file = include_str!("preserve_sequence_ex2.ron"); + assert_eq!(read_original(file), make_roundtrip(file)); +} diff --git a/third_party/rust/ron/tests/preserve_sequence_ex1.ron b/third_party/rust/ron/tests/preserve_sequence_ex1.ron new file mode 100644 index 0000000000..8373cfd379 --- /dev/null +++ b/third_party/rust/ron/tests/preserve_sequence_ex1.ron @@ -0,0 +1,20 @@ +( + boolean: true, + float: 8.2, + map: { + 1: '1', + 2: '4', + 3: '9', + 4: '1', + 5: '2', + 6: '3', + }, + nested: ( + a: "Decode me!", + b: 'z', + ), + tuple: ( + 3, + 7, + ), +)
\ No newline at end of file diff --git a/third_party/rust/ron/tests/preserve_sequence_ex2.ron b/third_party/rust/ron/tests/preserve_sequence_ex2.ron new file mode 100644 index 0000000000..42c0e5a190 --- /dev/null +++ b/third_party/rust/ron/tests/preserve_sequence_ex2.ron @@ -0,0 +1,15 @@ +( + boolean: true, + float: 8.2, + map: { + 1: '1', + }, + nested: ( + a: "Decode me!", + b: 'z', + ), + tuple: ( + 3, + 7, + ), +)
\ No newline at end of file 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); +} diff --git a/third_party/rust/ron/tests/struct_integers.rs b/third_party/rust/ron/tests/struct_integers.rs new file mode 100644 index 0000000000..385d53dab3 --- /dev/null +++ b/third_party/rust/ron/tests/struct_integers.rs @@ -0,0 +1,39 @@ +use serde::{Deserialize, Serialize}; + +#[derive(Debug, Eq, PartialEq, Serialize, Deserialize)] +struct S { + a: i8, + b: i16, + c: i32, + d: i64, + #[cfg(feature = "integer128")] + e: i128, + f: u8, + g: u16, + h: u32, + i: u64, + #[cfg(feature = "integer128")] + j: u128, +} + +#[test] +fn roundtrip() { + let s = S { + a: std::i8::MIN, + b: std::i16::MIN, + c: std::i32::MIN, + d: std::i64::MIN, + #[cfg(feature = "integer128")] + e: std::i128::MIN, + f: std::u8::MAX, + g: std::u16::MAX, + h: std::u32::MAX, + i: std::u64::MAX, + #[cfg(feature = "integer128")] + j: std::u128::MAX, + }; + let serialized = ron::ser::to_string(&s).unwrap(); + dbg!(&serialized); + let deserialized = ron::de::from_str(&serialized).unwrap(); + assert_eq!(s, deserialized,); +} diff --git a/third_party/rust/ron/tests/to_string_pretty.rs b/third_party/rust/ron/tests/to_string_pretty.rs new file mode 100644 index 0000000000..0e1f93d1aa --- /dev/null +++ b/third_party/rust/ron/tests/to_string_pretty.rs @@ -0,0 +1,21 @@ +use ron::ser::{to_string_pretty, PrettyConfig}; +use ron::to_string; +use serde::{Deserialize, Serialize}; + +#[derive(Debug, PartialEq, Deserialize, Serialize)] +struct Point { + x: f64, + y: f64, +} + +#[test] +fn test_struct_names() { + let value = Point { x: 1.0, y: 2.0 }; + let struct_name = to_string_pretty(&value, PrettyConfig::default().struct_names(true)); + assert_eq!( + struct_name, + Ok("Point(\n x: 1.0,\n y: 2.0,\n)".to_string()) + ); + let no_struct_name = to_string(&value); + assert_eq!(no_struct_name, Ok("(x:1.0,y:2.0)".to_string())); +} diff --git a/third_party/rust/ron/tests/unicode.rs b/third_party/rust/ron/tests/unicode.rs new file mode 100644 index 0000000000..0617eeed14 --- /dev/null +++ b/third_party/rust/ron/tests/unicode.rs @@ -0,0 +1,13 @@ +use ron::de::from_str; + +#[test] +fn test_char() { + let de: char = from_str("'Փ'").unwrap(); + assert_eq!(de, 'Փ'); +} + +#[test] +fn test_string() { + let de: String = from_str("\"My string: ऄ\"").unwrap(); + assert_eq!(de, "My string: ऄ"); +} diff --git a/third_party/rust/ron/tests/value.rs b/third_party/rust/ron/tests/value.rs new file mode 100644 index 0000000000..8256d3cf95 --- /dev/null +++ b/third_party/rust/ron/tests/value.rs @@ -0,0 +1,131 @@ +use ron::value::{Map, Number, Value}; +use serde::Serialize; +use std::f64; + +#[test] +fn bool() { + assert_eq!("true".parse(), Ok(Value::Bool(true))); + assert_eq!("false".parse(), Ok(Value::Bool(false))); +} + +#[test] +fn char() { + assert_eq!("'a'".parse(), Ok(Value::Char('a'))); +} + +#[test] +fn map() { + let mut map = Map::new(); + map.insert(Value::Char('a'), Value::Number(Number::new(1))); + map.insert(Value::Char('b'), Value::Number(Number::new(2f64))); + assert_eq!("{ 'a': 1, 'b': 2.0 }".parse(), Ok(Value::Map(map))); +} + +#[test] +fn number() { + assert_eq!("42".parse(), Ok(Value::Number(Number::new(42)))); + assert_eq!( + "3.141592653589793".parse(), + Ok(Value::Number(Number::new(f64::consts::PI))) + ); +} + +#[test] +fn option() { + let opt = Some(Box::new(Value::Char('c'))); + assert_eq!("Some('c')".parse(), Ok(Value::Option(opt))); +} + +#[test] +fn string() { + let normal = "\"String\""; + assert_eq!(normal.parse(), Ok(Value::String("String".into()))); + + let raw = "r\"Raw String\""; + assert_eq!(raw.parse(), Ok(Value::String("Raw String".into()))); + + let raw_hashes = "r#\"Raw String\"#"; + assert_eq!(raw_hashes.parse(), Ok(Value::String("Raw String".into()))); + + let raw_escaped = "r##\"Contains \"#\"##"; + assert_eq!( + raw_escaped.parse(), + Ok(Value::String("Contains \"#".into())) + ); + + let raw_multi_line = "r\"Multi\nLine\""; + assert_eq!( + raw_multi_line.parse(), + Ok(Value::String("Multi\nLine".into())) + ); +} + +#[test] +fn seq() { + let seq = vec![ + Value::Number(Number::new(1)), + Value::Number(Number::new(2f64)), + ]; + assert_eq!("[1, 2.0]".parse(), Ok(Value::Seq(seq))); +} + +#[test] +fn unit() { + use ron::error::{Error, Position, SpannedError}; + + assert_eq!("()".parse(), Ok(Value::Unit)); + assert_eq!("Foo".parse(), Ok(Value::Unit)); + + assert_eq!( + "".parse::<Value>(), + Err(SpannedError { + code: Error::Eof, + position: Position { col: 1, line: 1 } + }) + ); +} + +#[derive(Serialize)] +struct Scene(Option<(u32, u32)>); + +#[derive(Serialize)] +struct Scene2 { + foo: Option<(u32, u32)>, +} + +#[test] +fn roundtrip() { + use ron::{de::from_str, ser::to_string}; + + { + let s = to_string(&Scene2 { + foo: Some((122, 13)), + }) + .unwrap(); + println!("{}", s); + let scene: Value = from_str(&s).unwrap(); + println!("{:?}", scene); + } + { + let s = to_string(&Scene(Some((13, 122)))).unwrap(); + println!("{}", s); + let scene: Value = from_str(&s).unwrap(); + println!("{:?}", scene); + } +} + +#[test] +fn map_roundtrip_338() { + // https://github.com/ron-rs/ron/issues/338 + + let v: Value = ron::from_str("{}").unwrap(); + println!("{:?}", v); + + let ser = ron::to_string(&v).unwrap(); + println!("{:?}", ser); + + let roundtrip = ron::from_str(&ser).unwrap(); + println!("{:?}", roundtrip); + + assert_eq!(v, roundtrip); +} |