2
2
3
3
namespace CodeLineCounter . Tests
4
4
{
5
- public class CodeDuplicationCheckerTests
5
+ public class CodeDuplicationCheckerTests : IDisposable
6
6
{
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
+
7
16
[ Fact ]
8
17
public void DetectCodeDuplicationInFiles_ShouldDetectDuplicates ( )
9
18
{
19
+ using StringWriter consoleOutput = new ( ) ;
20
+ Console . SetOut ( consoleOutput ) ;
21
+
10
22
// 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" ) ;
13
25
14
26
var code1 = @"
15
27
public class TestClass
@@ -58,10 +70,14 @@ public void AnotherTestMethod()
58
70
[ Fact ]
59
71
public void DetectCodeDuplicationInSourceCode_ShouldDetectDuplicates ( )
60
72
{
61
- // Arrange
62
- var checker = new CodeDuplicationChecker ( ) ;
73
+ using ( StringWriter consoleOutput = new ( ) )
74
+ {
75
+ Console . SetOut ( consoleOutput ) ;
63
76
64
- var sourceCode1 = @"
77
+ // Arrange
78
+ var checker = new CodeDuplicationChecker ( ) ;
79
+
80
+ var sourceCode1 = @"
65
81
public class TestClass
66
82
{
67
83
public void TestMethod()
@@ -73,7 +89,7 @@ public void TestMethod()
73
89
}
74
90
}" ;
75
91
76
- var sourceCode2 = @"
92
+ var sourceCode2 = @"
77
93
public class AnotherTestClass
78
94
{
79
95
public void AnotherTestMethod()
@@ -85,27 +101,33 @@ public void AnotherTestMethod()
85
101
}
86
102
}" ;
87
103
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" ) ;
90
106
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
+ }
95
117
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
100
118
}
101
119
102
120
[ Fact ]
103
121
public void DetectCodeDuplicationInSourceCode_ShouldNotDetectDuplicatesForDifferentCode ( )
104
122
{
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 ( ) ;
107
129
108
- var sourceCode1 = @"
130
+ var sourceCode1 = @"
109
131
public class TestClass
110
132
{
111
133
public void TestMethod()
@@ -117,7 +139,7 @@ public void TestMethod()
117
139
}
118
140
}" ;
119
141
120
- var sourceCode2 = @"
142
+ var sourceCode2 = @"
121
143
public class AnotherTestClass
122
144
{
123
145
public void AnotherTestMethod()
@@ -126,16 +148,45 @@ public void AnotherTestMethod()
126
148
}
127
149
}" ;
128
150
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" ) ;
131
153
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 ( ) ;
136
158
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 ) ;
139
190
}
140
191
}
141
192
}
0 commit comments