Skip to content

Commit d40fbc8

Browse files
committed
refactor: improve test structure by rearranging Arrange, Act, and Assert sections in various test cases
1 parent b7740ac commit d40fbc8

11 files changed

+794
-621
lines changed

CodeLineCounter.Tests/CodeAnalyzerTests.cs

Lines changed: 43 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -31,70 +31,80 @@ public void TestAnalyzeSolution()
3131
[Fact]
3232
public void AnalyzeSourceCode_Should_Set_CurrentNamespace()
3333
{
34-
using StringWriter consoleOutput = new();
35-
Console.SetOut(consoleOutput);
36-
37-
// Arrange
38-
var projectNamespaceMetrics = new Dictionary<string, int>();
39-
var lines = new string[]
34+
using (StringWriter consoleOutput = new())
4035
{
36+
Console.SetOut(consoleOutput);
37+
38+
// Arrange
39+
var projectNamespaceMetrics = new Dictionary<string, int>();
40+
var lines = new string[]
41+
{
4142
"namespace MyNamespace",
4243
"{",
4344
" // Code goes here",
4445
"}"
45-
};
46+
};
4647

47-
// Act
48-
CodeMetricsAnalyzer.AnalyzeSourceCode(projectNamespaceMetrics, lines, out string? currentNamespace, out _, out _);
48+
// Act
49+
CodeMetricsAnalyzer.AnalyzeSourceCode(projectNamespaceMetrics, lines, out string? currentNamespace, out _, out _);
50+
51+
// Assert
52+
Assert.Equal("MyNamespace", currentNamespace);
53+
54+
}
4955

50-
// Assert
51-
Assert.Equal("MyNamespace", currentNamespace);
5256
}
5357

5458
[Fact]
5559
public void AnalyzeSourceCode_Should_Set_FileLineCount()
5660
{
57-
using StringWriter consoleOutput = new();
58-
Console.SetOut(consoleOutput);
59-
60-
// Arrange
61-
var projectNamespaceMetrics = new Dictionary<string, int>();
62-
var lines = new string[]
61+
using (StringWriter consoleOutput = new())
6362
{
63+
Console.SetOut(consoleOutput);
64+
65+
// Arrange
66+
var projectNamespaceMetrics = new Dictionary<string, int>();
67+
var lines = new string[]
68+
{
6469
"namespace MyNamespace",
6570
"{",
6671
" // Code goes here",
6772
"}"
68-
};
73+
};
6974

70-
// Act
71-
CodeMetricsAnalyzer.AnalyzeSourceCode(projectNamespaceMetrics, lines, out _, out int fileLineCount, out _);
75+
// Act
76+
CodeMetricsAnalyzer.AnalyzeSourceCode(projectNamespaceMetrics, lines, out _, out int fileLineCount, out _);
77+
78+
// Assert - 3 lines only because comment lines are ignored
79+
Assert.Equal(3, fileLineCount);
80+
}
7281

73-
// Assert - 3 lines only because comment lines are ignored
74-
Assert.Equal(3, fileLineCount);
7582
}
7683

7784
[Fact]
7885
public void AnalyzeSourceCode_Should_Set_FileCyclomaticComplexity()
7986
{
80-
using StringWriter consoleOutput = new();
81-
Console.SetOut(consoleOutput);
82-
83-
// Arrange
84-
var projectNamespaceMetrics = new Dictionary<string, int>();
85-
var lines = new string[]
87+
using (StringWriter consoleOutput = new())
8688
{
89+
Console.SetOut(consoleOutput);
90+
91+
// Arrange
92+
var projectNamespaceMetrics = new Dictionary<string, int>();
93+
var lines = new string[]
94+
{
8795
"namespace MyNamespace",
8896
"{",
8997
" // Code goes here",
9098
"}"
91-
};
99+
};
92100

93-
// Act
94-
CodeMetricsAnalyzer.AnalyzeSourceCode(projectNamespaceMetrics, lines, out _, out _, out int fileCyclomaticComplexity);
101+
// Act
102+
CodeMetricsAnalyzer.AnalyzeSourceCode(projectNamespaceMetrics, lines, out _, out _, out int fileCyclomaticComplexity);
103+
104+
// Assert
105+
Assert.Equal(1, fileCyclomaticComplexity);
106+
}
95107

96-
// Assert
97-
Assert.Equal(1, fileCyclomaticComplexity);
98108
}
99109

100110
[Fact]

CodeLineCounter.Tests/CodeDuplicationCheckerTests.cs

Lines changed: 40 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace CodeLineCounter.Tests
44
{
5-
public class CodeDuplicationCheckerTests : IDisposable
5+
public class CodeDuplicationCheckerTests : IDisposable
66
{
77
private readonly string _testDirectory;
88
private bool _disposed;
@@ -12,13 +12,13 @@ public CodeDuplicationCheckerTests()
1212
_testDirectory = Path.Combine(Path.GetTempPath(), "CodeDuplicationCheckerTests");
1313
Directory.CreateDirectory(_testDirectory);
1414
}
15-
15+
1616
[Fact]
1717
public void DetectCodeDuplicationInFiles_ShouldDetectDuplicates()
1818
{
1919
using StringWriter consoleOutput = new();
2020
Console.SetOut(consoleOutput);
21-
21+
2222
// Arrange
2323
var file1 = Path.Combine(_testDirectory, "TestFile1.cs");
2424
var file2 = Path.Combine(_testDirectory, "TestFile2.cs");
@@ -70,13 +70,14 @@ public void AnotherTestMethod()
7070
[Fact]
7171
public void DetectCodeDuplicationInSourceCode_ShouldDetectDuplicates()
7272
{
73-
using StringWriter consoleOutput = new();
74-
Console.SetOut(consoleOutput);
73+
using (StringWriter consoleOutput = new())
74+
{
75+
Console.SetOut(consoleOutput);
7576

76-
// Arrange
77-
var checker = new CodeDuplicationChecker();
77+
// Arrange
78+
var checker = new CodeDuplicationChecker();
7879

79-
var sourceCode1 = @"
80+
var sourceCode1 = @"
8081
public class TestClass
8182
{
8283
public void TestMethod()
@@ -88,7 +89,7 @@ public void TestMethod()
8889
}
8990
}";
9091

91-
var sourceCode2 = @"
92+
var sourceCode2 = @"
9293
public class AnotherTestClass
9394
{
9495
public void AnotherTestMethod()
@@ -100,30 +101,33 @@ public void AnotherTestMethod()
100101
}
101102
}";
102103

103-
var file1 = Path.Combine(_testDirectory, "TestFile3.cs");
104-
var file2 = Path.Combine(_testDirectory, "TestFile4.cs");
104+
var file1 = Path.Combine(_testDirectory, "TestFile3.cs");
105+
var file2 = Path.Combine(_testDirectory, "TestFile4.cs");
105106

106-
// Act
107-
checker.DetectCodeDuplicationInSourceCode(file1, sourceCode1);
108-
checker.DetectCodeDuplicationInSourceCode(file2, sourceCode2);
109-
var result = checker.GetCodeDuplicationMap();
107+
// Act
108+
checker.DetectCodeDuplicationInSourceCode(file1, sourceCode1);
109+
checker.DetectCodeDuplicationInSourceCode(file2, sourceCode2);
110+
var result = checker.GetCodeDuplicationMap();
111+
112+
// Assert
113+
Assert.NotEmpty(result);
114+
var duplicateEntry = result.First();
115+
Assert.Equal(2, duplicateEntry.Value.Count); // Both methods should be detected as duplicates
116+
}
110117

111-
// Assert
112-
Assert.NotEmpty(result);
113-
var duplicateEntry = result.First();
114-
Assert.Equal(2, duplicateEntry.Value.Count); // Both methods should be detected as duplicates
115118
}
116119

117120
[Fact]
118121
public void DetectCodeDuplicationInSourceCode_ShouldNotDetectDuplicatesForDifferentCode()
119122
{
120-
using StringWriter consoleOutput = new();
121-
Console.SetOut(consoleOutput);
123+
using (StringWriter consoleOutput = new())
124+
{
125+
Console.SetOut(consoleOutput);
122126

123-
// Arrange
124-
var checker = new CodeDuplicationChecker();
127+
// Arrange
128+
var checker = new CodeDuplicationChecker();
125129

126-
var sourceCode1 = @"
130+
var sourceCode1 = @"
127131
public class TestClass
128132
{
129133
public void TestMethod()
@@ -135,7 +139,7 @@ public void TestMethod()
135139
}
136140
}";
137141

138-
var sourceCode2 = @"
142+
var sourceCode2 = @"
139143
public class AnotherTestClass
140144
{
141145
public void AnotherTestMethod()
@@ -144,19 +148,21 @@ public void AnotherTestMethod()
144148
}
145149
}";
146150

147-
var file1 = Path.Combine(_testDirectory, "TestFile5.cs");
148-
var file2 = Path.Combine(_testDirectory, "TestFile6.cs");
151+
var file1 = Path.Combine(_testDirectory, "TestFile5.cs");
152+
var file2 = Path.Combine(_testDirectory, "TestFile6.cs");
149153

150-
// Act
151-
checker.DetectCodeDuplicationInSourceCode(file1, sourceCode1);
152-
checker.DetectCodeDuplicationInSourceCode(file2, sourceCode2);
153-
var result = checker.GetCodeDuplicationMap();
154+
// Act
155+
checker.DetectCodeDuplicationInSourceCode(file1, sourceCode1);
156+
checker.DetectCodeDuplicationInSourceCode(file2, sourceCode2);
157+
var result = checker.GetCodeDuplicationMap();
158+
159+
// Assert
160+
Assert.Empty(result); // No duplicates should be detected
161+
}
154162

155-
// Assert
156-
Assert.Empty(result); // No duplicates should be detected
157163
}
158164

159-
protected virtual void Dispose(bool disposing)
165+
protected virtual void Dispose(bool disposing)
160166
{
161167
if (!_disposed)
162168
{

0 commit comments

Comments
 (0)