Skip to content

Commit cf1522e

Browse files
authored
Merge pull request #27 from magic5644/feat-add-output-path
Feat add output path
2 parents 34b1657 + 1e2562f commit cf1522e

20 files changed

+1559
-709
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
/codeql*
2121
/*.dot
2222
/*snyk*
23+
/exports
2324

2425
# Mac OS
2526
.DS_Store

.vscode/launch.json

+10
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,16 @@
2020
"cwd": "${workspaceFolder}",
2121
"stopAtEntry": false,
2222
"console": "internalConsole"
23+
},
24+
{
25+
"name": "C#: CodeLineCounter - withOutputPath",
26+
"type": "coreclr",
27+
"request": "launch",
28+
"program": "${workspaceFolder}/CodeLineCounter/bin/Debug/net9.0/CodeLineCounter.dll",
29+
"args": ["-d", "${workspaceFolder}", "-output", "${workspaceFolder}\\exports" ],
30+
"cwd": "${workspaceFolder}",
31+
"stopAtEntry": false,
32+
"console": "internalConsole"
2333
}
2434
]
2535
}

CodeLineCounter.Tests/CodeAnalyzerTests.cs

+46-24
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,61 +31,80 @@ public void TestAnalyzeSolution()
2831
[Fact]
2932
public void AnalyzeSourceCode_Should_Set_CurrentNamespace()
3033
{
31-
// Arrange
32-
var projectNamespaceMetrics = new Dictionary<string, int>();
33-
var lines = new string[]
34+
using (StringWriter consoleOutput = new())
3435
{
36+
Console.SetOut(consoleOutput);
37+
38+
// Arrange
39+
var projectNamespaceMetrics = new Dictionary<string, int>();
40+
var lines = new string[]
41+
{
3542
"namespace MyNamespace",
3643
"{",
3744
" // Code goes here",
3845
"}"
39-
};
46+
};
4047

41-
// Act
42-
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+
}
4355

44-
// Assert
45-
Assert.Equal("MyNamespace", currentNamespace);
4656
}
4757

4858
[Fact]
4959
public void AnalyzeSourceCode_Should_Set_FileLineCount()
5060
{
51-
// Arrange
52-
var projectNamespaceMetrics = new Dictionary<string, int>();
53-
var lines = new string[]
61+
using (StringWriter consoleOutput = new())
5462
{
63+
Console.SetOut(consoleOutput);
64+
65+
// Arrange
66+
var projectNamespaceMetrics = new Dictionary<string, int>();
67+
var lines = new string[]
68+
{
5569
"namespace MyNamespace",
5670
"{",
5771
" // Code goes here",
5872
"}"
59-
};
73+
};
6074

61-
// Act
62-
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+
}
6381

64-
// Assert - 3 lines only because comment lines are ignored
65-
Assert.Equal(3, fileLineCount);
6682
}
6783

6884
[Fact]
6985
public void AnalyzeSourceCode_Should_Set_FileCyclomaticComplexity()
7086
{
71-
// Arrange
72-
var projectNamespaceMetrics = new Dictionary<string, int>();
73-
var lines = new string[]
87+
using (StringWriter consoleOutput = new())
7488
{
89+
Console.SetOut(consoleOutput);
90+
91+
// Arrange
92+
var projectNamespaceMetrics = new Dictionary<string, int>();
93+
var lines = new string[]
94+
{
7595
"namespace MyNamespace",
7696
"{",
7797
" // Code goes here",
7898
"}"
79-
};
99+
};
80100

81-
// Act
82-
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+
}
83107

84-
// Assert
85-
Assert.Equal(1, fileCyclomaticComplexity);
86108
}
87109

88110
[Fact]

CodeLineCounter.Tests/CodeDuplicationCheckerTests.cs

+80-29
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,26 @@
22

33
namespace CodeLineCounter.Tests
44
{
5-
public class CodeDuplicationCheckerTests
5+
public class CodeDuplicationCheckerTests : IDisposable
66
{
7+
private readonly string _testDirectory;
8+
private bool _disposed;
9+
10+
public CodeDuplicationCheckerTests()
11+
{
12+
_testDirectory = Path.Combine(Path.GetTempPath(), "CodeDuplicationCheckerTests");
13+
Directory.CreateDirectory(_testDirectory);
14+
}
15+
716
[Fact]
817
public void DetectCodeDuplicationInFiles_ShouldDetectDuplicates()
918
{
19+
using StringWriter consoleOutput = new();
20+
Console.SetOut(consoleOutput);
21+
1022
// Arrange
11-
var file1 = "TestFile1.cs";
12-
var file2 = "TestFile2.cs";
23+
var file1 = Path.Combine(_testDirectory, "TestFile1.cs");
24+
var file2 = Path.Combine(_testDirectory, "TestFile2.cs");
1325

1426
var code1 = @"
1527
public class TestClass
@@ -58,10 +70,14 @@ public void AnotherTestMethod()
5870
[Fact]
5971
public void DetectCodeDuplicationInSourceCode_ShouldDetectDuplicates()
6072
{
61-
// Arrange
62-
var checker = new CodeDuplicationChecker();
73+
using (StringWriter consoleOutput = new())
74+
{
75+
Console.SetOut(consoleOutput);
6376

64-
var sourceCode1 = @"
77+
// Arrange
78+
var checker = new CodeDuplicationChecker();
79+
80+
var sourceCode1 = @"
6581
public class TestClass
6682
{
6783
public void TestMethod()
@@ -73,7 +89,7 @@ public void TestMethod()
7389
}
7490
}";
7591

76-
var sourceCode2 = @"
92+
var sourceCode2 = @"
7793
public class AnotherTestClass
7894
{
7995
public void AnotherTestMethod()
@@ -85,27 +101,33 @@ public void AnotherTestMethod()
85101
}
86102
}";
87103

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

91-
// Act
92-
checker.DetectCodeDuplicationInSourceCode(file1, sourceCode1);
93-
checker.DetectCodeDuplicationInSourceCode(file2, sourceCode2);
94-
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+
}
95117

96-
// Assert
97-
Assert.NotEmpty(result);
98-
var duplicateEntry = result.First();
99-
Assert.Equal(2, duplicateEntry.Value.Count); // Both methods should be detected as duplicates
100118
}
101119

102120
[Fact]
103121
public void DetectCodeDuplicationInSourceCode_ShouldNotDetectDuplicatesForDifferentCode()
104122
{
105-
// Arrange
106-
var checker = new CodeDuplicationChecker();
123+
using (StringWriter consoleOutput = new())
124+
{
125+
Console.SetOut(consoleOutput);
126+
127+
// Arrange
128+
var checker = new CodeDuplicationChecker();
107129

108-
var sourceCode1 = @"
130+
var sourceCode1 = @"
109131
public class TestClass
110132
{
111133
public void TestMethod()
@@ -117,7 +139,7 @@ public void TestMethod()
117139
}
118140
}";
119141

120-
var sourceCode2 = @"
142+
var sourceCode2 = @"
121143
public class AnotherTestClass
122144
{
123145
public void AnotherTestMethod()
@@ -126,16 +148,45 @@ public void AnotherTestMethod()
126148
}
127149
}";
128150

129-
var file1 = "TestFile5.cs";
130-
var file2 = "TestFile6.cs";
151+
var file1 = Path.Combine(_testDirectory, "TestFile5.cs");
152+
var file2 = Path.Combine(_testDirectory, "TestFile6.cs");
131153

132-
// Act
133-
checker.DetectCodeDuplicationInSourceCode(file1, sourceCode1);
134-
checker.DetectCodeDuplicationInSourceCode(file2, sourceCode2);
135-
var result = checker.GetCodeDuplicationMap();
154+
// Act
155+
checker.DetectCodeDuplicationInSourceCode(file1, sourceCode1);
156+
checker.DetectCodeDuplicationInSourceCode(file2, sourceCode2);
157+
var result = checker.GetCodeDuplicationMap();
136158

137-
// Assert
138-
Assert.Empty(result); // No duplicates should be detected
159+
// Assert
160+
Assert.Empty(result); // No duplicates should be detected
161+
}
162+
163+
}
164+
165+
protected virtual void Dispose(bool disposing)
166+
{
167+
if (!_disposed)
168+
{
169+
if (disposing && Directory.Exists(_testDirectory))
170+
{
171+
// Dispose managed resources
172+
Directory.Delete(_testDirectory, true);
173+
}
174+
175+
// Dispose unmanaged resources (if any)
176+
177+
_disposed = true;
178+
}
179+
}
180+
181+
public void Dispose()
182+
{
183+
Dispose(true);
184+
GC.SuppressFinalize(this);
185+
}
186+
187+
~CodeDuplicationCheckerTests()
188+
{
189+
Dispose(false);
139190
}
140191
}
141192
}

0 commit comments

Comments
 (0)