Skip to content
Open
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
34 changes: 34 additions & 0 deletions InterfaceGenerator.Tests/MethodGenerationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,34 @@ public void GenericVoidMethodWithConstraints_IsImplemented()
_sut.GenericVoidMethodWithConstraints<object, StringBuilder>();
}

[Fact]
public void GenericVoidMethodWithValueTypeConstraints_IsImplemented()
{
var method = typeof(IMethodsTestService)
.GetMethods()
.First(x => x.Name == nameof(MethodsTestService.GenericVoidMethodWithValueTypeConstraints));

method.Should().NotBeNull();
method.ReturnType.Should().Be(typeof(void));

var genericArgs = method.GetGenericArguments();
genericArgs.Should().HaveCount(2);

genericArgs[0].IsValueType.Should().BeTrue();
genericArgs[0]
.GenericParameterAttributes.Should()
.HaveFlag(GenericParameterAttributes.DefaultConstructorConstraint)
.And.HaveFlag(GenericParameterAttributes.NotNullableValueTypeConstraint);

genericArgs[1].IsValueType.Should().BeTrue();
genericArgs[1]
.GenericParameterAttributes.Should()
.HaveFlag(GenericParameterAttributes.DefaultConstructorConstraint)
.And.HaveFlag(GenericParameterAttributes.NotNullableValueTypeConstraint);

_sut.GenericVoidMethodWithValueTypeConstraints<ValueTuple<int>, long>();
}

[Fact]
public void VoidMethodWithOptionalParams_IsImplemented()
{
Expand Down Expand Up @@ -315,6 +343,12 @@ public void GenericVoidMethodWithConstraints<TX, TY>()
{
}

public void GenericVoidMethodWithValueTypeConstraints<TX, TY>()
where TX : struct
where TY : unmanaged
{
}

public void VoidMethodWithOptionalParams(
string stringLiteral = "cGFyYW0=",
string stringConstant = StringConstant,
Expand Down
16 changes: 7 additions & 9 deletions InterfaceGenerator/TypeParameterSymbolExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,21 @@ internal static class TypeParameterSymbolExtensions
{
public static IEnumerable<string> EnumGenericConstraints(this ITypeParameterSymbol symbol)
{
// the class/struct/unmanaged/notnull constraint has to be the last
// the class/struct/unmanaged/notnull constraint has to be the first
// and cannot be combined with one another
if (symbol.HasNotNullConstraint)
{
yield return "notnull";
}

if (symbol.HasValueTypeConstraint)
else if (symbol.HasUnmanagedTypeConstraint)
{
yield return "struct";
yield return "unmanaged";
}

if (symbol.HasUnmanagedTypeConstraint)
else if (symbol.HasValueTypeConstraint)
{
yield return "unmanaged";
yield return "struct";
}

if (symbol.HasReferenceTypeConstraint)
else if (symbol.HasReferenceTypeConstraint)
{
yield return symbol.ReferenceTypeConstraintNullableAnnotation == NullableAnnotation.Annotated
? "class?"
Expand Down