summaryrefslogtreecommitdiffstats
path: root/third_party/rust/serde_with_macros/tests
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-19 00:47:55 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-19 00:47:55 +0000
commit26a029d407be480d791972afb5975cf62c9360a6 (patch)
treef435a8308119effd964b339f76abb83a57c29483 /third_party/rust/serde_with_macros/tests
parentInitial commit. (diff)
downloadfirefox-26a029d407be480d791972afb5975cf62c9360a6.tar.xz
firefox-26a029d407be480d791972afb5975cf62c9360a6.zip
Adding upstream version 124.0.1.upstream/124.0.1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'third_party/rust/serde_with_macros/tests')
-rw-r--r--third_party/rust/serde_with_macros/tests/apply.rs247
-rw-r--r--third_party/rust/serde_with_macros/tests/compiler-messages.rs9
-rw-r--r--third_party/rust/serde_with_macros/tests/serde_as_issue_267.rs9
-rw-r--r--third_party/rust/serde_with_macros/tests/serde_as_issue_538.rs30
-rw-r--r--third_party/rust/serde_with_macros/tests/skip_serializing_null.rs149
-rw-r--r--third_party/rust/serde_with_macros/tests/version_numbers.rs21
6 files changed, 465 insertions, 0 deletions
diff --git a/third_party/rust/serde_with_macros/tests/apply.rs b/third_party/rust/serde_with_macros/tests/apply.rs
new file mode 100644
index 0000000000..33b721325d
--- /dev/null
+++ b/third_party/rust/serde_with_macros/tests/apply.rs
@@ -0,0 +1,247 @@
+#![allow(dead_code)]
+
+use expect_test::expect;
+use serde_with_macros::apply;
+use std::collections::BTreeMap;
+
+/// Fields `a` and `c` match, as each has a fully specified pattern.
+#[test]
+fn test_apply_fully_specified() {
+ #[apply(
+ crate="serde_with_macros",
+
+ Option<String> => #[serde(skip)],
+ BTreeMap<String, String> => #[serde(skip)],
+)]
+ #[derive(Default, serde::Serialize)]
+ struct FooBar<'a> {
+ a: Option<String>,
+ b: Option<i32>,
+ c: BTreeMap<String, String>,
+ d: BTreeMap<String, i32>,
+ e: BTreeMap<i32, String>,
+ f: &'a str,
+ g: &'static str,
+
+ #[serde_with(skip_apply)]
+ skip: Option<String>,
+ }
+
+ expect![[r#"
+ {
+ "b": null,
+ "d": {},
+ "e": {},
+ "f": "",
+ "g": "",
+ "skip": null
+ }"#]]
+ .assert_eq(&serde_json::to_string_pretty(&FooBar::<'static>::default()).unwrap());
+}
+
+/// All fields match as `_` matches any type.
+///
+/// The `skip` field is ignored because of the `#[serde_with(skip_apply)]` attribute.
+#[test]
+fn test_apply_all() {
+ #[apply(
+ crate="serde_with_macros",
+
+ _ => #[serde(skip)],
+)]
+ #[derive(Default, serde::Serialize)]
+ struct FooBar<'a> {
+ a: Option<String>,
+ b: Option<i32>,
+ c: BTreeMap<String, String>,
+ d: BTreeMap<String, i32>,
+ e: BTreeMap<i32, String>,
+ f: &'a str,
+ g: &'static str,
+
+ #[serde_with(skip_apply)]
+ skip: Option<String>,
+ }
+
+ expect![[r#"
+ {
+ "skip": null
+ }"#]]
+ .assert_eq(&serde_json::to_string_pretty(&FooBar::<'static>::default()).unwrap());
+}
+
+/// Fields `a` and `b` match, since both are variants of `Option`.
+///
+/// No generic in the pattern allows matching with any number of generics on the fields.
+/// The `skip` field is ignored because of the `#[serde_with(skip_apply)]` attribute.
+#[test]
+fn test_apply_partial_no_generic() {
+ #[apply(
+ crate="serde_with_macros",
+
+ Option => #[serde(skip)],
+)]
+ #[derive(Default, serde::Serialize)]
+ struct FooBar<'a> {
+ a: Option<String>,
+ b: Option<i32>,
+ c: BTreeMap<String, String>,
+ d: BTreeMap<String, i32>,
+ e: BTreeMap<i32, String>,
+ f: &'a str,
+ g: &'static str,
+
+ #[serde_with(skip_apply)]
+ skip: Option<String>,
+ }
+
+ expect![[r#"
+ {
+ "c": {},
+ "d": {},
+ "e": {},
+ "f": "",
+ "g": "",
+ "skip": null
+ }"#]]
+ .assert_eq(&serde_json::to_string_pretty(&FooBar::<'static>::default()).unwrap());
+}
+
+/// Fields `c` and `d` match, as both have a `String` key and `_` matches any type.
+#[test]
+fn test_apply_partial_generic() {
+ #[apply(
+ crate="serde_with_macros",
+
+ BTreeMap<String, _> => #[serde(skip)],
+)]
+ #[derive(Default, serde::Serialize)]
+ struct FooBar<'a> {
+ a: Option<String>,
+ b: Option<i32>,
+ c: BTreeMap<String, String>,
+ d: BTreeMap<String, i32>,
+ e: BTreeMap<i32, String>,
+ f: &'a str,
+ g: &'static str,
+
+ #[serde_with(skip_apply)]
+ skip: Option<String>,
+ }
+
+ expect![[r#"
+ {
+ "a": null,
+ "b": null,
+ "e": {},
+ "f": "",
+ "g": "",
+ "skip": null
+ }"#]]
+ .assert_eq(&serde_json::to_string_pretty(&FooBar::<'static>::default()).unwrap());
+}
+
+/// Fields `f` and `g` match, since no lifetime matches any reference.
+#[test]
+fn test_apply_no_lifetime() {
+ #[apply(
+ crate="serde_with_macros",
+
+ &str => #[serde(skip)],
+)]
+ #[derive(Default, serde::Serialize)]
+ struct FooBar<'a> {
+ a: Option<String>,
+ b: Option<i32>,
+ c: BTreeMap<String, String>,
+ d: BTreeMap<String, i32>,
+ e: BTreeMap<i32, String>,
+ f: &'a str,
+ g: &'static str,
+
+ #[serde_with(skip_apply)]
+ skip: Option<String>,
+ }
+
+ expect![[r#"
+ {
+ "a": null,
+ "b": null,
+ "c": {},
+ "d": {},
+ "e": {},
+ "skip": null
+ }"#]]
+ .assert_eq(&serde_json::to_string_pretty(&FooBar::<'static>::default()).unwrap());
+}
+
+/// Field `f` matches as the lifetime is identical and `mut` is ignored.
+#[test]
+fn test_apply_lifetime() {
+ #[apply(
+ crate="serde_with_macros",
+
+ &'a mut str => #[serde(skip)],
+)]
+ #[derive(Default, serde::Serialize)]
+ struct FooBar<'a> {
+ a: Option<String>,
+ b: Option<i32>,
+ c: BTreeMap<String, String>,
+ d: BTreeMap<String, i32>,
+ e: BTreeMap<i32, String>,
+ f: &'a str,
+ g: &'static str,
+
+ #[serde_with(skip_apply)]
+ skip: Option<String>,
+ }
+
+ expect![[r#"
+ {
+ "a": null,
+ "b": null,
+ "c": {},
+ "d": {},
+ "e": {},
+ "g": "",
+ "skip": null
+ }"#]]
+ .assert_eq(&serde_json::to_string_pretty(&FooBar::<'static>::default()).unwrap());
+}
+
+/// No field matches as the explicit lifetimes are different
+#[test]
+fn test_apply_mismatched_lifetime() {
+ #[apply(
+ crate="serde_with_macros",
+
+ &'b str => #[serde(skip)],
+)]
+ #[derive(Default, serde::Serialize)]
+ struct FooBar<'a> {
+ a: Option<String>,
+ b: Option<i32>,
+ c: BTreeMap<String, String>,
+ d: BTreeMap<String, i32>,
+ e: BTreeMap<i32, String>,
+ f: &'a str,
+ g: &'static str,
+
+ #[serde_with(skip_apply)]
+ skip: Option<String>,
+ }
+
+ expect![[r#"
+ {
+ "a": null,
+ "b": null,
+ "c": {},
+ "d": {},
+ "e": {},
+ "f": "",
+ "g": "",
+ "skip": null
+ }"#]]
+ .assert_eq(&serde_json::to_string_pretty(&FooBar::<'static>::default()).unwrap());
+}
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..bbb16c32d6
--- /dev/null
+++ b/third_party/rust/serde_with_macros/tests/compiler-messages.rs
@@ -0,0 +1,9 @@
+#[cfg_attr(tarpaulin, ignore)]
+// The error messages are different on beta and nightly, thus breaking the test.
+#[rustversion::attr(beta, ignore)]
+#[rustversion::attr(nightly, ignore)]
+#[test]
+fn compile_test() {
+ let t = trybuild::TestCases::new();
+ t.compile_fail("tests/compile-fail/*.rs");
+}
diff --git a/third_party/rust/serde_with_macros/tests/serde_as_issue_267.rs b/third_party/rust/serde_with_macros/tests/serde_as_issue_267.rs
new file mode 100644
index 0000000000..2db1bbea00
--- /dev/null
+++ b/third_party/rust/serde_with_macros/tests/serde_as_issue_267.rs
@@ -0,0 +1,9 @@
+use serde::Serialize;
+use serde_with_macros::serde_as;
+
+// The field has no serde_as annotation and should not trigger any error
+#[serde_as(crate = "serde_with_macros")]
+#[derive(Serialize)]
+pub struct Thing {
+ pub id: u8,
+}
diff --git a/third_party/rust/serde_with_macros/tests/serde_as_issue_538.rs b/third_party/rust/serde_with_macros/tests/serde_as_issue_538.rs
new file mode 100644
index 0000000000..2ac2f08cc8
--- /dev/null
+++ b/third_party/rust/serde_with_macros/tests/serde_as_issue_538.rs
@@ -0,0 +1,30 @@
+//! Check for the correct processing of ExprGroups in types
+//!
+//! They occur when the types is passed in as a macro_rules argument like here.
+//! https://github.com/jonasbb/serde_with/issues/538
+
+macro_rules! t {
+ ($($param:ident : $ty:ty),*) => {
+ #[derive(Default)]
+ #[serde_with_macros::apply(
+ crate = "serde_with_macros",
+ Option => #[serde(skip_serializing_if = "Option::is_none")],
+ )]
+ #[derive(serde::Serialize)]
+ struct Data {
+ a: Option<String>,
+ b: Option<u64>,
+ c: Option<String>,
+ $(pub $param: $ty),*
+ }
+ }
+}
+
+t!(d: Option<bool>);
+
+#[test]
+fn t() {
+ let expected = r#"{}"#;
+ let data: Data = Default::default();
+ assert_eq!(expected, serde_json::to_string(&data).unwrap());
+}
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..53412ef1c7
--- /dev/null
+++ b/third_party/rust/serde_with_macros/tests/skip_serializing_null.rs
@@ -0,0 +1,149 @@
+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..35560b024b
--- /dev/null
+++ b/third_party/rust/serde_with_macros/tests/version_numbers.rs
@@ -0,0 +1,21 @@
+// Needed to supress a 2021 incompatability warning in the macro generated code
+// The non_fmt_panic lint is not yet available on most Rust versions
+#![allow(unknown_lints, non_fmt_panics)]
+
+#[test]
+fn test_html_root_url() {
+ version_sync::assert_html_root_url_updated!("src/lib.rs");
+}
+
+#[test]
+fn test_changelog() {
+ version_sync::assert_contains_regex!("CHANGELOG.md", r#"## \[{version}\]"#);
+}
+
+#[test]
+fn test_serde_with_dependency() {
+ version_sync::assert_contains_regex!(
+ "../serde_with/Cargo.toml",
+ r#"^serde_with_macros = .*? version = "={version}""#
+ );
+}