Ejemplo n.º 1
0
        public async Task ReportWarningOnAsyncVoidEventHandlerWithMyEventArgs()
        {
            var test    = @"
using System;

class Test {
    async void F(object sender, MyEventArgs e) {
    }
}

class MyEventArgs : EventArgs {}
";
            var withFix = @"
using System;

class Test {
    async System.Threading.Tasks.Task F(object sender, MyEventArgs e) {
    }
}

class MyEventArgs : EventArgs {}
";

            CodeAnalysis.Testing.DiagnosticResult expected = Verify.Diagnostic().WithLocation(5, 16);
            await Verify.VerifyCodeFixAsync(test, expected, withFix);
        }
        public async Task UsingTaskOfTReturningMethodInTaskReturningMethod_GeneratesError()
        {
            var test    = @"
using System.Threading.Tasks;
using Microsoft.VisualStudio.Threading;

class Test {
    Task F() {
        AsyncSemaphore lck = null;
        using (lck.EnterAsync())
        {
        }

        return TplExtensions.CompletedTask;
    }
}
";
            var withFix = @"
using System.Threading.Tasks;
using Microsoft.VisualStudio.Threading;

class Test {
    async Task F() {
        AsyncSemaphore lck = null;
        using (await lck.EnterAsync())
        {
        }
    }
}
";

            CodeAnalysis.Testing.DiagnosticResult expected = Verify.Diagnostic().WithSpan(8, 16, 8, 32);
            await Verify.VerifyCodeFixAsync(test, expected, withFix);
        }
        public async Task UsingTaskOfTCompoundExpressionInAsyncMethod_GeneratesError()
        {
            var test    = @"
using System;
using System.Threading.Tasks;
using Microsoft.VisualStudio.Threading;

class Test {
    async Task F() {
        Task<IDisposable> t1 = null, t2 = null;
        using (t1 ?? t2)
        {
        }
    }
}
";
            var withFix = @"
using System;
using System.Threading.Tasks;
using Microsoft.VisualStudio.Threading;

class Test {
    async Task F() {
        Task<IDisposable> t1 = null, t2 = null;
        using (await (t1 ?? t2))
        {
        }
    }
}
";

            CodeAnalysis.Testing.DiagnosticResult expected = Verify.Diagnostic().WithSpan(9, 16, 9, 24);
            await Verify.VerifyCodeFixAsync(test, expected, withFix);
        }
Ejemplo n.º 4
0
        public async Task ApplyFixesOnAsyncVoidMethod2()
        {
            var test    = @"
using System;
using System.Threading.Tasks;

class Test {
    async void F() {
        await Task.Yield();
    }
}
";
            var withFix = @"
using System;
using System.Threading.Tasks;

class Test {
    async Task F() {
        await Task.Yield();
    }
}
";

            CodeAnalysis.Testing.DiagnosticResult expected = Verify.Diagnostic().WithLocation(6, 16);
            await Verify.VerifyCodeFixAsync(test, expected, withFix);
        }
Ejemplo n.º 5
0
        public async Task ReportWarningOnAsyncVoidEventHandlerSimilarToAsyncEventHandler2()
        {
            var test = @"
using System;

class Test {
    async void F(string sender, EventArgs e) {
    }
}
";

            CodeAnalysis.Testing.DiagnosticResult expected = Verify.Diagnostic().WithLocation(5, 16);
            await Verify.VerifyAnalyzerAsync(test, expected);
        }
Ejemplo n.º 6
0
        public async Task ReportWarningOnAsyncVoidMethod()
        {
            var test = @"
using System;

class Test {
    async void F() {
    }
}
";

            CodeAnalysis.Testing.DiagnosticResult expected = Verify.Diagnostic().WithLocation(5, 16);
            await Verify.VerifyAnalyzerAsync(test, expected);
        }
Ejemplo n.º 7
0
        public async Task AffinityAssertion_WithinDebugAssert_ProducesDiagnostic()
        {
            var test = @"
using System;
using System.Diagnostics;
using Microsoft.VisualStudio.Shell;

class Test {
    void F() {
        System.Diagnostics.Debug.Assert(ThreadHelper.{|#0:CheckAccess|}());
    }
}
";

            CodeAnalysis.Testing.DiagnosticResult expected = Verify.Diagnostic().WithLocation(0);
            await Verify.VerifyAnalyzerAsync(test, expected);
        }
        public async Task JtfRunInProtectedMethodsOfInternalType_ProducesDiagnostic()
        {
            var test = @"
using Microsoft.VisualStudio.Threading;

class Test {
    JoinableTaskFactory jtf;

    protected void F() {
        jtf.Run(() => TplExtensions.CompletedTask);
    }
}
";

            CodeAnalysis.Testing.DiagnosticResult expected = Verify.Diagnostic().WithLocation(8, 13);
            await Verify.VerifyAnalyzerAsync(test, expected);
        }
        public async Task UsingTaskOfTLocal_GeneratesError()
        {
            var test = @"
using System;
using System.Threading.Tasks;
using Microsoft.VisualStudio.Threading;

class Test {
    void F() {
        Task<IDisposable> lck = null;
        using (lck)
        {
        }
    }
}
";

            CodeAnalysis.Testing.DiagnosticResult expected = Verify.Diagnostic().WithSpan(9, 16, 9, 19);
            await Verify.VerifyAnalyzerAsync(test, expected);
        }
        public async Task JTFRunFromPublicVoidMethod_GeneratesWarning()
        {
            var test = @"
using System.Threading.Tasks;
using Microsoft.VisualStudio.Threading;

public class Test {
    JoinableTaskFactory jtf;

    public void Foo() {
        jtf.Run(async delegate {
            await Task.Yield();
        });
    }
}
";

            CodeAnalysis.Testing.DiagnosticResult expected = Verify.Diagnostic().WithSpan(9, 13, 9, 16);
            await Verify.VerifyAnalyzerAsync(test, expected);
        }
Ejemplo n.º 11
0
        public async Task AffinityAssertion_WithinWhileBlock_ProducesDiagnostic()
        {
            var test = @"
using System;
using Microsoft.VisualStudio.Shell;

class Test {
    bool check;

    void F() {
        while (check)
        {
            ThreadHelper.{|#0:ThrowIfNotOnUIThread|}();
        }
    }
}
";

            CodeAnalysis.Testing.DiagnosticResult expected = Verify.Diagnostic().WithLocation(0);
            await Verify.VerifyAnalyzerAsync(test, expected);
        }
        public async Task JtfRunInImplicitInterfaceImplementationOfInternalInterface_ProducesDiagnostic()
        {
            var test = @"
using Microsoft.VisualStudio.Threading;

interface IFoo
{
    void F();
}

class Test : IFoo {
    JoinableTaskFactory jtf;

    public void F() {
        jtf.Run(() => TplExtensions.CompletedTask);
    }
}
";

            CodeAnalysis.Testing.DiagnosticResult expected = Verify.Diagnostic().WithLocation(13, 13);
            await Verify.VerifyAnalyzerAsync(test, expected);
        }
Ejemplo n.º 13
0
        public async Task AffinityAssertion_WithinAnyConditionalMethodArg_ProducesDiagnostic()
        {
            var test = @"
using System;
using System.Diagnostics;
using Microsoft.VisualStudio.Shell;

class Test {
    void F() {
        ThrowIfNot(ThreadHelper.{|#0:CheckAccess|}());
    }

    [Conditional(""DEBUG"")]
    private void ThrowIfNot(bool expr)
    {
        if (!expr) throw new InvalidOperationException();
    }
}
";

            CodeAnalysis.Testing.DiagnosticResult expected = Verify.Diagnostic().WithLocation(0);
            await Verify.VerifyAnalyzerAsync(test, expected);
        }