Skip to content

Commit

Permalink
🧪 Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
database64128 committed Dec 9, 2021
1 parent 054389b commit a3bf7dd
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 3 deletions.
65 changes: 65 additions & 0 deletions CubicBot.Telegram.Tests/ChatHelperTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
using CubicBot.Telegram.Utils;
using Telegram.Bot.Exceptions;
using Xunit;

namespace CubicBot.Telegram.Tests;

public class ChatHelperTests
{
[Fact]
public void Get_Retry_Wait_Time_Normal()
{
for (var i = 1; i <= 30; i++)
{
Test_Get_Retry_Wait_Time(i);
}
}

private static void Test_Get_Retry_Wait_Time(int seconds)
{
var ex = new ApiRequestException($"Too Many Requests: retry after {seconds}", 429);

var waitTimeSec = ChatHelper.GetRetryWaitTimeMs(ex) / 1000;

Assert.InRange(waitTimeSec, seconds + 1, seconds + 5);
}

[Theory]
[InlineData("💩")]
[InlineData("Super super super super super super super long error message.")]
public void Get_Retry_Wait_Time_Bad_Message(string message)
{
var ex = new ApiRequestException(message, 429);

var waitTimeMs = ChatHelper.GetRetryWaitTimeMs(ex);

Assert.Equal(15 * 1000, waitTimeMs);
}

[Theory]
[InlineData(null, "fakename", null, null)]
[InlineData("lol", "fakename", null, null)]
[InlineData("/", "fakename", null, null)]
[InlineData("/ arg", "fakename", null, null)]
[InlineData("/@", "fakename", null, null)]
[InlineData("/@ arg", "fakename", null, null)]
[InlineData("/@fakename", "fakename", null, null)]
[InlineData("/@fakename arg", "fakename", null, null)]
[InlineData("/@wrongname", "fakename", null, null)]
[InlineData("/start", "fakename", "start", null)]
[InlineData("/start ", "fakename", "start", null)]
[InlineData("/start ", "fakename", "start", null)]
[InlineData("/start arg", "fakename", "start", "arg")]
[InlineData("/start arg ", "fakename", "start", "arg")]
[InlineData("/start arg", "fakename", "start", "arg")]
[InlineData("/start@ arg", "fakename", "start", "arg")]
[InlineData("/start@wrongname arg", "fakename", null, null)]
[InlineData("/start@fakename arg", "fakename", "start", "arg")]
public void Parse_Message_Into_Command_And_Argument(string? message, string botUsername, string? expectedCommand, string? expectedArgument)
{
var (command, argument) = ChatHelper.ParseMessageIntoCommandAndArgument(message, botUsername);

Assert.Equal(expectedCommand, command);
Assert.Equal(expectedArgument, argument);
}
}
27 changes: 27 additions & 0 deletions CubicBot.Telegram.Tests/CubicBot.Telegram.Tests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.0.0" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="3.1.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\CubicBot.Telegram\CubicBot.Telegram.csproj" />
</ItemGroup>

</Project>
12 changes: 9 additions & 3 deletions CubicBot.sln
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.31205.134
# Visual Studio Version 17
VisualStudioVersion = 17.0.31903.59
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CubicBot.Telegram", "CubicBot.Telegram\CubicBot.Telegram.csproj", "{997C0DEC-6193-480D-9317-16053D8AD5FA}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CubicBot.Telegram", "CubicBot.Telegram\CubicBot.Telegram.csproj", "{997C0DEC-6193-480D-9317-16053D8AD5FA}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CubicBot.Telegram.Tests", "CubicBot.Telegram.Tests\CubicBot.Telegram.Tests.csproj", "{A4BCE05E-2876-459D-936C-B2A99F074AF7}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand All @@ -15,6 +17,10 @@ Global
{997C0DEC-6193-480D-9317-16053D8AD5FA}.Debug|Any CPU.Build.0 = Debug|Any CPU
{997C0DEC-6193-480D-9317-16053D8AD5FA}.Release|Any CPU.ActiveCfg = Release|Any CPU
{997C0DEC-6193-480D-9317-16053D8AD5FA}.Release|Any CPU.Build.0 = Release|Any CPU
{A4BCE05E-2876-459D-936C-B2A99F074AF7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{A4BCE05E-2876-459D-936C-B2A99F074AF7}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A4BCE05E-2876-459D-936C-B2A99F074AF7}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A4BCE05E-2876-459D-936C-B2A99F074AF7}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down

0 comments on commit a3bf7dd

Please sign in to comment.