From 5fd86456ac67b0c6299f52280e8ddd9014bed80d Mon Sep 17 00:00:00 2001 From: Steve Gilham Date: Mon, 10 Jul 2023 17:06:56 +0100 Subject: [PATCH] Skip anonymous types with zero code points just like we skip other compiler generated cruft --- AltCover.Engine/Visitor.fs | 32 ++++++++++++++++++++++---------- AltCover.Tests/Tests.fs | 10 ++++++++-- Samples/Sample21/UnitTest1.cs | 16 ++++++++++++++++ 3 files changed, 46 insertions(+), 12 deletions(-) diff --git a/AltCover.Engine/Visitor.fs b/AltCover.Engine/Visitor.fs index 88034f772..e30dfa897 100644 --- a/AltCover.Engine/Visitor.fs +++ b/AltCover.Engine/Visitor.fs @@ -284,6 +284,10 @@ module internal CoverageParameters = let mutable internal staticFilter: StaticFilter option = None + let effectiveStaticFilter () = + staticFilter + |> Option.defaultValue StaticFilter.Hidden + let internal showGenerated = ref false let generationFilter = @@ -747,6 +751,15 @@ module internal Visitor = t.Methods |> Seq.exists (fun m -> m.IsAbstract |> not) + [] + let internal stripAnonymous (t: TypeDefinition) = + t.Name.StartsWith("<>f__AnonymousType", StringComparison.Ordinal) + |> not + || CoverageParameters.effectiveStaticFilter () + <> StaticFilter.Hidden + let private visitModule (x: ModuleEntry) (buildSequence: Node -> seq) = zeroPoints () @@ -772,7 +785,8 @@ module internal Visitor = |> Seq.collect (fun x -> x.Module.GetAllTypes() |> Seq.cast - |> Seq.filter stripInterfaces) + |> Seq.filter stripInterfaces + |> Seq.filter stripAnonymous) |> Seq.collect ( (fun t -> let types = @@ -1108,9 +1122,7 @@ module internal Visitor = if significant m then StaticFilter.NoFilter else - match CoverageParameters.staticFilter with - | None -> StaticFilter.Hidden - | Some f -> f + CoverageParameters.effectiveStaticFilter () (m, key)) |> Seq.filter (fun (m, k) -> k <> StaticFilter.Hidden) @@ -1226,10 +1238,10 @@ module internal Visitor = let places = List.concat [ toNext; toJump ] let start = - places |> (boundaryOfList List.minBy) + places |> (boundaryOfList List.minBy) // This line to suppress let finish = - places |> (boundaryOfList List.maxBy) + places |> (boundaryOfList List.maxBy) // This line to suppress let range = Seq.unfold @@ -1376,7 +1388,7 @@ module internal Visitor = // possibly add MoveNext filtering let generated (i: Instruction) = - let before = firstOfSequencePoint dbg i // This line in suppress + let before = firstOfSequencePoint dbg i // This line to suppress let sp = dbg.GetSequencePoint before before.OpCode = OpCodes.Ldloc_0 @@ -1648,18 +1660,18 @@ module internal Visitor = "AvoidMessageChainsRule", Scope = "member", Target = - "AltCover.Visitor/I/generated@1379::Invoke(Mono.Cecil.Cil.Instruction)", + "AltCover.Visitor/I/generated@1391::Invoke(Mono.Cecil.Cil.Instruction)", Justification = "No direct call available")>] [,Microsoft.FSharp.Collections.FSharpList`1)", + "AltCover.Visitor/I/start@1241::Invoke(Microsoft.FSharp.Core.FSharpFunc`2,Microsoft.FSharp.Collections.FSharpList`1)", Justification = "Inlined library code")>] [,Microsoft.FSharp.Collections.FSharpList`1)", + "AltCover.Visitor/I/finish@1244::Invoke(Microsoft.FSharp.Core.FSharpFunc`2,Microsoft.FSharp.Collections.FSharpList`1)", Justification = "Inlined library code")>] () \ No newline at end of file diff --git a/AltCover.Tests/Tests.fs b/AltCover.Tests/Tests.fs index c4148d12e..491744b25 100644 --- a/AltCover.Tests/Tests.fs +++ b/AltCover.Tests/Tests.fs @@ -5636,12 +5636,15 @@ module AltCoverTests = test <@ - classes = [ "Sample21.Tests" + classes = [ "Sample21.Product" + "Sample21.Tests" "Sample21.Traditional" ] @> let expectedMethods = - [ "System.String Sample21.Traditional::DoSomething()" + [ "System.String Sample21.Product::Junk(System.String)" + "System.String Sample21.Traditional::DoSomething()" + "System.Void Sample21.Product::.ctor(System.String)" "System.Void Sample21.Tests::.ctor()" "System.Void Sample21.Tests::Setup()" "System.Void Sample21.Tests::Test1()" @@ -5703,6 +5706,7 @@ module AltCoverTests = [ "Sample21.IModern" "Sample21.Modern1" "Sample21.Modern2" + "Sample21.Product" "Sample21.Tests" "Sample21.Traditional" ] @@ -5711,9 +5715,11 @@ module AltCoverTests = let expectedMethods = [ "System.String Sample21.IModern::DoSomething()" "System.String Sample21.Modern2::DoSomething()" + "System.String Sample21.Product::Junk(System.String)" "System.String Sample21.Traditional::DoSomething()" "System.Void Sample21.Modern1::.ctor()" "System.Void Sample21.Modern2::.ctor()" + "System.Void Sample21.Product::.ctor(System.String)" "System.Void Sample21.Tests::.ctor()" "System.Void Sample21.Tests::Setup()" "System.Void Sample21.Tests::Test1()" diff --git a/Samples/Sample21/UnitTest1.cs b/Samples/Sample21/UnitTest1.cs index 5b7ea01ea..1737c5206 100644 --- a/Samples/Sample21/UnitTest1.cs +++ b/Samples/Sample21/UnitTest1.cs @@ -2,6 +2,22 @@ namespace Sample21 { + public class Product + { + public Product(string name) + { Name = name; } + + public string Name { get; set; } + + public static string Junk(string name) + { + var product = new Product(name); + var bonus = new { note = "You won!" }; + var shipmentWithBonus = new { address = "Somewhere St.", product, bonus }; + return shipmentWithBonus.ToString(); + } + } + public class Tests { [SetUp]