Make the MsTest source generator emit an override modifier when the base class has a TestContext property#1440
Make the MsTest source generator emit an override modifier when the base class has a TestContext property#1440SimonCropp merged 3 commits intoVerifyTests:mainfrom m-ringler:main
Conversation
|
@dotnet-policy-service agree company="Zeiss" |
|
@MattKotsenas got time to do a review? |
|
This looks fine to me, however supporting this scenario brings up caveats that are worth thinking through. Consider code like this: public class BaseClass
{
protected TestContext _testContext = null!;
public virtual TestContext TestContext { get { return _testContext; } set { _testContext = value; } }
}
[TestClass]
[UsesVerify]
public partial class TestClass : BaseClass
{
}BaseClass might expect that This gets more confusing if you add a third class to the mix: [TestClass]
public class TestClass2 : TestClass
{
public override TestContext TestContext { get; set; }
}without the additional TestContext property this inheritence hierarchy works fine. Prior to this change, adding the override results in CS0506. After this change, it compiles but fails at runtime with It's probably on balance an OK change to take, as I doubt many people are doing tricky things with the TestContext, but it's perhaps worth calling out in the docs? |
|
We could mitigate the 1st scenario by also calling the base setter in the generated setter. We could mitigate the 2nd scenario by sealing the overridden TestContext property. What do you think? |
|
@MattKotsenas thoughts? |
|
I didn't realize you could mark a single method as sealed! Both seem like good additions to prevent accidental misuse. @m-ringler , are you willing to try adding both to this PR? |
|
Yes, I will. |
|
@MattKotsenas, @SimonCropp |
MattKotsenas
left a comment
There was a problem hiding this comment.
LGTM. Left 1 item for code style, but no other concerns from my end. Thanks for this!
| static IEnumerable<IPropertySymbol> BaseTestContextProperties( | ||
| INamedTypeSymbol typeSymbol) | ||
| => | ||
| from baseClass in BaseClassesOf(typeSymbol) |
There was a problem hiding this comment.
nit: I think this is the only place in the codebase to use the query-style syntax. It's up to @SimonCropp but I'd assume you want method-style for consistency.
There was a problem hiding this comment.
i do prefer method-style. but i can fix that after merge
|
@m-ringler thanks for this. i will deploy it now |
When a base class of the TestClass already has a TestContext property this PR makes the source generator emit an
overridekeyword on the TestContext property that it generates.The behavior is unchanged when a TestContext property is declared in the TestClass itself.
Fixes #1437