Skip to content

Commit

Permalink
PerformDefinedConversions handles multiple conflicting conversions (#…
Browse files Browse the repository at this point in the history
…2429)

TypeUtility.PerformDefinedConversions can now handle the case where there is more than 1 "op_Implicit" or "op_Explicit" method
  • Loading branch information
louis-z committed Jan 10, 2022
1 parent 38445ec commit 145b4fc
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 23 deletions.
50 changes: 27 additions & 23 deletions src/xunit.v3.common/Utility/TypeUtility.cs
Expand Up @@ -148,42 +148,46 @@ public static string ConvertToSimpleTypeName(_ITypeInfo type)
if (argumentValue == null)
return null;

var argumentValueType = argumentValue.GetType();

// We don't need to check if we're passing null to a value type here, as MethodInfo.Invoke does this
if (argumentValueType == null)
return argumentValue;

// No need to perform conversion
if (parameterType.IsAssignableFrom(argumentValueType))
if (parameterType.IsAssignableFrom(argumentValue.GetType()))
return argumentValue;

// Implicit & explicit conversions to/from a type can be declared on either side of the relationship
// We need to check both possibilities.
return PerformDefinedConversions(argumentValue, parameterType)
?? PerformDefinedConversions(argumentValue, argumentValueType)
?? argumentValue;
return PerformDefinedConversions(argumentValue, parameterType) ?? argumentValue;
}

static object? PerformDefinedConversions(
object argumentValue,
Type conversionDeclaringType)
Type parameterType)
{
// argumentValue is known to not be null when we're called from TryConvertObject
var argumentValueType = argumentValue.GetType();

var methodTypes = new Type[] { argumentValueType };
var methodArguments = new object[] { argumentValue };

// Check if we can implicitly convert the argument type to the parameter type
var implicitMethod = conversionDeclaringType.GetRuntimeMethod("op_Implicit", methodTypes);
if (implicitMethod != null && implicitMethod.IsStatic && !IsByRefLikeType(implicitMethod.ReturnType))
return implicitMethod.Invoke(null, methodArguments);
bool isMatchingOperator(
MethodInfo m,
string name) =>
m.Name.Equals(name) &&
m.IsSpecialName && // Filter out non-operator methods that might bear this reserved name
m.IsStatic &&
!IsByRefLikeType(m.ReturnType) &&
m.GetParameters().Length == 1 &&
m.GetParameters()[0].ParameterType == argumentValueType &&
parameterType.IsAssignableFrom(m.ReturnType);

// Implicit & explicit conversions to/from a type can be declared on either side of the relationship.
// We need to check both possibilities.
foreach (var conversionDeclaringType in new[] { parameterType, argumentValueType })
{
var runtimeMethods = conversionDeclaringType.GetRuntimeMethods();

// Check if we can explicitly convert the argument type to the parameter type
var explicitMethod = conversionDeclaringType.GetRuntimeMethod("op_Explicit", methodTypes);
if (explicitMethod != null && explicitMethod.IsStatic && !IsByRefLikeType(explicitMethod.ReturnType))
return explicitMethod.Invoke(null, methodArguments);
var implicitMethod = runtimeMethods.FirstOrDefault(m => isMatchingOperator(m, "op_Implicit"));
if (implicitMethod != null)
return implicitMethod.Invoke(null, methodArguments);

var explicitMethod = runtimeMethods.FirstOrDefault(m => isMatchingOperator(m, "op_Explicit"));
if (explicitMethod != null)
return explicitMethod.Invoke(null, methodArguments);
}

return null;
}
Expand Down
23 changes: 23 additions & 0 deletions src/xunit.v3.core.tests/Acceptance/Xunit3TheoryAcceptanceTests.cs
Expand Up @@ -266,6 +266,8 @@ public async void ImplicitExplicitConversions()
results.OfType<_TestPassed>().Select(passed => results.OfType<_TestStarting>().Where(ts => ts.TestUniqueID == passed.TestUniqueID).Single().TestDisplayName).OrderBy(x => x),
displayName => Assert.Equal(@"Xunit3TheoryAcceptanceTests+TheoryTests+ClassWithOperatorConversions.ArgumentDeclaredExplicitConversion(value: ""abc"")", displayName),
displayName => Assert.Equal(@"Xunit3TheoryAcceptanceTests+TheoryTests+ClassWithOperatorConversions.ArgumentDeclaredImplicitConversion(value: ""abc"")", displayName),
displayName => Assert.Equal(@"Xunit3TheoryAcceptanceTests+TheoryTests+ClassWithOperatorConversions.DecimalToInt(value: 43)", displayName),
displayName => Assert.Equal(@"Xunit3TheoryAcceptanceTests+TheoryTests+ClassWithOperatorConversions.IntToDecimal(value: 43)", displayName),
displayName => Assert.Equal(@"Xunit3TheoryAcceptanceTests+TheoryTests+ClassWithOperatorConversions.IntToLong(i: 1)", displayName),
displayName => Assert.Equal(@"Xunit3TheoryAcceptanceTests+TheoryTests+ClassWithOperatorConversions.ParameterDeclaredExplicitConversion(e: Explicit { Value = ""abc"" })", displayName),
displayName => Assert.Equal(@"Xunit3TheoryAcceptanceTests+TheoryTests+ClassWithOperatorConversions.ParameterDeclaredImplicitConversion(i: Implicit { Value = ""abc"" })", displayName),
Expand Down Expand Up @@ -329,6 +331,27 @@ public void UIntToULong(ulong i)
Assert.Equal(1UL, i);
}

public static IEnumerable<object[]> DecimalArgument()
{
yield return new object[] { 43M };
}

// Decimal type offers multiple explicit conversions
[Theory]
[MemberData(nameof(DecimalArgument))]
public void DecimalToInt(int value)
{
Assert.Equal(43, value);
}

// Decimal type offers multiple implicit conversions
[Theory]
[InlineData(43)]
public void IntToDecimal(decimal value)
{
Assert.Equal(43M, value);
}

public class Explicit
{
public string? Value { get; set; }
Expand Down

0 comments on commit 145b4fc

Please sign in to comment.