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), (48, 0, 1), (48, 1, 1) ); } finally { File.Delete(path); } }
public void Condition_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 <Conditions>(instance => { // We call method to trigger coverage hits instance.If(true); // For now we have only async Run helper return(Task.CompletedTask); }, 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); // Asserts on doc/lines/branches result.Document("Instrumentation.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); } }
public void SkipAutoProps(bool skipAutoProps) { string path = Path.GetTempFileName(); try { FunctionExecutor.Run(async(string[] parameters) => { CoveragePrepareResult coveragePrepareResult = await TestInstrumentationHelper.Run <AutoProps>(instance => { instance.AutoPropsNonInit = 10; instance.AutoPropsInit = 20; int readVal = instance.AutoPropsNonInit; readVal = instance.AutoPropsInit; return(Task.CompletedTask); }, persistPrepareResultToFile: parameters[0], skipAutoProps: bool.Parse(parameters[1])); return(0); }, new string[] { path, skipAutoProps.ToString() }); if (skipAutoProps) { TestInstrumentationHelper.GetCoverageResult(path) .Document("Instrumentation.AutoProps.cs") .AssertNonInstrumentedLines(BuildConfiguration.Debug, 12, 12) .AssertLinesCoveredFromTo(BuildConfiguration.Debug, 7, 11) .AssertLinesCovered(BuildConfiguration.Debug, (13, 1)); } else { TestInstrumentationHelper.GetCoverageResult(path) .Document("Instrumentation.AutoProps.cs") .AssertLinesCoveredFromTo(BuildConfiguration.Debug, 7, 13); } } finally { File.Delete(path); } }
public void SkipRecordWithProperties(bool skipAutoProps) { string path = Path.GetTempFileName(); try { FunctionExecutor.Run(async(string[] parameters) => { CoveragePrepareResult coveragePrepareResult = await TestInstrumentationHelper.Run <ClassWithAutoRecordProperties>(instance => { return(Task.CompletedTask); }, persistPrepareResultToFile: parameters[0], skipAutoProps: bool.Parse(parameters[1])); return(0); }, new string[] { path, skipAutoProps.ToString() }); if (skipAutoProps) { TestInstrumentationHelper.GetCoverageResult(path) .Document("Instrumentation.AutoProps.cs") .AssertNonInstrumentedLines(BuildConfiguration.Debug, 29, 29) .AssertNonInstrumentedLines(BuildConfiguration.Release, 29, 29) .AssertLinesCovered(BuildConfiguration.Debug, (32, 1), (33, 1), (34, 1)) .AssertLinesCovered(BuildConfiguration.Release, (33, 1)); } else { TestInstrumentationHelper.GetCoverageResult(path) .Document("Instrumentation.AutoProps.cs") .AssertLinesCovered(BuildConfiguration.Debug, (29, 3), (31, 1), (32, 1), (33, 1), (34, 1)) .AssertLinesCovered(BuildConfiguration.Release, (29, 3), (31, 1), (33, 1)); } } finally { File.Delete(path); } }
public void ExcludeFromCodeCoverage_CompilerGeneratedMethodsAndTypes() { string path = Path.GetTempFileName(); try { FunctionExecutor.Run(async(string[] pathSerialize) => { CoveragePrepareResult coveragePrepareResult = await TestInstrumentationHelper.Run <MethodsWithExcludeFromCodeCoverageAttr>(instance => { ((Task <int>)instance.Test("test")).ConfigureAwait(false).GetAwaiter().GetResult(); return(Task.CompletedTask); }, persistPrepareResultToFile: pathSerialize[0]); return(0); }, new string[] { path }); CoverageResult result = TestInstrumentationHelper.GetCoverageResult(path); var document = result.Document("Instrumentation.ExcludeFromCoverage.cs"); // Invoking method "Test" of class "MethodsWithExcludeFromCodeCoverageAttr" we expect to cover 100% lines for MethodsWithExcludeFromCodeCoverageAttr Assert.DoesNotContain(document.Lines, l => (l.Value.Class == "Coverlet.Core.Samples.Tests.MethodsWithExcludeFromCodeCoverageAttr" || // Compiler generated l.Value.Class.StartsWith("Coverlet.Core.Samples.Tests.MethodsWithExcludeFromCodeCoverageAttr/")) && l.Value.Hits == 0); // and 0% for MethodsWithExcludeFromCodeCoverageAttr2 Assert.DoesNotContain(document.Lines, l => (l.Value.Class == "Coverlet.Core.Samples.Tests.MethodsWithExcludeFromCodeCoverageAttr2" || // Compiler generated l.Value.Class.StartsWith("Coverlet.Core.Samples.Tests.MethodsWithExcludeFromCodeCoverageAttr2/")) && l.Value.Hits == 1); } finally { File.Delete(path); } }
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); } }