summary refs log tree commit diff stats
path: root/tests
diff options
context:
space:
mode:
authorSoniEx2 <endermoneymod@gmail.com>2021-09-16 10:51:39 -0300
committerSoniEx2 <endermoneymod@gmail.com>2021-09-16 10:51:39 -0300
commit134727652dc3a65bc5cf6a4d3cfb1c24fd023d5e (patch)
treeaf84264ada4c309cbf78a48e9030748a7ecac110 /tests
[Project] Soni's Serde Utilities
Some utilities for use with serde.
Diffstat (limited to 'tests')
-rw-r--r--tests/maybe.rs48
1 files changed, 48 insertions, 0 deletions
diff --git a/tests/maybe.rs b/tests/maybe.rs
new file mode 100644
index 0000000..68db667
--- /dev/null
+++ b/tests/maybe.rs
@@ -0,0 +1,48 @@
+use serde::Deserialize;
+
+use serde_json::from_str;
+
+use serde_util::MayBe;
+
+#[test]
+fn test_is_t() {
+    let json = "256";
+    assert!(from_str::<MayBe<f64>>(json).unwrap().is_some());
+}
+
+#[test]
+fn test_is_not_t() {
+    let json = "{}";
+    assert!(from_str::<MayBe<f64>>(json).unwrap().is_none());
+}
+
+#[test]
+fn test_is_missing() {
+    #[derive(Deserialize)]
+    struct Foo {
+        bar: MayBe<f64>,
+    }
+    let json = "{}";
+    assert!(from_str::<Foo>(json).unwrap().bar.is_none());
+}
+
+#[test]
+fn test_t_in_struct() {
+    #[derive(Deserialize)]
+    struct Foo {
+        bar: MayBe<f64>,
+    }
+    let json = "{\"bar\": 123}";
+    assert!(from_str::<Foo>(json).unwrap().bar.is_some());
+}
+
+
+#[test]
+fn test_not_t_in_struct() {
+    #[derive(Deserialize)]
+    struct Foo {
+        bar: MayBe<f64>,
+    }
+    let json = "{\"bar\": []}";
+    assert!(from_str::<Foo>(json).unwrap().bar.is_none());
+}