summaryrefslogtreecommitdiffstats
path: root/third_party/rust/ron/tests/359_deserialize_seed.rs
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 19:33:14 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 19:33:14 +0000
commit36d22d82aa202bb199967e9512281e9a53db42c9 (patch)
tree105e8c98ddea1c1e4784a60a5a6410fa416be2de /third_party/rust/ron/tests/359_deserialize_seed.rs
parentInitial commit. (diff)
downloadfirefox-esr-upstream.tar.xz
firefox-esr-upstream.zip
Adding upstream version 115.7.0esr.upstream/115.7.0esrupstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'third_party/rust/ron/tests/359_deserialize_seed.rs')
-rw-r--r--third_party/rust/ron/tests/359_deserialize_seed.rs57
1 files changed, 57 insertions, 0 deletions
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 },
+ })
+ );
+}