Skip to content

Commit 22ddad3

Browse files
committed
test: refactor console output handling in various test cases
1 parent f0f9aa9 commit 22ddad3

9 files changed

Lines changed: 131 additions & 15 deletions

File tree

CodeLineCounter.Tests/CodeAnalyzerTests.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ public class CodeAnalyzerTests
99
[Fact]
1010
public void TestAnalyzeSolution()
1111
{
12+
using StringWriter consoleOutput = new();
13+
Console.SetOut(consoleOutput);
14+
1215
string basePath = FileUtils.GetBasePath();
1316
var solutionPath = Path.GetFullPath(Path.Combine(basePath, "..", "..", "..", "..", "CodeLineCounter.sln"));
1417

@@ -28,6 +31,9 @@ public void TestAnalyzeSolution()
2831
[Fact]
2932
public void AnalyzeSourceCode_Should_Set_CurrentNamespace()
3033
{
34+
using StringWriter consoleOutput = new();
35+
Console.SetOut(consoleOutput);
36+
3137
// Arrange
3238
var projectNamespaceMetrics = new Dictionary<string, int>();
3339
var lines = new string[]
@@ -48,6 +54,9 @@ public void AnalyzeSourceCode_Should_Set_CurrentNamespace()
4854
[Fact]
4955
public void AnalyzeSourceCode_Should_Set_FileLineCount()
5056
{
57+
using StringWriter consoleOutput = new();
58+
Console.SetOut(consoleOutput);
59+
5160
// Arrange
5261
var projectNamespaceMetrics = new Dictionary<string, int>();
5362
var lines = new string[]
@@ -68,6 +77,9 @@ public void AnalyzeSourceCode_Should_Set_FileLineCount()
6877
[Fact]
6978
public void AnalyzeSourceCode_Should_Set_FileCyclomaticComplexity()
7079
{
80+
using StringWriter consoleOutput = new();
81+
Console.SetOut(consoleOutput);
82+
7183
// Arrange
7284
var projectNamespaceMetrics = new Dictionary<string, int>();
7385
var lines = new string[]

CodeLineCounter.Tests/CodeDuplicationCheckerTests.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ public CodeDuplicationCheckerTests()
1616
[Fact]
1717
public void DetectCodeDuplicationInFiles_ShouldDetectDuplicates()
1818
{
19+
using StringWriter consoleOutput = new();
20+
Console.SetOut(consoleOutput);
21+
1922
// Arrange
2023
var file1 = Path.Combine(_testDirectory, "TestFile1.cs");
2124
var file2 = Path.Combine(_testDirectory, "TestFile2.cs");
@@ -67,6 +70,9 @@ public void AnotherTestMethod()
6770
[Fact]
6871
public void DetectCodeDuplicationInSourceCode_ShouldDetectDuplicates()
6972
{
73+
using StringWriter consoleOutput = new();
74+
Console.SetOut(consoleOutput);
75+
7076
// Arrange
7177
var checker = new CodeDuplicationChecker();
7278

@@ -111,6 +117,9 @@ public void AnotherTestMethod()
111117
[Fact]
112118
public void DetectCodeDuplicationInSourceCode_ShouldNotDetectDuplicatesForDifferentCode()
113119
{
120+
using StringWriter consoleOutput = new();
121+
Console.SetOut(consoleOutput);
122+
114123
// Arrange
115124
var checker = new CodeDuplicationChecker();
116125

CodeLineCounter.Tests/CoreUtilsTests.cs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -121,15 +121,22 @@ public void ParseArguments_handles_invalid_format_option()
121121
{
122122
// Arrange
123123
string[] args = new[] { "-format", "INVALID" };
124-
using StringWriter consoleOutput = new();
125-
Console.SetOut(consoleOutput);
124+
Settings result;
125+
string sortieConsole;
126126

127127
// Act
128-
var result = CoreUtils.ParseArguments(args);
128+
using (var sw = new StringWriter())
129+
{
130+
Console.SetOut(sw);
131+
result = CoreUtils.ParseArguments(args);
132+
sortieConsole = sw.ToString();
133+
}
129134

130-
// Assert
135+
// Assert
131136
Assert.Equal(CoreUtils.ExportFormat.CSV, result.Format);
132-
Assert.Contains("Invalid format", consoleOutput.ToString());
137+
Assert.Contains("Invalid format", sortieConsole.ToString());
138+
139+
133140
}
134141

135142
[Fact]
@@ -226,7 +233,7 @@ public void GetUserChoice_handles_invalid_input(string input)
226233
[Fact]
227234
public void DisplaySolutions_Should_Write_Solutions_To_Console()
228235
{
229-
236+
230237
var envNewLine = Environment.NewLine;
231238
// Arrange
232239
List<string> solutionFiles =

CodeLineCounter.Tests/CsvHandlerTests.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ public CsvHandlerTests()
2323
[Fact]
2424
public void Serialize_ValidData_WritesToFile()
2525
{
26+
using StringWriter consoleOutput = new();
27+
Console.SetOut(consoleOutput);
28+
2629
// Arrange
2730
var data = new List<TestRecord>
2831
{
@@ -47,6 +50,9 @@ public void Serialize_ValidData_WritesToFile()
4750
[Fact]
4851
public void Deserialize_ValidFile_ReturnsData()
4952
{
53+
using StringWriter consoleOutput = new();
54+
Console.SetOut(consoleOutput);
55+
5056
// Arrange
5157
string filePath = Path.Combine(_testDirectory,"test_2.csv");
5258
var data = new List<string>
@@ -72,6 +78,9 @@ public void Deserialize_ValidFile_ReturnsData()
7278
[Fact]
7379
public void Serialize_EmptyData_WritesEmptyFile()
7480
{
81+
using StringWriter consoleOutput = new();
82+
Console.SetOut(consoleOutput);
83+
7584
// Arrange
7685
var data = new List<TestRecord>();
7786
string filePath = Path.Combine(_testDirectory,"test_3.csv");
@@ -90,6 +99,9 @@ public void Serialize_EmptyData_WritesEmptyFile()
9099
[Fact]
91100
public void Deserialize_EmptyFile_ReturnsEmptyList()
92101
{
102+
using StringWriter consoleOutput = new();
103+
Console.SetOut(consoleOutput);
104+
93105
// Arrange
94106
string filePath = Path.Combine(_testDirectory,"test_4.csv");
95107
File.WriteAllText(filePath, "Id,Name");

CodeLineCounter.Tests/FileUtilsTests.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ public FileUtilsTests()
1515
[Fact]
1616
public void GetSolutionFiles_Should_Return_List_Of_Solution_Files()
1717
{
18+
using StringWriter consoleOutput = new();
19+
Console.SetOut(consoleOutput);
1820
// Arrange
1921
var basePath = FileUtils.GetBasePath();
2022
var solutionPath = Path.GetFullPath(Path.Combine(basePath, "..", "..", "..", ".."));
@@ -32,6 +34,8 @@ public void GetSolutionFiles_Should_Return_List_Of_Solution_Files()
3234
[Fact]
3335
public void GetBasePath_Should_Return_NonEmptyString()
3436
{
37+
using StringWriter consoleOutput = new();
38+
Console.SetOut(consoleOutput);
3539
// Act
3640
string basePath = FileUtils.GetBasePath();
3741

@@ -43,6 +47,8 @@ public void GetBasePath_Should_Return_NonEmptyString()
4347
[Fact]
4448
public void get_solution_files_throws_exception_for_nonexistent_directory()
4549
{
50+
using StringWriter consoleOutput = new();
51+
Console.SetOut(consoleOutput);
4652
// Arrange
4753
var nonExistentPath = Path.Combine(_testDirectory, Guid.NewGuid().ToString());
4854

@@ -54,6 +60,8 @@ public void get_solution_files_throws_exception_for_nonexistent_directory()
5460
[Fact]
5561
public void get_project_files_throws_when_file_not_exists()
5662
{
63+
using StringWriter consoleOutput = new();
64+
Console.SetOut(consoleOutput);
5765
// Arrange
5866
var nonExistentPath = Path.Combine(_testDirectory, "nonexistent.sln");
5967

CodeLineCounter.Tests/HashUtilsTests.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ public class HashUtilsTests
77
[Fact]
88
public void ComputeHash_EmptyString_ReturnsEmptyString()
99
{
10+
using StringWriter consoleOutput = new();
11+
Console.SetOut(consoleOutput);
12+
1013
// Arrange
1114
string input = "";
1215

@@ -20,6 +23,9 @@ public void ComputeHash_EmptyString_ReturnsEmptyString()
2023
[Fact]
2124
public void ComputeHash_NullString_ReturnsEmptyString()
2225
{
26+
using StringWriter consoleOutput = new();
27+
Console.SetOut(consoleOutput);
28+
2329
// Arrange
2430
string? input = null;
2531

@@ -33,6 +39,9 @@ public void ComputeHash_NullString_ReturnsEmptyString()
3339
[Fact]
3440
public void ComputeHash_ValidString_ReturnsHash()
3541
{
42+
using StringWriter consoleOutput = new();
43+
Console.SetOut(consoleOutput);
44+
3645
// Arrange
3746
string input = "Hello, World!";
3847

@@ -47,6 +56,9 @@ public void ComputeHash_ValidString_ReturnsHash()
4756
[Fact]
4857
public void ComputeHash_DuplicateStrings_ReturnSameHash()
4958
{
59+
using StringWriter consoleOutput = new();
60+
Console.SetOut(consoleOutput);
61+
5062
// Arrange
5163
string input1 = "Hello, World!";
5264
string input2 = "Hello, World!";

CodeLineCounter.Tests/JsonHandlerTests.cs

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,18 @@
33

44
namespace CodeLineCounter.Tests
55
{
6-
public class JsonHandlerTests
6+
public class JsonHandlerTests :IDisposable
77
{
8+
9+
private readonly string _testDirectory;
10+
private bool _disposed;
11+
12+
public JsonHandlerTests()
13+
{
14+
_testDirectory = Path.Combine(Path.GetTempPath(), "JsonHandlerTests");
15+
Directory.CreateDirectory(_testDirectory);
16+
}
17+
818
public class TestClass
919
{
1020
public int Id { get; set; }
@@ -20,8 +30,11 @@ public TestClass(int id, string name)
2030
[Fact]
2131
public void deserialize_valid_json_file_returns_expected_objects()
2232
{
33+
using StringWriter consoleOutput = new();
34+
Console.SetOut(consoleOutput);
35+
2336
// Arrange
24-
var testFilePath = "test.json";
37+
var testFilePath = Path.Combine(_testDirectory, "test.json");
2538
var expectedData = new[] { new TestClass(id: 1, name: "Test") };
2639
File.WriteAllText(testFilePath, JsonSerializer.Serialize(expectedData));
2740

@@ -36,5 +49,32 @@ public void deserialize_valid_json_file_returns_expected_objects()
3649
File.Delete(testFilePath);
3750
}
3851

52+
protected virtual void Dispose(bool disposing)
53+
{
54+
if (!_disposed)
55+
{
56+
if (disposing && Directory.Exists(_testDirectory))
57+
{
58+
// Dispose managed resources
59+
Directory.Delete(_testDirectory, true);
60+
}
61+
62+
// Dispose unmanaged resources (if any)
63+
64+
_disposed = true;
65+
}
66+
}
67+
68+
public void Dispose()
69+
{
70+
Dispose(true);
71+
GC.SuppressFinalize(this);
72+
}
73+
74+
~JsonHandlerTests()
75+
{
76+
Dispose(false);
77+
}
78+
3979
}
4080
}

CodeLineCounter.Tests/SolutionAnalyzerTests.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,12 @@ public SolutionAnalyzerTest()
2020
public void analyze_and_export_solution_succeeds_with_valid_inputs()
2121
{
2222
// Arrange
23+
using var sw = new StringWriter();
24+
Console.SetOut(sw);
2325
var basePath = FileUtils.GetBasePath();
2426
var solutionPath = Path.GetFullPath(Path.Combine(basePath, "..", "..", "..", ".."));
2527
solutionPath = Path.Combine(solutionPath, "CodeLineCounter.sln");
26-
using var sw = new StringWriter();
27-
Console.SetOut(sw);
28+
2829
var verbose = false;
2930
var format = CoreUtils.ExportFormat.JSON;
3031

@@ -39,6 +40,8 @@ public void analyze_and_export_solution_succeeds_with_valid_inputs()
3940
public void analyze_and_export_solution_throws_on_invalid_path()
4041
{
4142
// Arrange
43+
using var sw = new StringWriter();
44+
Console.SetOut(sw);
4245
var invalidPath = "";
4346
var verbose = false;
4447
var format = CoreUtils.ExportFormat.JSON;

CodeLineCounter/Utils/CoreUtils.cs

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ public static void DisplaySolutions(List<string> solutionFiles)
138138
}
139139
}
140140

141-
public static string GetExportFileNameWithExtension(string filePath, CoreUtils.ExportFormat format, string? outputPath = null)
141+
public static string GetExportFileNameWithExtension(string filePath, ExportFormat format, string? outputPath = null)
142142
{
143143
string fileName = Path.GetFileName(filePath);
144144
if (filePath == null)
@@ -147,15 +147,28 @@ public static string GetExportFileNameWithExtension(string filePath, CoreUtils.E
147147
}
148148
string newExtension = format switch
149149
{
150-
CoreUtils.ExportFormat.CSV => ".csv",
151-
CoreUtils.ExportFormat.JSON => ".json",
150+
ExportFormat.CSV => ".csv",
151+
ExportFormat.JSON => ".json",
152152
_ => throw new ArgumentException($"Unsupported format: {format}", nameof(format))
153153
};
154154

155+
string currentExtension = Path.GetExtension(filePath);
156+
155157
// If file already has the desired extension, keep it, otherwise change it
156-
if (!Path.GetExtension(fileName).Equals(newExtension, StringComparison.OrdinalIgnoreCase))
158+
if (!currentExtension.Equals(newExtension, StringComparison.OrdinalIgnoreCase))
159+
{
160+
if (!string.IsNullOrEmpty(currentExtension))
161+
{
162+
fileName = Path.ChangeExtension(fileName, newExtension);
163+
}
164+
else
165+
{
166+
fileName = fileName + newExtension;
167+
}
168+
}
169+
else
157170
{
158-
fileName = Path.ChangeExtension(fileName, newExtension);
171+
fileName = filePath;
159172
}
160173

161174
// If an output directory is specified, combine the path

0 commit comments

Comments
 (0)