Add SolutionContainerSystem tests (#6726)

This commit is contained in:
Ygg01
2022-02-18 03:42:39 +01:00
committed by GitHub
parent 20a32fcacd
commit 4ac750d986
4 changed files with 1158 additions and 971 deletions

View File

@@ -0,0 +1,195 @@
using System.Threading.Tasks;
using Content.Server.Chemistry.EntitySystems;
using Content.Shared.Chemistry.Components;
using Content.Shared.FixedPoint;
using NUnit.Framework;
using Robust.Shared.GameObjects;
using Robust.Shared.Map;
namespace Content.IntegrationTests.Tests.Chemistry;
// We are adding two non-reactive solutions in these tests
// To ensure volume(A) + volume(B) = volume(A+B)
// reactions can change this assumption
[TestFixture]
[TestOf(typeof(SolutionContainerSystem))]
public sealed class SolutionSystemTests : ContentIntegrationTest
{
private const string Prototypes = @"
- type: entity
id: SolutionTarget
components:
- type: SolutionContainerManager
solutions:
beaker:
maxVol: 50
";
[Test]
public async Task TryAddTwoNonReactiveReagent()
{
var options = new ServerContentIntegrationOption { ExtraPrototypes = Prototypes };
var server = StartServer(options);
await server.WaitIdleAsync();
var entityManager = server.ResolveDependency<IEntityManager>();
var mapManager = server.ResolveDependency<IMapManager>();
var coordinates = GetMainEntityCoordinates(mapManager);
EntityUid beaker;
server.Assert(() =>
{
var oilQuantity = FixedPoint2.New(15);
var waterQuantity = FixedPoint2.New(10);
var oilAdded = new Solution("Oil", oilQuantity);
var originalWater = new Solution("Water", waterQuantity);
beaker = entityManager.SpawnEntity("SolutionTarget", coordinates);
Assert.That(EntitySystem.Get<SolutionContainerSystem>()
.TryGetSolution(beaker, "beaker", out var solution));
solution.AddSolution(originalWater);
Assert.That(EntitySystem.Get<SolutionContainerSystem>()
.TryAddSolution(beaker, solution, oilAdded));
solution.ContainsReagent("Water", out var water);
solution.ContainsReagent("Oil", out var oil);
Assert.That(water, Is.EqualTo(waterQuantity));
Assert.That(oil, Is.EqualTo(oilQuantity));
});
await server.WaitIdleAsync();
}
// This test mimics current behavior
// i.e. if adding too much `TryAddSolution` adding will fail
[Test]
public async Task TryAddTooMuchNonReactiveReagent()
{
var options = new ServerContentIntegrationOption { ExtraPrototypes = Prototypes };
var server = StartServer(options);
await server.WaitIdleAsync();
var entityManager = server.ResolveDependency<IEntityManager>();
var mapManager = server.ResolveDependency<IMapManager>();
var coordinates = GetMainEntityCoordinates(mapManager);
EntityUid beaker;
server.Assert(() =>
{
var oilQuantity = FixedPoint2.New(1500);
var waterQuantity = FixedPoint2.New(10);
var oilAdded = new Solution("Oil", oilQuantity);
var originalWater = new Solution("Water", waterQuantity);
beaker = entityManager.SpawnEntity("SolutionTarget", coordinates);
Assert.That(EntitySystem.Get<SolutionContainerSystem>()
.TryGetSolution(beaker, "beaker", out var solution));
solution.AddSolution(originalWater);
Assert.That(EntitySystem.Get<SolutionContainerSystem>()
.TryAddSolution(beaker, solution, oilAdded), Is.False);
solution.ContainsReagent("Water", out var water);
solution.ContainsReagent("Oil", out var oil);
Assert.That(water, Is.EqualTo(waterQuantity));
Assert.That(oil, Is.EqualTo(FixedPoint2.Zero));
});
await server.WaitIdleAsync();
}
// Unlike TryAddSolution this adds and two solution without then splits leaving only threshold in original
[Test]
public async Task TryMixAndOverflowTooMuchReagent()
{
var options = new ServerContentIntegrationOption { ExtraPrototypes = Prototypes };
var server = StartServer(options);
await server.WaitIdleAsync();
var entityManager = server.ResolveDependency<IEntityManager>();
var mapManager = server.ResolveDependency<IMapManager>();
var coordinates = GetMainEntityCoordinates(mapManager);
EntityUid beaker;
server.Assert(() =>
{
int ratio = 9;
int threshold = 20;
var waterQuantity = FixedPoint2.New(10);
var oilQuantity = FixedPoint2.New(ratio * waterQuantity.Int());
var oilAdded = new Solution("Oil", oilQuantity);
var originalWater = new Solution("Water", waterQuantity);
beaker = entityManager.SpawnEntity("SolutionTarget", coordinates);
Assert.That(EntitySystem.Get<SolutionContainerSystem>()
.TryGetSolution(beaker, "beaker", out var solution));
solution.AddSolution(originalWater);
Assert.That(EntitySystem.Get<SolutionContainerSystem>()
.TryMixAndOverflow(beaker, solution, oilAdded, threshold, out var overflowingSolution));
Assert.That(solution.CurrentVolume, Is.EqualTo(FixedPoint2.New(threshold)));
solution.ContainsReagent("Water", out var waterMix);
solution.ContainsReagent("Oil", out var oilMix);
Assert.That(waterMix, Is.EqualTo(FixedPoint2.New(threshold / (ratio + 1))));
Assert.That(oilMix, Is.EqualTo(FixedPoint2.New(threshold / (ratio + 1) * ratio)));
Assert.That(overflowingSolution.CurrentVolume, Is.EqualTo(FixedPoint2.New(80)));
overflowingSolution.ContainsReagent("Water", out var waterOverflow);
overflowingSolution.ContainsReagent("Oil", out var oilOverFlow);
Assert.That(waterOverflow, Is.EqualTo(waterQuantity - waterMix));
Assert.That(oilOverFlow, Is.EqualTo(oilQuantity - oilMix));
});
await server.WaitIdleAsync();
}
// TryMixAndOverflow will fail if Threshold larger than MaxVolume
[Test]
public async Task TryMixAndOverflowTooBigOverflow()
{
var options = new ServerContentIntegrationOption { ExtraPrototypes = Prototypes };
var server = StartServer(options);
await server.WaitIdleAsync();
var entityManager = server.ResolveDependency<IEntityManager>();
var mapManager = server.ResolveDependency<IMapManager>();
var coordinates = GetMainEntityCoordinates(mapManager);
EntityUid beaker;
server.Assert(() =>
{
int ratio = 9;
int threshold = 60;
var waterQuantity = FixedPoint2.New(10);
var oilQuantity = FixedPoint2.New(ratio * waterQuantity.Int());
var oilAdded = new Solution("Oil", oilQuantity);
var originalWater = new Solution("Water", waterQuantity);
beaker = entityManager.SpawnEntity("SolutionTarget", coordinates);
Assert.That(EntitySystem.Get<SolutionContainerSystem>()
.TryGetSolution(beaker, "beaker", out var solution));
solution.AddSolution(originalWater);
Assert.That(EntitySystem.Get<SolutionContainerSystem>()
.TryMixAndOverflow(beaker, solution, oilAdded, threshold, out _),
Is.False);
});
await server.WaitIdleAsync();
}
}

View File

@@ -1,14 +1,11 @@
using System.Diagnostics.CodeAnalysis;
using System.Text;
using Content.Server.Chemistry.Components.SolutionManager;
using Content.Server.Database;
using Content.Shared.Chemistry.Components;
using Content.Shared.Chemistry.Reagent;
using Content.Shared.FixedPoint;
using Robust.Shared.GameObjects;
namespace Content.Server.Chemistry.EntitySystems
{
namespace Content.Server.Chemistry.EntitySystems;
public sealed partial class SolutionContainerSystem
{
public void Refill(EntityUid targetUid, Solution targetSolution, Solution addedSolution,
@@ -146,11 +143,11 @@ namespace Content.Server.Chemistry.EntitySystems
{
sb.Append(", ");
}
sb.AppendFormat("{0}: {1}u", id, quantity);
sb.AppendFormat("{0}: {1}u", id, quantity);
}
sb.Append(']');
return sb.ToString();
}
}
}

View File

@@ -1,8 +1,5 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using Content.Server.Chemistry.Components.SolutionManager;
using Content.Server.Fluids.Components;
using Content.Shared.Chemistry;
using Content.Shared.Chemistry.Components;
using Content.Shared.Chemistry.Reaction;
@@ -10,15 +7,11 @@ using Content.Shared.Chemistry.Reagent;
using Content.Shared.Examine;
using Content.Shared.FixedPoint;
using JetBrains.Annotations;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Localization;
using Robust.Shared.Log;
using Robust.Shared.Prototypes;
using Robust.Shared.Utility;
namespace Content.Server.Chemistry.EntitySystems
{
namespace Content.Server.Chemistry.EntitySystems;
/// <summary>
/// This event alerts system that the solution was changed
/// </summary>
@@ -105,7 +98,8 @@ namespace Content.Server.Chemistry.EntitySystems
return;
var filledVolumePercent = solution.CurrentVolume.Float() / solution.MaxVolume.Float();
appearanceComponent.SetData(SolutionContainerVisuals.VisualState, new SolutionContainerVisualState(solution.Color, filledVolumePercent));
appearanceComponent.SetData(SolutionContainerVisuals.VisualState,
new SolutionContainerVisualState(solution.Color, filledVolumePercent));
}
/// <summary>
@@ -231,12 +225,14 @@ namespace Content.Server.Chemistry.EntitySystems
/// <summary>
/// Adds a solution to the container, overflowing the rest.
/// Unlike TryAddSolution it will ignore size limits.
/// It will
/// Unlike <see cref="TryAddSolution"/> it will ignore size limits.
/// </summary>
/// <param name="targetUid">entity holding targetSolution</param>
/// <param name="targetSolution">The container to which we try to add.</param>
/// <param name="addedSolution">solution being added</param>
/// <param name="overflowThreshold">After addition this much will be left in targetSolution</param>
/// <param name="overflowThreshold">After addition this much will be left in targetSolution. Should be less
/// than targetSolution.TotalVolume</param>
/// <param name="overflowingSolution">Solution that exceeded overflowThreshold</param>
/// <returns></returns>
public bool TryMixAndOverflow(EntityUid targetUid, Solution targetSolution,
@@ -244,7 +240,7 @@ namespace Content.Server.Chemistry.EntitySystems
FixedPoint2 overflowThreshold,
[NotNullWhen(true)] out Solution? overflowingSolution)
{
if (addedSolution.TotalVolume == 0)
if (addedSolution.TotalVolume == 0 || overflowThreshold > targetSolution.MaxVolume)
{
overflowingSolution = null;
return false;
@@ -342,6 +338,7 @@ namespace Content.Server.Chemistry.EntitySystems
// Thermal energy and temperature management.
#region Thermal Energy and Temperature
/// <summary>
@@ -391,4 +388,3 @@ namespace Content.Server.Chemistry.EntitySystems
#endregion Thermal Energy and Temperature
}
}

View File

@@ -1,12 +1,11 @@
using Content.Shared.Chemistry.Components;
using Content.Shared.Chemistry.Reagent;
using Content.Shared.FixedPoint;
using Robust.Shared.IoC;
using Robust.Shared.Prototypes;
using NUnit.Framework;
namespace Content.Tests.Shared.Chemistry
{
namespace Content.Tests.Shared.Chemistry;
[TestFixture, Parallelizable, TestOf(typeof(Solution))]
public sealed class Solution_Tests : ContentUnitTest
{
@@ -353,6 +352,7 @@ namespace Content.Tests.Shared.Chemistry
}
// Tests concerning thermal energy and temperature.
#region Thermal Energy and Temperature
[Test]
@@ -481,4 +481,3 @@ namespace Content.Tests.Shared.Chemistry
#endregion Thermal Energy and Temperature
}
}