14
14
15
15
#[ cfg( test) ]
16
16
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 < ( ) > ;
19
21
20
22
#[ allow( dead_code) ]
21
23
mod protos {
@@ -24,44 +26,198 @@ mod test {
24
26
}
25
27
use protos:: MessageWithI64 ;
26
28
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
+ }
31
120
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
+ }
38
143
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!( { } ) ) ;
41
153
Ok ( ( ) )
42
154
}
43
155
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
+ }
50
175
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!( { } ) ) ;
53
185
Ok ( ( ) )
54
186
}
55
187
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
+ }
62
211
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!( { } ) ) ;
65
221
Ok ( ( ) )
66
222
}
67
223
}
0 commit comments