public void If_DoesNotReturnAttribute_InstrumentsCorrect() { string path = Path.GetTempFileName(); try { FunctionExecutor.Run(async(string[] pathSerialize) => { CoveragePrepareResult coveragePrepareResult = await TestInstrumentationHelper.Run <DoesNotReturn>(instance => { try { instance.If(); } catch (Exception) { } return(Task.CompletedTask); }, persistPrepareResultToFile: pathSerialize[0]); return(0); }, new string[] { path }); CoverageResult result = TestInstrumentationHelper.GetCoverageResult(path); result.Document("Instrumentation.DoesNotReturn.cs") .AssertInstrumentLines(BuildConfiguration.Debug, 7, 8, 19, 20, 22, 23, 24, 25, 29, 30) .AssertNonInstrumentedLines(BuildConfiguration.Debug, 26, 27); } finally { File.Delete(path); } }
public void WithLeave_DoesNotReturnAttribute_InstrumentsCorrect() { string path = Path.GetTempFileName(); try { FunctionExecutor.Run(async(string[] pathSerialize) => { CoveragePrepareResult coveragePrepareResult = await TestInstrumentationHelper.Run <DoesNotReturn>(instance => { try { instance.WithLeave(); } catch (Exception) { } return(Task.CompletedTask); }, persistPrepareResultToFile: pathSerialize[0]); return(0); }, new string[] { path }); CoverageResult result = TestInstrumentationHelper.GetCoverageResult(path); result.Document("Instrumentation.DoesNotReturn.cs") .AssertInstrumentLines(BuildConfiguration.Debug, 7, 8, 147, 149, 150, 151, 152, 153, 154, 155, 156, 159, 161, 166, 167, 168) .AssertNonInstrumentedLines(BuildConfiguration.Debug, 163, 164); } finally { File.Delete(path); } }
public void FiltersAndFinallies_DoesNotReturnAttribute_InstrumentsCorrect() { string path = Path.GetTempFileName(); try { FunctionExecutor.Run(async(string[] pathSerialize) => { CoveragePrepareResult coveragePrepareResult = await TestInstrumentationHelper.Run <DoesNotReturn>(instance => { try { instance.FiltersAndFinallies(); } catch (Exception) { } return(Task.CompletedTask); }, persistPrepareResultToFile: pathSerialize[0]); return(0); }, new string[] { path }); CoverageResult result = TestInstrumentationHelper.GetCoverageResult(path); result.Document("Instrumentation.DoesNotReturn.cs") .AssertInstrumentLines(BuildConfiguration.Debug, 7, 8, 171, 173, 174, 175, 179, 180, 181, 182, 185, 186, 187, 188, 192, 193, 194) .AssertNonInstrumentedLines(BuildConfiguration.Debug, 176, 177, 183, 184, 189, 190, 195, 196, 197); } finally { File.Delete(path); } }
public void Subtle_DoesNotReturnAttribute_InstrumentsCorrect() { string path = Path.GetTempFileName(); try { FunctionExecutor.Run(async(string[] pathSerialize) => { CoveragePrepareResult coveragePrepareResult = await TestInstrumentationHelper.Run <DoesNotReturn>(instance => { try { instance.Subtle(); } catch (Exception) { } return(Task.CompletedTask); }, persistPrepareResultToFile: pathSerialize[0]); return(0); }, new string[] { path }); CoverageResult result = TestInstrumentationHelper.GetCoverageResult(path); result.Document("Instrumentation.DoesNotReturn.cs") .AssertInstrumentLines(BuildConfiguration.Debug, 7, 8, 72, 73, 75, 78, 82, 83, 86, 87, 91, 92, 95, 101, 102, 103) .AssertNonInstrumentedLines(BuildConfiguration.Debug, 79, 80, 88, 96, 98, 99); } finally { File.Delete(path); } }
public void CallsGenericClassDoesNotReturn_DoesNotReturnAttribute_InstrumentsCorrect() { string path = Path.GetTempFileName(); try { FunctionExecutor.Run(async(string[] pathSerialize) => { CoveragePrepareResult coveragePrepareResult = await TestInstrumentationHelper.Run <DoesNotReturn>(instance => { try { instance.CallsGenericClassDoesNotReturn(); } catch (Exception) { } return(Task.CompletedTask); }, persistPrepareResultToFile: pathSerialize[0], doesNotReturnAttributes: _ => new string[] { "DoesNotReturnAttribute" }); return(0); }, new string[] { path }); CoverageResult result = TestInstrumentationHelper.GetCoverageResult(path); result.Document("Instrumentation.DoesNotReturn.cs") .AssertInstrumentLines(BuildConfiguration.Debug, 134, 135, 140, 141, 142) .AssertNonInstrumentedLines(BuildConfiguration.Debug, 143, 144); } finally { File.Delete(path); } }
public void Yield_Two() { string path = Path.GetTempFileName(); try { FunctionExecutor.Run(async(string[] pathSerialize) => { CoveragePrepareResult coveragePrepareResult = await TestInstrumentationHelper.Run <Yield>(instance => { foreach (dynamic _ in instance.Two()) { ; } return(Task.CompletedTask); }, persistPrepareResultToFile: pathSerialize[0]); return(0); }, new string[] { path }); CoverageResult result = TestInstrumentationHelper.GetCoverageResult(path); result.Document("Instrumentation.Yield.cs") .Method("System.Boolean Coverlet.Core.Samples.Tests.Yield/<Two>d__1::MoveNext()") .AssertLinesCovered((14, 1), (15, 1)) .ExpectedTotalNumberOfBranches(0); } finally { File.Delete(path); } }
public void Switch_DoesNotReturnAttribute_InstrumentsCorrect() { string path = Path.GetTempFileName(); try { FunctionExecutor.Run(async(string[] pathSerialize) => { CoveragePrepareResult coveragePrepareResult = await TestInstrumentationHelper.Run <DoesNotReturn>(instance => { try { instance.Switch(); } catch (Exception) { } return(Task.CompletedTask); }, persistPrepareResultToFile: pathSerialize[0]); return(0); }, new string[] { path }); CoverageResult result = TestInstrumentationHelper.GetCoverageResult(path); result.Document("Instrumentation.DoesNotReturn.cs") .AssertInstrumentLines(BuildConfiguration.Debug, 7, 8, 33, 34, 36, 39, 40, 44, 45, 49, 50, 52, 53, 55, 56, 58, 59, 61, 62, 64, 65, 68, 69) .AssertNonInstrumentedLines(BuildConfiguration.Debug, 41, 42); } finally { File.Delete(path); } }
public void SelectionStatements_Switch() { string path = Path.GetTempFileName(); try { RemoteExecutor.Invoke(async pathSerialize => { CoveragePrepareResult coveragePrepareResult = await TestInstrumentationHelper.Run <SelectionStatements>(instance => { instance.Switch(1); return(Task.CompletedTask); }, persistPrepareResultToFile: pathSerialize); return(0); }, path).Dispose(); CoverageResult result = TestInstrumentationHelper.GetCoverageResult(path); result.Document("Instrumentation.SelectionStatements.cs") .AssertLinesCovered(BuildConfiguration.Release, (24, 1), (26, 0), (28, 0)) .AssertBranchesCovered(BuildConfiguration.Release, (24, 1, 1)) .AssertLinesCovered(BuildConfiguration.Debug, (20, 1), (21, 1), (24, 1), (30, 1)) .AssertBranchesCovered(BuildConfiguration.Debug, (21, 0, 0), (21, 1, 1), (21, 2, 0), (21, 3, 0)); } finally { File.Delete(path); } }
public void Yield_SingleWithSwitch() { string path = Path.GetTempFileName(); try { FunctionExecutor.Run(async(string[] pathSerialize) => { CoveragePrepareResult coveragePrepareResult = await TestInstrumentationHelper.Run <Yield>(instance => { foreach (var _ in instance.OneWithSwitch(2)) { ; } return(Task.CompletedTask); }, persistPrepareResultToFile: pathSerialize[0]); return(0); }, new string[] { path }); CoverageResult result = TestInstrumentationHelper.GetCoverageResult(path); result.Document("Instrumentation.Yield.cs") .Method("System.Boolean Coverlet.Core.Samples.Tests.Yield/<OneWithSwitch>d__2::MoveNext()") .AssertLinesCovered(BuildConfiguration.Debug, (21, 1), (30, 1), (31, 1), (37, 1)) .ExpectedTotalNumberOfBranches(1); } finally { File.Delete(path); } }
public void Yield_Enumerable() { string path = Path.GetTempFileName(); try { FunctionExecutor.Run(async(string[] pathSerialize) => { CoveragePrepareResult coveragePrepareResult = await TestInstrumentationHelper.Run <Yield>(instance => { foreach (var _ in instance.Enumerable(new[] { "one", "two", "three", "four" })) { ; } return(Task.CompletedTask); }, persistPrepareResultToFile: pathSerialize[0]); return(0); }, new string[] { path }); CoverageResult result = TestInstrumentationHelper.GetCoverageResult(path); result.Document("Instrumentation.Yield.cs") .Method("System.Boolean Coverlet.Core.Samples.Tests.Yield/<Enumerable>d__4::MoveNext()") .AssertLinesCovered(BuildConfiguration.Debug, (48, 1), (49, 1), (50, 4), (51, 5), (52, 1), (54, 4), (55, 4), (56, 4), (57, 1)) .ExpectedTotalNumberOfBranches(1); } finally { File.Delete(path); } }
public void ExcludeFromCodeCoverageCompilerGeneratedMethodsAndTypes_Issue670() { string path = Path.GetTempFileName(); try { FunctionExecutor.Run(async(string[] pathSerialize) => { CoveragePrepareResult coveragePrepareResult = await TestInstrumentationHelper.Run <MethodsWithExcludeFromCodeCoverageAttr_Issue670>(instance => { instance.Test("test"); return(Task.CompletedTask); }, persistPrepareResultToFile: pathSerialize[0]); return(0); }, new string[] { path }); CoverageResult result = TestInstrumentationHelper.GetCoverageResult(path); result.Document("Instrumentation.ExcludeFromCoverage.Issue670.cs") .AssertLinesCovered(BuildConfiguration.Debug, (8, 1), (9, 1), (10, 1), (11, 1)) .AssertNonInstrumentedLines(BuildConfiguration.Debug, 15, 53); } finally { File.Delete(path); } }
public void ExcludeFromCodeCoverage_CompilerGeneratedMethodsAndTypes_NestedMembers() { string path = Path.GetTempFileName(); try { RemoteExecutor.Invoke(async pathSerialize => { CoveragePrepareResult coveragePrepareResult = await TestInstrumentationHelper.Run <MethodsWithExcludeFromCodeCoverageAttr_NestedStateMachines>(instance => { instance.Test(); return(Task.CompletedTask); }, persistPrepareResultToFile: pathSerialize); return(0); }, path).Dispose(); CoverageResult result = TestInstrumentationHelper.GetCoverageResult(path); result.Document("Instrumentation.ExcludeFromCoverage.NestedStateMachines.cs") .AssertLinesCovered(BuildConfiguration.Debug, (14, 1), (15, 1), (16, 1)) .AssertNonInstrumentedLines(BuildConfiguration.Debug, 9, 11); } finally { File.Delete(path); } }
public void AsyncAwait() { string path = Path.GetTempFileName(); try { RemoteExecutor.Invoke(async pathSerialize => { CoveragePrepareResult coveragePrepareResult = await TestInstrumentationHelper.Run <AsyncAwait>(instance => { instance.SyncExecution(); int res = ((Task <int>)instance.AsyncExecution(true)).ConfigureAwait(false).GetAwaiter().GetResult(); res = ((Task <int>)instance.AsyncExecution(1)).ConfigureAwait(false).GetAwaiter().GetResult(); res = ((Task <int>)instance.AsyncExecution(2)).ConfigureAwait(false).GetAwaiter().GetResult(); res = ((Task <int>)instance.AsyncExecution(3)).ConfigureAwait(false).GetAwaiter().GetResult(); res = ((Task <int>)instance.ContinuationCalled()).ConfigureAwait(false).GetAwaiter().GetResult(); res = ((Task <int>)instance.ConfigureAwait()).ConfigureAwait(false).GetAwaiter().GetResult(); return(Task.CompletedTask); }, persistPrepareResultToFile: pathSerialize); return(0); }, path).Dispose(); CoverageResult result = TestInstrumentationHelper.GetCoverageResult(path); result.Document("Instrumentation.AsyncAwait.cs") .AssertLinesCovered(BuildConfiguration.Debug, // AsyncExecution(bool) (10, 1), (11, 1), (12, 1), (14, 1), (16, 1), (17, 0), (18, 0), (19, 0), (21, 1), (22, 1), // Async (25, 9), (26, 9), (27, 9), (28, 9), // SyncExecution (31, 1), (32, 1), (33, 1), // Sync (36, 1), (37, 1), (38, 1), // AsyncExecution(int) (41, 3), (42, 3), (43, 3), (46, 1), (47, 1), (48, 1), (51, 1), (52, 1), (53, 1), (56, 1), (57, 1), (58, 1), (59, 1), (62, 0), (63, 0), (64, 0), (65, 0), (68, 0), (70, 3), (71, 3), // ContinuationNotCalled (74, 0), (75, 0), (76, 0), (77, 0), (78, 0), // ContinuationCalled -> line 83 should be 1 hit some issue with Continuation state machine (81, 1), (82, 1), (83, 2), (84, 1), (85, 1), // ConfigureAwait (89, 1), (90, 1) ) .AssertBranchesCovered(BuildConfiguration.Debug, (16, 0, 0), (16, 1, 1), (43, 0, 3), (43, 1, 1), (43, 2, 1), (43, 3, 1), (43, 4, 0)) // Real branch should be 2, we should try to remove compiler generated branch in method ContinuationNotCalled/ContinuationCalled // for Continuation state machine .ExpectedTotalNumberOfBranches(BuildConfiguration.Debug, 4); } finally { File.Delete(path); } }
public void CoverageResult_NegativeLineCoverage_TranslatedToMaxValueOfInt32() { var instrumenterResult = new InstrumenterResult { HitsFilePath = "HitsFilePath", SourceLink = "SourceLink", ModulePath = "ModulePath" }; instrumenterResult.HitCandidates.Add(new HitCandidate(false, 0, 1, 1)); var document = new Document { Index = 0, Path = "Path0" }; document.Lines.Add(1, new Line { Class = "Class0", Hits = 0, Method = "Method0", Number = 1 }); instrumenterResult.Documents.Add("document", document); var coveragePrepareResult = new CoveragePrepareResult { UseSourceLink = true, Results = new[] { instrumenterResult }, Parameters = new CoverageParameters() }; Stream memoryStream = new MemoryStream(); var binaryWriter = new BinaryWriter(memoryStream); binaryWriter.Write(1); binaryWriter.Write(-1); memoryStream.Position = 0; var fileSystemMock = new Mock <IFileSystem>(); fileSystemMock.Setup(x => x.Exists(It.IsAny <string>())).Returns(true); fileSystemMock.Setup(x => x.NewFileStream(It.IsAny <string>(), FileMode.Open, FileAccess.Read)) .Returns(memoryStream); var coverage = new Coverage(coveragePrepareResult, new Mock <ILogger>().Object, new Mock <IInstrumentationHelper>().Object, fileSystemMock.Object, new Mock <ISourceRootTranslator>().Object); CoverageResult coverageResult = coverage.GetCoverageResult(); coverageResult.Document("document").AssertLinesCovered(BuildConfiguration.Debug, (1, int.MaxValue)); }
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); } }
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); } }
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 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 }); CoverageResult 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) .ExpectedTotalNumberOfBranches(BuildConfiguration.Release, 6); } finally { File.Delete(path); } }