Skip to content

Commit

Permalink
Added Any method to AttributeManager
Browse files Browse the repository at this point in the history
  • Loading branch information
ben_singer committed Nov 29, 2024
1 parent 2b962ae commit 088c05c
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 1 deletion.
64 changes: 64 additions & 0 deletions NetAF.Tests/Assets/Attributes/AttributeManager_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
26 changes: 25 additions & 1 deletion NetAF/Assets/Attributes/AttributeManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/// <summary>
/// Get if there is any of a specified attribute.
/// </summary>
/// <param name="attributeName">The name of the attribute.</param>
/// <returns>True if there are any, else false.</returns>
public bool Any(string attributeName)
{
return GetValue(attributes.Keys.FirstOrDefault(x => x.Name.InsensitiveEquals(attributeName))) > 0;
}

/// <summary>
/// Get if there is any of a specified attribute.
/// </summary>
/// <param name="attribute">The attribute.</param>
/// <returns>True if there are any, else false.</returns>
public bool Any(Attribute attribute)
{
if (attribute == null)
return false;

return GetValue(attribute) > 0;
}

/// <summary>
Expand Down

0 comments on commit 088c05c

Please sign in to comment.