Index: Core/Common/src/Core.Common.Gui/Converters/KeyValueAsRoundedDoubleWithoutTrailingZeroesElementAttribute.cs =================================================================== diff -u -r0b30baa3fa65d8b4159b5bcd9d80ddb77a2aafb8 -r7ce1153d6af94b372e527cfb2c9128924e340a4f --- Core/Common/src/Core.Common.Gui/Converters/KeyValueAsRoundedDoubleWithoutTrailingZeroesElementAttribute.cs (.../KeyValueAsRoundedDoubleWithoutTrailingZeroesElementAttribute.cs) (revision 0b30baa3fa65d8b4159b5bcd9d80ddb77a2aafb8) +++ Core/Common/src/Core.Common.Gui/Converters/KeyValueAsRoundedDoubleWithoutTrailingZeroesElementAttribute.cs (.../KeyValueAsRoundedDoubleWithoutTrailingZeroesElementAttribute.cs) (revision 7ce1153d6af94b372e527cfb2c9128924e340a4f) @@ -54,21 +54,21 @@ public override string GetName(object source) { - PropertyInfo unitPropertyInfo = GetUnit(source); - - if (unitPropertyInfo == null || - unitPropertyInfo.GetValue(source, new object[0]) == null) - { - return base.GetName(source); - } - PropertyInfo namePropertyInfo = source.GetType().GetProperty(namePropertyName); if (namePropertyInfo == null) { throw new ArgumentException($"Name property '{namePropertyName}' was not found on type {source.GetType().Name}."); } - return $"{Convert.ToString(namePropertyInfo.GetValue(source, new object[0]))} [{Convert.ToString(unitPropertyInfo.GetValue(source, new object[0]))}]"; + PropertyInfo unitPropertyInfo = GetUnit(source); + + object propertyInfoObject = unitPropertyInfo.GetValue(source, new object[0]); + if (propertyInfoObject == null) + { + return base.GetName(source); + } + + return $"{Convert.ToString(namePropertyInfo.GetValue(source, new object[0]))} [{Convert.ToString(propertyInfoObject)}]"; } /// Index: Core/Common/test/Core.Common.Gui.Test/Converters/KeyValueAsRoundedDoubleWithoutTrailingZeroesElementAttributeTest.cs =================================================================== diff -u -r0b30baa3fa65d8b4159b5bcd9d80ddb77a2aafb8 -r7ce1153d6af94b372e527cfb2c9128924e340a4f --- Core/Common/test/Core.Common.Gui.Test/Converters/KeyValueAsRoundedDoubleWithoutTrailingZeroesElementAttributeTest.cs (.../KeyValueAsRoundedDoubleWithoutTrailingZeroesElementAttributeTest.cs) (revision 0b30baa3fa65d8b4159b5bcd9d80ddb77a2aafb8) +++ Core/Common/test/Core.Common.Gui.Test/Converters/KeyValueAsRoundedDoubleWithoutTrailingZeroesElementAttributeTest.cs (.../KeyValueAsRoundedDoubleWithoutTrailingZeroesElementAttributeTest.cs) (revision 7ce1153d6af94b372e527cfb2c9128924e340a4f) @@ -34,29 +34,35 @@ public void Constructor_WithoutNamePropertyName_ThrowsArgumentNullException() { // Call - void Call() => new KeyValueAsRoundedDoubleWithoutTrailingZeroesElementAttribute(null, nameof(TestObject.Unit), "value"); + void Call() => new KeyValueAsRoundedDoubleWithoutTrailingZeroesElementAttribute(null, + nameof(TestObject.Unit), + "value"); // Assert var exception = Assert.Throws(Call); Assert.AreEqual("namePropertyName", exception.ParamName); } - + [Test] public void Constructor_WithoutUnitPropertyName_ThrowsArgumentNullException() { // Call - void Call() => new KeyValueAsRoundedDoubleWithoutTrailingZeroesElementAttribute("name", null, "value"); + void Call() => new KeyValueAsRoundedDoubleWithoutTrailingZeroesElementAttribute("name", + null, + "value"); // Assert var exception = Assert.Throws(Call); Assert.AreEqual("unitPropertyName", exception.ParamName); } - + [Test] public void Constructor_WithoutValuePropertyName_ThrowsArgumentNullException() { // Call - void Call() => new KeyValueAsRoundedDoubleWithoutTrailingZeroesElementAttribute("name", nameof(TestObject.Unit), null); + void Call() => new KeyValueAsRoundedDoubleWithoutTrailingZeroesElementAttribute("name", + nameof(TestObject.Unit), + null); // Assert var exception = Assert.Throws(Call); @@ -67,7 +73,9 @@ public void Constructor_WithParameters_CreatesNewInstance() { // Call - var attribute = new KeyValueAsRoundedDoubleWithoutTrailingZeroesElementAttribute("name", nameof(TestObject.Unit), "value"); + var attribute = new KeyValueAsRoundedDoubleWithoutTrailingZeroesElementAttribute("name", + nameof(TestObject.Unit), + "value"); // Assert Assert.IsInstanceOf(attribute); @@ -79,7 +87,9 @@ // Setup const string expectedName = "expectedName"; - var attribute = new KeyValueAsRoundedDoubleWithoutTrailingZeroesElementAttribute(nameof(TestObject.Name), nameof(TestObject.Unit), nameof(TestObject.Value)); + var attribute = new KeyValueAsRoundedDoubleWithoutTrailingZeroesElementAttribute(nameof(TestObject.Name), + nameof(TestObject.Unit), + nameof(TestObject.Value)); // Call string name = attribute.GetName(new TestObject @@ -92,28 +102,34 @@ } [Test] - public void GetName_WithObjectWithNonStringProperty_ReturnsValueOfProperty() + public void GetName_WithObjectWithProperties_ReturnsValueOfProperties() { // Setup - int expectedName = new Random(21).Next(3, 50); + const string expectedName = "expectedName"; + const string expectedUnit = "kN"; - var attribute = new KeyValueAsRoundedDoubleWithoutTrailingZeroesElementAttribute(nameof(TestObject.NonStringName), nameof(TestObject.Unit), nameof(TestObject.NonRoundedDoubleValue)); + var attribute = new KeyValueAsRoundedDoubleWithoutTrailingZeroesElementAttribute(nameof(TestObject.Name), + nameof(TestObject.Unit), + nameof(TestObject.Value)); // Call string name = attribute.GetName(new TestObject { - NonStringName = expectedName + Name = expectedName, + Unit = expectedUnit }); // Assert - Assert.AreEqual(Convert.ToString(expectedName), name); + Assert.AreEqual($"{expectedName} [{expectedUnit}]", name); } [Test] public void GetName_WithObjectWithoutPropertyWithName_ThrowsArgumentException() { // Setup - var attribute = new KeyValueAsRoundedDoubleWithoutTrailingZeroesElementAttribute("IDoNotExist", nameof(TestObject.Unit),nameof(TestObject.Value)); + var attribute = new KeyValueAsRoundedDoubleWithoutTrailingZeroesElementAttribute("IDoNotExist", + nameof(TestObject.Unit), + nameof(TestObject.Value)); // Call void Call() => attribute.GetName(new TestObject()); @@ -122,12 +138,14 @@ const string expectedMessage = "Name property 'IDoNotExist' was not found on type TestObject."; TestHelper.AssertThrowsArgumentExceptionAndTestMessage(Call, expectedMessage); } - + [Test] public void GetName_WithObjectWithoutPropertyWithUnit_ThrowsArgumentException() { // Setup - var attribute = new KeyValueAsRoundedDoubleWithoutTrailingZeroesElementAttribute("name", "IDoNotExist",nameof(TestObject.Value)); + var attribute = new KeyValueAsRoundedDoubleWithoutTrailingZeroesElementAttribute(nameof(TestObject.Name), + "IDoNotExist", + nameof(TestObject.Value)); // Call void Call() => attribute.GetName(new TestObject()); @@ -136,14 +154,16 @@ const string expectedMessage = "Unit property 'IDoNotExist' was not found on type TestObject."; TestHelper.AssertThrowsArgumentExceptionAndTestMessage(Call, expectedMessage); } - + [Test] [SetCulture("nl-NL")] public void GetValue_WithObjectWithRoundedDoubleProperty_ReturnsValueOfProperty() { // Setup var roundedDoubleValue = new RoundedDouble(5, 5.12345); - var attribute = new KeyValueAsRoundedDoubleWithoutTrailingZeroesElementAttribute(nameof(TestObject.Name), nameof(TestObject.Unit),nameof(TestObject.Value)); + var attribute = new KeyValueAsRoundedDoubleWithoutTrailingZeroesElementAttribute(nameof(TestObject.Name), + nameof(TestObject.Unit), + nameof(TestObject.Value)); // Call string value = attribute.GetValue(new TestObject @@ -166,7 +186,9 @@ { // Setup var roundedDoubleValue = new RoundedDouble(5, doubleValue); - var attribute = new KeyValueAsRoundedDoubleWithoutTrailingZeroesElementAttribute(nameof(TestObject.Name), nameof(TestObject.Unit),nameof(TestObject.Value)); + var attribute = new KeyValueAsRoundedDoubleWithoutTrailingZeroesElementAttribute(nameof(TestObject.Name), + nameof(TestObject.Unit), + nameof(TestObject.Value)); // Call string value = attribute.GetValue(new TestObject @@ -184,7 +206,9 @@ // Setup int expectedValue = new Random(21).Next(3, 50); - var attribute = new KeyValueAsRoundedDoubleWithoutTrailingZeroesElementAttribute(nameof(TestObject.NonStringName), nameof(TestObject.Unit),nameof(TestObject.NonRoundedDoubleValue)); + var attribute = new KeyValueAsRoundedDoubleWithoutTrailingZeroesElementAttribute(nameof(TestObject.NonStringName), + nameof(TestObject.Unit), + nameof(TestObject.NonRoundedDoubleValue)); // Call void Call() => @@ -202,7 +226,9 @@ public void GetValue_WithObjectWithoutPropertyWithName_ThrowsArgumentException() { // Setup - var attribute = new KeyValueAsRoundedDoubleWithoutTrailingZeroesElementAttribute(nameof(TestObject.Name), nameof(TestObject.Unit),"IDoNotExist"); + var attribute = new KeyValueAsRoundedDoubleWithoutTrailingZeroesElementAttribute(nameof(TestObject.Name), + nameof(TestObject.Unit), + "IDoNotExist"); // Call void Call() => attribute.GetValue(new TestObject()); @@ -219,6 +245,8 @@ public RoundedDouble Value { get; set; } public int NonStringName { get; set; } + + public int NonStringUnit { get; set; } public int NonRoundedDoubleValue { get; set; } } }