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
7 changes: 4 additions & 3 deletions CodeLineCounter.Tests/CodeAnalyzerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public void TestAnalyzeSolution()
Assert.NotEmpty(projectTotals);
Assert.NotEqual(0, totalLines);
Assert.NotEqual(0, totalFiles);
Assert.NotNull(duplicationMap);
}

[Fact]
Expand All @@ -38,7 +39,7 @@ public void AnalyzeSourceCode_Should_Set_CurrentNamespace()
};

// Act
CodeAnalyzer.AnalyzeSourceCode(projectNamespaceMetrics, file, lines, out string? currentNamespace, out int fileLineCount, out int fileCyclomaticComplexity);
CodeAnalyzer.AnalyzeSourceCode(projectNamespaceMetrics, file, lines, out string? currentNamespace, out _, out _);

// Assert
Assert.Equal("MyNamespace", currentNamespace);
Expand All @@ -60,7 +61,7 @@ public void AnalyzeSourceCode_Should_Set_FileLineCount()
};

// Act
CodeAnalyzer.AnalyzeSourceCode(projectNamespaceMetrics, file, lines, out string? currentNamespace, out int fileLineCount, out int fileCyclomaticComplexity);
CodeAnalyzer.AnalyzeSourceCode(projectNamespaceMetrics, file, lines, out _, out int fileLineCount, out _);

// Assert - 3 lines only because comment lines are ignored
Assert.Equal(3, fileLineCount);
Expand All @@ -82,7 +83,7 @@ public void AnalyzeSourceCode_Should_Set_FileCyclomaticComplexity()
};

// Act
CodeAnalyzer.AnalyzeSourceCode(projectNamespaceMetrics, file, lines, out string? currentNamespace, out int fileLineCount, out int fileCyclomaticComplexity);
CodeAnalyzer.AnalyzeSourceCode(projectNamespaceMetrics, file, lines, out _, out _, out int fileCyclomaticComplexity);

// Assert
Assert.Equal(1, fileCyclomaticComplexity);
Expand Down
81 changes: 43 additions & 38 deletions CodeLineCounter.Tests/CoreUtilsTests.cs
Original file line number Diff line number Diff line change
@@ -1,61 +1,62 @@
using CodeLineCounter.Utils;

namespace CodeLineCounter.Utils.Tests
namespace CodeLineCounter.Tests
{
public class CoreUtilsTests
{
[Fact]
public void ParseArguments_Should_Return_Correct_Values()
{
// Arrange
string[] args = new string[] { "-verbose", "-d", "testDirectory" };
string[] args = ["-verbose", "-d", "testDirectory"];

// Act
var result = CoreUtils.ParseArguments(args);
var (Verbose, DirectoryPath, _) = CoreUtils.ParseArguments(args);

// Assert
Assert.True(result.Verbose);
Assert.Equal("testDirectory", result.DirectoryPath);
Assert.True(Verbose);
Assert.Equal("testDirectory", DirectoryPath);
}

[Fact]
public void ParseArguments_help_Should_Return_Correct_Values()
{
// Arrange
string[] args = new string[] { "-help" };
string[] args = ["-help"];

// Act
var result = CoreUtils.ParseArguments(args);
var (_, _, Help) = CoreUtils.ParseArguments(args);

// Assert
Assert.True(result.Help);
Assert.True(Help);
}

[Fact]
public void ParseArguments_Should_Return_Default_Values_When_No_Arguments_Passed()
{
// Arrange
string[] args = new string[0];
string[] args = [];

// Act
var result = CoreUtils.ParseArguments(args);
var (Verbose, DirectoryPath, _) = CoreUtils.ParseArguments(args);

// Assert
Assert.False(result.Verbose);
Assert.Null(result.DirectoryPath);
Assert.False(Verbose);
Assert.Null(DirectoryPath);
}

[Fact]
public void ParseArguments_Should_Ignore_Invalid_Arguments()
{
// Arrange
string[] args = new string[] { "-invalid", "-d", "testDirectory" };
string[] args = ["-invalid", "-d", "testDirectory"];

// Act
var result = CoreUtils.ParseArguments(args);
var (Verbose, DirectoryPath, _) = CoreUtils.ParseArguments(args);

// Assert
Assert.False(result.Verbose);
Assert.Equal("testDirectory", result.DirectoryPath);
Assert.False(Verbose);
Assert.Equal("testDirectory", DirectoryPath);
}

[Fact]
Expand Down Expand Up @@ -96,52 +97,50 @@ public void DisplaySolutions_Should_Write_Solutions_To_Console()

var envNewLine = Environment.NewLine;
// Arrange
List<string> solutionFiles = new List<string>
{
List<string> solutionFiles =
[
"Solution1.sln",
"Solution2.sln",
"Solution3.sln"
};
];

// Redirect console output to a StringWriter
using (StringWriter sw = new StringWriter())
using StringWriter sw = new();
Console.SetOut(sw);

// Act
CoreUtils.DisplaySolutions(solutionFiles);

// Assert
string expectedOutput = $"Available solutions:{envNewLine}";
for (int i = 0; i < solutionFiles.Count; i++)
{
Console.SetOut(sw);

// Act
CoreUtils.DisplaySolutions(solutionFiles);

// Assert
string expectedOutput = $"Available solutions:{envNewLine}";
for (int i = 0; i < solutionFiles.Count; i++)
{
expectedOutput += $"{i + 1}. {solutionFiles[i]}{envNewLine}";
}
Assert.Equal(expectedOutput, sw.ToString());
expectedOutput += $"{i + 1}. {solutionFiles[i]}{envNewLine}";
}
Assert.Equal(expectedOutput, sw.ToString());
}

[Fact]
public void GetFilenamesList_Should_Return_List_Of_Filenames()
{
// Arrange
List<string> solutionFiles = new List<string>
{
List<string> solutionFiles =
[
"Solution1.sln",
"Solution2.sln",
"Solution3.sln"
};
];

// Act
List<string> result = CoreUtils.GetFilenamesList(solutionFiles);

// Assert
List<string> expectedFilenames = new List<string>
{
List<string> expectedFilenames =
[
"Solution1.sln",
"Solution2.sln",
"Solution3.sln"
};
];
Assert.Equal(expectedFilenames, result);
}

Expand All @@ -151,25 +150,31 @@ public void CheckSettings_WhenHelpIsTrue_ReturnsFalse()
{
// Arrange
(bool Verbose, string? DirectoryPath, bool Help) settings = (true, null, true);
using var sw = new StringWriter();
Console.SetOut(sw);

// Act
var result = CoreUtils.CheckSettings(settings);

// Assert
Assert.False(result);
Assert.Contains("Usage:", sw.ToString());
}

[Fact]
public void CheckSettings_WhenDirectoryPathIsNull_ReturnsFalse()
{
// Arrange
(bool Verbose, string? DirectoryPath, bool Help) settings = (Verbose: false, DirectoryPath: null, Help: false);
using var sw = new StringWriter();
Console.SetOut(sw);

// Act
var result = CoreUtils.CheckSettings(settings);

// Assert
Assert.False(result);
Assert.Contains("Please provide the directory path", sw.ToString());
}

[Fact]
Expand Down
22 changes: 11 additions & 11 deletions CodeLineCounter.Tests/CsvExporterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ public void ExportCodeDuplicationsToCsv_ShouldCallSerializeWithCorrectParameters
var filePath = "test4.csv";
var duplications = new List<DuplicationCode>
{
new DuplicationCode { CodeHash = "hash1", FilePath = "file1.cs", MethodName = "method1", StartLine = 10, NbLines = 20 },
new DuplicationCode { CodeHash = "hash2", FilePath = "file2.cs", MethodName = "method2", StartLine = 8, NbLines = 10 }
new() { CodeHash = "hash1", FilePath = "file1.cs", MethodName = "method1", StartLine = 10, NbLines = 20 },
new() { CodeHash = "hash2", FilePath = "file2.cs", MethodName = "method2", StartLine = 8, NbLines = 10 }
};


Expand All @@ -91,16 +91,16 @@ public void ExportCodeDuplicationsToCsv_ShouldCallSerializeWithCorrectParameters

}

private List<NamespaceMetrics> GetSampleNamespaceMetrics()
private static List<NamespaceMetrics> GetSampleNamespaceMetrics()
{
return new List<NamespaceMetrics>
{
return
[
new NamespaceMetrics { ProjectName="Project1", ProjectPath = ".", NamespaceName = "Namespace1", FileName = "File1", FilePath = ".", LineCount = 100, CyclomaticComplexity = 0, CodeDuplications = 0 },
new NamespaceMetrics { ProjectName="Project2", ProjectPath = ".", NamespaceName = "Namespace2", FileName = "File2", FilePath = ".", LineCount = 200, CyclomaticComplexity = 5, CodeDuplications = 2 }
};
];
}

private Dictionary<string, int> GetSampleProjectTotals()
private static Dictionary<string, int> GetSampleProjectTotals()
{
return new Dictionary<string, int>
{
Expand All @@ -109,13 +109,13 @@ private Dictionary<string, int> GetSampleProjectTotals()
};
}

private List<DuplicationCode> GetSampleDuplicationCodes()
private static List<DuplicationCode> GetSampleDuplicationCodes()
{
return new List<DuplicationCode>
{
return
[
new DuplicationCode { CodeHash = "Code1", FilePath = ".", MethodName = "Method1", StartLine = 10 , NbLines = 20 },
new DuplicationCode { CodeHash = "Code2", FilePath = ".", MethodName = "Method2", StartLine = 15 , NbLines = 25 }
};
];
}
}
}
7 changes: 4 additions & 3 deletions CodeLineCounter.Tests/CsvHandlerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
using CsvHelper;
using System.Linq;
using Xunit;
using CodeLineCounter.Utils;

namespace CodeLineCounter.Utils.Tests
namespace CodeLineCounter.Tests
{
public class CsvHandlerTests
{
Expand All @@ -21,8 +22,8 @@ public void Serialize_ValidData_WritesToFile()
// Arrange
var data = new List<TestRecord>
{
new TestRecord { Id = 1, Name = "Alice" },
new TestRecord { Id = 2, Name = "Bob" }
new() { Id = 1, Name = "Alice" },
new() { Id = 2, Name = "Bob" }
};
string filePath = "test_1.csv";

Expand Down
3 changes: 2 additions & 1 deletion CodeLineCounter.Tests/FileUtilsTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using CodeLineCounter.Utils;

namespace CodeLineCounter.Utils.Tests
namespace CodeLineCounter.Tests
{
public class FileUtilsTests
{
Expand Down
3 changes: 2 additions & 1 deletion CodeLineCounter.Tests/HashUtilsTest.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using CodeLineCounter.Utils;

namespace CodeLineCounter.Utils.Tests
namespace CodeLineCounter.Tests
{
public class HashUtilsTests
{
Expand Down
2 changes: 1 addition & 1 deletion CodeLineCounter/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ private static void AnalyzeAndExportSolution(string solutionPath, bool verbose)
Console.WriteLine($"Percentage of duplicated code: {percentageDuplication:F2} %");

Parallel.Invoke(
() => CsvExporter.ExportToCsv(csvFilePath, metrics.ToList(), projectTotals, totalLines, duplicationMap, solutionPath),
() => CsvExporter.ExportToCsv(csvFilePath, metrics, projectTotals, totalLines, duplicationMap, solutionPath),
() => CsvExporter.ExportCodeDuplicationsToCsv(duplicationCsvFilePath, duplicationMap)
);

Expand Down
2 changes: 1 addition & 1 deletion CodeLineCounter/Services/CodeAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ private static void AnalyzeProject(string solutionDirectory, string projectFile,
string projectName = Path.GetFileNameWithoutExtension(projectFile);
string relativeProjectPath = solutionDirectory != null ? Path.GetRelativePath(solutionDirectory, projectFile) : projectFile;

var files = projectDirectory != null ? FileUtils.GetAllCsFiles(projectDirectory) : new List<string>();
var files = projectDirectory != null ? FileUtils.GetAllCsFiles(projectDirectory) : [];

int projectLineCount = 0;
var projectNamespaceMetrics = new Dictionary<string, int>();
Expand Down
4 changes: 2 additions & 2 deletions CodeLineCounter/Services/CodeDuplicationChecker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class CodeDuplicationChecker
{
private readonly ConcurrentDictionary<string, HashSet<DuplicationCode>> duplicationMap;
private readonly ConcurrentDictionary<string, HashSet<DuplicationCode>> hashMap;
private readonly object duplicationLock = new object();
private readonly object duplicationLock = new();

public CodeDuplicationChecker()
{
Expand Down Expand Up @@ -63,7 +63,7 @@ public void DetectCodeDuplicationInSourceCode(string normalizedPath, string sour
NbLines = nbLines
};

hashMap.AddOrUpdate(hash, new HashSet<DuplicationCode> { duplicationCode },
hashMap.AddOrUpdate(hash, [duplicationCode],
(key, set) =>
{
lock (set)
Expand Down
18 changes: 7 additions & 11 deletions CodeLineCounter/Utils/CSVHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,20 @@

namespace CodeLineCounter.Utils
{
public class CsvHandler
public static class CsvHandler
{
public static void Serialize<T>(IEnumerable<T> data, string filePath)
{
using (var writer = new StreamWriter(filePath))
using (var csv = new CsvWriter(writer, CultureInfo.InvariantCulture))
{
csv.WriteRecords(data);
}
using var writer = new StreamWriter(filePath);
using var csv = new CsvWriter(writer, CultureInfo.InvariantCulture);
csv.WriteRecords(data);
}

public static IEnumerable<T> Deserialize<T>(string filePath)
{
using (var reader = new StreamReader(filePath))
using (var csv = new CsvReader(reader, CultureInfo.InvariantCulture))
{
return csv.GetRecords<T>().ToList();
}
using var reader = new StreamReader(filePath);
using var csv = new CsvReader(reader, CultureInfo.InvariantCulture);
return csv.GetRecords<T>().ToList();
}
}

Expand Down
2 changes: 1 addition & 1 deletion CodeLineCounter/Utils/CoreUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public static int GetUserChoice(int solutionCount)

public static List<string> GetFilenamesList(List<string> solutionFiles)
{
List<string> listOfFilenames = new List<string>();
List<string> listOfFilenames = [];
for (int i = 0; i < solutionFiles.Count; i++)
{
if (File.Exists(solutionFiles[i]))
Expand Down
Loading