Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add more tests for custom types #3827

Merged
merged 3 commits into from
Dec 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
222 changes: 222 additions & 0 deletions tests/IceRpc.Slice.Tests/DictionaryMappingTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,65 @@ await DictionaryMappingOperationsProxy.Response.DecodeReturnDictionaryOfMyCompac
Assert.That(r, Is.EqualTo(value));
}

[Test]
public async Task Operation_returning_a_dictionary_of_custom_value_types()
{
// Arrange
var value = new Dictionary<DateTime, DateTime>
{
[DateTime.UnixEpoch.AddYears(1)] = DateTime.UnixEpoch.AddYears(1),
[DateTime.UnixEpoch.AddYears(2)] = DateTime.UnixEpoch.AddYears(2),
[DateTime.UnixEpoch.AddYears(3)] = DateTime.UnixEpoch.AddYears(3)
};
PipeReader responsePayload =
IDictionaryMappingOperationsService.Response.EncodeReturnDictionaryOfCustomValueType(value);
using var request = new OutgoingRequest(new ServiceAddress(Protocol.IceRpc));
var response = new IncomingResponse(request, FakeConnectionContext.Instance)
{
Payload = responsePayload
};

// Act
Dictionary<DateTime, DateTime> r =
await DictionaryMappingOperationsProxy.Response.DecodeReturnDictionaryOfCustomValueTypeAsync(
response,
request,
InvalidProxy.Instance,
default);

// Assert
Assert.That(r, Is.EqualTo(value));
}

[Test]
public async Task Operation_returning_a_dictionary_of_custom_reference_types()
{
// Arrange
var value = new Dictionary<Uri, Uri>
{
[new Uri("icerpc://localhost")] = new Uri("icerpc://localhost"),
[new Uri("ice://localhost")] = new Uri("ice://localhost")
};
PipeReader responsePayload =
IDictionaryMappingOperationsService.Response.EncodeReturnDictionaryOfCustomReferenceType(value);
using var request = new OutgoingRequest(new ServiceAddress(Protocol.IceRpc));
var response = new IncomingResponse(request, FakeConnectionContext.Instance)
{
Payload = responsePayload
};

// Act
Dictionary<Uri, Uri> r =
await DictionaryMappingOperationsProxy.Response.DecodeReturnDictionaryOfCustomReferenceTypeAsync(
response,
request,
InvalidProxy.Instance,
default);

// Assert
Assert.That(r, Is.EqualTo(value));
}

[Test]
public async Task Operation_returning_a_dictionary_of_optional_int32()
{
Expand Down Expand Up @@ -295,6 +354,65 @@ await DictionaryMappingOperationsProxy.Response.DecodeReturnDictionaryOfOptional
Assert.That(r, Is.EqualTo(value));
}

[Test]
public async Task Operation_returning_a_dictionary_of_optional_custom_value_types()
{
// Arrange
var value = new Dictionary<DateTime, DateTime?>
{
[DateTime.UnixEpoch.AddYears(1)] = DateTime.UnixEpoch.AddYears(1),
[DateTime.UnixEpoch.AddYears(2)] = null,
[DateTime.UnixEpoch.AddYears(3)] = DateTime.UnixEpoch.AddYears(3)
};
PipeReader responsePayload =
IDictionaryMappingOperationsService.Response.EncodeReturnDictionaryOfOptionalCustomValueType(value);
using var request = new OutgoingRequest(new ServiceAddress(Protocol.IceRpc));
var response = new IncomingResponse(request, FakeConnectionContext.Instance)
{
Payload = responsePayload
};

// Act
Dictionary<DateTime, DateTime?> r =
await DictionaryMappingOperationsProxy.Response.DecodeReturnDictionaryOfOptionalCustomValueTypeAsync(
response,
request,
InvalidProxy.Instance,
default);

// Assert
Assert.That(r, Is.EqualTo(value));
}

[Test]
public async Task Operation_returning_a_dictionary_of_optional_custom_reference_types()
{
// Arrange
var value = new Dictionary<Uri, Uri?>
{
[new Uri("icerpc://localhost")] = new Uri("icerpc://localhost"),
[new Uri("ice://localhost")] = null
};
PipeReader responsePayload =
IDictionaryMappingOperationsService.Response.EncodeReturnDictionaryOfOptionalCustomReferenceType(value);
using var request = new OutgoingRequest(new ServiceAddress(Protocol.IceRpc));
var response = new IncomingResponse(request, FakeConnectionContext.Instance)
{
Payload = responsePayload
};

// Act
Dictionary<Uri, Uri?> r =
await DictionaryMappingOperationsProxy.Response.DecodeReturnDictionaryOfOptionalCustomReferenceTypeAsync(
response,
request,
InvalidProxy.Instance,
default);

// Assert
Assert.That(r, Is.EqualTo(value));
}

[Test]
public async Task Operation_returning_a_custom_dictionary()
{
Expand Down Expand Up @@ -447,6 +565,57 @@ await IDictionaryMappingOperationsService.Request.DecodeSendDictionaryOfMyCompac
Assert.That(decodedValue, Is.EqualTo(value));
}

[Test]
public async Task Operation_sending_a_dictionary_of_custom_value_types()
{
// Arrange
var value = new Dictionary<DateTime, DateTime>
{
[DateTime.UnixEpoch.AddYears(1)] = DateTime.UnixEpoch.AddYears(1),
[DateTime.UnixEpoch.AddYears(2)] = DateTime.UnixEpoch.AddYears(2),
[DateTime.UnixEpoch.AddYears(3)] = DateTime.UnixEpoch.AddYears(3)
};

// Act
var requestPayload = DictionaryMappingOperationsProxy.Request.EncodeSendDictionaryOfCustomValueType(value);

// Assert
using var request = new IncomingRequest(Protocol.IceRpc, FakeConnectionContext.Instance)
{
Payload = requestPayload
};
Dictionary<DateTime, DateTime> decodedValue =
await IDictionaryMappingOperationsService.Request.DecodeSendDictionaryOfCustomValueTypeAsync(
request,
default);
Assert.That(decodedValue, Is.EqualTo(value));
}

[Test]
public async Task Operation_sending_a_dictionary_of_custom_reference_types()
{
// Arrange
var value = new Dictionary<Uri, Uri>
{
[new Uri("icerpc://localhost")] = new Uri("icerpc://localhost"),
[new Uri("ice://localhost")] = new Uri("ice://localhost")
};

// Act
var requestPayload = DictionaryMappingOperationsProxy.Request.EncodeSendDictionaryOfCustomReferenceType(value);

// Assert
using var request = new IncomingRequest(Protocol.IceRpc, FakeConnectionContext.Instance)
{
Payload = requestPayload
};
Dictionary<Uri, Uri> decodedValue =
await IDictionaryMappingOperationsService.Request.DecodeSendDictionaryOfCustomReferenceTypeAsync(
request,
default);
Assert.That(decodedValue, Is.EqualTo(value));
}

[Test]
public async Task Operation_sending_a_dictionary_of_optional_int32()
{
Expand Down Expand Up @@ -548,6 +717,59 @@ await IDictionaryMappingOperationsService.Request.DecodeSendDictionaryOfOptional
Assert.That(decodedValue, Is.EqualTo(value));
}

[Test]
public async Task Operation_sending_a_dictionary_of_optional_custom_value_types()
{
// Arrange
var value = new Dictionary<DateTime, DateTime?>
{
[DateTime.UnixEpoch.AddYears(1)] = DateTime.UnixEpoch.AddYears(1),
[DateTime.UnixEpoch.AddYears(2)] = null,
[DateTime.UnixEpoch.AddYears(3)] = DateTime.UnixEpoch.AddYears(3)
};

// Act
var requestPayload =
DictionaryMappingOperationsProxy.Request.EncodeSendDictionaryOfOptionalCustomValueType(value);

// Assert
using var request = new IncomingRequest(Protocol.IceRpc, FakeConnectionContext.Instance)
{
Payload = requestPayload
};
Dictionary<DateTime, DateTime?> decodedValue =
await IDictionaryMappingOperationsService.Request.DecodeSendDictionaryOfOptionalCustomValueTypeAsync(
request,
default);
Assert.That(decodedValue, Is.EqualTo(value));
}

[Test]
public async Task Operation_sending_a_dictionary_of_optional_custom_reference_types()
{
// Arrange
var value = new Dictionary<Uri, Uri?>
{
[new Uri("icerpc://localhost")] = new Uri("icerpc://localhost"),
[new Uri("ice://localhost")] = null
};

// Act
var requestPayload =
DictionaryMappingOperationsProxy.Request.EncodeSendDictionaryOfOptionalCustomReferenceType(value);

// Assert
using var request = new IncomingRequest(Protocol.IceRpc, FakeConnectionContext.Instance)
{
Payload = requestPayload
};
Dictionary<Uri, Uri?> decodedValue =
await IDictionaryMappingOperationsService.Request.DecodeSendDictionaryOfOptionalCustomReferenceTypeAsync(
request,
default);
Assert.That(decodedValue, Is.EqualTo(value));
}

[Test]
public async Task Operation_sending_a_custom_dictionary()
{
Expand Down
12 changes: 12 additions & 0 deletions tests/IceRpc.Slice.Tests/DictionaryMappingTests.slice
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ interface DictionaryMappingOperations {
returnDictionaryOfMyCompactStruct() -> Dictionary<MyCompactStruct, MyCompactStruct>
sendDictionaryOfMyCompactStruct(p: Dictionary<MyCompactStruct, MyCompactStruct>)

returnDictionaryOfCustomValueType() -> Dictionary<WellKnownTypes::TimeStamp, WellKnownTypes::TimeStamp>
sendDictionaryOfCustomValueType(p: Dictionary<WellKnownTypes::TimeStamp, WellKnownTypes::TimeStamp>)

returnDictionaryOfCustomReferenceType() -> Dictionary<WellKnownTypes::Uri, WellKnownTypes::Uri>
sendDictionaryOfCustomReferenceType(p: Dictionary<WellKnownTypes::Uri, WellKnownTypes::Uri>)

returnDictionaryOfOptionalInt32() -> Dictionary<int32, int32?>
sendDictionaryOfOptionalInt32(p: Dictionary<int32, int32?>)

Expand All @@ -29,6 +35,12 @@ interface DictionaryMappingOperations {
returnDictionaryOfOptionalMyCompactStruct() -> Dictionary<MyCompactStruct, MyCompactStruct?>
sendDictionaryOfOptionalMyCompactStruct(p: Dictionary<MyCompactStruct, MyCompactStruct?>)

returnDictionaryOfOptionalCustomValueType() -> Dictionary<WellKnownTypes::TimeStamp, WellKnownTypes::TimeStamp?>
sendDictionaryOfOptionalCustomValueType(p: Dictionary<WellKnownTypes::TimeStamp, WellKnownTypes::TimeStamp?>)

returnDictionaryOfOptionalCustomReferenceType() -> Dictionary<WellKnownTypes::Uri, WellKnownTypes::Uri?>
sendDictionaryOfOptionalCustomReferenceType(p: Dictionary<WellKnownTypes::Uri, WellKnownTypes::Uri?>)

// We don't need to add tests for other cs::type arguments because the code path is always the same. The argument
// must be a non-abstract generic type that implements ICollection<KeyValuePair<TKey, TValue>> and provides a
// constructor with an initial capacity parameter.
Expand Down
Loading