Skip to content

Commit

Permalink
Add calculator service tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Syriiin committed Nov 20, 2023
1 parent 492afb7 commit 36b67ea
Show file tree
Hide file tree
Showing 16 changed files with 259 additions and 2 deletions.
17 changes: 17 additions & 0 deletions Difficalcy.Catch.Tests/CatchCalculatorServiceTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using Difficalcy.Catch.Models;
using Difficalcy.Catch.Services;
using Difficalcy.Services;
using Difficalcy.Tests;

namespace Difficalcy.Catch.Tests;

public class CatchCalculatorServiceTest : CalculatorServiceTest<CatchScore, CatchDifficulty, CatchPerformance, CatchCalculation>
{
protected override CalculatorService<CatchScore, CatchDifficulty, CatchPerformance, CatchCalculation> CalculatorService => new CatchCalculatorService(new DummyCache(), new TestBeatmapProvider("osu.Game.Rulesets.Catch"));

[Theory]
[InlineData(4.0505463516206195d, 164.5770866821372d, "diffcalc-test", 0)]
[InlineData(5.1696411260785498d, 291.43480971713944d, "diffcalc-test", 64)]
public void Test(double expectedDifficultyTotal, double expectedPerformanceTotal, string beatmapId, int mods)
=> base.TestGetCalculationReturnsCorrectValues(expectedDifficultyTotal, expectedPerformanceTotal, new CatchScore { BeatmapId = beatmapId, Mods = mods });
}
29 changes: 29 additions & 0 deletions Difficalcy.Catch.Tests/Difficalcy.Catch.Tests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<Project Sdk="Microsoft.NET.Sdk">

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

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

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.1.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.2">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Difficalcy.Tests\Difficalcy.Tests.csproj" />
<ProjectReference Include="..\Difficalcy.Catch\Difficalcy.Catch.csproj" />
</ItemGroup>

</Project>
1 change: 1 addition & 0 deletions Difficalcy.Catch.Tests/Usings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
global using Xunit;
29 changes: 29 additions & 0 deletions Difficalcy.Mania.Tests/Difficalcy.Mania.Tests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<Project Sdk="Microsoft.NET.Sdk">

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

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

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.1.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.2">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Difficalcy.Tests\Difficalcy.Tests.csproj" />
<ProjectReference Include="..\Difficalcy.Mania\Difficalcy.Mania.csproj" />
</ItemGroup>

</Project>
17 changes: 17 additions & 0 deletions Difficalcy.Mania.Tests/ManiaCalculatorServiceTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using Difficalcy.Mania.Models;
using Difficalcy.Mania.Services;
using Difficalcy.Services;
using Difficalcy.Tests;

namespace Difficalcy.Mania.Tests;

public class ManiaCalculatorServiceTest : CalculatorServiceTest<ManiaScore, ManiaDifficulty, ManiaPerformance, ManiaCalculation>
{
protected override CalculatorService<ManiaScore, ManiaDifficulty, ManiaPerformance, ManiaCalculation> CalculatorService => new ManiaCalculatorService(new DummyCache(), new TestBeatmapProvider("osu.Game.Rulesets.Mania"));

[Theory]
[InlineData(2.3449735700206298d, 43.846469224942766d, "diffcalc-test", 0)]
[InlineData(2.7879104989252959d, 65.81066322316853d, "diffcalc-test", 64)]
public void Test(double expectedDifficultyTotal, double expectedPerformanceTotal, string beatmapId, int mods)
=> base.TestGetCalculationReturnsCorrectValues(expectedDifficultyTotal, expectedPerformanceTotal, new ManiaScore { BeatmapId = beatmapId, Mods = mods });
}
1 change: 1 addition & 0 deletions Difficalcy.Mania.Tests/Usings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
global using Xunit;
29 changes: 29 additions & 0 deletions Difficalcy.Osu.Tests/Difficalcy.Osu.Tests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<Project Sdk="Microsoft.NET.Sdk">

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

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

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.1.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.2">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Difficalcy.Tests\Difficalcy.Tests.csproj" />
<ProjectReference Include="..\Difficalcy.Osu\Difficalcy.Osu.csproj" />
</ItemGroup>

</Project>
17 changes: 17 additions & 0 deletions Difficalcy.Osu.Tests/OsuCalculatorServiceTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using Difficalcy.Osu.Models;
using Difficalcy.Osu.Services;
using Difficalcy.Services;
using Difficalcy.Tests;

namespace Difficalcy.Osu.Tests;

public class OsuCalculatorServiceTest : CalculatorServiceTest<OsuScore, OsuDifficulty, OsuPerformance, OsuCalculation>
{
protected override CalculatorService<OsuScore, OsuDifficulty, OsuPerformance, OsuCalculation> CalculatorService => new OsuCalculatorService(new DummyCache(), new TestBeatmapProvider("osu.Game.Rulesets.Osu"));

[Theory]
[InlineData(6.5867229481955389d, 273.31622171900824d, "diffcalc-test", 0)]
[InlineData(8.2730989071947896d, 567.4273062942285d, "diffcalc-test", 64)]
public void Test(double expectedDifficultyTotal, double expectedPerformanceTotal, string beatmapId, int mods)
=> base.TestGetCalculationReturnsCorrectValues(expectedDifficultyTotal, expectedPerformanceTotal, new OsuScore { BeatmapId = beatmapId, Mods = mods });
}
1 change: 1 addition & 0 deletions Difficalcy.Osu.Tests/Usings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
global using Xunit;
29 changes: 29 additions & 0 deletions Difficalcy.Taiko.Tests/Difficalcy.Taiko.Tests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<Project Sdk="Microsoft.NET.Sdk">

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

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

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.1.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.2">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Difficalcy.Tests\Difficalcy.Tests.csproj" />
<ProjectReference Include="..\Difficalcy.Taiko\Difficalcy.Taiko.csproj" />
</ItemGroup>

</Project>
17 changes: 17 additions & 0 deletions Difficalcy.Taiko.Tests/TaikoCalculatorServiceTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using Difficalcy.Taiko.Models;
using Difficalcy.Taiko.Services;
using Difficalcy.Services;
using Difficalcy.Tests;

namespace Difficalcy.Taiko.Tests;

public class TaikoCalculatorServiceTest : CalculatorServiceTest<TaikoScore, TaikoDifficulty, TaikoPerformance, TaikoCalculation>
{
protected override CalculatorService<TaikoScore, TaikoDifficulty, TaikoPerformance, TaikoCalculation> CalculatorService => new TaikoCalculatorService(new DummyCache(), new TestBeatmapProvider("osu.Game.Rulesets.Taiko"));

[Theory]
[InlineData(2.2420075288523802d, 100.38891498669157d, "diffcalc-test", 0)]
[InlineData(3.134084469440479d, 165.39807330636233d, "diffcalc-test", 64)]
public void Test(double expectedDifficultyTotal, double expectedPerformanceTotal, string beatmapId, int mods)
=> base.TestGetCalculationReturnsCorrectValues(expectedDifficultyTotal, expectedPerformanceTotal, new TaikoScore { BeatmapId = beatmapId, Mods = mods });
}
1 change: 1 addition & 0 deletions Difficalcy.Taiko.Tests/Usings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
global using Xunit;
4 changes: 2 additions & 2 deletions Difficalcy.Tests/CalculatorServiceTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public async void TestGetCalculationReturnsCorrectValues(double expectedDifficul
{
var calculation = await CalculatorService.GetCalculation(score);

Assert.Equal(expectedDifficultyTotal, calculation.Difficulty.Total);
Assert.Equal(expectedPerformanceTotal, calculation.Performance.Total);
Assert.Equal(expectedDifficultyTotal, calculation.Difficulty.Total, 4);
Assert.Equal(expectedPerformanceTotal, calculation.Performance.Total, 4);
}
}
41 changes: 41 additions & 0 deletions Difficalcy.Tests/TestBeatmapProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using System.Reflection;

namespace Difficalcy.Services
{
public class TestBeatmapProvider : IBeatmapProvider
{
private string _resourceAssemblyName;

public TestBeatmapProvider(string resourceAssemblyName)
{
_resourceAssemblyName = resourceAssemblyName;
}

public Task<bool> EnsureBeatmap(string beatmapId)
{
var resourceName = $"{_resourceAssemblyName}.Resources.{beatmapId}";
var info = ResourceAssembly.GetManifestResourceInfo(resourceName);
return Task.FromResult(info != null);
}

public Stream GetBeatmapStream(string beatmapId)
{
var resourceNamespace = "Testing.Beatmaps";
var resourceName = $"{resourceNamespace}.{beatmapId}.osu";
var fullResourceName = $"{_resourceAssemblyName}.Resources.{resourceName}";
var stream = ResourceAssembly.GetManifestResourceStream(fullResourceName);
if (stream == null)
throw new Exception($@"Unable to find resource ""{fullResourceName}"" in assembly ""{_resourceAssemblyName}""");
return stream;
}

private Assembly ResourceAssembly
{
get
{
string localPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!;
return Assembly.LoadFrom(Path.Combine(localPath, $"{_resourceAssemblyName}.dll"));
}
}
}
}
24 changes: 24 additions & 0 deletions Difficalcy.sln
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Difficalcy.Mania", "Diffica
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Difficalcy.Tests", "Difficalcy.Tests\Difficalcy.Tests.csproj", "{FBA3098D-8E2B-41A4-BF92-83A25567FFCD}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Difficalcy.Osu.Tests", "Difficalcy.Osu.Tests\Difficalcy.Osu.Tests.csproj", "{8CCF92AA-359E-481F-BB67-DD4C1B601378}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Difficalcy.Taiko.Tests", "Difficalcy.Taiko.Tests\Difficalcy.Taiko.Tests.csproj", "{E7FF119C-05AD-4D77-85AA-D3DC7D8D6E3F}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Difficalcy.Catch.Tests", "Difficalcy.Catch.Tests\Difficalcy.Catch.Tests.csproj", "{D377B0B0-F89C-4A2B-A4EB-533F7BAD0552}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Difficalcy.Mania.Tests", "Difficalcy.Mania.Tests\Difficalcy.Mania.Tests.csproj", "{EEA08AAD-9A2C-4C31-980D-3ED0B661E017}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -48,5 +56,21 @@ Global
{FBA3098D-8E2B-41A4-BF92-83A25567FFCD}.Debug|Any CPU.Build.0 = Debug|Any CPU
{FBA3098D-8E2B-41A4-BF92-83A25567FFCD}.Release|Any CPU.ActiveCfg = Release|Any CPU
{FBA3098D-8E2B-41A4-BF92-83A25567FFCD}.Release|Any CPU.Build.0 = Release|Any CPU
{8CCF92AA-359E-481F-BB67-DD4C1B601378}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{8CCF92AA-359E-481F-BB67-DD4C1B601378}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8CCF92AA-359E-481F-BB67-DD4C1B601378}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8CCF92AA-359E-481F-BB67-DD4C1B601378}.Release|Any CPU.Build.0 = Release|Any CPU
{E7FF119C-05AD-4D77-85AA-D3DC7D8D6E3F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{E7FF119C-05AD-4D77-85AA-D3DC7D8D6E3F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E7FF119C-05AD-4D77-85AA-D3DC7D8D6E3F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E7FF119C-05AD-4D77-85AA-D3DC7D8D6E3F}.Release|Any CPU.Build.0 = Release|Any CPU
{D377B0B0-F89C-4A2B-A4EB-533F7BAD0552}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D377B0B0-F89C-4A2B-A4EB-533F7BAD0552}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D377B0B0-F89C-4A2B-A4EB-533F7BAD0552}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D377B0B0-F89C-4A2B-A4EB-533F7BAD0552}.Release|Any CPU.Build.0 = Release|Any CPU
{EEA08AAD-9A2C-4C31-980D-3ED0B661E017}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{EEA08AAD-9A2C-4C31-980D-3ED0B661E017}.Debug|Any CPU.Build.0 = Debug|Any CPU
{EEA08AAD-9A2C-4C31-980D-3ED0B661E017}.Release|Any CPU.ActiveCfg = Release|Any CPU
{EEA08AAD-9A2C-4C31-980D-3ED0B661E017}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal
4 changes: 4 additions & 0 deletions tooling.Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,13 @@ WORKDIR /app
COPY Difficalcy.sln .
COPY Difficalcy/Difficalcy.csproj ./Difficalcy/
COPY Difficalcy.Catch/Difficalcy.Catch.csproj ./Difficalcy.Catch/
COPY Difficalcy.Catch.Tests/Difficalcy.Catch.Tests.csproj ./Difficalcy.Catch.Tests/
COPY Difficalcy.Mania/Difficalcy.Mania.csproj ./Difficalcy.Mania/
COPY Difficalcy.Mania.Tests/Difficalcy.Mania.Tests.csproj ./Difficalcy.Mania.Tests/
COPY Difficalcy.Osu/Difficalcy.Osu.csproj ./Difficalcy.Osu/
COPY Difficalcy.Osu.Tests/Difficalcy.Osu.Tests.csproj ./Difficalcy.Osu.Tests/
COPY Difficalcy.Taiko/Difficalcy.Taiko.csproj ./Difficalcy.Taiko/
COPY Difficalcy.Taiko.Tests/Difficalcy.Taiko.Tests.csproj ./Difficalcy.Taiko.Tests/
COPY Difficalcy.Tests/Difficalcy.Tests.csproj ./Difficalcy.Tests/

RUN dotnet restore
Expand Down

0 comments on commit 36b67ea

Please sign in to comment.