Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
/codeql*
/*.dot
/*snyk*
/exports

# Mac OS
.DS_Store
Expand Down
10 changes: 10 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,16 @@
"cwd": "${workspaceFolder}",
"stopAtEntry": false,
"console": "internalConsole"
},
{
"name": "C#: CodeLineCounter - withOutputPath",
"type": "coreclr",
"request": "launch",
"program": "${workspaceFolder}/CodeLineCounter/bin/Debug/net9.0/CodeLineCounter.dll",
"args": ["-d", "${workspaceFolder}", "-output", "${workspaceFolder}\\exports" ],
"cwd": "${workspaceFolder}",
"stopAtEntry": false,
"console": "internalConsole"
}
]
}
70 changes: 46 additions & 24 deletions CodeLineCounter.Tests/CodeAnalyzerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ public class CodeAnalyzerTests
[Fact]
public void TestAnalyzeSolution()
{
using StringWriter consoleOutput = new();
Console.SetOut(consoleOutput);

string basePath = FileUtils.GetBasePath();
var solutionPath = Path.GetFullPath(Path.Combine(basePath, "..", "..", "..", "..", "CodeLineCounter.sln"));

Expand All @@ -28,61 +31,80 @@ public void TestAnalyzeSolution()
[Fact]
public void AnalyzeSourceCode_Should_Set_CurrentNamespace()
{
// Arrange
var projectNamespaceMetrics = new Dictionary<string, int>();
var lines = new string[]
using (StringWriter consoleOutput = new())
{
Console.SetOut(consoleOutput);

// Arrange
var projectNamespaceMetrics = new Dictionary<string, int>();
var lines = new string[]
{
"namespace MyNamespace",
"{",
" // Code goes here",
"}"
};
};

// Act
CodeMetricsAnalyzer.AnalyzeSourceCode(projectNamespaceMetrics, lines, out string? currentNamespace, out _, out _);
// Act
CodeMetricsAnalyzer.AnalyzeSourceCode(projectNamespaceMetrics, lines, out string? currentNamespace, out _, out _);

// Assert
Assert.Equal("MyNamespace", currentNamespace);

}

// Assert
Assert.Equal("MyNamespace", currentNamespace);
}

[Fact]
public void AnalyzeSourceCode_Should_Set_FileLineCount()
{
// Arrange
var projectNamespaceMetrics = new Dictionary<string, int>();
var lines = new string[]
using (StringWriter consoleOutput = new())
{
Console.SetOut(consoleOutput);

// Arrange
var projectNamespaceMetrics = new Dictionary<string, int>();
var lines = new string[]
{
"namespace MyNamespace",
"{",
" // Code goes here",
"}"
};
};

// Act
CodeMetricsAnalyzer.AnalyzeSourceCode(projectNamespaceMetrics, lines, out _, out int fileLineCount, out _);
// Act
CodeMetricsAnalyzer.AnalyzeSourceCode(projectNamespaceMetrics, lines, out _, out int fileLineCount, out _);

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

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

[Fact]
public void AnalyzeSourceCode_Should_Set_FileCyclomaticComplexity()
{
// Arrange
var projectNamespaceMetrics = new Dictionary<string, int>();
var lines = new string[]
using (StringWriter consoleOutput = new())
{
Console.SetOut(consoleOutput);

// Arrange
var projectNamespaceMetrics = new Dictionary<string, int>();
var lines = new string[]
{
"namespace MyNamespace",
"{",
" // Code goes here",
"}"
};
};

// Act
CodeMetricsAnalyzer.AnalyzeSourceCode(projectNamespaceMetrics, lines, out _, out _, out int fileCyclomaticComplexity);
// Act
CodeMetricsAnalyzer.AnalyzeSourceCode(projectNamespaceMetrics, lines, out _, out _, out int fileCyclomaticComplexity);

// Assert
Assert.Equal(1, fileCyclomaticComplexity);
}

// Assert
Assert.Equal(1, fileCyclomaticComplexity);
}

[Fact]
Expand Down
109 changes: 80 additions & 29 deletions CodeLineCounter.Tests/CodeDuplicationCheckerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,26 @@

namespace CodeLineCounter.Tests
{
public class CodeDuplicationCheckerTests
public class CodeDuplicationCheckerTests : IDisposable
{
private readonly string _testDirectory;
private bool _disposed;

public CodeDuplicationCheckerTests()
{
_testDirectory = Path.Combine(Path.GetTempPath(), "CodeDuplicationCheckerTests");
Directory.CreateDirectory(_testDirectory);
}

[Fact]
public void DetectCodeDuplicationInFiles_ShouldDetectDuplicates()
{
using StringWriter consoleOutput = new();
Console.SetOut(consoleOutput);

// Arrange
var file1 = "TestFile1.cs";
var file2 = "TestFile2.cs";
var file1 = Path.Combine(_testDirectory, "TestFile1.cs");
var file2 = Path.Combine(_testDirectory, "TestFile2.cs");

var code1 = @"
public class TestClass
Expand Down Expand Up @@ -58,10 +70,14 @@ public void AnotherTestMethod()
[Fact]
public void DetectCodeDuplicationInSourceCode_ShouldDetectDuplicates()
{
// Arrange
var checker = new CodeDuplicationChecker();
using (StringWriter consoleOutput = new())
{
Console.SetOut(consoleOutput);

var sourceCode1 = @"
// Arrange
var checker = new CodeDuplicationChecker();

var sourceCode1 = @"
public class TestClass
{
public void TestMethod()
Expand All @@ -73,7 +89,7 @@ public void TestMethod()
}
}";

var sourceCode2 = @"
var sourceCode2 = @"
public class AnotherTestClass
{
public void AnotherTestMethod()
Expand All @@ -85,27 +101,33 @@ public void AnotherTestMethod()
}
}";

var file1 = "TestFile3.cs";
var file2 = "TestFile4.cs";
var file1 = Path.Combine(_testDirectory, "TestFile3.cs");
var file2 = Path.Combine(_testDirectory, "TestFile4.cs");

// Act
checker.DetectCodeDuplicationInSourceCode(file1, sourceCode1);
checker.DetectCodeDuplicationInSourceCode(file2, sourceCode2);
var result = checker.GetCodeDuplicationMap();
// Act
checker.DetectCodeDuplicationInSourceCode(file1, sourceCode1);
checker.DetectCodeDuplicationInSourceCode(file2, sourceCode2);
var result = checker.GetCodeDuplicationMap();

// Assert
Assert.NotEmpty(result);
var duplicateEntry = result.First();
Assert.Equal(2, duplicateEntry.Value.Count); // Both methods should be detected as duplicates
}

// Assert
Assert.NotEmpty(result);
var duplicateEntry = result.First();
Assert.Equal(2, duplicateEntry.Value.Count); // Both methods should be detected as duplicates
}

[Fact]
public void DetectCodeDuplicationInSourceCode_ShouldNotDetectDuplicatesForDifferentCode()
{
// Arrange
var checker = new CodeDuplicationChecker();
using (StringWriter consoleOutput = new())
{
Console.SetOut(consoleOutput);

// Arrange
var checker = new CodeDuplicationChecker();

var sourceCode1 = @"
var sourceCode1 = @"
public class TestClass
{
public void TestMethod()
Expand All @@ -117,7 +139,7 @@ public void TestMethod()
}
}";

var sourceCode2 = @"
var sourceCode2 = @"
public class AnotherTestClass
{
public void AnotherTestMethod()
Expand All @@ -126,16 +148,45 @@ public void AnotherTestMethod()
}
}";

var file1 = "TestFile5.cs";
var file2 = "TestFile6.cs";
var file1 = Path.Combine(_testDirectory, "TestFile5.cs");
var file2 = Path.Combine(_testDirectory, "TestFile6.cs");

// Act
checker.DetectCodeDuplicationInSourceCode(file1, sourceCode1);
checker.DetectCodeDuplicationInSourceCode(file2, sourceCode2);
var result = checker.GetCodeDuplicationMap();
// Act
checker.DetectCodeDuplicationInSourceCode(file1, sourceCode1);
checker.DetectCodeDuplicationInSourceCode(file2, sourceCode2);
var result = checker.GetCodeDuplicationMap();

// Assert
Assert.Empty(result); // No duplicates should be detected
// Assert
Assert.Empty(result); // No duplicates should be detected
}

}

protected virtual void Dispose(bool disposing)
{
if (!_disposed)
{
if (disposing && Directory.Exists(_testDirectory))
{
// Dispose managed resources
Directory.Delete(_testDirectory, true);
}

// Dispose unmanaged resources (if any)

_disposed = true;
}
}

public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}

~CodeDuplicationCheckerTests()
{
Dispose(false);
}
}
}
Loading
Loading