diff --git a/src/ImageProcessorCore/Image/ImageProperty.cs b/src/ImageProcessorCore/Image/ImageProperty.cs index ef432ada24..7e447c5fca 100644 --- a/src/ImageProcessorCore/Image/ImageProperty.cs +++ b/src/ImageProcessorCore/Image/ImageProperty.cs @@ -12,7 +12,7 @@ namespace ImageProcessorCore /// the copyright information, the date, where the image was created /// or some other information. /// - public struct ImageProperty : IEquatable + public class ImageProperty : IEquatable { /// /// Initializes a new instance of the struct. @@ -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; } @@ -56,7 +58,7 @@ namespace ImageProcessorCore /// public static bool operator ==(ImageProperty left, ImageProperty right) { - return left.Equals(right); + return Equals(left, right); } /// @@ -75,7 +77,7 @@ namespace ImageProcessorCore /// public static bool operator !=(ImageProperty left, ImageProperty right) { - return !left.Equals(right); + return !Equals(left, right); } /// @@ -90,14 +92,9 @@ namespace ImageProcessorCore /// public override bool Equals(object obj) { - if (!(obj is ImageProperty)) - { - return false; - } + ImageProperty other = obj as ImageProperty; - ImageProperty other = (ImageProperty)obj; - - 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; } } @@ -136,7 +137,17 @@ namespace ImageProcessorCore /// An object to compare with this object. public bool Equals(ImageProperty other) { - return this.Name.Equals(other.Name) && this.Value.Equals(other.Value); + if (ReferenceEquals(other, null)) + { + return false; + } + + if (ReferenceEquals(this, other)) + { + return true; + } + + 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 new file mode 100644 index 0000000000..5a82f139de --- /dev/null +++ b/tests/ImageProcessorCore.Tests/Image/ImagePropertyTests.cs @@ -0,0 +1,78 @@ +// +// Copyright (c) James Jackson-South and contributors. +// Licensed under the Apache License, Version 2.0. +// + +namespace ImageProcessorCore.Tests +{ + using System; + using Xunit; + + /// + /// Tests the class. + /// + public class ImagePropertyTests + { + /// + /// Tests the equality operators for inequality. + /// + [Fact] + public void AreEqual() + { + ImageProperty property1 = new ImageProperty("Foo", "Bar"); + ImageProperty property2 = new ImageProperty("Foo", "Bar"); + ImageProperty property3 = null; + + Assert.Equal(property1, property2); + Assert.True(property1 == property2); + Assert.Equal(property3, null); + } + + /// + /// Tests the equality operators for equality. + /// + [Fact] + public void AreNotEqual() + { + 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")); + + Assert.NotEqual(property1, null); + + Assert.NotEqual(property1, property2); + Assert.True(property1 != property2); + + Assert.NotEqual(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() + { + Assert.Throws(() => new ImageProperty(null, "Foo")); + + Assert.Throws(() => new ImageProperty(string.Empty, "Foo")); + } + + /// + /// 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); + } + } +}