22
33namespace 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