From 06e2f0eb8f1ef4c79df027910d7e187dde52626c Mon Sep 17 00:00:00 2001 From: dirk Date: Tue, 9 Aug 2016 20:03:52 +0200 Subject: [PATCH] ImageProperty is no longer a struct and added unit tests for equality. Former-commit-id: e140d562a45ef0dc7fa3e1e5dfc3ec330040b696 Former-commit-id: 9256abfb4eca778a2051d32edc198979bd359de2 Former-commit-id: 0076f821040d894bb10e923c4a5a370932eae48b --- src/ImageProcessorCore/Image/ImageProperty.cs | 22 +++++--- .../Image/ImagePropertyTests.cs | 52 +++++++++++++++++++ 2 files changed, 68 insertions(+), 6 deletions(-) create mode 100644 tests/ImageProcessorCore.Tests/Image/ImagePropertyTests.cs 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); + } + + } +}