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

FP NUnit1001: CustomTypeConverters could convert from anything #559

Conversation

Corniel
Copy link
Contributor

@Corniel Corniel commented Jun 26, 2023

I stumbled into this, as I use this feature quite often:

[TestCase(23.00)]
pulic void TestSomething(Qowaiv.Financial.Amount amount)
{
  // ...
}

Where Qowaiv.Financial.Amount is a value type that has its own type converter that not only can convert from string, but also from int, long, double, float, and decimal. This works like a charm, so it too bad that this falsely flags NUnit1001.

I've provided a fix, But if the implementation should be changed, just let me no.

Note that none existing test started to fail with this changed behavior.

@@ -210,6 +210,10 @@ private static bool HasBuiltInImplicitConversion(ITypeSymbol argumentType, IType
}
}
}
else if (targetTypeSymbol.GetAttributes().Any(att => att.AttributeClass?.GetFullMetadataName() == typeof(TypeConverterAttribute).FullName))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not behind a computer, but isn't TypeConverter not already dealt with?
You added a vase when targetType is null?
The CustomTypeConverter in your test doesn't override the Can convert from tested in the existing code.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TypeConverters are only dealt with for strings. And the code tries to check for types it can access the code from. In most cases that is not possible. So basically, you can not know if it is supported or not, but because of that, you should report on it. Specially because the default severity is an error, and it runs perfectly.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. I am on leave without access to a development PC until middle of July. I will look further then.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I forgot to look at.
Your added case can be removed. Instead we can remove the condition on the line below.

@manfred-brands manfred-brands force-pushed the fp-type-converters-can-also-convert-from-other-types branch from 1bd4371 to b59c277 Compare August 15, 2023 04:07
Copy link
Member

@manfred-brands manfred-brands left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added a commit to your branch simplifying the solution.
Please check if it works for your use case.

Comment on lines 274 to 292
[TypeConverter(typeof(CustomTypeConverter))]
struct CustomType { }
class CustomTypeConverter : TypeConverter { }");

RoslynAssert.Valid(this.analyzer, testCode);
}

[Test]
public void AnalyzeArgumentIsSomeConvertedToTypeWithCustomConverter()
{
var testCode = TestUtility.WrapClassInNamespaceAndAddUsing(@"
using System.ComponentModel;

class C
{
[TestCase(17)]
public void Test(CustomType p) { }
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is already a test for this case, but one that assumes failure.

@@ -210,6 +210,10 @@ private static bool HasBuiltInImplicitConversion(ITypeSymbol argumentType, IType
}
}
}
else if (targetTypeSymbol.GetAttributes().Any(att => att.AttributeClass?.GetFullMetadataName() == typeof(TypeConverterAttribute).FullName))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I forgot to look at.
Your added case can be removed. Instead we can remove the condition on the line below.

@Corniel
Copy link
Contributor Author

Corniel commented Aug 15, 2023

@manfred-brands I saw you made the changes yourself. Would you like me to make any changes in this PR?

@manfred-brands
Copy link
Member

@manfred-brands I saw you made the changes yourself. Would you like me to make any changes in this PR?

No. Are you happy with the changes?

@manfred-brands manfred-brands merged commit 156c9c0 into nunit:master Aug 19, 2023
4 checks passed
@Corniel Corniel deleted the fp-type-converters-can-also-convert-from-other-types branch August 19, 2023 19:07
@mikkelbu mikkelbu added this to the Release 3.7 / 2.7 milestone Aug 20, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants