diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-28 14:29:10 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-28 14:29:10 +0000 |
commit | 2aa4a82499d4becd2284cdb482213d541b8804dd (patch) | |
tree | b80bf8bf13c3766139fbacc530efd0dd9d54394c /third_party/rust/ron/tests | |
parent | Initial commit. (diff) | |
download | firefox-2aa4a82499d4becd2284cdb482213d541b8804dd.tar.xz firefox-2aa4a82499d4becd2284cdb482213d541b8804dd.zip |
Adding upstream version 86.0.1.upstream/86.0.1upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'third_party/rust/ron/tests')
22 files changed, 1344 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..a10a11e27e --- /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().skip(1).next().unwrap(), + Value::String("shell command".to_string()) + ); + } + _ => panic!(), + }, + _ => panic!(), + } + + 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().skip(1).next().unwrap(), + Value::String("debug message".to_string()) + ); + } + _ => panic!(), + }, + _ => panic!(), + } +} 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..827ed8e498 --- /dev/null +++ b/third_party/rust/ron/tests/147_empty_sets_serialisation.rs @@ -0,0 +1,69 @@ +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>, + deep_vec: HashMap<Key, Vec<()>>, + deep_map: HashMap<Key, HashMap<Key, Enum>>, +} + +#[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() + .with_enumerate_arrays(true) + .with_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/240_array_pretty.rs b/third_party/rust/ron/tests/240_array_pretty.rs new file mode 100644 index 0000000000..957be1572c --- /dev/null +++ b/third_party/rust/ron/tests/240_array_pretty.rs @@ -0,0 +1,14 @@ +use ron::ser::{to_string_pretty, PrettyConfig}; + +#[test] +fn small_array() { + let arr = &[(), (), ()][..]; + assert_eq!( + to_string_pretty(&arr, PrettyConfig::new().with_new_line("\n".to_string())).unwrap(), + "[ + (), + (), + (), +]" + ); +} 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..f5d6f73c12 --- /dev/null +++ b/third_party/rust/ron/tests/comments.rs @@ -0,0 +1,52 @@ +use ron::de::{from_str, Error as RonErr, ErrorCode, Position}; + +#[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: ErrorCode::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..5c74f28299 --- /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() + .with_depth_limit(1) + .with_separate_tuple_members(true) + .with_enumerate_arrays(true) + .with_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..20cc87ee1e --- /dev/null +++ b/third_party/rust/ron/tests/escape.rs @@ -0,0 +1,67 @@ +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"); +} + +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() { + check_same("Hello\0World!".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..81157257ab --- /dev/null +++ b/third_party/rust/ron/tests/extensions.rs @@ -0,0 +1,81 @@ +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); +} + +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..e19726b1ba --- /dev/null +++ b/third_party/rust/ron/tests/floats.rs @@ -0,0 +1,30 @@ +use ron::{ + de::from_str, + ser::{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 pretty = PrettyConfig::new().with_decimal_floats(false); + let without_decimal = to_string_pretty(&1.0, pretty).unwrap(); + assert_eq!(without_decimal, "1"); + + let pretty = PrettyConfig::new().with_decimal_floats(false); + let without_decimal = to_string_pretty(&1.1, pretty).unwrap(); + assert_eq!(without_decimal, "1.1"); + + let pretty = PrettyConfig::new().with_decimal_floats(true); + let with_decimal = to_string_pretty(&1.0, pretty).unwrap(); + assert_eq!(with_decimal, "1.0"); + + let pretty = PrettyConfig::new().with_decimal_floats(true); + let with_decimal = to_string_pretty(&1.1, pretty).unwrap(); + assert_eq!(with_decimal, "1.1"); +} 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..4e8b5e4b47 --- /dev/null +++ b/third_party/rust/ron/tests/large_number.rs @@ -0,0 +1,14 @@ +use ron::value::Number; + +#[test] +fn test_large_number() { + use ron::value::Value; + 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)) + ); +} 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..f53bcb2cf3 --- /dev/null +++ b/third_party/rust/ron/tests/min_max.rs @@ -0,0 +1,65 @@ +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() + ); +} + +#[test] +fn test_i128_min() { + assert_eq!( + std::i128::MIN, + from_str(&to_string(&std::i128::MIN).unwrap()).unwrap() + ); +} + +#[test] +fn test_i128_max() { + assert_eq!( + std::i128::MAX, + from_str(&to_string(&std::i128::MAX).unwrap()).unwrap() + ); +} + +#[test] +fn test_u128_min() { + assert_eq!( + std::u128::MIN, + from_str(&to_string(&std::u128::MIN).unwrap()).unwrap() + ); +} + +#[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..26b97a53de --- /dev/null +++ b/third_party/rust/ron/tests/numbers.rs @@ -0,0 +1,22 @@ +use ron::de::from_str; + +#[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)); +} + +#[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)); +} + +#[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)); +} 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..db34a1d397 --- /dev/null +++ b/third_party/rust/ron/tests/preserve_sequence.rs @@ -0,0 +1,53 @@ +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 = match from_str(source) { + Ok(x) => x, + Err(e) => { + println!("Failed to load config: {}", e); + std::process::exit(1); + } + }; + let pretty = PrettyConfig::new() + .with_depth_limit(3) + .with_separate_tuple_members(true) + .with_enumerate_arrays(true) + .with_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..08022f0fbe --- /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() + .with_enumerate_arrays(true) + .with_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, 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().with_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..c7f80a0a5a --- /dev/null +++ b/third_party/rust/ron/tests/struct_integers.rs @@ -0,0 +1,35 @@ +use serde::{Deserialize, Serialize}; + +#[derive(Debug, Eq, PartialEq, Serialize, Deserialize)] +struct S { + a: i8, + b: i16, + c: i32, + d: i64, + e: i128, + f: u8, + g: u16, + h: u32, + i: u64, + j: u128, +} + +#[test] +fn roundtrip() { + let s = S { + a: std::i8::MIN, + b: std::i16::MIN, + c: std::i32::MIN, + d: std::i64::MIN, + e: std::i128::MIN, + f: std::u8::MAX, + g: std::u16::MAX, + h: std::u32::MAX, + i: std::u64::MAX, + 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/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..1e1ff5b991 --- /dev/null +++ b/third_party/rust/ron/tests/value.rs @@ -0,0 +1,111 @@ +use ron::value::{Map, Number, Value}; +use serde::Serialize; + +#[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.1415".parse(), Ok(Value::Number(Number::new(3.1415f64)))); +} + +#[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, ErrorCode, Position}; + + assert_eq!("()".parse(), Ok(Value::Unit)); + assert_eq!("Foo".parse(), Ok(Value::Unit)); + + assert_eq!( + "".parse::<Value>(), + Err(Error { + code: ErrorCode::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); + } +} |