From 088c05c47bf4d262710ada5a09944011d808c12e Mon Sep 17 00:00:00 2001 From: ben_singer Date: Fri, 29 Nov 2024 16:42:25 +0000 Subject: [PATCH] Added Any method to AttributeManager --- .../Attributes/AttributeManager_Tests.cs | 64 +++++++++++++++++++ NetAF/Assets/Attributes/AttributeManager.cs | 26 +++++++- 2 files changed, 89 insertions(+), 1 deletion(-) diff --git a/NetAF.Tests/Assets/Attributes/AttributeManager_Tests.cs b/NetAF.Tests/Assets/Attributes/AttributeManager_Tests.cs index e20eea5..855999b 100644 --- a/NetAF.Tests/Assets/Attributes/AttributeManager_Tests.cs +++ b/NetAF.Tests/Assets/Attributes/AttributeManager_Tests.cs @@ -199,5 +199,69 @@ public void GivenSubtract10From5WhenMin0_WhenSubtract_ThenValueIs0() Assert.AreEqual(0, result); } + + [TestMethod] + public void GivenNonExistentAttributeIdentifiedByString_WhenAny_ThenReturnFalse() + { + AttributeManager manager = new(); + + var result = manager.Any("test"); + + Assert.IsFalse(result); + } + + [TestMethod] + public void GivenExistentAttributeIdentifiedByStringWithValueOf0_WhenAny_ThenReturnFalse() + { + AttributeManager manager = new(); + manager.Add("test", 0); + + var result = manager.Any("test"); + + Assert.IsFalse(result); + } + + [TestMethod] + public void GivenExistentAttributeIdentifiedByStringWithValueOf1_WhenAny_ThenReturnTrue() + { + AttributeManager manager = new(); + manager.Add("test", 1); + + var result = manager.Any("test"); + + Assert.IsTrue(result); + } + + [TestMethod] + public void GivenNonExistentAttribute_WhenAny_ThenReturnFalse() + { + AttributeManager manager = new(); + + var result = manager.Any(new Attribute("test", string.Empty, 0, 1)); + + Assert.IsFalse(result); + } + + [TestMethod] + public void GivenExistentAttribute_WhenAny_ThenReturnFalse() + { + AttributeManager manager = new(); + manager.Add("test", 0); + + var result = manager.Any(new Attribute("test", string.Empty, 0, 1)); + + Assert.IsFalse(result); + } + + [TestMethod] + public void GivenExistentAttribute_WhenAny_ThenReturnTrue() + { + AttributeManager manager = new(); + manager.Add("test", 1); + + var result = manager.Any(new Attribute("test", string.Empty, 0, 1)); + + Assert.IsTrue(result); + } } } diff --git a/NetAF/Assets/Attributes/AttributeManager.cs b/NetAF/Assets/Attributes/AttributeManager.cs index feb3e93..d4d77b0 100644 --- a/NetAF/Assets/Attributes/AttributeManager.cs +++ b/NetAF/Assets/Attributes/AttributeManager.cs @@ -109,7 +109,31 @@ public int GetValue(Attribute attribute) if (attribute == null) return 0; - return attributes.TryGetValue(attribute, out var value) ? value : 0; + var match = GetMatch(attribute); + return attributes.TryGetValue(match, out var value) ? value : 0; + } + + /// + /// Get if there is any of a specified attribute. + /// + /// The name of the attribute. + /// True if there are any, else false. + public bool Any(string attributeName) + { + return GetValue(attributes.Keys.FirstOrDefault(x => x.Name.InsensitiveEquals(attributeName))) > 0; + } + + /// + /// Get if there is any of a specified attribute. + /// + /// The attribute. + /// True if there are any, else false. + public bool Any(Attribute attribute) + { + if (attribute == null) + return false; + + return GetValue(attribute) > 0; } ///