Skip to content

Commit 94662ee

Browse files
authored
Merge pull request #28 from magic5644/refactor-code-introduce-exceptions
Summary of PR #28: Refactor error handling and simplify graph processing Title: Refactor error handling and simplify graph processing Description: Introduces custom exception classes for better error handling and user feedback. Simplifies node and edge extraction processes. Enhances degree calculation and error messaging in DependencyGraphGenerator and CoreUtils. File Changes: CodeLineCounter.Tests/CodeAnalyzerTests.cs Refactored tests to remove redundant console output redirection. Simplified test setup and assertions. CodeLineCounter.Tests/CodeDuplicationCheckerTests.cs Refactored tests to inherit from TestBase. Removed redundant console output redirection. Simplified test setup and assertions. CodeLineCounter.Tests/CoreUtilsTests.cs Refactored tests to inherit from TestBase. Removed redundant console output redirection. Simplified test setup and assertions. CodeLineCounter.Tests/CsvHandlerTests.cs Refactored tests to inherit from TestBase. Removed redundant console output redirection. Simplified test setup and assertions. CodeLineCounter.Tests/CyclomaticComplexityCalculatorTests.cs Refactored tests to inherit from TestBase. Removed redundant console output redirection. Simplified test setup and assertions. CodeLineCounter.Tests/DataExporterTests.cs Refactored tests to inherit from TestBase. Removed redundant console output redirection. Simplified test setup and assertions. CodeLineCounter.Tests/DependencyGraphGeneratorTests.cs Refactored tests to inherit from TestBase. Removed redundant console output redirection. Simplified test setup and assertions. CodeLineCounter.Tests/FileUtilsTests.cs Refactored tests to inherit from TestBase. Removed redundant console output redirection. Simplified test setup and assertions. CodeLineCounter.Tests/HashUtilsTests.cs Removed redundant console output redirection. Simplified test setup and assertions. CodeLineCounter.Tests/JsonHandlerTests.cs Refactored tests to inherit from TestBase. Removed redundant console output redirection. Simplified test setup and assertions. CodeLineCounter.Tests/SolutionAnalyzerTests.cs Refactored tests to inherit from TestBase. Removed redundant console output redirection. Simplified test setup and assertions. CodeLineCounter.Tests/TestBase.cs Added a new base class for tests to handle console redirection and cleanup. CodeLineCounter/Exceptions/HelpRequestedException.cs Added a new custom exception for help requests. CodeLineCounter/Exceptions/InvalidExportFormatException.cs Added a new custom exception for invalid export formats. CodeLineCounter/Exceptions/InvalidNumberException.cs Added a new custom exception for invalid number inputs. CodeLineCounter/Exceptions/InvalidSelectionException.cs Added a new custom exception for invalid selection inputs. CodeLineCounter/Models/Settings.cs Updated IsValid method to throw custom exceptions. CodeLineCounter/Program.cs Added try-catch blocks for better error handling. CodeLineCounter/Services/DependencyGraphGenerator.cs Minor formatting changes. CodeLineCounter/Services/SolutionAnalyzer.cs Updated error handling to use Console.WriteLine instead of Console.Error.WriteLine. CodeLineCounter/Utils/CoreUtils.cs Updated methods to throw custom exceptions for invalid inputs.
2 parents 158dbe8 + 543c303 commit 94662ee

21 files changed

+951
-1397
lines changed

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)