| 1 |
//! Test Cases |
| 2 |
|
| 3 |
use pretty_assertions::assert_eq; |
| 4 |
use serde::{Deserialize, Serialize}; |
| 5 |
use serde_json::json; |
| 6 |
use serde_with_macros::skip_serializing_none; |
| 7 |
|
| 8 |
macro_rules! test { |
| 9 |
($fn:ident, $struct:ident) => { |
| 10 |
#[test] |
| 11 |
fn $fn() { |
| 12 |
let expected = json!({}); |
| 13 |
let data = $struct { |
| 14 |
a: None, |
| 15 |
b: None, |
| 16 |
c: None, |
| 17 |
d: None, |
| 18 |
}; |
| 19 |
let res = serde_json::to_value(&data).unwrap(); |
| 20 |
assert_eq!(expected, res); |
| 21 |
assert_eq!(data, serde_json::from_value(res).unwrap()); |
| 22 |
} |
| 23 |
}; |
| 24 |
} |
| 25 |
|
| 26 |
macro_rules! test_tuple { |
| 27 |
($fn:ident, $struct:ident) => { |
| 28 |
#[test] |
| 29 |
fn $fn() { |
| 30 |
let expected = json!([]); |
| 31 |
let data = $struct(None, None); |
| 32 |
let res = serde_json::to_value(&data).unwrap(); |
| 33 |
assert_eq!(expected, res); |
| 34 |
} |
| 35 |
}; |
| 36 |
} |
| 37 |
|
| 38 |
#[skip_serializing_none] |
| 39 |
#[derive(Debug, Eq, PartialEq, Serialize, Deserialize)] |
| 40 |
struct DataBasic { |
| 41 |
a: Option<String>, |
| 42 |
b: Option<String>, |
| 43 |
c: Option<String>, |
| 44 |
d: Option<String>, |
| 45 |
} |
| 46 |
test!(test_basic, DataBasic); |
| 47 |
|
| 48 |
// This tests different ways of qualifying the Option type |
| 49 |
#[allow(unused_qualifications)] |
| 50 |
#[skip_serializing_none] |
| 51 |
#[derive(Debug, Eq, PartialEq, Serialize, Deserialize)] |
| 52 |
struct DataFullyQualified { |
| 53 |
a: ::std::option::Option<String>, |
| 54 |
b: std::option::Option<String>, |
| 55 |
c: ::std::option::Option<i64>, |
| 56 |
d: core::option::Option<String>, |
| 57 |
} |
| 58 |
test!(test_fully_qualified, DataFullyQualified); |
| 59 |
|
| 60 |
fn never<T>(_t: &T) -> bool { |
| 61 |
false |
| 62 |
} |
| 63 |
|
| 64 |
#[skip_serializing_none] |
| 65 |
#[derive(Debug, Eq, PartialEq, Serialize, Deserialize)] |
| 66 |
struct DataExistingAnnotation { |
| 67 |
#[serde(skip_serializing_if = "Option::is_none")] |
| 68 |
a: Option<String>, |
| 69 |
#[serde(default, skip_serializing_if = "Option::is_none", rename = "abc")] |
| 70 |
b: Option<String>, |
| 71 |
#[serde(default)] |
| 72 |
c: Option<String>, |
| 73 |
#[serde(skip_serializing_if = "never")] |
| 74 |
#[serde(rename = "name")] |
| 75 |
d: Option<String>, |
| 76 |
} |
| 77 |
|
| 78 |
#[test] |
| 79 |
fn test_existing_annotation() { |
| 80 |
let expected = json!({ "name": null }); |
| 81 |
let data = DataExistingAnnotation { |
| 82 |
a: None, |
| 83 |
b: None, |
| 84 |
c: None, |
| 85 |
d: None, |
| 86 |
}; |
| 87 |
let res = serde_json::to_value(&data).unwrap(); |
| 88 |
assert_eq!(expected, res); |
| 89 |
assert_eq!(data, serde_json::from_value(res).unwrap()); |
| 90 |
} |
| 91 |
|
| 92 |
#[skip_serializing_none] |
| 93 |
#[derive(Debug, Eq, PartialEq, Serialize, Deserialize)] |
| 94 |
struct DataSerializeAlways { |
| 95 |
#[serialize_always] |
| 96 |
a: Option<String>, |
| 97 |
#[serialize_always] |
| 98 |
b: Option<String>, |
| 99 |
c: i64, |
| 100 |
#[serialize_always] |
| 101 |
d: Option<String>, |
| 102 |
} |
| 103 |
|
| 104 |
#[test] |
| 105 |
fn test_serialize_always() { |
| 106 |
let expected = json!({ |
| 107 |
"a": null, |
| 108 |
"b": null, |
| 109 |
"c": 0, |
| 110 |
"d": null |
| 111 |
}); |
| 112 |
let data = DataSerializeAlways { |
| 113 |
a: None, |
| 114 |
b: None, |
| 115 |
c: 0, |
| 116 |
d: None, |
| 117 |
}; |
| 118 |
let res = serde_json::to_value(&data).unwrap(); |
| 119 |
assert_eq!(expected, res); |
| 120 |
assert_eq!(data, serde_json::from_value(res).unwrap()); |
| 121 |
} |
| 122 |
|
| 123 |
// This tests different ways of qualifying the Option type |
| 124 |
#[allow(unused_qualifications)] |
| 125 |
#[skip_serializing_none] |
| 126 |
#[derive(Debug, Eq, PartialEq, Serialize)] |
| 127 |
struct DataTuple(Option<String>, std::option::Option<String>); |
| 128 |
test_tuple!(test_tuple, DataTuple); |
| 129 |
|
| 130 |
// This tests different ways of qualifying the Option type |
| 131 |
#[allow(unused_qualifications)] |
| 132 |
#[skip_serializing_none] |
| 133 |
#[derive(Debug, Eq, PartialEq, Serialize)] |
| 134 |
enum DataEnum { |
| 135 |
Tuple(Option<i64>, std::option::Option<bool>), |
| 136 |
Struct { |
| 137 |
a: Option<String>, |
| 138 |
b: Option<String>, |
| 139 |
}, |
| 140 |
} |
| 141 |
|
| 142 |
#[test] |
| 143 |
fn test_enum() { |
| 144 |
let expected = json!({ |
| 145 |
"Tuple": [] |
| 146 |
}); |
| 147 |
let data = DataEnum::Tuple(None, None); |
| 148 |
let res = serde_json::to_value(data).unwrap(); |
| 149 |
assert_eq!(expected, res); |
| 150 |
|
| 151 |
let expected = json!({ |
| 152 |
"Struct": {} |
| 153 |
}); |
| 154 |
let data = DataEnum::Struct { a: None, b: None }; |
| 155 |
let res = serde_json::to_value(data).unwrap(); |
| 156 |
assert_eq!(expected, res); |
| 157 |
} |