@@ -158,7 +158,45 @@ impl Error for UStatus {}
158158mod tests {
159159 use super :: * ;
160160
161- use protobuf:: { Enum , EnumOrUnknown } ;
161+ use protobuf:: { well_known_types:: any:: Any , Enum , EnumOrUnknown , Message } ;
162+
163+ #[ test]
164+ // [utest->req~ustatus-data-model-impl~1]
165+ fn test_ustatus_fail_with_code ( ) {
166+ let details = vec ! [ Any {
167+ type_url: "https://google.com/timestamp" . to_string( ) ,
168+ ..Default :: default ( )
169+ } ] ;
170+ UCode :: VALUES . iter ( ) . for_each ( |code| {
171+ let mut ustatus = UStatus :: fail_with_code ( * code, "the message" ) ;
172+ // just make sure that the field exists and we can assign a value to it
173+ ustatus. details = details. clone ( ) ;
174+ assert ! (
175+ ustatus. code. enum_value( ) . is_ok_and( |v| v == * code)
176+ && ustatus. message. is_some_and( |v| v == "the message" )
177+ ) ;
178+ } ) ;
179+ }
180+
181+ #[ test]
182+ // [utest->req~ustatus-data-model-proto~1]
183+ fn test_proto_serialization ( ) {
184+ let ustatus = UStatus {
185+ code : UCode :: CANCELLED . into ( ) ,
186+ message : Some ( "the message" . to_string ( ) ) ,
187+ details : vec ! [ Any {
188+ type_url: "https://google.com/timestamp" . to_string( ) ,
189+ ..Default :: default ( )
190+ } ] ,
191+ ..Default :: default ( )
192+ } ;
193+ let proto = ustatus
194+ . write_to_bytes ( )
195+ . expect ( "failed to serialize to protobuf" ) ;
196+ let deserialized_status =
197+ UStatus :: parse_from_bytes ( proto. as_slice ( ) ) . expect ( "failed to deserialize protobuf" ) ;
198+ assert_eq ! ( ustatus, deserialized_status) ;
199+ }
162200
163201 #[ test]
164202 fn test_is_failed ( ) {
0 commit comments