diff options
Diffstat (limited to 'third_party/rust/serde_with_macros/tests')
7 files changed, 235 insertions, 0 deletions
diff --git a/third_party/rust/serde_with_macros/tests/compile-fail/skip-none-always.rs b/third_party/rust/serde_with_macros/tests/compile-fail/skip-none-always.rs new file mode 100644 index 0000000000..b52272439b --- /dev/null +++ b/third_party/rust/serde_with_macros/tests/compile-fail/skip-none-always.rs @@ -0,0 +1,30 @@ +extern crate serde; +extern crate serde_with_macros; + +use serde::Serialize; +use serde_with_macros::skip_serializing_none; + +#[skip_serializing_none] +#[derive(Serialize)] +struct Data { + #[serialize_always] + #[serde(skip_serializing_if = "Option::is_none")] + a: Option<char>, +} + +#[skip_serializing_none] +#[derive(Serialize)] +struct Data2( + #[serialize_always] + #[serde(skip_serializing_if = "Option::is_none")] + Option<char>, +); + +#[skip_serializing_none] +#[derive(Serialize)] +struct Data3 { + #[serialize_always] + a: char, +} + +fn main() {} diff --git a/third_party/rust/serde_with_macros/tests/compile-fail/skip-none-always.stderr b/third_party/rust/serde_with_macros/tests/compile-fail/skip-none-always.stderr new file mode 100644 index 0000000000..226d7b55c8 --- /dev/null +++ b/third_party/rust/serde_with_macros/tests/compile-fail/skip-none-always.stderr @@ -0,0 +1,17 @@ +error: The attributes `serialize_always` and `serde(skip_serializing_if = "...")` cannot be used on the same field: `a`. + --> $DIR/skip-none-always.rs:7:1 + | +7 | #[skip_serializing_none] + | ^^^^^^^^^^^^^^^^^^^^^^^^ + +error: The attributes `serialize_always` and `serde(skip_serializing_if = "...")` cannot be used on the same field. + --> $DIR/skip-none-always.rs:15:1 + | +15 | #[skip_serializing_none] + | ^^^^^^^^^^^^^^^^^^^^^^^^ + +error: `serialize_always` may only be used on fields of type `Option`. + --> $DIR/skip-none-always.rs:23:1 + | +23 | #[skip_serializing_none] + | ^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/third_party/rust/serde_with_macros/tests/compile-fail/skip-none-not-struct.rs b/third_party/rust/serde_with_macros/tests/compile-fail/skip-none-not-struct.rs new file mode 100644 index 0000000000..f6776db778 --- /dev/null +++ b/third_party/rust/serde_with_macros/tests/compile-fail/skip-none-not-struct.rs @@ -0,0 +1,8 @@ +extern crate serde_with_macros; + +use serde_with_macros::skip_serializing_none; + +#[skip_serializing_none] +fn test() {} + +fn main() {} diff --git a/third_party/rust/serde_with_macros/tests/compile-fail/skip-none-not-struct.stderr b/third_party/rust/serde_with_macros/tests/compile-fail/skip-none-not-struct.stderr new file mode 100644 index 0000000000..479fa7ea75 --- /dev/null +++ b/third_party/rust/serde_with_macros/tests/compile-fail/skip-none-not-struct.stderr @@ -0,0 +1,5 @@ +error: The attribute can only be applied to struct or enum definitions. + --> $DIR/skip-none-not-struct.rs:5:1 + | +5 | #[skip_serializing_none] + | ^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/third_party/rust/serde_with_macros/tests/compiler-messages.rs b/third_party/rust/serde_with_macros/tests/compiler-messages.rs new file mode 100644 index 0000000000..01373a3d3b --- /dev/null +++ b/third_party/rust/serde_with_macros/tests/compiler-messages.rs @@ -0,0 +1,15 @@ +extern crate rustversion; +extern crate trybuild; + +// This test fails for older compiler versions since the error messages are different. +#[rustversion::attr(before(1.35), ignore)] +#[test] +fn compile_test() { + // This test does not work under tarpaulin, so skip it if detected + if std::env::var("TARPAULIN") == Ok("1".to_string()) { + return; + } + + let t = trybuild::TestCases::new(); + t.compile_fail("tests/compile-fail/*.rs"); +} diff --git a/third_party/rust/serde_with_macros/tests/skip_serializing_null.rs b/third_party/rust/serde_with_macros/tests/skip_serializing_null.rs new file mode 100644 index 0000000000..7fb43bc923 --- /dev/null +++ b/third_party/rust/serde_with_macros/tests/skip_serializing_null.rs @@ -0,0 +1,154 @@ +extern crate pretty_assertions; +extern crate serde; +extern crate serde_json; +extern crate serde_with_macros; + +use pretty_assertions::assert_eq; +use serde::{Deserialize, Serialize}; +use serde_json::json; +use serde_with_macros::skip_serializing_none; + +macro_rules! test { + ($fn:ident, $struct:ident) => { + #[test] + fn $fn() { + let expected = json!({}); + let data = $struct { + a: None, + b: None, + c: None, + d: None, + }; + let res = serde_json::to_value(&data).unwrap(); + assert_eq!(expected, res); + assert_eq!(data, serde_json::from_value(res).unwrap()); + } + }; +} + +macro_rules! test_tuple { + ($fn:ident, $struct:ident) => { + #[test] + fn $fn() { + let expected = json!([]); + let data = $struct(None, None); + let res = serde_json::to_value(&data).unwrap(); + assert_eq!(expected, res); + } + }; +} + +#[skip_serializing_none] +#[derive(Debug, Eq, PartialEq, Serialize, Deserialize)] +struct DataBasic { + a: Option<String>, + b: Option<String>, + c: Option<String>, + d: Option<String>, +} +test!(test_basic, DataBasic); + +#[skip_serializing_none] +#[derive(Debug, Eq, PartialEq, Serialize, Deserialize)] +struct DataFullyQualified { + a: ::std::option::Option<String>, + b: std::option::Option<String>, + c: ::std::option::Option<i64>, + d: core::option::Option<String>, +} +test!(test_fully_qualified, DataFullyQualified); + +fn never<T>(_t: &T) -> bool { + false +} + +#[skip_serializing_none] +#[derive(Debug, Eq, PartialEq, Serialize, Deserialize)] +struct DataExistingAnnotation { + #[serde(skip_serializing_if = "Option::is_none")] + a: Option<String>, + #[serde(default, skip_serializing_if = "Option::is_none", rename = "abc")] + b: Option<String>, + #[serde(default)] + c: Option<String>, + #[serde(skip_serializing_if = "never")] + #[serde(rename = "name")] + d: Option<String>, +} + +#[test] +fn test_existing_annotation() { + let expected = json!({ "name": null }); + let data = DataExistingAnnotation { + a: None, + b: None, + c: None, + d: None, + }; + let res = serde_json::to_value(&data).unwrap(); + assert_eq!(expected, res); + assert_eq!(data, serde_json::from_value(res).unwrap()); +} + +#[skip_serializing_none] +#[derive(Debug, Eq, PartialEq, Serialize, Deserialize)] +struct DataSerializeAlways { + #[serialize_always] + a: Option<String>, + #[serialize_always] + b: Option<String>, + c: i64, + #[serialize_always] + d: Option<String>, +} + +#[test] +fn test_serialize_always() { + let expected = json!({ + "a": null, + "b": null, + "c": 0, + "d": null + }); + let data = DataSerializeAlways { + a: None, + b: None, + c: 0, + d: None, + }; + let res = serde_json::to_value(&data).unwrap(); + assert_eq!(expected, res); + assert_eq!(data, serde_json::from_value(res).unwrap()); +} + +#[skip_serializing_none] +#[derive(Debug, Eq, PartialEq, Serialize)] +struct DataTuple(Option<String>, std::option::Option<String>); +test_tuple!(test_tuple, DataTuple); + +#[skip_serializing_none] +#[derive(Debug, Eq, PartialEq, Serialize)] +enum DataEnum { + Tuple(Option<i64>, std::option::Option<bool>), + Struct { + a: Option<String>, + b: Option<String>, + }, +} + +#[test] +fn test_enum() { + let expected = json!({ + "Tuple": [] + }); + let data = DataEnum::Tuple(None, None); + let res = serde_json::to_value(&data).unwrap(); + assert_eq!(expected, res); + + let expected = json!({ + "Struct": {} + }); + let data = DataEnum::Struct { a: None, b: None }; + let res = serde_json::to_value(&data).unwrap(); + assert_eq!(expected, res); +} diff --git a/third_party/rust/serde_with_macros/tests/version_numbers.rs b/third_party/rust/serde_with_macros/tests/version_numbers.rs new file mode 100644 index 0000000000..9cbd3b3a5a --- /dev/null +++ b/third_party/rust/serde_with_macros/tests/version_numbers.rs @@ -0,0 +1,6 @@ +extern crate version_sync; + +#[test] +fn test_html_root_url() { + version_sync::assert_html_root_url_updated!("src/lib.rs"); +} |