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