Esempio n. 1
0
        public void SelectionStatements_If()
        {
            // We need to pass file name to remote process where it save instrumentation result
            // Similar to msbuild input/output
            string path = Path.GetTempFileName();

            try
            {
                // Lambda will run in a custom process to avoid issue with statics and file locking
                RemoteExecutor.Invoke(async pathSerialize =>
                {
                    // Run load and call a delegate passing class as dynamic to simplify method call
                    CoveragePrepareResult coveragePrepareResult = await TestInstrumentationHelper.Run <SelectionStatements>(instance =>
                    {
                        // We call method to trigger coverage hits
                        instance.If(true);

                        // For now we have only async Run helper
                        return(Task.CompletedTask);
                    }, persistPrepareResultToFile: pathSerialize);

                    // we return 0 if we return something different assert fail
                    return(0);
                }, path).Dispose();

                // We retrive and load CoveragePrepareResult and run coverage calculation
                // Similar to msbuild coverage result task
                CoverageResult result = TestInstrumentationHelper.GetCoverageResult(path);

                // Generate html report to check
                // TestInstrumentationHelper.GenerateHtmlReport(result);

                // Asserts on doc/lines/branches
                result.Document("Instrumentation.SelectionStatements.cs")
                // (line, hits)
                .AssertLinesCovered((11, 1), (15, 0))
                // (line,ordinal,hits)
                .AssertBranchesCovered((9, 0, 1), (9, 1, 0));
            }
            finally
            {
                // Cleanup tmp file
                File.Delete(path);
            }
        }
Esempio n. 2
0
        public void Lambda_Issue343()
        {
            string path = Path.GetTempFileName();

            try
            {
                RemoteExecutor.Invoke(async pathSerialize =>
                {
                    CoveragePrepareResult coveragePrepareResult = await TestInstrumentationHelper.Run <Lambda_Issue343>(instance =>
                    {
                        instance.InvokeAnonymous_Test();
                        ((Task <bool>)instance.InvokeAnonymousAsync_Test()).ConfigureAwait(false).GetAwaiter().GetResult();
                        return(Task.CompletedTask);
                    }, persistPrepareResultToFile: pathSerialize);
                    return(0);
                }, path).Dispose();

                CoverageResult result = TestInstrumentationHelper.GetCoverageResult(path);

                result.Document("Instrumentation.Lambda.cs")
                .AssertLinesCoveredAllBut(BuildConfiguration.Debug, 23, 51)
                .AssertBranchesCovered(BuildConfiguration.Debug,
                                       // Expected branches
                                       (22, 0, 0),
                                       (22, 1, 1),
                                       (50, 2, 0),
                                       (50, 3, 1),
                                       // Unexpected branches
                                       (20, 0, 1),
                                       (20, 1, 1),
                                       (49, 0, 1),
                                       (49, 1, 0),
                                       (54, 4, 0),
                                       (54, 5, 1),
                                       (39, 0, 1),
                                       (39, 1, 0),
                                       (48, 0, 1),
                                       (48, 1, 1)
                                       );
            }
            finally
            {
                File.Delete(path);
            }
        }
Esempio n. 3
0
        public void AwaitUsing()
        {
            string path = Path.GetTempFileName();

            try
            {
                FunctionExecutor.Run(async(string[] pathSerialize) =>
                {
                    CoveragePrepareResult coveragePrepareResult = await TestInstrumentationHelper.Run <AwaitUsing>(instance =>
                    {
                        ((ValueTask)instance.HasAwaitUsing()).GetAwaiter().GetResult();
                        ((Task)instance.Issue914_Repro()).GetAwaiter().GetResult();

                        return(Task.CompletedTask);
                    }, persistPrepareResultToFile: pathSerialize[0]);
                    return(0);
                }, new string[] { path });

                TestInstrumentationHelper.GetCoverageResult(path)
                .Document("Instrumentation.AwaitUsing.cs")
                .AssertLinesCovered(BuildConfiguration.Debug,
                                    // HasAwaitUsing()
                                    (13, 1), (14, 1), (15, 1), (16, 1), (17, 1),
                                    // Issue914_Repro()
                                    (21, 1), (22, 1), (23, 1), (24, 1),
                                    // Issue914_Repro_Example1()
                                    (28, 1), (29, 1), (30, 1),
                                    // Issue914_Repro_Example2()
                                    (34, 1), (35, 1), (36, 1), (37, 1),
                                    // MyTransaction.DisposeAsync()
                                    (43, 2), (44, 2), (45, 2)
                                    )
                .ExpectedTotalNumberOfBranches(BuildConfiguration.Debug, 0);
            }
            finally
            {
                File.Delete(path);
            }
        }
        public void AsyncIterator()
        {
            string path = Path.GetTempFileName();

            try
            {
                FunctionExecutor.Run(async(string[] pathSerialize) =>
                {
                    CoveragePrepareResult coveragePrepareResult = await TestInstrumentationHelper.Run <AsyncIterator>(instance =>
                    {
                        int res = ((Task <int>)instance.Issue1104_Repro()).GetAwaiter().GetResult();

                        return(Task.CompletedTask);
                    }, persistPrepareResultToFile: pathSerialize[0]);
                    return(0);
                }, new string[] { path });

                TestInstrumentationHelper.GetCoverageResult(path)
                .Document("Instrumentation.AsyncIterator.cs")
                .AssertLinesCovered(BuildConfiguration.Debug,
                                    // Issue1104_Repro()
                                    (14, 1), (15, 1), (17, 203), (18, 100), (19, 100), (20, 100), (22, 1), (23, 1),
                                    // CreateSequenceAsync()
                                    (26, 1), (27, 202), (28, 100), (29, 100), (30, 100), (31, 100), (32, 1)
                                    )
                .AssertBranchesCovered(BuildConfiguration.Debug,
                                       // Issue1104_Repro(),
                                       (17, 0, 1), (17, 1, 100),
                                       // CreateSequenceAsync()
                                       (27, 0, 1), (27, 1, 100)
                                       )
                .ExpectedTotalNumberOfBranches(BuildConfiguration.Debug, 2);
            }
            finally
            {
                File.Delete(path);
            }
        }
        public void CatchBlock_Issue465()
        {
            string path = Path.GetTempFileName();

            try
            {
                FunctionExecutor.Run(async(string[] pathSerialize) =>
                {
                    CoveragePrepareResult coveragePrepareResult = await TestInstrumentationHelper.Run <CatchBlock>(instance =>
                    {
                        instance.Test();
                        instance.Test_Catch();
                        ((Task)instance.TestAsync()).ConfigureAwait(false).GetAwaiter().GetResult();
                        ((Task)instance.TestAsync_Catch()).ConfigureAwait(false).GetAwaiter().GetResult();

                        instance.Test(true);
                        instance.Test_Catch(true);
                        ((Task)instance.TestAsync(true)).ConfigureAwait(false).GetAwaiter().GetResult();
                        ((Task)instance.TestAsync_Catch(true)).ConfigureAwait(false).GetAwaiter().GetResult();

                        instance.Test(false);
                        instance.Test_Catch(false);
                        ((Task)instance.TestAsync(false)).ConfigureAwait(false).GetAwaiter().GetResult();
                        ((Task)instance.TestAsync_Catch(false)).ConfigureAwait(false).GetAwaiter().GetResult();

                        instance.Test_WithTypedCatch();
                        instance.Test_Catch_WithTypedCatch();
                        ((Task)instance.TestAsync_WithTypedCatch()).ConfigureAwait(false).GetAwaiter().GetResult();
                        ((Task)instance.TestAsync_Catch_WithTypedCatch()).ConfigureAwait(false).GetAwaiter().GetResult();

                        instance.Test_WithTypedCatch(true);
                        instance.Test_Catch_WithTypedCatch(true);
                        ((Task)instance.TestAsync_WithTypedCatch(true)).ConfigureAwait(false).GetAwaiter().GetResult();
                        ((Task)instance.TestAsync_Catch_WithTypedCatch(true)).ConfigureAwait(false).GetAwaiter().GetResult();

                        instance.Test_WithTypedCatch(false);
                        instance.Test_Catch_WithTypedCatch(false);
                        ((Task)instance.TestAsync_WithTypedCatch(false)).ConfigureAwait(false).GetAwaiter().GetResult();
                        ((Task)instance.TestAsync_Catch_WithTypedCatch(false)).ConfigureAwait(false).GetAwaiter().GetResult();

                        instance.Test_WithNestedCatch(true);
                        instance.Test_Catch_WithNestedCatch(true);
                        ((Task)instance.TestAsync_WithNestedCatch(true)).ConfigureAwait(false).GetAwaiter().GetResult();
                        ((Task)instance.TestAsync_Catch_WithNestedCatch(true)).ConfigureAwait(false).GetAwaiter().GetResult();

                        instance.Test_WithNestedCatch(false);
                        instance.Test_Catch_WithNestedCatch(false);
                        ((Task)instance.TestAsync_WithNestedCatch(false)).ConfigureAwait(false).GetAwaiter().GetResult();
                        ((Task)instance.TestAsync_Catch_WithNestedCatch(false)).ConfigureAwait(false).GetAwaiter().GetResult();

                        return(Task.CompletedTask);
                    }, persistPrepareResultToFile: pathSerialize[0]);
                    return(0);
                }, new string[] { path });

                var res = TestInstrumentationHelper.GetCoverageResult(path);
                res.Document("Instrumentation.CatchBlock.cs")
                .AssertLinesCoveredAllBut(BuildConfiguration.Debug, 45, 59, 113, 127, 137, 138, 139, 153, 154, 155, 156, 175, 189, 199, 200, 201, 222, 223, 224, 225, 252, 266, 335, 349)
                .ExpectedTotalNumberOfBranches(BuildConfiguration.Debug, 6);
            }
            finally
            {
                File.Delete(path);
            }
        }