diff --git a/src/ImageProcessorCore/Image/ImageProperty.cs b/src/ImageProcessorCore/Image/ImageProperty.cs index fee4ab516..7e447c5fc 100644 --- a/src/ImageProcessorCore/Image/ImageProperty.cs +++ b/src/ImageProcessorCore/Image/ImageProperty.cs @@ -21,6 +21,8 @@ namespace ImageProcessorCore /// The value of the property. public ImageProperty(string name, string value) { + Guard.NotNullOrEmpty(name, nameof(name)); + this.Name = name; this.Value = value; } @@ -92,12 +94,7 @@ namespace ImageProcessorCore { ImageProperty other = obj as ImageProperty; - if (other == null) - { - return false; - } - - return other.Name == this.Name && other.Value == this.Value; + return Equals(other); } /// @@ -111,7 +108,11 @@ namespace ImageProcessorCore unchecked { int hashCode = this.Name.GetHashCode(); - hashCode = (hashCode * 397) ^ this.Value.GetHashCode(); + if (this.Value != null) + { + hashCode = (hashCode * 397) ^ this.Value.GetHashCode(); + } + return hashCode; } } @@ -146,7 +147,7 @@ namespace ImageProcessorCore return true; } - return this.Name.Equals(other.Name) && this.Value.Equals(other.Value); + return this.Name.Equals(other.Name) && Equals(this.Value, other.Value); } } } diff --git a/tests/ImageProcessorCore.Tests/Image/ImagePropertyTests.cs b/tests/ImageProcessorCore.Tests/Image/ImagePropertyTests.cs index bee588e94..edd3479af 100644 --- a/tests/ImageProcessorCore.Tests/Image/ImagePropertyTests.cs +++ b/tests/ImageProcessorCore.Tests/Image/ImagePropertyTests.cs @@ -5,6 +5,7 @@ namespace ImageProcessorCore.Tests { + using System; using Xunit; /// @@ -36,6 +37,7 @@ namespace ImageProcessorCore.Tests ImageProperty property1 = new ImageProperty("Foo", "Bar"); ImageProperty property2 = new ImageProperty("Foo", "Foo"); ImageProperty property3 = new ImageProperty("Bar", "Bar"); + ImageProperty property4 = new ImageProperty("Foo", null); Assert.False(property1.Equals("Foo")); @@ -45,8 +47,34 @@ namespace ImageProcessorCore.Tests Assert.True(property1 != property2); Assert.NotEqual(property1, property3); - Assert.True(property1 != property3); + Assert.NotEqual(property1, property4); } + /// + /// Tests whether the constructor throws an exception when the property name is null or empty. + /// + [Fact] + public void ConstructorThrowsWhenNameIsNullOrEmpty() + { + Exception ex = Record.Exception(() => new ImageProperty(null, "Foo")); + Assert.IsType(ex); + + ex = Record.Exception(() => new ImageProperty(string.Empty, "Foo")); + Assert.IsType(ex); + } + + /// + /// Tests whether the constructor correctly assigns properties. + /// + [Fact] + public void ConstructorAssignsProperties() + { + ImageProperty property = new ImageProperty("Foo", null); + Assert.Equal("Foo", property.Name); + Assert.Equal(null, property.Value); + + property = new ImageProperty("Foo", string.Empty); + Assert.Equal(string.Empty, property.Value); + } } }