Skip to content

Commit 3d1eaa6

Browse files
Mohammad DehghanMohammad Dehghan
authored andcommitted
Test ResetHits()
1 parent 5cbeaf6 commit 3d1eaa6

File tree

2 files changed

+54
-9
lines changed

2 files changed

+54
-9
lines changed

SG.CodeCoverage.Tests.NetFx/InstrumenterTester.cs

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -126,17 +126,28 @@ public void RunIsPrimeInApp(int number)
126126
WaitForAppIdle();
127127
}
128128

129-
private void CheckAppStarted()
129+
public CoverageResult GetCoverageResult()
130130
{
131-
if (_process == null)
132-
throw new InvalidOperationException("Sample application is not started.");
131+
CheckInstrumented();
132+
return RecordingController.CollectResultAndReset();
133133
}
134134

135-
public CoverageResult GetCoverageResult()
135+
public void ResetHits()
136+
{
137+
CheckInstrumented();
138+
RecordingController.ResetHits();
139+
}
140+
141+
private void CheckInstrumented()
136142
{
137143
if (InstrumentedAssemblyPath == null || RecordingController == null)
138144
throw new InvalidOperationException("Sample assembly is not instrumented.");
139-
return RecordingController.CollectResultAndReset();
145+
}
146+
147+
private void CheckAppStarted()
148+
{
149+
if (_process == null)
150+
throw new InvalidOperationException("Sample application is not started.");
140151
}
141152
}
142153
}

SG.CodeCoverage.Tests.NetFx/TestInstrumentation.cs

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,37 @@ public void Coverage_IsPrime7()
6868
.Concat(Methods.SampleStruct));
6969
}
7070

71+
[TestMethod]
72+
public void Coverage_ResetHits_IsPrime2()
73+
{
74+
_tester.RunApp();
75+
_tester.ResetHits();
76+
_tester.RunIsPrimeInApp(2);
77+
AssertVisitedFilesAndMethods(
78+
new string[] { Files.App }
79+
.Append(Files.PrimeCalculator),
80+
Methods.RunCommand
81+
.Concat(Methods.PrimeCalculator.IsPrimeAndIsLessThan2)
82+
.Append(Methods.get_Commands));
83+
}
84+
85+
[TestMethod]
86+
public void Coverage_ResetHits_IsPrime7()
87+
{
88+
_tester.RunApp();
89+
_tester.ResetHits();
90+
_tester.RunIsPrimeInApp(7);
91+
AssertVisitedFilesAndMethods(
92+
new string[] { Files.App }
93+
.Append(Files.PrimeCalculator)
94+
.Append(Files.SampleStruct),
95+
Methods.RunCommand
96+
.Concat(Methods.PrimeCalculator.IsPrimeAndIsLessThan2)
97+
.Concat(Methods.PrimeCalculator.GetUpperBound)
98+
.Concat(Methods.SampleStruct)
99+
.Append(Methods.get_Commands));
100+
}
101+
71102

72103
[TestCleanup]
73104
public void ExitApp()
@@ -90,12 +121,12 @@ private void AssertVisitedFilesAndMethods(
90121
private static void ShouldVisit(IReadOnlyList<string> expectedNames, IReadOnlyList<string> actualNames, string what)
91122
{
92123
Assert.AreEqual(expectedNames.Count, actualNames.Count, $"visited {what}s");
93-
foreach(var expected in expectedNames)
124+
foreach (var expected in expectedNames)
94125
{
95126
bool found = false;
96-
foreach(var actual in actualNames)
127+
foreach (var actual in actualNames)
97128
{
98-
if(actual.EndsWith(expected))
129+
if (actual.EndsWith(expected))
99130
{
100131
found = true;
101132
break;
@@ -123,16 +154,19 @@ private static class Files
123154

124155
private static class Methods
125156
{
157+
public static string get_Commands = "App::get_Commands()";
158+
126159
public static readonly IEnumerable<string> Startup = new string[]
127160
{
128161
"Program::Main(System.String[])",
129162
"App::.ctor()",
130-
"App::get_Commands()",
163+
get_Commands,
131164
"App::Run(System.String[])",
132165
"Command::.ctor(System.String,System.String,System.Action`1<System.String>)",
133166
"Command::get_Help()",
134167
};
135168

169+
136170
public static readonly IEnumerable<string> RunCommand = new string[]
137171
{
138172
"App::RunCommand(System.String[])",

0 commit comments

Comments
 (0)