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

WIP #798

Closed
wants to merge 8 commits into from
Closed

WIP #798

wants to merge 8 commits into from

Conversation

Bartleby2718
Copy link
Contributor

@manfred-brands @mikkelbu I've been looking into #759 in the past couple of days, but I'm getting the following error message in tests:

CS0103 The name 'Assert.ThatAsync' does not exist in the current context
at line 24 and character 18 in file TestClass.cs | await ↓Assert.ThatAsync(() => GetResultAsync(), Is.EqualTo(42));

I suppose this is because I didn't restrict the Analyzer/CodeFix to NUnit 4+. Is it possible to do so? If not, should I consider this not startable yet?

I tried updating TestUtility.WrapMethodInClassNamespaceAndAddUsings() to add #if NUNIT4 like this, but I'm still getting the error:

    #pragma warning disable CS8019
    using System;
    using System.Threading.Tasks;
    using NUnit.Framework;
    #if NUNIT4
    using NUnit.Framework.Legacy;
    #else
    using ClassicAssert = NUnit.Framework.Assert;
    #endif
    
    #pragma warning restore CS8019
    
    
    namespace NUnit.Analyzers.Tests.Targets.TestCaseUsage
    {
        [TestFixture]
        public class TestClass
        {
    #if NUNIT4
    
            [Test]
            public async Task Test1()
            {
                await Assert.ThatAsync(() => GetResultAsync(), Is.EqualTo(42));
            }
    
            private static Task<int> GetResultAsync() => Task.FromResult(42);
    #endif
        }
    }

@Bartleby2718 Bartleby2718 marked this pull request as draft December 3, 2024 03:03
@manfred-brands
Copy link
Member

I suppose this is because I didn't restrict the Analyzer/CodeFix to NUnit 4+.

Correct and yes this is possible.
Your analyzer needs to override the AnalyzAssertInvocation that has the Version nunitVersion parameter.
See UpdateStringFormatToInterpolatableStringAnalyzer for an example.
And then only continue if the user uses NUnit4: nunitVersion.Major > 4

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.

2 participants