// // Licensed under the MIT License. See LICENSE file in the project root for full license information. // namespace MUnique.OpenMU.AttributeSystem.Tests; /// /// Tests for the . /// [TestFixture] public class ComposableAttributeTests { private ComposableAttribute _composableAttribute = null!; /// /// Sets up each test case. /// [SetUp] public void Setup() { var attributeDefinition = new AttributeDefinition(new Guid("52263EA9-F309-475D-B10B-352D3BFD7650"), "Test attribute", "Test attribute"); this._composableAttribute = new ComposableAttribute(attributeDefinition); } /// /// Tests if the value is 0 after creation. /// [Test] public void ValueIsNullAfterCreation() { Assert.That(this._composableAttribute.Value, Is.EqualTo(0)); } /// /// Tests if the value is updated after adding an element. /// [Test] public void ValueAfterAddedElement() { var element = new ConstantElement(4711); this._composableAttribute.AddElement(element); Assert.That(this._composableAttribute.Value, Is.EqualTo(element.Value)); } /// /// Tests if the value of multiple elements is combined in by using . /// [Test] public void ValueOfMultipleRawElements() { var element1 = new ConstantElement(3000); var element2 = new ConstantElement(5000); this._composableAttribute.AddElement(element1); this._composableAttribute.AddElement(element2); Assert.That(this._composableAttribute.Value, Is.EqualTo(element1.Value + element2.Value)); } /// /// Tests if the value of multiple elements is combined in /// by using in the first element and /// by using in the second element. /// [Test] public void ValueWithRawAndMultiplierElements() { var element1 = new ConstantElement(3000); var element2 = new SimpleElement { Value = 5, AggregateType = AggregateType.Multiplicate }; this._composableAttribute.AddElement(element1); this._composableAttribute.AddElement(element2); Assert.That(this._composableAttribute.Value, Is.EqualTo(element1.Value * element2.Value)); } /// /// Tests if the value of multiple elements is combined in /// by using in the first element, /// by using in the second element and /// by using in the last element. /// [Test] public void ValueWithRawMultiplierAndFinalElements() { var element1 = new ConstantElement(3000); var element2 = new SimpleElement { Value = 5, AggregateType = AggregateType.Multiplicate }; var element3 = new SimpleElement { Value = 1000, AggregateType = AggregateType.AddFinal }; this._composableAttribute.AddElement(element1); this._composableAttribute.AddElement(element2); this._composableAttribute.AddElement(element3); Assert.That(this._composableAttribute.Value, Is.EqualTo((element1.Value * element2.Value) + element3.Value)); } /// /// Tests if the value of multiple elements is combined in /// by using one element and /// by using several elements. /// [Test] public void ValueWithRawAndMultipleMaximumElements() { var element1 = new ConstantElement(3000); var element2 = new SimpleElement { Value = 5, AggregateType = AggregateType.Maximum }; var element3 = new SimpleElement { Value = 1000, AggregateType = AggregateType.Maximum }; this._composableAttribute.AddElement(element1); this._composableAttribute.AddElement(element2); this._composableAttribute.AddElement(element3); Assert.That(this._composableAttribute.Value, Is.EqualTo(element1.Value + Math.Max(element2.Value, element3.Value))); } /// /// Tests if the value of multiple elements is combined in /// by using elements exclusively. /// A element of value 1 should be assumed. /// [Test] public void ValueWithMultiplierElementsOnly() { var element1 = new SimpleElement { Value = 5, AggregateType = AggregateType.Multiplicate }; var element2 = new SimpleElement { Value = 1000, AggregateType = AggregateType.Multiplicate }; this._composableAttribute.AddElement(element1); this._composableAttribute.AddElement(element2); Assert.That(this._composableAttribute.Value, Is.EqualTo(element1.Value * element2.Value)); } /// /// Tests if the value of multiple elements is combined in /// by using in the first element and /// by using in the second element. /// [Test] public void ValueWithMultiplierAndFinalElements() { var element1 = new SimpleElement { Value = 5, AggregateType = AggregateType.Multiplicate }; var element2 = new SimpleElement { Value = 1000, AggregateType = AggregateType.AddFinal }; this._composableAttribute.AddElement(element1); this._composableAttribute.AddElement(element2); Assert.That(this._composableAttribute.Value, Is.EqualTo(1000)); } /// /// Tests if the updated correctly after an element got removed. /// [Test] public void ValueCorrectAfterElementRemoved() { var element1 = new ConstantElement(3000); var element2 = new SimpleElement { Value = 5, AggregateType = AggregateType.Multiplicate }; var element3 = new SimpleElement { Value = 1000, AggregateType = AggregateType.AddFinal }; this._composableAttribute.AddElement(element1); this._composableAttribute.AddElement(element2); this._composableAttribute.AddElement(element3); Assert.That(this._composableAttribute.Value, Is.EqualTo((element1.Value * element2.Value) + element3.Value)); this._composableAttribute.RemoveElement(element2); Assert.That(this._composableAttribute.Value, Is.EqualTo(element1.Value + element3.Value)); } /// /// Tests if the is called when the depending element value changed. /// [Test] public void ValueChangedEvent() { var element = new SimpleElement { Value = 5 }; this._composableAttribute.AddElement(element); var eventCalled = false; this._composableAttribute.ValueChanged += (_, _) => eventCalled = true; element.Value = 6; Assert.That(eventCalled, Is.True); } /// /// Tests if the is called when a new depending element was added. /// [Test] public void ValueChangedEventWhenElementAdded() { var element = new SimpleElement { Value = 5 }; bool eventCalled = false; this._composableAttribute.ValueChanged += (_, _) => eventCalled = true; this._composableAttribute.AddElement(element); Assert.That(eventCalled, Is.True); } /// /// Tests if the is called when a new depending element was removed. /// [Test] public void ValueChangedEventWhenElementRemoved() { var element = new SimpleElement { Value = 5 }; this._composableAttribute.AddElement(element); bool eventCalled = false; this._composableAttribute.ValueChanged += (_, _) => eventCalled = true; this._composableAttribute.RemoveElement(element); Assert.That(eventCalled, Is.True); } }