summaryrefslogtreecommitdiffstats
path: root/third_party/rust/serde_with/tests/derives
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-19 01:47:29 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-19 01:47:29 +0000
commit0ebf5bdf043a27fd3dfb7f92e0cb63d88954c44d (patch)
treea31f07c9bcca9d56ce61e9a1ffd30ef350d513aa /third_party/rust/serde_with/tests/derives
parentInitial commit. (diff)
downloadfirefox-esr-0ebf5bdf043a27fd3dfb7f92e0cb63d88954c44d.tar.xz
firefox-esr-0ebf5bdf043a27fd3dfb7f92e0cb63d88954c44d.zip
Adding upstream version 115.8.0esr.upstream/115.8.0esr
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'third_party/rust/serde_with/tests/derives')
-rw-r--r--third_party/rust/serde_with/tests/derives/deserialize_fromstr.rs96
-rw-r--r--third_party/rust/serde_with/tests/derives/lib.rs15
-rw-r--r--third_party/rust/serde_with/tests/derives/serialize_display.rs39
3 files changed, 150 insertions, 0 deletions
diff --git a/third_party/rust/serde_with/tests/derives/deserialize_fromstr.rs b/third_party/rust/serde_with/tests/derives/deserialize_fromstr.rs
new file mode 100644
index 0000000000..26d60bf08f
--- /dev/null
+++ b/third_party/rust/serde_with/tests/derives/deserialize_fromstr.rs
@@ -0,0 +1,96 @@
+use super::*;
+use core::{
+ num::ParseIntError,
+ str::{FromStr, ParseBoolError},
+};
+use pretty_assertions::assert_eq;
+use serde_with::DeserializeFromStr;
+
+#[derive(Debug, PartialEq, DeserializeFromStr)]
+struct A {
+ a: u32,
+ b: bool,
+}
+
+impl FromStr for A {
+ type Err = String;
+
+ /// Parse a value like `123<>true`
+ fn from_str(s: &str) -> Result<Self, Self::Err> {
+ let mut parts = s.split("<>");
+ let number = parts
+ .next()
+ .ok_or_else(|| "Missing first value".to_string())?
+ .parse()
+ .map_err(|err: ParseIntError| err.to_string())?;
+ let bool = parts
+ .next()
+ .ok_or_else(|| "Missing second value".to_string())?
+ .parse()
+ .map_err(|err: ParseBoolError| err.to_string())?;
+ Ok(Self { a: number, b: bool })
+ }
+}
+
+#[test]
+fn test_deserialize_fromstr() {
+ check_deserialization(A { a: 159, b: true }, "\"159<>true\"");
+ check_deserialization(A { a: 999, b: false }, "\"999<>false\"");
+ check_deserialization(A { a: 0, b: true }, "\"0<>true\"");
+}
+
+#[test]
+fn test_deserialize_from_bytes() {
+ use serde::de::{value::Error, Deserialize, Deserializer, Visitor};
+
+ // Unfortunately serde_json is too clever (i.e. handles bytes gracefully)
+ // so instead create a custom deserializer which can only deserialize bytes.
+ // All other deserialize_* fns are forwarded to deserialize_bytes
+ struct ByteDeserializer(&'static [u8]);
+
+ impl<'de> Deserializer<'de> for ByteDeserializer {
+ type Error = Error;
+
+ fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Self::Error>
+ where
+ V: Visitor<'de>,
+ {
+ self.deserialize_bytes(visitor)
+ }
+
+ fn deserialize_bytes<V>(self, visitor: V) -> Result<V::Value, Self::Error>
+ where
+ V: Visitor<'de>,
+ {
+ visitor.visit_bytes(self.0)
+ }
+
+ serde::forward_to_deserialize_any! {
+ bool i8 i16 i32 i64 i128 u8 u16 u32 u64 u128 f32 f64 char str string
+ byte_buf option unit unit_struct newtype_struct seq tuple
+ tuple_struct map struct enum identifier ignored_any
+ }
+ }
+
+ // callstack: A::deserialize -> deserialize_str -> deserialize_any ->
+ // deserialize_bytes -> visit_bytes -> visit_str -> success!
+ let a = A::deserialize(ByteDeserializer(b"159<>true")).unwrap();
+
+ assert_eq!(A { a: 159, b: true }, a);
+}
+
+#[test]
+fn test_deserialize_fromstr_in_vec() {
+ check_deserialization(
+ vec![
+ A { a: 123, b: false },
+ A { a: 0, b: true },
+ A { a: 999, b: true },
+ ],
+ r#"[
+ "123<>false",
+ "0<>true",
+ "999<>true"
+ ]"#,
+ );
+}
diff --git a/third_party/rust/serde_with/tests/derives/lib.rs b/third_party/rust/serde_with/tests/derives/lib.rs
new file mode 100644
index 0000000000..a43a3c3535
--- /dev/null
+++ b/third_party/rust/serde_with/tests/derives/lib.rs
@@ -0,0 +1,15 @@
+#![allow(
+ // clippy is broken and shows wrong warnings
+ // clippy on stable does not know yet about the lint name
+ unknown_lints,
+ // https://github.com/rust-lang/rust-clippy/issues/8867
+ clippy::derive_partial_eq_without_eq,
+)]
+
+mod deserialize_fromstr;
+mod serialize_display;
+#[path = "../utils.rs"]
+mod utils;
+
+use expect_test::expect;
+use utils::*;
diff --git a/third_party/rust/serde_with/tests/derives/serialize_display.rs b/third_party/rust/serde_with/tests/derives/serialize_display.rs
new file mode 100644
index 0000000000..e660fa7535
--- /dev/null
+++ b/third_party/rust/serde_with/tests/derives/serialize_display.rs
@@ -0,0 +1,39 @@
+use super::*;
+use core::fmt;
+use serde_with::SerializeDisplay;
+
+#[derive(Debug, SerializeDisplay)]
+struct A {
+ a: u32,
+ b: bool,
+}
+
+impl fmt::Display for A {
+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+ write!(f, "->{} <> {}<-", self.a, self.b)
+ }
+}
+
+#[test]
+fn test_serialize_display() {
+ check_serialization(A { a: 123, b: false }, expect![[r#""->123 <> false<-""#]]);
+ check_serialization(A { a: 0, b: true }, expect![[r#""->0 <> true<-""#]]);
+ check_serialization(A { a: 999, b: true }, expect![[r#""->999 <> true<-""#]]);
+}
+
+#[test]
+fn test_serialize_display_in_vec() {
+ check_serialization(
+ vec![
+ A { a: 123, b: false },
+ A { a: 0, b: true },
+ A { a: 999, b: true },
+ ],
+ expect![[r#"
+ [
+ "->123 <> false<-",
+ "->0 <> true<-",
+ "->999 <> true<-"
+ ]"#]],
+ );
+}