Skip to content

Commit c20526a

Browse files
committed
Add tests for coverage merger
1 parent 43444bc commit c20526a

File tree

5 files changed

+362
-2
lines changed

5 files changed

+362
-2
lines changed
Lines changed: 187 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
1+
using Microsoft.VisualStudio.TestTools.UnitTesting;
2+
using SG.CodeCoverage.Coverage;
3+
using SG.CodeCoverage.Tests.NetFx;
4+
using System;
5+
using System.Collections.Generic;
6+
using System.Collections.ObjectModel;
7+
using System.Linq;
8+
using System.Text;
9+
using System.Threading.Tasks;
10+
11+
namespace SG.CodeCoverage.Coverage.Tests
12+
{
13+
[TestClass]
14+
public class CoverageResultTests
15+
{
16+
[TestMethod]
17+
[Description("Merge should return first assemblies if second one has nothing in it")]
18+
public void MergeCoverageResultsScenario1()
19+
{
20+
// Arrange
21+
var ver = new Common.VersionInfo(1, 0, 0);
22+
var guid = Guid.NewGuid();
23+
24+
var cov1 = CoverageMock
25+
.WithAssembly("ASM_A")
26+
.WithType("Type_A")
27+
.WithMethod("Meth_A", 5)
28+
.WithMethod("Meth_B", 10)
29+
.WithType("Type_B")
30+
.WithMethod("Meth_C", 20)
31+
.WithAssembly("ASM_B")
32+
.WithType("Type_B")
33+
.WithMethod("Meth_A", 100)
34+
.Mock();
35+
36+
var cov2 = CoverageMock.Mock();
37+
38+
var asms2 = new List<CoverageAssemblyResult>().AsReadOnly();
39+
40+
// Act
41+
var merged = cov1.MergeWith(cov2);
42+
43+
// Assert
44+
45+
Assert.IsTrue(merged.Assemblies.Count == cov1.Assemblies.Count);
46+
Assert.IsTrue(merged.Assemblies.Sum(x => x.Types.Count) == cov1.Assemblies.Sum(x => x.Types.Count));
47+
}
48+
49+
[TestMethod]
50+
[Description("Testing a rich scenario")]
51+
public void MergeCoverageResultsScenario2()
52+
{
53+
// Arrange
54+
var cov1 = CoverageMock
55+
.WithAssembly("ASM_A")
56+
.WithType("Type_A")
57+
.WithMethod("Meth_A", 5)
58+
.WithMethod("Meth_B", 10)
59+
.WithType("Type_B")
60+
.WithMethod("Meth_A", 10)
61+
.WithMethod("Meth_C", 5)
62+
.WithAssembly("ASM_B")
63+
.WithType("Type_B")
64+
.WithMethod("Meth_D", 0)
65+
.Mock();
66+
67+
var cov2 = CoverageMock
68+
.WithAssembly("ASM_A")
69+
.WithType("Type_A")
70+
.WithMethod("Meth_A", 0)
71+
.WithType("Type_B")
72+
.WithMethod("Meth_A", 5)
73+
.WithAssembly("ASM_C")
74+
.WithType("Type_D")
75+
.WithMethod("Meth_R", 0)
76+
.Mock();
77+
78+
// Act
79+
var merged = cov1.MergeWith(cov2);
80+
81+
// Assert
82+
Assert.IsTrue(merged.FindAssembly("ASM_C") is CoverageAssemblyResult);
83+
Assert.IsTrue(merged.FindType("ASM_C", "Type_D") is CoverageTypeResult);
84+
Assert.IsTrue(merged.FindMethod("ASM_C", "Type_D", "Meth_R") is CoverageMethodResult);
85+
86+
Assert.IsTrue(merged.FindMethod("ASM_A", "Type_A", "Meth_A").VisitCount == 5);
87+
Assert.IsFalse(merged.FindMethod("ASM_B", "Type_B", "Meth_D").IsVisited);
88+
Assert.IsTrue(merged.FindMethod("ASM_A", "Type_B", "Meth_A").VisitCount == 15);
89+
}
90+
91+
[TestMethod]
92+
[Description("Merge should sum up visit counts")]
93+
public void MergeCoverageResultsScenario3()
94+
{
95+
// Arrange
96+
var cov1 = CoverageMock
97+
.WithAssembly("ASM_A")
98+
.WithType("Type_A")
99+
.WithMethod("Meth_A", 5)
100+
.Mock();
101+
102+
var cov2 = CoverageMock
103+
.WithAssembly("ASM_A")
104+
.WithType("Type_A")
105+
.WithMethod("Meth_A", 10)
106+
.Mock();
107+
108+
// Act
109+
var merged = cov1.MergeWith(cov2);
110+
111+
// Assert
112+
Assert.IsTrue(merged.FindMethod("ASM_A", "Type_A", "Meth_A").VisitCount == 15);
113+
}
114+
115+
[TestMethod]
116+
[Description("Merge should add assemblies from both sides")]
117+
public void MergeCoverageResultsScenario4()
118+
{
119+
// Arrange
120+
var cov1 = CoverageMock
121+
.WithAssembly("ASM_A")
122+
.WithType("Type_A")
123+
.WithMethod("Meth_A", 5)
124+
.Mock();
125+
126+
var cov2 = CoverageMock
127+
.WithAssembly("ASM_B")
128+
.WithType("Type_B")
129+
.WithMethod("Meth_B", 10)
130+
.Mock();
131+
132+
// Act
133+
var merged = cov1.MergeWith(cov2);
134+
135+
// Assert
136+
Assert.IsTrue(merged.Assemblies.Count == 2);
137+
}
138+
139+
[TestMethod]
140+
[Description("Merge should add types from both sides")]
141+
public void MergeCoverageResultsScenario5()
142+
{
143+
// Arrange
144+
var cov1 = CoverageMock
145+
.WithAssembly("ASM_A")
146+
.WithType("Type_A")
147+
.WithMethod("Meth_A", 5)
148+
.Mock();
149+
150+
var cov2 = CoverageMock
151+
.WithAssembly("ASM_A")
152+
.WithType("Type_B")
153+
.WithMethod("Meth_B", 10)
154+
.Mock();
155+
156+
// Act
157+
var merged = cov1.MergeWith(cov2);
158+
159+
// Assert
160+
Assert.IsTrue(merged.FindAssembly("ASM_A").Types.Count == 2);
161+
}
162+
163+
[TestMethod]
164+
[Description("Merge should add methods from both sides")]
165+
public void MergeCoverageResultsScenario6()
166+
{
167+
// Arrange
168+
var cov1 = CoverageMock
169+
.WithAssembly("ASM_A")
170+
.WithType("Type_A")
171+
.WithMethod("Meth_A", 5)
172+
.Mock();
173+
174+
var cov2 = CoverageMock
175+
.WithAssembly("ASM_A")
176+
.WithType("Type_A")
177+
.WithMethod("Meth_B", 10)
178+
.Mock();
179+
180+
// Act
181+
var merged = cov1.MergeWith(cov2);
182+
183+
// Assert
184+
Assert.IsTrue(merged.FindType("ASM_A", "Type_A").Methods.Count == 2);
185+
}
186+
}
187+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using SG.CodeCoverage.Coverage;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
8+
namespace SG.CodeCoverage.Tests.NetFx
9+
{
10+
public static class CoverageHelper
11+
{
12+
public static CoverageAssemblyResult FindAssembly(this CoverageResult cov, string asm)
13+
{
14+
return cov.Assemblies?.FirstOrDefault(x => x.Name == asm);
15+
}
16+
17+
public static CoverageTypeResult FindType(this CoverageResult cov, string asm, string type)
18+
{
19+
return cov.FindAssembly(asm)?.Types.FirstOrDefault(x => x.FullName == type);
20+
}
21+
22+
public static CoverageMethodResult FindMethod(this CoverageResult cov, string asm, string type, string method)
23+
{
24+
return cov.FindType(asm, type)?.Methods.FirstOrDefault(x => x.FullName == method);
25+
}
26+
}
27+
}
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
using SG.CodeCoverage.Common;
7+
using SG.CodeCoverage.Coverage;
8+
9+
10+
namespace SG.CodeCoverage.Tests.NetFx
11+
{
12+
public static class CoverageMock
13+
{
14+
public static CoverageMockBuilder WithAssembly(string name)
15+
{
16+
var builder = new CoverageMockBuilder();
17+
return builder.WithAssembly(name);
18+
}
19+
20+
public static CoverageResult Mock() =>
21+
new CoverageResult(CoverageMockBuilder.MockVerstion, CoverageMockBuilder.MockGuid, new List<CoverageAssemblyResult>().AsReadOnly());
22+
23+
}
24+
25+
public class CoverageMockBuilder
26+
{
27+
public static VersionInfo MockVerstion = new VersionInfo(1, 0, 0);
28+
public static Guid MockGuid = Guid.Parse("d20321ca-1435-4147-aafd-0a5822ea099e");
29+
30+
private List<AssemblyMock> Assemblies { get; } = new List<AssemblyMock>();
31+
32+
private AssemblyMock _currentAssembly;
33+
public CoverageMockBuilder WithAssembly(string name)
34+
{
35+
if (_currentAssembly != null)
36+
{
37+
Assemblies.Add(new AssemblyMock { Name = _currentAssembly.Name, Types = _currentAssembly.Types });
38+
}
39+
40+
_currentAssembly = new AssemblyMock { Name = name };
41+
return this;
42+
}
43+
44+
public CoverageAssemblyBuilder WithType(string typeName)
45+
{
46+
var type = new TypeMock { Name = typeName };
47+
_currentAssembly.Types.Add(type);
48+
return new CoverageAssemblyBuilder(this, _currentAssembly, type);
49+
}
50+
51+
public CoverageResult Mock()
52+
{
53+
var asms = Assemblies.ToList();
54+
asms.Add(_currentAssembly);
55+
return new CoverageResult(
56+
MockVerstion,
57+
MockGuid,
58+
asms.Select(a => new CoverageAssemblyResult(
59+
name: a.Name,
60+
types: a.Types.Select(t => new CoverageTypeResult
61+
(
62+
63+
fullName: t.Name,
64+
methods: t.Methods.Select(m => new CoverageMethodResult(m.Name, "MOCK.SOURCE", 1, 0, 10, 0, m.VisitCount)).ToList().AsReadOnly()
65+
)).ToList().AsReadOnly()
66+
)).ToList().AsReadOnly());
67+
}
68+
69+
}
70+
71+
public class CoverageAssemblyBuilder
72+
{
73+
private CoverageMockBuilder ParentBuilder { get; }
74+
75+
private AssemblyMock _currentAssembly;
76+
private TypeMock _currentType;
77+
78+
private bool _currentTypeAlreadyAdded = true;
79+
80+
public List<TypeMock> Types { get; } = new List<TypeMock>();
81+
82+
internal CoverageAssemblyBuilder(CoverageMockBuilder parent, AssemblyMock currentAssembly, TypeMock currentType)
83+
{
84+
ParentBuilder = parent;
85+
_currentAssembly = currentAssembly;
86+
_currentType = currentType;
87+
}
88+
89+
90+
public CoverageMockBuilder WithAssembly(string assemblyName)
91+
{
92+
_currentAssembly.Types.AddRange(Types);
93+
if (!_currentTypeAlreadyAdded)
94+
{
95+
_currentAssembly.Types.Add(_currentType);
96+
}
97+
return ParentBuilder.WithAssembly(assemblyName);
98+
}
99+
100+
public CoverageAssemblyBuilder WithType(string typeName)
101+
{
102+
if (!_currentTypeAlreadyAdded)
103+
{
104+
Types.Add(_currentType);
105+
}
106+
_currentTypeAlreadyAdded = false;
107+
108+
_currentType = new TypeMock { Name = typeName };
109+
return this;
110+
}
111+
112+
public CoverageAssemblyBuilder WithMethod(string methodName, int visitCount)
113+
{
114+
_currentType.Methods.Add(new MethodMock { Name = methodName, VisitCount = visitCount });
115+
return this;
116+
}
117+
118+
public CoverageResult Mock()
119+
{
120+
return ParentBuilder.Mock();
121+
}
122+
}
123+
124+
public class AssemblyMock
125+
{
126+
public string Name { get; set; }
127+
128+
public List<TypeMock> Types { get; set; } = new List<TypeMock>();
129+
}
130+
131+
public class TypeMock
132+
{
133+
public string Name { get; set; }
134+
public List<MethodMock> Methods { get; set; } = new List<MethodMock>();
135+
}
136+
137+
public class MethodMock
138+
{
139+
public string Name { get; set; }
140+
public int VisitCount { get; set; }
141+
}
142+
143+
}

SG.CodeCoverage.Tests.NetFx/SG.CodeCoverage.Tests.NetFx.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,9 @@
6161
<Reference Include="System.Core" />
6262
</ItemGroup>
6363
<ItemGroup>
64+
<Compile Include="CoverageHelper.cs" />
65+
<Compile Include="CoverageMock.cs" />
66+
<Compile Include="Coverage\CoverageResultTests.cs" />
6467
<Compile Include="Properties\AssemblyInfo.cs" />
6568
<Compile Include="InstrumenterTester.cs" />
6669
<Compile Include="TestInstrumentation.cs" />

SG.CodeCoverage/Coverage/CoverageResult.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ public CoverageResult MergeWith(CoverageResult otherResult)
156156
} else
157157
{
158158
// Assembly is already present, check for types
159-
foreach(var type in targetAssembly.Types)
159+
foreach(var type in asm.Types)
160160
{
161161
var targetType = targetAssembly.Types.Find(a => a.FullName == type.FullName);
162162
if (targetType == null)
@@ -180,7 +180,7 @@ public CoverageResult MergeWith(CoverageResult otherResult)
180180
else
181181
{
182182
// Type is already present, check for methods
183-
foreach(var meth in targetType.Methods)
183+
foreach(var meth in type.Methods)
184184
{
185185
var targetMethod = targetType.Methods.Find(a => a.FullName == meth.FullName);
186186
if(targetMethod == null)

0 commit comments

Comments
 (0)