Skip to content

Commit

Permalink
Skip anonymous types with zero code points just like we skip other co…
Browse files Browse the repository at this point in the history
…mpiler generated cruft
  • Loading branch information
SteveGilham committed Jul 10, 2023
1 parent 886583d commit 5fd8645
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 12 deletions.
32 changes: 22 additions & 10 deletions AltCover.Engine/Visitor.fs
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down Expand Up @@ -747,6 +751,15 @@ module internal Visitor =
t.Methods
|> Seq.exists (fun m -> m.IsAbstract |> not)

[<SuppressMessage("Gendarme.Rules.Maintainability",
"AvoidUnnecessarySpecializationRule",
Justification = "Avoid spurious generality")>]
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<Node>) =
zeroPoints ()

Expand All @@ -772,7 +785,8 @@ module internal Visitor =
|> Seq.collect (fun x ->
x.Module.GetAllTypes()
|> Seq.cast<TypeDefinition>
|> Seq.filter stripInterfaces)
|> Seq.filter stripInterfaces
|> Seq.filter stripAnonymous)
|> Seq.collect (
(fun t ->
let types =
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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")>]
[<assembly: SuppressMessage("Gendarme.Rules.Exceptions",
"InstantiateArgumentExceptionCorrectlyRule",
Scope = "member", // MethodDefinition
Target =
"AltCover.Visitor/I/start@1229::Invoke(Microsoft.FSharp.Core.FSharpFunc`2<Mono.Cecil.Cil.Instruction,System.Int32>,Microsoft.FSharp.Collections.FSharpList`1<Mono.Cecil.Cil.Instruction>)",
"AltCover.Visitor/I/start@1241::Invoke(Microsoft.FSharp.Core.FSharpFunc`2<Mono.Cecil.Cil.Instruction,System.Int32>,Microsoft.FSharp.Collections.FSharpList`1<Mono.Cecil.Cil.Instruction>)",
Justification = "Inlined library code")>]
[<assembly: SuppressMessage("Gendarme.Rules.Exceptions",
"InstantiateArgumentExceptionCorrectlyRule",
Scope = "member", // MethodDefinition
Target =
"AltCover.Visitor/I/finish@1232::Invoke(Microsoft.FSharp.Core.FSharpFunc`2<Mono.Cecil.Cil.Instruction,System.Int32>,Microsoft.FSharp.Collections.FSharpList`1<Mono.Cecil.Cil.Instruction>)",
"AltCover.Visitor/I/finish@1244::Invoke(Microsoft.FSharp.Core.FSharpFunc`2<Mono.Cecil.Cil.Instruction,System.Int32>,Microsoft.FSharp.Collections.FSharpList`1<Mono.Cecil.Cil.Instruction>)",
Justification = "Inlined library code")>]
()
10 changes: 8 additions & 2 deletions AltCover.Tests/Tests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -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()"
Expand Down Expand Up @@ -5703,6 +5706,7 @@ module AltCoverTests =
[ "Sample21.IModern"
"Sample21.Modern1"
"Sample21.Modern2"
"Sample21.Product"
"Sample21.Tests"
"Sample21.Traditional" ]

Expand All @@ -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()"
Expand Down
16 changes: 16 additions & 0 deletions Samples/Sample21/UnitTest1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down

0 comments on commit 5fd8645

Please sign in to comment.