Add unit tests

This commit is contained in:
Laura Hausmann 2023-01-23 17:51:10 +01:00
parent 39617a58a8
commit 028b838bee
Signed by: zotan
GPG key ID: D044E84C5BE01605
4 changed files with 95 additions and 0 deletions

View file

@ -0,0 +1,59 @@
namespace tgcli.Tests;
public class MessagePaging {
[Theory]
[InlineData(0)]
[InlineData(76)]
[InlineData(77)]
[InlineData(147)]
[InlineData(148)]
[InlineData(218)]
[InlineData(219)]
[InlineData(289)]
public void Test1(int offset) {
const string testMessage =
"this is a test string please ignore 1, this is a test string please ignore 2, this is a test string please ignore 3, this is a test string please ignore 4, this is a test string please ignore 5, this is a test string please ignore 6, this is a test string please ignore 7, this is a test str.";
const int testBufferWidth = 80;
Assert.Equal(ReferenceMethods.GetViewIntoMessageBuffer(testMessage, offset, testBufferWidth),
Util.GetViewIntoMessageBuffer(testMessage, offset, testBufferWidth));
}
private static class ReferenceMethods {
internal static (string messageBuffer, int relativeCursorPosition)
GetViewIntoMessageBuffer(string message, int absoluteCursorPosition, int bufferWidth) {
const int wraparoundOffsetPre = 2; // number of "untouchable" characters moving the cursor onto will cause a wrap on the right screen edge
const int wraparoundOffsetPost = 5; // number of "untouchable" characters moving the cursor onto will cause a wrap on the left screen edge
const int wraparoundOffsetPreW = wraparoundOffsetPre + 1; // offset + 1 (character on the edge), for easier calculations
const int wraparoundOffsetPostW = wraparoundOffsetPost + 1; // offset + 1 (character on the edge), for easier calculations
if (absoluteCursorPosition > message.Length)
throw new ArgumentOutOfRangeException();
if (message.Length < bufferWidth)
return (message, absoluteCursorPosition);
if (absoluteCursorPosition < bufferWidth - wraparoundOffsetPre - 1)
return (Util.TruncateString(message, bufferWidth, $"{Util.Ansi.Inverse}>{Util.Ansi.InverseOff}"), absoluteCursorPosition);
// now we can be sure the message needs at least one wrap
// first wrap
// get rid of the content shown on the zeroth wrap, which is buf width minus wraparoundPreW (respects > character on screen edge)
var finalMessage = message[(bufferWidth - wraparoundOffsetPreW - wraparoundOffsetPost)..];
var finalCursorPos = absoluteCursorPosition - bufferWidth + wraparoundOffsetPreW + wraparoundOffsetPostW;
// successive wraps
// repeat above steps (but counting the new < character) until the string fits into the buffer
// it fits into the buffer when cursorPos >= bufferwidth minus wraparound (this time respecting > character absent on first wrap)
while (finalCursorPos >= bufferWidth - wraparoundOffsetPreW) {
finalMessage = finalMessage[(bufferWidth - wraparoundOffsetPreW - wraparoundOffsetPostW)..];
finalCursorPos = finalCursorPos - bufferWidth + wraparoundOffsetPreW + wraparoundOffsetPostW;
}
finalMessage = Util.TruncateString(finalMessage, bufferWidth - 1, $"{Util.Ansi.Inverse}>{Util.Ansi.InverseOff}");
return ($"{Util.Ansi.Inverse}<{Util.Ansi.InverseOff}" + finalMessage, finalCursorPos);
}
}
}

1
tgcli.Tests/Usings.cs Normal file
View file

@ -0,0 +1 @@
global using Xunit;

View file

@ -0,0 +1,28 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.3.2" />
<PackageReference Include="xunit" Version="2.4.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="3.1.2">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\tgcli\tgcli.csproj" />
</ItemGroup>
</Project>

View file

@ -1,7 +1,10 @@

Microsoft Visual Studio Solution File, Format Version 12.00
#
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "tgcli", "tgcli\tgcli.csproj", "{26C5A85E-DDBB-4358-84A7-4A6A577428CB}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "tgcli.Tests", "tgcli.Tests\tgcli.Tests.csproj", "{2A95F0DD-72BF-4B43-BB81-E143A7C800FB}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@ -12,5 +15,9 @@ Global
{26C5A85E-DDBB-4358-84A7-4A6A577428CB}.Debug|Any CPU.Build.0 = Debug|Any CPU
{26C5A85E-DDBB-4358-84A7-4A6A577428CB}.Release|Any CPU.ActiveCfg = Release|Any CPU
{26C5A85E-DDBB-4358-84A7-4A6A577428CB}.Release|Any CPU.Build.0 = Release|Any CPU
{2A95F0DD-72BF-4B43-BB81-E143A7C800FB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{2A95F0DD-72BF-4B43-BB81-E143A7C800FB}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2A95F0DD-72BF-4B43-BB81-E143A7C800FB}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2A95F0DD-72BF-4B43-BB81-E143A7C800FB}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal