]> git.smokeofanarchy.ru Git - space-station-14.git/commitdiff
Content changes & tests for engine prototype validation PR (#27188)
authorLeon Friedrich <60421075+ElectroJr@users.noreply.github.com>
Wed, 24 Apr 2024 14:27:13 +0000 (02:27 +1200)
committerGitHub <noreply@github.com>
Wed, 24 Apr 2024 14:27:13 +0000 (00:27 +1000)
* Content changes & tests for engine prototype validation PR

* A

Content.IntegrationTests/Tests/Linter/StaticFieldValidationTest.cs [new file with mode: 0644]
Content.Shared/IdentityManagement/Components/IdentityBlockerComponent.cs
Content.YAMLLinter/Program.cs

diff --git a/Content.IntegrationTests/Tests/Linter/StaticFieldValidationTest.cs b/Content.IntegrationTests/Tests/Linter/StaticFieldValidationTest.cs
new file mode 100644 (file)
index 0000000..30724b5
--- /dev/null
@@ -0,0 +1,150 @@
+using System.Collections.Generic;
+using System.Linq;
+using Content.Shared.Tag;
+using Robust.Shared.Prototypes;
+using Robust.Shared.Reflection;
+using Robust.Shared.Serialization.Manager.Attributes;
+
+namespace Content.IntegrationTests.Tests.Linter;
+
+/// <summary>
+/// Verify that the yaml linter successfully validates static fields
+/// </summary>
+[TestFixture]
+public sealed class StaticFieldValidationTest
+{
+    [Test]
+    public async Task TestStaticFieldValidation()
+    {
+        await using var pair = await PoolManager.GetServerClient();
+        var protoMan = pair.Server.ProtoMan;
+
+        var protos = new Dictionary<Type, HashSet<string>>();
+        foreach (var kind in protoMan.EnumeratePrototypeKinds())
+        {
+            var ids = protoMan.EnumeratePrototypes(kind).Select(x => x.ID).ToHashSet();
+            protos.Add(kind, ids);
+        }
+
+        Assert.That(protoMan.ValidateStaticFields(typeof(StringValid), protos).Count, Is.Zero);
+        Assert.That(protoMan.ValidateStaticFields(typeof(StringArrayValid), protos).Count, Is.Zero);
+        Assert.That(protoMan.ValidateStaticFields(typeof(EntProtoIdValid), protos).Count, Is.Zero);
+        Assert.That(protoMan.ValidateStaticFields(typeof(EntProtoIdArrayValid), protos).Count, Is.Zero);
+        Assert.That(protoMan.ValidateStaticFields(typeof(ProtoIdTestValid), protos).Count, Is.Zero);
+        Assert.That(protoMan.ValidateStaticFields(typeof(ProtoIdArrayValid), protos).Count, Is.Zero);
+        Assert.That(protoMan.ValidateStaticFields(typeof(ProtoIdListValid), protos).Count, Is.Zero);
+        Assert.That(protoMan.ValidateStaticFields(typeof(ProtoIdSetValid), protos).Count, Is.Zero);
+        Assert.That(protoMan.ValidateStaticFields(typeof(PrivateProtoIdArrayValid), protos).Count, Is.Zero);
+        
+        Assert.That(protoMan.ValidateStaticFields(typeof(StringInvalid), protos).Count, Is.EqualTo(1));
+        Assert.That(protoMan.ValidateStaticFields(typeof(StringArrayInvalid), protos).Count, Is.EqualTo(2));
+        Assert.That(protoMan.ValidateStaticFields(typeof(EntProtoIdInvalid), protos).Count, Is.EqualTo(1));
+        Assert.That(protoMan.ValidateStaticFields(typeof(EntProtoIdArrayInvalid), protos).Count, Is.EqualTo(2));
+        Assert.That(protoMan.ValidateStaticFields(typeof(ProtoIdTestInvalid), protos).Count, Is.EqualTo(1));
+        Assert.That(protoMan.ValidateStaticFields(typeof(ProtoIdArrayInvalid), protos).Count, Is.EqualTo(2));
+        Assert.That(protoMan.ValidateStaticFields(typeof(ProtoIdListInvalid), protos).Count, Is.EqualTo(2));
+        Assert.That(protoMan.ValidateStaticFields(typeof(ProtoIdSetInvalid), protos).Count, Is.EqualTo(2));
+        Assert.That(protoMan.ValidateStaticFields(typeof(PrivateProtoIdArrayInvalid), protos).Count, Is.EqualTo(2));
+        
+        await pair.CleanReturnAsync();
+    }
+
+    [TestPrototypes]
+    private const string TestPrototypes = @"
+- type: entity
+  id: StaticFieldTestEnt
+
+- type: Tag
+  id: StaticFieldTestTag
+";
+
+    [Reflect(false)] private sealed class StringValid
+    {
+        [ValidatePrototypeId<TagPrototype>] public static string Tag = "StaticFieldTestTag";
+    }
+
+    [Reflect(false)] private sealed class StringInvalid
+    {
+        [ValidatePrototypeId<TagPrototype>] public static string Tag = string.Empty;
+    }
+
+    [Reflect(false)] private sealed class StringArrayValid
+    {
+        [ValidatePrototypeId<TagPrototype>] public static string[] Tag = {"StaticFieldTestTag", "StaticFieldTestTag"};
+    }
+
+    [Reflect(false)] private sealed class StringArrayInvalid
+    {
+        [ValidatePrototypeId<TagPrototype>] public static string[] Tag = {string.Empty, "StaticFieldTestTag", string.Empty};
+    }
+
+    [Reflect(false)] private sealed class EntProtoIdValid
+    {
+        public static EntProtoId Tag = "StaticFieldTestEnt";
+    }
+
+    [Reflect(false)] private sealed class EntProtoIdInvalid
+    {
+        public static EntProtoId Tag = string.Empty;
+    }
+
+    [Reflect(false)] private sealed class EntProtoIdArrayValid
+    {
+        public static EntProtoId[] Tag = {"StaticFieldTestEnt", "StaticFieldTestEnt"};
+    }
+
+    [Reflect(false)] private sealed class EntProtoIdArrayInvalid
+    {
+        public static EntProtoId[] Tag = {string.Empty, "StaticFieldTestEnt", string.Empty};
+    }
+
+    [Reflect(false)] private sealed class ProtoIdTestValid
+    {
+        public static ProtoId<TagPrototype> Tag = "StaticFieldTestTag";
+    }
+
+    [Reflect(false)] private sealed class ProtoIdTestInvalid
+    {
+        public static ProtoId<TagPrototype> Tag = string.Empty;
+    }
+
+    [Reflect(false)] private sealed class ProtoIdArrayValid
+    {
+        public static ProtoId<TagPrototype>[] Tag = {"StaticFieldTestTag", "StaticFieldTestTag"};
+    }
+
+    [Reflect(false)] private sealed class ProtoIdArrayInvalid
+    {
+        public static ProtoId<TagPrototype>[] Tag = {string.Empty, "StaticFieldTestTag", string.Empty};
+    }
+
+    [Reflect(false)] private sealed class ProtoIdListValid
+    {
+        public static List<ProtoId<TagPrototype>> Tag = new() {"StaticFieldTestTag", "StaticFieldTestTag"};
+    }
+
+    [Reflect(false)] private sealed class ProtoIdListInvalid
+    {
+        public static List<ProtoId<TagPrototype>> Tag = new() {string.Empty, "StaticFieldTestTag", string.Empty};
+    }
+
+    [Reflect(false)] private sealed class ProtoIdSetValid
+    {
+        public static HashSet<ProtoId<TagPrototype>> Tag = new() {"StaticFieldTestTag", "StaticFieldTestTag"};
+    }
+
+    [Reflect(false)] private sealed class ProtoIdSetInvalid
+    {
+        public static HashSet<ProtoId<TagPrototype>> Tag = new() {string.Empty, "StaticFieldTestTag", string.Empty, " "};
+    }
+
+    [Reflect(false)] private sealed class PrivateProtoIdArrayValid
+    {
+        private static ProtoId<TagPrototype>[] Tag = {"StaticFieldTestTag", "StaticFieldTestTag"};
+    }
+
+    [Reflect(false)] private sealed class PrivateProtoIdArrayInvalid
+    {
+        private static ProtoId<TagPrototype>[] Tag = {string.Empty, "StaticFieldTestTag", string.Empty};
+    }
+}
index 3857063783dd341ed96238491112d8b325352837..e7a88b6ef29e3df0444370a575d97d3fe3f1b7fe 100644 (file)
@@ -6,6 +6,7 @@ namespace Content.Shared.IdentityManagement.Components;
 [RegisterComponent, NetworkedComponent]
 public sealed partial class IdentityBlockerComponent : Component
 {
+    [DataField]
     public bool Enabled = true;
 
     /// <summary>
index b23faa48fcd0c066a0c62c954d547f527929db58..7f0b740fe8ced16411d46ac5f16337ce9bbe6eef 100644 (file)
@@ -99,7 +99,7 @@ namespace Content.YAMLLinter
                         yamlErrors[kind] = set;
                 }
 
-                fieldErrors = protoMan.ValidateFields(prototypes);
+                fieldErrors = protoMan.ValidateStaticFields(prototypes);
             });
 
             return (yamlErrors, fieldErrors);