Skip to content

Commit d86df14

Browse files
committed
refactor: introduce custom exception classes for improved error handling and user feedback
1 parent 158dbe8 commit d86df14

21 files changed

Lines changed: 1040 additions & 1397 deletions

CodeLineCounter.Tests/CodeAnalyzerTests.cs

Lines changed: 44 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -4,123 +4,92 @@
44

55
namespace CodeLineCounter.Tests
66
{
7-
public class CodeAnalyzerTests
7+
public class CodeAnalyzerTests : TestBase
88
{
9-
private readonly TextWriter _originalConsoleOut;
10-
11-
public CodeAnalyzerTests()
12-
{
13-
_originalConsoleOut = Console.Out;
14-
}
15-
169
[Fact]
1710
public void TestAnalyzeSolution()
1811
{
19-
using (StringWriter consoleOutput = new())
20-
{
21-
Console.SetOut(consoleOutput);
22-
23-
string basePath = FileUtils.GetBasePath();
24-
var solutionPath = Path.GetFullPath(Path.Combine(basePath, "..", "..", "..", "..", "CodeLineCounter.sln"));
25-
26-
// Act
27-
var (metrics, projectTotals, totalLines, totalFiles, duplicationMap, dependencies) = CodeMetricsAnalyzer.AnalyzeSolution(solutionPath);
28-
29-
// Assert
30-
Assert.NotNull(metrics);
31-
Assert.NotEmpty(metrics);
32-
Assert.NotEmpty(projectTotals);
33-
Assert.NotEqual(0, totalLines);
34-
Assert.NotEqual(0, totalFiles);
35-
Assert.NotNull(duplicationMap);
36-
Assert.NotNull(dependencies);
37-
}
38-
// Reset console output
39-
Console.SetOut(_originalConsoleOut);
12+
13+
string basePath = FileUtils.GetBasePath();
14+
var solutionPath = Path.GetFullPath(Path.Combine(basePath, "..", "..", "..", "..", "CodeLineCounter.sln"));
15+
16+
// Act
17+
var (metrics, projectTotals, totalLines, totalFiles, duplicationMap, dependencies) = CodeMetricsAnalyzer.AnalyzeSolution(solutionPath);
18+
19+
// Assert
20+
Assert.NotNull(metrics);
21+
Assert.NotEmpty(metrics);
22+
Assert.NotEmpty(projectTotals);
23+
Assert.NotEqual(0, totalLines);
24+
Assert.NotEqual(0, totalFiles);
25+
Assert.NotNull(duplicationMap);
26+
Assert.NotNull(dependencies);
27+
4028
}
4129

4230
[Fact]
4331
public void AnalyzeSourceCode_Should_Set_CurrentNamespace()
4432
{
45-
using (StringWriter consoleOutput = new())
46-
{
47-
Console.SetOut(consoleOutput);
4833

49-
// Arrange
50-
var projectNamespaceMetrics = new Dictionary<string, int>();
51-
var lines = new string[]
52-
{
34+
// Arrange
35+
var projectNamespaceMetrics = new Dictionary<string, int>();
36+
var lines = new string[]
37+
{
5338
"namespace MyNamespace",
5439
"{",
5540
" // Code goes here",
5641
"}"
57-
};
58-
59-
// Act
60-
CodeMetricsAnalyzer.AnalyzeSourceCode(projectNamespaceMetrics, lines, out string? currentNamespace, out _, out _);
42+
};
6143

62-
// Assert
63-
Assert.Equal("MyNamespace", currentNamespace);
44+
// Act
45+
CodeMetricsAnalyzer.AnalyzeSourceCode(projectNamespaceMetrics, lines, out string? currentNamespace, out _, out _);
6446

65-
}
66-
// Reset console output
67-
Console.SetOut(_originalConsoleOut);
47+
// Assert
48+
Assert.Equal("MyNamespace", currentNamespace);
6849

6950
}
7051

7152
[Fact]
7253
public void AnalyzeSourceCode_Should_Set_FileLineCount()
7354
{
74-
using (StringWriter consoleOutput = new())
75-
{
76-
Console.SetOut(consoleOutput);
7755

78-
// Arrange
79-
var projectNamespaceMetrics = new Dictionary<string, int>();
80-
var lines = new string[]
81-
{
56+
// Arrange
57+
var projectNamespaceMetrics = new Dictionary<string, int>();
58+
var lines = new string[]
59+
{
8260
"namespace MyNamespace",
8361
"{",
8462
" // Code goes here",
8563
"}"
86-
};
64+
};
8765

88-
// Act
89-
CodeMetricsAnalyzer.AnalyzeSourceCode(projectNamespaceMetrics, lines, out _, out int fileLineCount, out _);
66+
// Act
67+
CodeMetricsAnalyzer.AnalyzeSourceCode(projectNamespaceMetrics, lines, out _, out int fileLineCount, out _);
9068

91-
// Assert - 3 lines only because comment lines are ignored
92-
Assert.Equal(3, fileLineCount);
93-
}
94-
// Reset console output
95-
Console.SetOut(_originalConsoleOut);
69+
// Assert - 3 lines only because comment lines are ignored
70+
Assert.Equal(3, fileLineCount);
9671

9772
}
9873

9974
[Fact]
10075
public void AnalyzeSourceCode_Should_Set_FileCyclomaticComplexity()
10176
{
102-
using (StringWriter consoleOutput = new())
103-
{
104-
Console.SetOut(consoleOutput);
10577

106-
// Arrange
107-
var projectNamespaceMetrics = new Dictionary<string, int>();
108-
var lines = new string[]
109-
{
78+
// Arrange
79+
var projectNamespaceMetrics = new Dictionary<string, int>();
80+
var lines = new string[]
81+
{
11082
"namespace MyNamespace",
11183
"{",
11284
" // Code goes here",
11385
"}"
114-
};
86+
};
11587

116-
// Act
117-
CodeMetricsAnalyzer.AnalyzeSourceCode(projectNamespaceMetrics, lines, out _, out _, out int fileCyclomaticComplexity);
88+
// Act
89+
CodeMetricsAnalyzer.AnalyzeSourceCode(projectNamespaceMetrics, lines, out _, out _, out int fileCyclomaticComplexity);
11890

119-
// Assert
120-
Assert.Equal(1, fileCyclomaticComplexity);
121-
}
122-
// Reset console output
123-
Console.SetOut(_originalConsoleOut);
91+
// Assert
92+
Assert.Equal(1, fileCyclomaticComplexity);
12493

12594
}
12695

0 commit comments

Comments
 (0)