Skip to content

Commit 0268e0b

Browse files
committed
refactor: enhance test structure and improve output handling in SolutionAnalyzerTests
1 parent d40fbc8 commit 0268e0b

1 file changed

Lines changed: 97 additions & 65 deletions

File tree

CodeLineCounter.Tests/SolutionAnalyzerTests.cs

Lines changed: 97 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -8,49 +8,82 @@ public class SolutionAnalyzerTest : IDisposable
88
{
99

1010
private readonly string _testDirectory;
11+
private readonly string _testSolutionPath;
12+
private readonly string _outputPath;
1113
private bool _disposed;
1214

1315
public SolutionAnalyzerTest()
1416
{
1517
_testDirectory = Path.Combine(Path.GetTempPath(), "SolutionAnalyzerTest");
18+
_testSolutionPath = Path.Combine(_testDirectory, "TestSolution.sln");
19+
_outputPath = _testDirectory;
1620
Directory.CreateDirectory(_testDirectory);
21+
// Create minimal test solution if it doesn't exist
22+
if (!File.Exists(_testSolutionPath))
23+
{
24+
File.WriteAllText(_testSolutionPath, @"
25+
Microsoft Visual Studio Solution File, Format Version 12.00
26+
# Visual Studio Version 17
27+
VisualStudioVersion = 17.0.31903.59
28+
MinimumVisualStudioVersion = 10.0.40219.1
29+
Project(""{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}"") = ""TestProject"", ""TestProject\TestProject.csproj"", ""{F60AAFF8-A3B6-4D3C-9372-06A0F1B6F82B}""
30+
EndProject");
31+
}
1732
}
1833

1934
[Fact]
2035
public void analyze_and_export_solution_succeeds_with_valid_inputs()
2136
{
2237
// Arrange
23-
var basePath = FileUtils.GetBasePath();
24-
var solutionPath = Path.GetFullPath(Path.Combine(basePath, "..", "..", "..", ".."));
25-
solutionPath = Path.Combine(solutionPath, "CodeLineCounter.sln");
38+
var solutionPath = _testSolutionPath;
39+
var verbose = true;
40+
var format = CoreUtils.ExportFormat.CSV;
41+
var outputPath = _outputPath;
2642

27-
var verbose = false;
28-
var format = CoreUtils.ExportFormat.JSON;
43+
try
44+
{
45+
// Redirect console output
46+
using (StringWriter stringWriter = new StringWriter())
47+
{
2948

30-
// Act & Assert
31-
using (var sw = new StringWriter())
49+
Console.SetOut(stringWriter);
50+
51+
// Act
52+
var exception = Record.Exception(() =>
53+
{
54+
SolutionAnalyzer.AnalyzeAndExportSolution(_testSolutionPath, verbose, format, outputPath);
55+
});
56+
57+
// Assert
58+
Assert.Null(exception);
59+
Assert.True(File.Exists(Path.Combine(outputPath, "TestSolution-CodeMetrics.csv")));
60+
}
61+
62+
}
63+
finally
3264
{
33-
Console.SetOut(sw);
34-
var exception = Record.Exception(() =>
35-
SolutionAnalyzer.AnalyzeAndExportSolution(solutionPath, verbose, format));
65+
// Cleanup
3666

37-
Assert.Null(exception);
67+
if (File.Exists(solutionPath))
68+
{
69+
File.Delete(solutionPath);
70+
}
3871
}
3972
}
4073

4174
[Fact]
4275
public void analyze_and_export_solution_throws_on_invalid_path()
4376
{
44-
// Arrange
45-
46-
var invalidPath = "";
47-
var verbose = false;
48-
var format = CoreUtils.ExportFormat.JSON;
49-
50-
// Act & Assert
5177
using (var sw = new StringWriter())
5278
{
5379
Console.SetOut(sw);
80+
// Arrange
81+
82+
var invalidPath = "";
83+
var verbose = false;
84+
var format = CoreUtils.ExportFormat.JSON;
85+
86+
// Act & Assert
5487
var exception = Assert.Throws<UnauthorizedAccessException>(() =>
5588
SolutionAnalyzer.AnalyzeAndExportSolution(invalidPath, verbose, format));
5689

@@ -89,24 +122,23 @@ public void PerformAnalysis_ShouldReturnCorrectAnalysisResult()
89122
[Fact]
90123
public void OutputAnalysisResults_ShouldPrintCorrectOutput()
91124
{
92-
// Arrange
93-
var result = new AnalysisResult
94-
{
95-
Metrics = new List<NamespaceMetrics>(),
96-
ProjectTotals = new Dictionary<string, int>(),
97-
TotalLines = 1000,
98-
TotalFiles = 10,
99-
DuplicationMap = new List<DuplicationCode>(),
100-
DependencyList = new List<DependencyRelation>(),
101-
ProcessingTime = TimeSpan.FromSeconds(10),
102-
SolutionFileName = "CodeLineCounter.sln",
103-
DuplicatedLines = 100
104-
};
105-
var verbose = true;
106-
107125
using (var sw = new StringWriter())
108126
{
109127
Console.SetOut(sw);
128+
// Arrange
129+
var result = new AnalysisResult
130+
{
131+
Metrics = new List<NamespaceMetrics>(),
132+
ProjectTotals = new Dictionary<string, int>(),
133+
TotalLines = 1000,
134+
TotalFiles = 10,
135+
DuplicationMap = new List<DuplicationCode>(),
136+
DependencyList = new List<DependencyRelation>(),
137+
ProcessingTime = TimeSpan.FromSeconds(10),
138+
SolutionFileName = "CodeLineCounter.sln",
139+
DuplicatedLines = 100
140+
};
141+
var verbose = true;
110142

111143
// Act
112144
SolutionAnalyzer.OutputAnalysisResults(result, verbose);
@@ -124,40 +156,39 @@ public void OutputAnalysisResults_ShouldPrintCorrectOutput()
124156
[Fact]
125157
public void OutputDetailedMetrics_ShouldPrintMetricsAndProjectTotals()
126158
{
127-
// Arrange
128-
var metrics = new List<NamespaceMetrics>
129-
{
130-
new NamespaceMetrics
131-
{
132-
ProjectName = "Project1",
133-
ProjectPath = "/path/to/project1",
134-
NamespaceName = "Namespace1",
135-
FileName = "File1.cs",
136-
FilePath = "/path/to/project1/File1.cs",
137-
LineCount = 100,
138-
CyclomaticComplexity = 10
139-
},
140-
new NamespaceMetrics
141-
{
142-
ProjectName = "Project2",
143-
ProjectPath = "/path/to/project2",
144-
NamespaceName = "Namespace2",
145-
FileName = "File2.cs",
146-
FilePath = "/path/to/project2/File2.cs",
147-
LineCount = 200,
148-
CyclomaticComplexity = 20
149-
}
150-
};
151-
152-
var projectTotals = new Dictionary<string, int>
153-
{
154-
{ "Project1", 100 },
155-
{ "Project2", 200 }
156-
};
157-
158159
using (var sw = new StringWriter())
159160
{
160161
Console.SetOut(sw);
162+
// Arrange
163+
var metrics = new List<NamespaceMetrics>
164+
{
165+
new NamespaceMetrics
166+
{
167+
ProjectName = "Project1",
168+
ProjectPath = "/path/to/project1",
169+
NamespaceName = "Namespace1",
170+
FileName = "File1.cs",
171+
FilePath = "/path/to/project1/File1.cs",
172+
LineCount = 100,
173+
CyclomaticComplexity = 10
174+
},
175+
new NamespaceMetrics
176+
{
177+
ProjectName = "Project2",
178+
ProjectPath = "/path/to/project2",
179+
NamespaceName = "Namespace2",
180+
FileName = "File2.cs",
181+
FilePath = "/path/to/project2/File2.cs",
182+
LineCount = 200,
183+
CyclomaticComplexity = 20
184+
}
185+
};
186+
187+
var projectTotals = new Dictionary<string, int>
188+
{
189+
{ "Project1", 100 },
190+
{ "Project2", 200 }
191+
};
161192

162193
// Act
163194
SolutionAnalyzer.OutputDetailedMetrics(metrics, projectTotals);
@@ -177,7 +208,7 @@ public void OutputDetailedMetrics_ShouldPrintMetricsAndProjectTotals()
177208
[Fact]
178209
public void export_results_with_valid_input_exports_all_files()
179210
{
180-
// Act
211+
// Act
181212
using (var sw = new StringWriter())
182213
{
183214
// Arrange
@@ -214,6 +245,7 @@ protected virtual void Dispose(bool disposing)
214245
if (disposing && Directory.Exists(_testDirectory))
215246
{
216247
// Dispose managed resources
248+
File.Delete(_testSolutionPath);
217249
Directory.Delete(_testDirectory, true);
218250
}
219251

0 commit comments

Comments
 (0)