-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
157 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
...ests.Should_serialize_message_with_well_known_types_when_values_are_not_set.approved.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"doubleValue": null, | ||
"floatValue": null, | ||
"int64Value": null, | ||
"uint64Value": null, | ||
"int32Value": null, | ||
"uint32Value": null, | ||
"boolValue": null, | ||
"stringValue": null | ||
} |
10 changes: 10 additions & 0 deletions
10
...pesTests.Should_serialize_message_with_well_known_types_when_values_are_set.approved.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"doubleValue": 1.7976931348623157E+308, | ||
"floatValue": 3.4028235E+38, | ||
"int64Value": 9223372036854775807, | ||
"uint64Value": 18446744073709551615, | ||
"int32Value": 2147483647, | ||
"uint32Value": 4294967295, | ||
"boolValue": true, | ||
"stringValue": "some_string_value" | ||
} |
94 changes: 94 additions & 0 deletions
94
test/Protobuf.System.Text.Json.Tests/MessageWithWellKnownTypesTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
using System.Text.Json; | ||
using System.Text.Json.Protobuf.Tests; | ||
using System.Text.Json.Serialization; | ||
using Protobuf.System.Text.Json.Tests.Utils; | ||
using Shouldly; | ||
using SmartAnalyzers.ApprovalTestsExtensions; | ||
using Xunit; | ||
|
||
namespace Protobuf.System.Text.Json.Tests; | ||
|
||
public class MessageWithWellKnownTypesTests | ||
{ | ||
[Fact] | ||
public void Should_serialize_message_with_well_known_types_when_values_are_set() | ||
{ | ||
// Arrange | ||
var msg = new MessageWithWellKnownTypes | ||
{ | ||
DoubleValue = double.MaxValue, | ||
FloatValue = float.MaxValue, | ||
Int64Value = long.MaxValue, | ||
Uint64Value = ulong.MaxValue, | ||
Int32Value = int.MaxValue, | ||
Uint32Value = uint.MaxValue, | ||
BoolValue = true, | ||
StringValue = "some_string_value", | ||
}; | ||
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_well_known_types_when_values_are_not_set() | ||
{ | ||
// Arrange | ||
var msg = new MessageWithWellKnownTypes(); | ||
var jsonSerializerOptions = TestHelper.CreateJsonSerializerOptions(); | ||
jsonSerializerOptions.DefaultIgnoreCondition = JsonIgnoreCondition.Never; | ||
|
||
// Act | ||
var serialized = JsonSerializer.Serialize(msg, jsonSerializerOptions); | ||
|
||
// Assert | ||
var approver = new ExplicitApprover(); | ||
approver.VerifyJson(serialized); | ||
} | ||
|
||
[Fact] | ||
public void Should_serialize_and_deserialize_message_with_well_known_types_when_values_are_set() | ||
{ | ||
// Arrange | ||
var msg = new MessageWithWellKnownTypes | ||
{ | ||
DoubleValue = double.MaxValue, | ||
FloatValue = float.MaxValue, | ||
Int64Value = long.MaxValue, | ||
Uint64Value = ulong.MaxValue, | ||
Int32Value = int.MaxValue, | ||
Uint32Value = uint.MaxValue, | ||
BoolValue = true, | ||
StringValue = "some_string_value" | ||
}; | ||
var jsonSerializerOptions = TestHelper.CreateJsonSerializerOptions(); | ||
var serialized = JsonSerializer.Serialize(msg, jsonSerializerOptions); | ||
|
||
// Act | ||
var deserialized = JsonSerializer.Deserialize<MessageWithWellKnownTypes>(serialized, jsonSerializerOptions); | ||
|
||
|
||
// Assert | ||
deserialized.ShouldBe(msg); | ||
} | ||
|
||
[Fact] | ||
public void Should_serialize_and_deserialize_message_with_well_known_types_when_values_are_not_set() | ||
{ | ||
// Arrange | ||
var msg = new MessageWithWellKnownTypes(); | ||
var jsonSerializerOptions = TestHelper.CreateJsonSerializerOptions(); | ||
var serialized = JsonSerializer.Serialize(msg, jsonSerializerOptions); | ||
|
||
// Act | ||
var deserialized = JsonSerializer.Deserialize<MessageWithWellKnownTypes>(serialized, jsonSerializerOptions); | ||
|
||
// Assert | ||
deserialized.ShouldBe(msg); | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
test/Protobuf.System.Text.Json.Tests/Protos/message_with_well_known_types.proto
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
syntax = "proto3"; | ||
|
||
option csharp_namespace = "System.Text.Json.Protobuf.Tests"; | ||
|
||
import "google/protobuf/wrappers.proto"; | ||
|
||
message MessageWithWellKnownTypes { | ||
google.protobuf.DoubleValue double_value = 1; | ||
|
||
google.protobuf.FloatValue float_value = 2; | ||
|
||
google.protobuf.Int64Value int_64_value = 3; | ||
|
||
google.protobuf.UInt64Value uint_64_value = 4; | ||
|
||
google.protobuf.Int32Value int_32_Value = 5; | ||
|
||
google.protobuf.UInt32Value uint_32_Value = 6; | ||
|
||
google.protobuf.BoolValue bool_value = 7; | ||
|
||
google.protobuf.StringValue string_value = 8; | ||
} |