@@ -31,6 +31,7 @@ use crate::error::ClientError;
31
31
#[ derive( Copy , Clone , PartialEq , Eq , Hash ) ]
32
32
pub struct Height {
33
33
/// Previously known as "epoch"
34
+ #[ cfg_attr( feature = "serde" , serde( default ) ) ]
34
35
revision_number : u64 ,
35
36
36
37
/// The height of a block
@@ -179,37 +180,52 @@ impl FromStr for Height {
179
180
}
180
181
}
181
182
182
- #[ test]
183
- fn test_valid_height ( ) {
184
- assert_eq ! (
185
- "1-1" . parse:: <Height >( ) . unwrap( ) ,
186
- Height {
187
- revision_number: 1 ,
188
- revision_height: 1
189
- }
190
- ) ;
191
- assert_eq ! (
192
- "1-10" . parse:: <Height >( ) . unwrap( ) ,
193
- Height {
194
- revision_number: 1 ,
195
- revision_height: 10
196
- }
197
- ) ;
198
- }
199
-
200
- #[ test]
201
- fn test_invalid_height ( ) {
202
- assert ! ( "0-0" . parse:: <Height >( ) . is_err( ) ) ;
203
- assert ! ( "0-" . parse:: <Height >( ) . is_err( ) ) ;
204
- assert ! ( "-0" . parse:: <Height >( ) . is_err( ) ) ;
205
- assert ! ( "-" . parse:: <Height >( ) . is_err( ) ) ;
206
- assert ! ( "1-1-1" . parse:: <Height >( ) . is_err( ) ) ;
207
-
208
- let decoding_err = "1" . parse :: < Height > ( ) . unwrap_err ( ) ;
209
- let decoding_err = decoding_err. to_string ( ) ;
210
- assert ! ( decoding_err. contains( "height `1` not properly formatted" ) ) ;
211
-
212
- let decoding_err = "" . parse :: < Height > ( ) . unwrap_err ( ) ;
213
- let decoding_err = decoding_err. to_string ( ) ;
214
- assert ! ( decoding_err. contains( "height `` not properly formatted" ) ) ;
183
+ #[ cfg( test) ]
184
+ mod tests {
185
+ use super :: * ;
186
+
187
+ #[ test]
188
+ fn test_valid_height ( ) {
189
+ assert_eq ! (
190
+ "1-1" . parse:: <Height >( ) . unwrap( ) ,
191
+ Height {
192
+ revision_number: 1 ,
193
+ revision_height: 1
194
+ }
195
+ ) ;
196
+ assert_eq ! (
197
+ "1-10" . parse:: <Height >( ) . unwrap( ) ,
198
+ Height {
199
+ revision_number: 1 ,
200
+ revision_height: 10
201
+ }
202
+ ) ;
203
+ }
204
+
205
+ #[ test]
206
+ fn test_invalid_height ( ) {
207
+ assert ! ( "0-0" . parse:: <Height >( ) . is_err( ) ) ;
208
+ assert ! ( "0-" . parse:: <Height >( ) . is_err( ) ) ;
209
+ assert ! ( "-0" . parse:: <Height >( ) . is_err( ) ) ;
210
+ assert ! ( "-" . parse:: <Height >( ) . is_err( ) ) ;
211
+ assert ! ( "1-1-1" . parse:: <Height >( ) . is_err( ) ) ;
212
+
213
+ let decoding_err = "1" . parse :: < Height > ( ) . unwrap_err ( ) ;
214
+ let decoding_err = decoding_err. to_string ( ) ;
215
+ assert ! ( decoding_err. contains( "height `1` not properly formatted" ) ) ;
216
+
217
+ let decoding_err = "" . parse :: < Height > ( ) . unwrap_err ( ) ;
218
+ let decoding_err = decoding_err. to_string ( ) ;
219
+ assert ! ( decoding_err. contains( "height `` not properly formatted" ) ) ;
220
+ }
221
+
222
+ #[ test]
223
+ fn test_empty_rev_number_deserialization ( ) {
224
+ // #1262: ibc-go uses `omitempty` in JSON serialization
225
+ let json_str = r#"{"revision_height": 10}"# ;
226
+ let actual: Height = serde_json:: from_str ( json_str) . unwrap ( ) ;
227
+ let expected = Height :: new ( 0 , 10 ) . unwrap ( ) ;
228
+
229
+ assert_eq ! ( actual, expected) ;
230
+ }
215
231
}
0 commit comments