Skip to content

Commit da42158

Browse files
committed
WIP - update tests
1 parent c57405b commit da42158

File tree

3 files changed

+408
-30
lines changed

3 files changed

+408
-30
lines changed

src/wkt/tests/generated/primitive_types.proto

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,22 @@ message MessageWithI64 {
7373
map<int64, int64> map_key_value = 6;
7474
}
7575

76+
// A test message for u64.
77+
message MessageWithU64 {
78+
// A singular field.
79+
uint64 singular = 1;
80+
// An optional field.
81+
optional uint64 optional = 2;
82+
// A repeated field.
83+
repeated uint64 repeated = 3;
84+
// Test u64 as values.
85+
map<string, uint64> map_value = 4;
86+
// Test u64 as keys.
87+
map<uint64, string> map_key = 5;
88+
// Test u64 as both keys and values.
89+
map<uint64, uint64> map_key_value = 6;
90+
}
91+
7692
// A test message for bytes.
7793
message MessageWithBytes {
7894
// A singular field.

src/wkt/tests/message_with_i64.rs

Lines changed: 186 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,10 @@
1414

1515
#[cfg(test)]
1616
mod test {
17-
use serde_json::json;
18-
type Result = std::result::Result<(), Box<dyn std::error::Error>>;
17+
use serde_json::{Value, json};
18+
use test_case::test_case;
19+
20+
type Result = anyhow::Result<()>;
1921

2022
#[allow(dead_code)]
2123
mod protos {
@@ -24,44 +26,198 @@ mod test {
2426
}
2527
use protos::MessageWithI64;
2628

27-
// 1 << 60 is too large to be represented as a JSON number, those are
28-
// always IEEE 754 double precision floating point numbers, which only
29-
// has about 52 bits of mantissa.
30-
const TEST_VALUE: i64 = 1_i64 << 60;
29+
#[test_case(123, 123)]
30+
#[test_case(-234, -234)]
31+
#[test_case("345", 345)]
32+
#[test_case("-456", -456)]
33+
#[test_case("567.0", 567)]
34+
#[test_case("-789.0", -789)]
35+
fn test_singular<T>(input: T, want: i64) -> Result
36+
where
37+
T: serde::ser::Serialize,
38+
{
39+
let value = json!({"singular": input});
40+
let got = serde_json::from_value::<MessageWithI64>(value)?;
41+
let output = json!({"singular": want.to_string()});
42+
assert_eq!(got, MessageWithI64::new().set_singular(want));
43+
let trip = serde_json::to_value(&got)?;
44+
assert_eq!(trip, output);
45+
Ok(())
46+
}
47+
48+
#[test_case(json!({}))]
49+
#[test_case(json!({"singular": 0}))]
50+
// TODO(#2349) - #[test_case(json!({"singular": null}))]
51+
fn test_singular_default(input: Value) -> Result {
52+
let want = MessageWithI64::new().set_singular(0);
53+
let got = serde_json::from_value::<MessageWithI64>(input)?;
54+
assert_eq!(got, want);
55+
let output = serde_json::to_value(&got)?;
56+
assert_eq!(output, json!({}));
57+
Ok(())
58+
}
59+
60+
#[test_case(0, 0)]
61+
#[test_case(123, 123)]
62+
#[test_case(-234, -234)]
63+
#[test_case("345", 345)]
64+
#[test_case("-456", -456)]
65+
#[test_case("567.0", 567)]
66+
#[test_case("-789.0", -789)]
67+
fn test_optional<T>(input: T, want: i64) -> Result
68+
where
69+
T: serde::ser::Serialize,
70+
{
71+
let value = json!({"optional": input});
72+
let got = serde_json::from_value::<MessageWithI64>(value)?;
73+
let output = json!({"optional": want.to_string()});
74+
assert_eq!(got, MessageWithI64::new().set_optional(want));
75+
let trip = serde_json::to_value(&got)?;
76+
assert_eq!(trip, output);
77+
Ok(())
78+
}
79+
80+
#[test_case(json!({}))]
81+
#[test_case(json!({"optional": null}))]
82+
fn test_optional_none(input: Value) -> Result {
83+
let want = MessageWithI64::new().set_or_clear_optional(None::<i64>);
84+
let got = serde_json::from_value::<MessageWithI64>(input)?;
85+
assert_eq!(got, want);
86+
Ok(())
87+
}
88+
89+
#[test_case(0, 0)]
90+
#[test_case(123, 123)]
91+
#[test_case(-234, -234)]
92+
#[test_case("345", 345)]
93+
#[test_case("-456", -456)]
94+
#[test_case("567.0", 567)]
95+
#[test_case("-789.0", -789)]
96+
fn test_repeated<T>(input: T, want: i64) -> Result
97+
where
98+
T: serde::ser::Serialize,
99+
{
100+
let value = json!({"repeated": [input]});
101+
let got = serde_json::from_value::<MessageWithI64>(value)?;
102+
let output = json!({"repeated": [want.to_string()]});
103+
assert_eq!(got, MessageWithI64::new().set_repeated([want]));
104+
let trip = serde_json::to_value(&got)?;
105+
assert_eq!(trip, output);
106+
Ok(())
107+
}
108+
109+
#[test_case(json!({}))]
110+
#[test_case(json!({"repeated": []}))]
111+
// TODO(#2349) - #[test_case(json!({"repeated": null}))]
112+
fn test_repeated_default(input: Value) -> Result {
113+
let want = MessageWithI64::new();
114+
let got = serde_json::from_value::<MessageWithI64>(input)?;
115+
assert_eq!(got, want);
116+
let output = serde_json::to_value(&got)?;
117+
assert_eq!(output, json!({}));
118+
Ok(())
119+
}
31120

32-
#[test]
33-
fn test_singular() -> Result {
34-
let msg = MessageWithI64::new().set_singular(TEST_VALUE);
35-
let got = serde_json::to_value(&msg)?;
36-
let want = json!({"singular": format!("{TEST_VALUE}")});
37-
assert_eq!(want, got);
121+
#[test_case(0, 0)]
122+
#[test_case(123, 123)]
123+
#[test_case(-234, -234)]
124+
#[test_case("345", 345)]
125+
#[test_case("-456", -456)]
126+
#[test_case("567.0", 567)]
127+
#[test_case("-789.0", -789)]
128+
fn test_map_value<T>(input: T, want: i64) -> Result
129+
where
130+
T: serde::ser::Serialize,
131+
{
132+
let value = json!({"mapValue": {"test": input}});
133+
let got = serde_json::from_value::<MessageWithI64>(value)?;
134+
let output = json!({"mapValue": {"test": want.to_string()}});
135+
assert_eq!(
136+
got,
137+
MessageWithI64::new().set_map_value([("test".to_string(), want)])
138+
);
139+
let trip = serde_json::to_value(&got)?;
140+
assert_eq!(trip, output);
141+
Ok(())
142+
}
38143

39-
let roundtrip = serde_json::from_value::<MessageWithI64>(got)?;
40-
assert_eq!(msg, roundtrip);
144+
#[test_case(json!({}))]
145+
#[test_case(json!({"mapValue": {}}))]
146+
// TODO(#2349) - #[test_case(json!({"mapValue": null}))]
147+
fn test_map_value_default(input: Value) -> Result {
148+
let want = MessageWithI64::default();
149+
let got = serde_json::from_value::<MessageWithI64>(input)?;
150+
assert_eq!(got, want);
151+
let output = serde_json::to_value(&got)?;
152+
assert_eq!(output, json!({}));
41153
Ok(())
42154
}
43155

44-
#[test]
45-
fn test_optional() -> Result {
46-
let msg = MessageWithI64::new().set_optional(TEST_VALUE);
47-
let got = serde_json::to_value(&msg)?;
48-
let want = json!({"optional": format!("{TEST_VALUE}")});
49-
assert_eq!(want, got);
156+
#[test_case("0", 0)]
157+
#[test_case("123", 123)]
158+
#[test_case("-234", -234)]
159+
#[test_case("345", 345)]
160+
#[test_case("-456", -456)]
161+
#[test_case("567.0", 567)]
162+
#[test_case("-789.0", -789)]
163+
fn test_map_key<T>(input: T, want: i64) -> Result
164+
where
165+
T: Into<String>,
166+
{
167+
let value = json!({"mapKey": {input: "test"}});
168+
let got = serde_json::from_value::<MessageWithI64>(value)?;
169+
let output = json!({"mapKey": {want.to_string(): "test"}});
170+
assert_eq!(got, MessageWithI64::new().set_map_key([(want, "test")]));
171+
let trip = serde_json::to_value(&got)?;
172+
assert_eq!(trip, output);
173+
Ok(())
174+
}
50175

51-
let roundtrip = serde_json::from_value::<MessageWithI64>(got)?;
52-
assert_eq!(msg, roundtrip);
176+
#[test_case(json!({}))]
177+
#[test_case(json!({"mapKey": {}}))]
178+
// TODO(#2349) - #[test_case(json!({"mapKey": null}))]
179+
fn test_map_key_default(input: Value) -> Result {
180+
let want = MessageWithI64::default();
181+
let got = serde_json::from_value::<MessageWithI64>(input)?;
182+
assert_eq!(got, want);
183+
let output = serde_json::to_value(&got)?;
184+
assert_eq!(output, json!({}));
53185
Ok(())
54186
}
55187

56-
#[test]
57-
fn test_repeated() -> Result {
58-
let msg = MessageWithI64::new().set_repeated([TEST_VALUE]);
59-
let got = serde_json::to_value(&msg)?;
60-
let want = json!({"repeated": [format!("{TEST_VALUE}")]});
61-
assert_eq!(want, got);
188+
#[test_case("0", "0", 0, 0; "string zero")]
189+
#[test_case("0", 0, 0, 0)]
190+
#[test_case("0.0", 0, 0, 0)]
191+
#[test_case("123", 234, 123, 234)]
192+
#[test_case("123.0", "345", 123, 345)]
193+
#[test_case("-789", 456, -789, 456)]
194+
#[test_case("-789.0", "567", -789, 567)]
195+
fn test_map_key_value<K, V>(key: K, value: V, want_key: i64, want_value: i64) -> Result
196+
where
197+
K: Into<String>,
198+
V: serde::Serialize,
199+
{
200+
let value = json!({"mapKeyValue": {key: value}});
201+
let got = serde_json::from_value::<MessageWithI64>(value)?;
202+
let output = json!({"mapKeyValue": {want_key.to_string(): want_value.to_string()}});
203+
assert_eq!(
204+
got,
205+
MessageWithI64::new().set_map_key_value([(want_key, want_value)])
206+
);
207+
let trip = serde_json::to_value(&got)?;
208+
assert_eq!(trip, output);
209+
Ok(())
210+
}
62211

63-
let roundtrip = serde_json::from_value::<MessageWithI64>(got)?;
64-
assert_eq!(msg, roundtrip);
212+
#[test_case(json!({}))]
213+
#[test_case(json!({"mapKeyValue": {}}))]
214+
// TODO(#2349) - #[test_case(json!({"mapKeyValue": null}))]
215+
fn test_map_key_value_default(input: Value) -> Result {
216+
let want = MessageWithI64::default();
217+
let got = serde_json::from_value::<MessageWithI64>(input)?;
218+
assert_eq!(got, want);
219+
let output = serde_json::to_value(&got)?;
220+
assert_eq!(output, json!({}));
65221
Ok(())
66222
}
67223
}

0 commit comments

Comments
 (0)