Skip to content

Commit 526e114

Browse files
committed
Adding xUnit Test
1 parent 5a5d409 commit 526e114

8 files changed

Lines changed: 156 additions & 861 deletions

File tree

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net9.0</TargetFramework>
5+
<ImplicitUsings>disable</ImplicitUsings>
6+
<Nullable>enable</Nullable>
7+
<IsPackable>false</IsPackable>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<PackageReference Include="coverlet.collector" Version="6.0.2" />
12+
<PackageReference Include="FluentAssertions" Version="8.6.0" />
13+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.12.0" />
14+
<PackageReference Include="xunit" Version="2.9.3" />
15+
<PackageReference Include="xunit.runner.visualstudio" Version="3.1.4">
16+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
17+
<PrivateAssets>all</PrivateAssets>
18+
</PackageReference>
19+
</ItemGroup>
20+
21+
<ItemGroup>
22+
<ProjectReference Include="..\Chizl.FileCompare.csproj" />
23+
</ItemGroup>
24+
25+
<ItemGroup>
26+
<Using Include="Xunit" />
27+
</ItemGroup>
28+
29+
<ItemGroup>
30+
<Folder Include="TestFiles\" />
31+
</ItemGroup>
32+
33+
</Project>
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
using System;
2+
using System.IO;
3+
using System.Linq;
4+
using FluentAssertions;
5+
6+
namespace Chizl.FileCompare.Tests
7+
{
8+
public class DiffToolTests : IDisposable
9+
{
10+
private readonly string _tempDir;
11+
12+
public DiffToolTests()
13+
{
14+
_tempDir = Path.Combine(Path.GetTempPath(), "ChizlDiffTests");
15+
Directory.CreateDirectory(_tempDir);
16+
}
17+
18+
public void Dispose()
19+
{
20+
if (Directory.Exists(_tempDir))
21+
Directory.Delete(_tempDir, true);
22+
}
23+
24+
private string CreateTempFile(string name, byte[] bytes)
25+
{
26+
var path = Path.Combine(_tempDir, name);
27+
File.WriteAllBytes(path, bytes);
28+
return path;
29+
}
30+
31+
private string CreateTempFile(string name, string[] lines)
32+
{
33+
var path = Path.Combine(_tempDir, name);
34+
File.WriteAllLines(path, lines);
35+
return path;
36+
}
37+
38+
[Fact]
39+
public void CompareFiles_IdenticalAscii_ReturnsNoChanges()
40+
{
41+
var file1 = CreateTempFile("file1.txt", new[] { "line1", "line2" });
42+
var file2 = CreateTempFile("file2.txt", new[] { "line1", "line2" });
43+
44+
var result = DiffTool.CompareFiles(file1, file2);
45+
46+
result.HasException.Should().BeFalse();
47+
result.IsBinary.Should().BeFalse();
48+
result.Diffs.Added.Should().Be(0);
49+
result.Diffs.Deleted.Should().Be(0);
50+
result.Diffs.Modified.Should().Be(0);
51+
}
52+
53+
[Fact]
54+
public void CompareFiles_ModifiedAscii_ReturnsModify()
55+
{
56+
var file1 = CreateTempFile("file1.txt", new[] { "using System;", "using IO;" });
57+
var file2 = CreateTempFile("file2.txt", new[] { "using System;", "using IO2;" });
58+
59+
var result = DiffTool.CompareFiles(file1, file2);
60+
61+
result.LineComparison.Any(l => l.DiffType == DiffType.Modified).Should().BeTrue();
62+
}
63+
64+
[Fact]
65+
public void CompareFiles_InsertsAndDeletes_ReturnsCorrectCounts()
66+
{
67+
var file1 = CreateTempFile("file1.txt", new[] { "line1", "line2", "line3" });
68+
var file2 = CreateTempFile("file2.txt", new[] { "line1", "line2a", "line4" });
69+
70+
var result = DiffTool.CompareFiles(file1, file2);
71+
72+
result.Diffs.Added.Should().Be(1); // line4
73+
result.Diffs.Deleted.Should().Be(1); // line2 -> line2a
74+
result.Diffs.Modified.Should().Be(1); // merged line2 -> line2a
75+
}
76+
77+
[Fact]
78+
public void CompareFiles_BinaryFiles_ReturnsIsBinaryTrue()
79+
{
80+
var file1 = CreateTempFile("file1.bin", new byte[] { 0, 1, 2, 3 });
81+
var file2 = CreateTempFile("file2.bin", new byte[] { 0, 1, 2, 4 });
82+
83+
var result = DiffTool.CompareFiles(file1, file2);
84+
85+
result.IsBinary.Should().BeTrue();
86+
}
87+
88+
[Fact]
89+
public void CompareFiles_MissingFile_ThrowsException()
90+
{
91+
var file1 = Path.Combine(_tempDir, "doesnotexist.txt");
92+
var file2 = CreateTempFile("file2.txt", new[] { "line1" });
93+
94+
var result = DiffTool.CompareFiles(file1, file2);
95+
96+
result.HasException.Should().BeTrue();
97+
result.Exception.Should().BeOfType<ArgumentException>();
98+
}
99+
}
100+
}

Chizl.FileCompare.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,13 @@
2020
</PropertyGroup>
2121

2222
<ItemGroup>
23+
<Compile Remove="Chizl.FileCompare.Tests\**" />
2324
<Compile Remove="Chizl.FileComparer\**" />
2425
<Compile Remove="src\**" />
26+
<EmbeddedResource Remove="Chizl.FileCompare.Tests\**" />
2527
<EmbeddedResource Remove="Chizl.FileComparer\**" />
2628
<EmbeddedResource Remove="src\**" />
29+
<None Remove="Chizl.FileCompare.Tests\**" />
2730
<None Remove="Chizl.FileComparer\**" />
2831
<None Remove="src\**" />
2932
</ItemGroup>

Chizl.FileCompare.sln

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Chizl.FileComparer", "Chizl
99
EndProject
1010
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConsoleDemo", "src\ConsoleDemo\ConsoleDemo.csproj", "{A02E297F-BD1B-439A-AC3F-FFD97C7EA345}"
1111
EndProject
12+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Chizl.FileCompare.Tests", "Chizl.FileCompare.Tests\Chizl.FileCompare.Tests.csproj", "{6F62B7AD-8BB6-6305-F7E0-A682B437A428}"
13+
EndProject
1214
Global
1315
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1416
Debug|Any CPU = Debug|Any CPU
@@ -27,6 +29,10 @@ Global
2729
{A02E297F-BD1B-439A-AC3F-FFD97C7EA345}.Debug|Any CPU.Build.0 = Debug|Any CPU
2830
{A02E297F-BD1B-439A-AC3F-FFD97C7EA345}.Release|Any CPU.ActiveCfg = Release|Any CPU
2931
{A02E297F-BD1B-439A-AC3F-FFD97C7EA345}.Release|Any CPU.Build.0 = Release|Any CPU
32+
{6F62B7AD-8BB6-6305-F7E0-A682B437A428}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
33+
{6F62B7AD-8BB6-6305-F7E0-A682B437A428}.Debug|Any CPU.Build.0 = Debug|Any CPU
34+
{6F62B7AD-8BB6-6305-F7E0-A682B437A428}.Release|Any CPU.ActiveCfg = Release|Any CPU
35+
{6F62B7AD-8BB6-6305-F7E0-A682B437A428}.Release|Any CPU.Build.0 = Release|Any CPU
3036
EndGlobalSection
3137
GlobalSection(SolutionProperties) = preSolution
3238
HideSolutionNode = FALSE

0 commit comments

Comments
 (0)