diff --git a/src/ImageProcessorCore/Image/ImageProperty.cs b/src/ImageProcessorCore/Image/ImageProperty.cs index ef432ada24..fee4ab516d 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. @@ -56,7 +56,7 @@ namespace ImageProcessorCore /// public static bool operator ==(ImageProperty left, ImageProperty right) { - return left.Equals(right); + return Equals(left, right); } /// @@ -75,7 +75,7 @@ namespace ImageProcessorCore /// public static bool operator !=(ImageProperty left, ImageProperty right) { - return !left.Equals(right); + return !Equals(left, right); } /// @@ -90,13 +90,13 @@ namespace ImageProcessorCore /// public override bool Equals(object obj) { - if (!(obj is ImageProperty)) + ImageProperty other = obj as ImageProperty; + + if (other == null) { return false; } - ImageProperty other = (ImageProperty)obj; - return other.Name == this.Name && other.Value == this.Value; } @@ -136,6 +136,16 @@ namespace ImageProcessorCore /// An object to compare with this object. public bool Equals(ImageProperty other) { + if (ReferenceEquals(other, null)) + { + return false; + } + + if (ReferenceEquals(this, other)) + { + return true; + } + return this.Name.Equals(other.Name) && this.Value.Equals(other.Value); } } diff --git a/tests/ImageProcessorCore.Tests/Image/ImagePropertyTests.cs b/tests/ImageProcessorCore.Tests/Image/ImagePropertyTests.cs new file mode 100644 index 0000000000..bee588e944 --- /dev/null +++ b/tests/ImageProcessorCore.Tests/Image/ImagePropertyTests.cs @@ -0,0 +1,52 @@ +// +// Copyright (c) James Jackson-South and contributors. +// Licensed under the Apache License, Version 2.0. +// + +namespace ImageProcessorCore.Tests +{ + 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"); + + Assert.False(property1.Equals("Foo")); + + Assert.NotEqual(property1, null); + + Assert.NotEqual(property1, property2); + Assert.True(property1 != property2); + + Assert.NotEqual(property1, property3); + Assert.True(property1 != property3); + } + + } +}