Skip to content

Commit

Permalink
Add MessageQueue unit tests & fix a few things
Browse files Browse the repository at this point in the history
  • Loading branch information
ButterscotchV committed Jan 29, 2024
1 parent 5e9355c commit b0fbfbb
Show file tree
Hide file tree
Showing 5 changed files with 153 additions and 3 deletions.
32 changes: 32 additions & 0 deletions ButterSTT.Tests/ButterSTT.Tests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<Project Sdk="Microsoft.NET.Sdk">

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

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

<ItemGroup>
<PackageReference Include="CSharpier.MsBuild" Version="0.27.2">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="MSTest.TestAdapter" Version="3.2.0" />
<PackageReference Include="MSTest.TestFramework" Version="3.2.0" />
<PackageReference Include="coverlet.collector" Version="6.0.0" />
<PackageReference Include="xunit" Version="2.6.6" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.6">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>

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

</Project>
112 changes: 112 additions & 0 deletions ButterSTT.Tests/MessageSystem/MessageQueueTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
using ButterSTT.TextProcessing;
using Xunit;
using Xunit.Abstractions;

namespace ButterSTT.MessageSystem.Tests
{
public class MessageQueueTests
{
private readonly ITestOutputHelper output;

public MessageQueueTests(ITestOutputHelper output)
{
this.output = output;
}

[Fact()]
public void TimeoutTest()
{
var queue = new MessageQueue { WordTime = TimeSpan.Zero };

var firstMessage = "Testing the queue system.";
queue.CurParagraph = EnglishTextParser.ParseParagraph(firstMessage);
queue.FinishCurrentParagraph();
var curMessage = queue.GetCurrentMessage();
output.WriteLine($"First message: \"{curMessage}\"");
Assert.Equal(firstMessage, curMessage);

var secondMessage = "Second test message.";
queue.CurParagraph = EnglishTextParser.ParseParagraph(secondMessage);
queue.FinishCurrentParagraph();
curMessage = queue.GetCurrentMessage();
output.WriteLine($"Second message: \"{curMessage}\"");
Assert.Equal(secondMessage, curMessage);
}

[Fact()]
public void CombineTest()
{
var queue = new MessageQueue { WordTime = TimeSpan.Zero };

var firstMessage = "Testing the queue system.";
queue.CurParagraph = EnglishTextParser.ParseParagraph(firstMessage);
queue.FinishCurrentParagraph();
var secondMessage = "Second test message.";
queue.CurParagraph = EnglishTextParser.ParseParagraph(secondMessage);
queue.FinishCurrentParagraph();

var curMessage = queue.GetCurrentMessage();
output.WriteLine($"Combined message: \"{curMessage}\"");
Assert.Equal($"{firstMessage} {secondMessage}", curMessage);
}

[Fact()]
public void RealtimeTest()
{
var queue = new MessageQueue { WordTime = TimeSpan.Zero };

// Likely incomplete word
queue.CurParagraph = EnglishTextParser.ParseParagraph("Testing th");
var curMessage = queue.GetCurrentMessage();
output.WriteLine($"First message: \"{curMessage}\"");
Assert.Equal("Testing", curMessage);

// Complete sentence
var secondMessage = "Testing the queue system.";
queue.CurParagraph = EnglishTextParser.ParseParagraph(secondMessage);
curMessage = queue.GetCurrentMessage();
output.WriteLine($"Second message: \"{curMessage}\"");
Assert.Equal(secondMessage, curMessage);

// Complete and partial sentence with one complete word
var thirdMessage = "Testing the queue system. Second ";
queue.CurParagraph = EnglishTextParser.ParseParagraph(thirdMessage);
curMessage = queue.GetCurrentMessage();
output.WriteLine($"Third message: \"{curMessage}\"");
Assert.Equal(thirdMessage.Trim(), curMessage);

// Two complete sentences
var fourthMessage = "Testing the queue system. Second test message.";
queue.CurParagraph = EnglishTextParser.ParseParagraph(fourthMessage);
curMessage = queue.GetCurrentMessage();
output.WriteLine($"Fourth message: \"{curMessage}\"");
Assert.Equal(fourthMessage, curMessage);
}

// Not yet implemented, should fail
[Fact()]
public void RealtimeAppendingTest()
{
var queue = new MessageQueue { WordTime = TimeSpan.MaxValue };

// Initial message, fully completed
var firstMessage = "Testing the queue system.";
queue.CurParagraph = EnglishTextParser.ParseParagraph(firstMessage);
queue.FinishCurrentParagraph();

// Add a partial sentence to it
var secondMessage = "Second test ";
queue.CurParagraph = EnglishTextParser.ParseParagraph(secondMessage);
var curMessage = queue.GetCurrentMessage();
output.WriteLine($"Appended message: \"{curMessage}\"");
Assert.Equal($"{firstMessage} {secondMessage.Trim()}", curMessage);

// Replace the partial message
var thirdMessage = "Second test message.";
queue.CurParagraph = EnglishTextParser.ParseParagraph(thirdMessage);
curMessage = queue.GetCurrentMessage();
output.WriteLine($"Appended message: \"{curMessage}\"");
Assert.Equal($"{firstMessage} {thirdMessage}", curMessage);
}
}
}
6 changes: 6 additions & 0 deletions ButterSTT.sln
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ButterSTT", "ButterSTT\Butt
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CoreOSC", "Dependencies\CoreOSC-VRC-UTF8\CoreOSC\CoreOSC.csproj", "{4919CC8D-D8F3-423F-944C-7E375D0E16EF}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ButterSTT.Tests", "ButterSTT.Tests\ButterSTT.Tests.csproj", "{FCE266B7-2036-499B-92FB-263C8A39D570}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -20,6 +22,10 @@ Global
{4919CC8D-D8F3-423F-944C-7E375D0E16EF}.Debug|Any CPU.Build.0 = Debug|Any CPU
{4919CC8D-D8F3-423F-944C-7E375D0E16EF}.Release|Any CPU.ActiveCfg = Release|Any CPU
{4919CC8D-D8F3-423F-944C-7E375D0E16EF}.Release|Any CPU.Build.0 = Release|Any CPU
{FCE266B7-2036-499B-92FB-263C8A39D570}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{FCE266B7-2036-499B-92FB-263C8A39D570}.Debug|Any CPU.Build.0 = Debug|Any CPU
{FCE266B7-2036-499B-92FB-263C8A39D570}.Release|Any CPU.ActiveCfg = Release|Any CPU
{FCE266B7-2036-499B-92FB-263C8A39D570}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
4 changes: 2 additions & 2 deletions ButterSTT/MessageSystem/MessageQueue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public string GetCurrentMessage()
{
while (
MessageWordQueue.TryPeek(out var expiredWord)
&& DateTime.Now - expiredWord.DisplayTime > WordTime
&& DateTime.UtcNow - expiredWord.DisplayTime >= WordTime
)
{
CurMessageLength -= MessageWordQueue.Dequeue().Text.Length;
Expand All @@ -80,7 +80,7 @@ public string GetCurrentMessage()
)
{
var word = WordQueue.Dequeue();
MessageWordQueue.Enqueue(new MessageWord(word, DateTime.Now));
MessageWordQueue.Enqueue(new MessageWord(word, DateTime.UtcNow));
CurMessageLength += word.Length;
}

Expand Down
2 changes: 1 addition & 1 deletion ButterSTT/TextProcessing/EnglishTextParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public static Paragraph ParseParagraph(string text)
{
Sentence[] sentences = SentenceKeepUrl()
.Matches(text)
.Select(m => ParseSentence(m.Value.Trim()))
.Select(m => ParseSentence(m.Value))
.ToArray();
return new Paragraph(sentences);
}
Expand Down

0 comments on commit b0fbfbb

Please sign in to comment.