]> git.smokeofanarchy.ru Git - space-station-14.git/commitdiff
Add storage fill test (#15758)
authorLeon Friedrich <60421075+ElectroJr@users.noreply.github.com>
Tue, 25 Apr 2023 00:30:35 +0000 (12:30 +1200)
committerGitHub <noreply@github.com>
Tue, 25 Apr 2023 00:30:35 +0000 (10:30 +1000)
Content.IntegrationTests/PoolManager.cs
Content.IntegrationTests/Tests/StorageTest.cs
Resources/Prototypes/Entities/Clothing/Belt/belts.yml

index 92e96bd15cfcb8b9b39b0b2eda714fe3e8d42218..7c7f442ef510168fea877958f6adda72d732359a 100644 (file)
@@ -670,6 +670,25 @@ we are just going to end this here to save a lot of time. This is the exception
 
         Assert.That(passed);
     }
+
+    /// <summary>
+    ///     Helper method that retrieves all entity prototypes that have some component.
+    /// </summary>
+    public static List<EntityPrototype> GetEntityPrototypes<T>(RobustIntegrationTest.IntegrationInstance instance) where T : Component
+    {
+        var protoMan = instance.ResolveDependency<IPrototypeManager>();
+        var compFact = instance.ResolveDependency<IComponentFactory>();
+
+        var id = compFact.GetComponentName(typeof(T));
+        var list = new List<EntityPrototype>();
+        foreach (var ent in protoMan.EnumeratePrototypes<EntityPrototype>())
+        {
+            if (ent.Components.ContainsKey(id))
+                list.Add(ent);
+        }
+
+        return list;
+    }
 }
 
 /// <summary>
index 21b5e7c40ab3407b78d1ef52a8df26eb2a1326e1..fca27733bab3887ebf78103fa93ab5eb8fa8994c 100644 (file)
@@ -1,10 +1,15 @@
 #nullable enable
+using System;
+using System.Collections.Generic;
+using System.Linq;
 using System.Threading.Tasks;
 using Content.Server.Storage.Components;
 using Content.Shared.Item;
 using Content.Shared.Storage;
 using NUnit.Framework;
+using Robust.Shared.GameObjects;
 using Robust.Shared.Prototypes;
+using Robust.UnitTesting;
 
 namespace Content.IntegrationTests.Tests
 {
@@ -60,5 +65,84 @@ namespace Content.IntegrationTests.Tests
             });
             await pairTracker.CleanReturnAsync();
         }
+
+        [Test]
+        public async Task TestSufficientSpaceForFill()
+        {
+            await using var pairTracker = await PoolManager.GetServerClient(new PoolSettings{NoClient = true});
+            var server = pairTracker.Pair.Server;
+
+            var protoMan = server.ResolveDependency<IPrototypeManager>();
+            var compFact = server.ResolveDependency<IComponentFactory>();
+            var id = compFact.GetComponentName(typeof(StorageFillComponent));
+
+            Assert.Multiple(() =>
+            {
+                foreach (var proto in PoolManager.GetEntityPrototypes<StorageFillComponent>(server))
+                {
+                    int capacity;
+                    var isEntStorage = false;
+
+                    if (proto.TryGetComponent<ServerStorageComponent>("Storage", out var storage))
+                    {
+                        capacity = storage.StorageCapacityMax;
+                    }
+                    else if (proto.TryGetComponent<EntityStorageComponent>("EntityStorage", out var entStorage))
+                    {
+                        capacity = entStorage.Capacity;
+                        isEntStorage = true;
+                    }
+                    else
+                    {
+                        Assert.Fail($"Entity {proto.ID} has storage-fill without a storage component!");
+                        continue;
+                    }
+
+                    var fill = (StorageFillComponent) proto.Components[id].Component;
+                    var size = GetFillSize(fill, isEntStorage);
+                    Assert.That(size, Is.LessThanOrEqualTo(capacity), $"{proto.ID} storage fill is too large.");
+                }
+            });
+
+            int GetEntrySize(EntitySpawnEntry entry, bool isEntStorage)
+            {
+                if (entry.PrototypeId == null)
+                    return 0;
+
+                if (!protoMan.TryIndex<EntityPrototype>(entry.PrototypeId, out var proto))
+                {
+                    Assert.Fail($"Unknown prototype: {entry.PrototypeId}");
+                    return 0;
+                }
+
+                if (isEntStorage)
+                    return entry.Amount;
+
+                if (proto.TryGetComponent<ItemComponent>("Item", out var item))
+                    return item.Size * entry.Amount;
+
+                Assert.Fail($"Prototype is missing item comp: {entry.PrototypeId}");
+                return 0;
+            }
+
+            int GetFillSize(StorageFillComponent fill, bool isEntStorage)
+            {
+                var totalSize = 0;
+                var groups = new Dictionary<string, int>();
+                foreach (var entry in fill.Contents)
+                {
+                    var size = GetEntrySize(entry, isEntStorage);
+
+                    if (entry.GroupId == null)
+                        totalSize += size;
+                    else
+                        groups[entry.GroupId] = Math.Max(size, groups.GetValueOrDefault(entry.GroupId));
+                }
+
+                return totalSize + groups.Values.Sum();
+            }
+
+            await pairTracker.CleanReturnAsync();
+        }
     }
 }
index 9c0d1058f8ad5e39aa69c39508376a0a3baa27dc..be11fbdd3f660f4995ac20dfe5dc5997897de9bb 100644 (file)
   - type: Clothing
     sprite: Clothing/Belt/militarywebbingmed.rsi
   - type: Item
-    size: 60
+    size: 70
   - type: Storage
-    capacity: 60
+    capacity: 70
 
 - type: entity
   parent: ClothingBeltBase