1+ using System . Text . Json ;
2+ using System . Text . Json . Protobuf . Tests ;
3+ using Google . Protobuf . WellKnownTypes ;
4+ using Protobuf . System . Text . Json . Tests . Utils ;
5+ using Shouldly ;
6+ using SmartAnalyzers . ApprovalTestsExtensions ;
7+ using Xunit ;
8+
9+ namespace Protobuf . System . Text . Json . Tests ;
10+
11+ public class MessageWithDurationTests
12+ {
13+ [ Fact ]
14+ public void Should_serialize_message_with_duration ( )
15+ {
16+ // Arrange
17+ var msg = new MessageWithDuration
18+ {
19+ DurationProperty = new Duration { Seconds = 256 , Nanos = 512 }
20+ } ;
21+
22+ var jsonSerializerOptions = TestHelper . CreateJsonSerializerOptions ( ) ;
23+
24+ // Act
25+ var serialized = JsonSerializer . Serialize ( msg , jsonSerializerOptions ) ;
26+
27+ // Assert
28+ var approver = new ExplicitApprover ( ) ;
29+ approver . VerifyJson ( serialized ) ;
30+ }
31+
32+ [ Fact ]
33+ public void Should_serialize_message_with_duration_when_value_not_set ( )
34+ {
35+ // Arrange
36+ var msg = new MessageWithDuration ( ) ;
37+
38+ var jsonSerializerOptions = TestHelper . CreateJsonSerializerOptions ( ) ;
39+
40+ // Act
41+ var serialized = JsonSerializer . Serialize ( msg , jsonSerializerOptions ) ;
42+
43+ // Assert
44+ var approver = new ExplicitApprover ( ) ;
45+ approver . VerifyJson ( serialized ) ;
46+ }
47+
48+ [ Fact ]
49+ public void Should_serialize_and_deserialize_message_with_duration ( )
50+ {
51+ // Arrange
52+ var msg = new MessageWithDuration
53+ {
54+ DurationProperty = new Duration { Seconds = 256 , Nanos = 500 }
55+ } ;
56+ var jsonSerializerOptions = TestHelper . CreateJsonSerializerOptions ( ) ;
57+
58+ // Act
59+ var serialized = JsonSerializer . Serialize ( msg , jsonSerializerOptions ) ;
60+ var deserialized = JsonSerializer . Deserialize < MessageWithDuration > ( serialized , jsonSerializerOptions ) ;
61+
62+ // Assert
63+ deserialized . ShouldNotBeNull ( ) ;
64+ deserialized . ShouldBeEquivalentTo ( msg ) ;
65+ }
66+
67+ [ Fact ]
68+ public void Should_serialize_and_deserialize_message_with_duration_when_value_is_not_set ( )
69+ {
70+ // Arrange
71+ var msg = new MessageWithDuration ( ) ;
72+ var jsonSerializerOptions = TestHelper . CreateJsonSerializerOptions ( ) ;
73+
74+ // Act
75+ var serialized = JsonSerializer . Serialize ( msg , jsonSerializerOptions ) ;
76+ var deserialized = JsonSerializer . Deserialize < MessageWithDuration > ( serialized , jsonSerializerOptions ) ;
77+
78+ // Assert
79+ deserialized . ShouldNotBeNull ( ) ;
80+ deserialized . ShouldBeEquivalentTo ( msg ) ;
81+ }
82+
83+ [ Fact ]
84+ public void Should_serialize_duration_property_as_complex_object_when_TreatDurationAsTimeSpan_option_set_to_false ( )
85+ {
86+ // Arrange
87+ var msg = new MessageWithDuration
88+ {
89+ DurationProperty = new Duration { Seconds = 256 , Nanos = 512 }
90+ } ;
91+
92+ var jsonSerializerOptions = new JsonSerializerOptions ( ) ;
93+ jsonSerializerOptions . PropertyNamingPolicy = JsonNamingPolicy . CamelCase ;
94+ jsonSerializerOptions . AddProtobufSupport ( options =>
95+ {
96+ options . TreatDurationAsTimeSpan = false ;
97+ } ) ;
98+
99+ // Act
100+ var serialized = JsonSerializer . Serialize ( msg , jsonSerializerOptions ) ;
101+
102+ // Assert
103+ var approver = new ExplicitApprover ( ) ;
104+ approver . VerifyJson ( serialized ) ;
105+ }
106+
107+ [ Fact ]
108+ public void Should_deserialize_duration_property_from_complex_object_when_TreatDurationAsTimeSpan_option_set_to_false ( )
109+ {
110+ // Arrange
111+ var msg = new MessageWithDuration
112+ {
113+ DurationProperty = new Duration { Seconds = 256 , Nanos = 512 }
114+ } ;
115+
116+ var jsonSerializerOptions = new JsonSerializerOptions ( ) ;
117+ jsonSerializerOptions . PropertyNamingPolicy = JsonNamingPolicy . CamelCase ;
118+ jsonSerializerOptions . AddProtobufSupport ( options =>
119+ {
120+ options . TreatDurationAsTimeSpan = false ;
121+ } ) ;
122+
123+ // Act
124+ var payload = JsonSerializer . Serialize ( msg , jsonSerializerOptions ) ;
125+ var deserialized = JsonSerializer . Deserialize < MessageWithDuration > ( payload , jsonSerializerOptions ) ;
126+
127+ // Assert
128+ deserialized . ShouldNotBeNull ( ) ;
129+ deserialized . ShouldBeEquivalentTo ( msg ) ;
130+
131+ }
132+ }
0 commit comments