コード例 #1
0
        public void LocalFunctionWithLambdaCoverage()
        {
            string source = @"
using System;

public class Program
{
    public static void Main(string[] args)                                  // Method 1
    {
        TestMain();
        Microsoft.CodeAnalysis.Runtime.Instrumentation.FlushPayload();
    }
    
    static void TestMain()                                                  // Method 2
    {
        new D().M1();
    } 
}

public class D
{
    public void M1()                                                        // Method 4
    {
        L1();
        void L1()
        {
            var f = new Func<int>(
                () => 1
            );

            var f1 = new Func<int>(
                () => 2
            );

            var f2 = new Func<int, int>(
                (x) => x + 3
            );

            var f3 = new Func<int, int>(
                x => x + 4
            );

            f();
            f3(2);
        }
    }

    // Method 5 is the synthesized instance constructor for D.
}
" + InstrumentationHelperSource;

            var checker = new CSharpInstrumentationChecker();
            checker.Method(1, 1, "public static void Main")
                .True("TestMain();")
                .True("Microsoft.CodeAnalysis.Runtime.Instrumentation.FlushPayload();");
            checker.Method(2, 1, "static void TestMain")
                .True("new D().M1();");
            checker.Method(4, 1, "public void M1()")
                .True("L1();")
                .True("1")
                .True("var f = new Func<int>")
                .False("2")
                .True("var f1 = new Func<int>")
                .False("x + 3")
                .True("var f2 = new Func<int, int>")
                .True("x + 4")
                .True("var f3 = new Func<int, int>")
                .True("f();")
                .True("f3(2);");
            checker.Method(5, 1, snippet: null, expectBodySpan: false);
            checker.Method(7, 1)
                .True()
                .False()
                .True()
                .True()
                .True()
                .True()
                .True()
                .True()
                .True()
                .True()
                .True()
                .True()
                .True();

            CompilationVerifier verifier = CompileAndVerify(source, expectedOutput: checker.ExpectedOutput, options: TestOptions.ReleaseExe);
            verifier.VerifyDiagnostics();
            checker.CompleteCheck(verifier.Compilation, source);

            verifier = CompileAndVerify(source, expectedOutput: checker.ExpectedOutput, options: TestOptions.DebugExe);
            verifier.VerifyDiagnostics();
            checker.CompleteCheck(verifier.Compilation, source);
        }
コード例 #2
0
        public void NonStaticImplicitBlockMethodsCoverage()
        {
            string source = @"
using System;

public class Program
{
    public int Prop { get; }

    public int Prop2 { get; } = 25;

    public int Prop3 { get; set; }                                              // Methods 3 and 4

    public Program()                                                            // Method 5
    {
        Prop = 12;
        Prop3 = 12;
        Prop2 = Prop3;
    }

    public static void Main(string[] args)                                      // Method 6
    {
        new Program();
        Microsoft.CodeAnalysis.Runtime.Instrumentation.FlushPayload();
    }
}
" + InstrumentationHelperSource;

            var checker = new CSharpInstrumentationChecker();
            checker.Method(3, 1, "public int Prop3")
                .True("get");
            checker.Method(4, 1, "public int Prop3")
                .True("set");
            checker.Method(5, 1, "public Program()")
                .True("25")
                .True("Prop = 12;")
                .True("Prop3 = 12;")
                .True("Prop2 = Prop3;");
            checker.Method(6, 1, "public static void Main")
                .True("new Program();")
                .True("Microsoft.CodeAnalysis.Runtime.Instrumentation.FlushPayload();");
            checker.Method(8, 1)
                .True()
                .False()
                .True()
                .True()
                .True()
                .True()
                .True()
                .True()
                .True()
                .True()
                .True()
                .True()
                .True();

            CompilationVerifier verifier = CompileAndVerify(source, expectedOutput: checker.ExpectedOutput, options: TestOptions.ReleaseExe);
            verifier.VerifyDiagnostics();
            checker.CompleteCheck(verifier.Compilation, source);

            verifier = CompileAndVerify(source, expectedOutput: checker.ExpectedOutput, options: TestOptions.DebugExe);
            verifier.VerifyDiagnostics();
            checker.CompleteCheck(verifier.Compilation, source);
        }