Skip to content

Commit

Permalink
Add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
josepho0918 committed May 30, 2024
1 parent 8fbf4c9 commit aaa786c
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 5 deletions.
6 changes: 6 additions & 0 deletions TrimTail.sln
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ VisualStudioVersion = 17.10.34916.146
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TrimTail", "TrimTail\TrimTail.csproj", "{39D166FE-7C39-4B23-88F5-452204C811D4}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TrimTailTests", "TrimTailTests\TrimTailTests.csproj", "{561B2D63-F0D4-40BA-8061-A25DA82D1307}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -15,6 +17,10 @@ Global
{39D166FE-7C39-4B23-88F5-452204C811D4}.Debug|Any CPU.Build.0 = Debug|Any CPU
{39D166FE-7C39-4B23-88F5-452204C811D4}.Release|Any CPU.ActiveCfg = Release|Any CPU
{39D166FE-7C39-4B23-88F5-452204C811D4}.Release|Any CPU.Build.0 = Release|Any CPU
{561B2D63-F0D4-40BA-8061-A25DA82D1307}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{561B2D63-F0D4-40BA-8061-A25DA82D1307}.Debug|Any CPU.Build.0 = Debug|Any CPU
{561B2D63-F0D4-40BA-8061-A25DA82D1307}.Release|Any CPU.ActiveCfg = Release|Any CPU
{561B2D63-F0D4-40BA-8061-A25DA82D1307}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
18 changes: 13 additions & 5 deletions TrimTail/Program.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
namespace TrimTail
{
internal class Program
public class Program
{
private static readonly SemaphoreSlim sem = new(1);

private static bool HasTrailingBlanks(in string file_path)
public static bool HasTrailingBlanks(in string file_path)
{
using var file = File.OpenText(file_path);
string? line;
Expand All @@ -19,7 +19,7 @@ private static bool HasTrailingBlanks(in string file_path)
return false;
}

private static void RemoveTrailingBlanks(in string file_path)
public static void RemoveTrailingBlanks(in string file_path)
{
if (!HasTrailingBlanks(file_path)) return;

Expand Down Expand Up @@ -47,10 +47,18 @@ private static void RemoveTrailingBlanks(in string file_path)
}
}
}
File.Replace(temp_path, file_path, null);
try
{
File.Replace(temp_path, file_path, null);
}
catch (IOException)
{
File.Copy(temp_path, file_path, true);
File.Delete(temp_path);
}
}

private static void ProcessDir(string dir_path, HashSet<string> allowed_exts)
public static void ProcessDir(string dir_path, HashSet<string> allowed_exts)
{
Console.WriteLine($"Processing directory: {dir_path}");

Expand Down
58 changes: 58 additions & 0 deletions TrimTailTests/TrimTailTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
using TrimTail;

namespace TrimTailTests
{
[TestClass]
public class TrimTailTests
{
[TestMethod]
public void TestHasTrailingBlanks()
{
const string filePath = "test.txt";

File.WriteAllText(filePath, "Test w/ trailing spaces ");
Assert.IsTrue(Program.HasTrailingBlanks(filePath));
File.Delete(filePath);

File.WriteAllText(filePath, "Test w/ trailing tabs\t\t");
Assert.IsTrue(Program.HasTrailingBlanks(filePath));
File.Delete(filePath);

File.WriteAllText(filePath, "Test w/o trailing blanks");
Assert.IsFalse(Program.HasTrailingBlanks(filePath));
File.Delete(filePath);
}

[TestMethod]
public void TestRemoveTrailingBlanks()
{
const string filePath = "test.txt";
string content;

File.WriteAllText(filePath, "Test w/ trailing blanks ");
Program.RemoveTrailingBlanks(filePath);
content = File.ReadAllText(filePath);
Assert.AreEqual("Test w/ trailing blanks", content);
File.Delete(filePath);

File.WriteAllText(filePath, "Test w/ trailing blanks and newline \r\n");
Program.RemoveTrailingBlanks(filePath);
content = File.ReadAllText(filePath);
Assert.AreEqual("Test w/ trailing blanks and newline\r\n", content);
File.Delete(filePath);
}

[TestMethod]
public void TestProcessDir()
{
const string dirPath = "testDir";
Directory.CreateDirectory(dirPath);
File.WriteAllText(Path.Combine(dirPath, "test1.txt"), "This is a test with trailing blanks ");
File.WriteAllText(Path.Combine(dirPath, "test2.txt"), "Another test with trailing blanks ");
Program.ProcessDir(dirPath, [".txt"]);
var files = Directory.EnumerateFiles(dirPath, "*", SearchOption.AllDirectories);
Assert.IsTrue(files.All(filePath => !File.ReadAllText(filePath).EndsWith(' ')));
Directory.Delete(dirPath, true);
}
}
}
27 changes: 27 additions & 0 deletions TrimTailTests/TrimTailTests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="coverlet.collector" Version="6.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="MSTest.TestAdapter" Version="3.1.1" />
<PackageReference Include="MSTest.TestFramework" Version="3.1.1" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\TrimTail\TrimTail.csproj" />
</ItemGroup>

<ItemGroup>
<Using Include="Microsoft.VisualStudio.TestTools.UnitTesting" />
</ItemGroup>

</Project>

0 comments on commit aaa786c

Please sign in to comment.