From 06e2f0eb8f1ef4c79df027910d7e187dde52626c Mon Sep 17 00:00:00 2001 From: dirk Date: Tue, 9 Aug 2016 20:03:52 +0200 Subject: [PATCH 1/3] 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); + } + + } +} From 40494be867ab894cef1e9eb7736fa27ebec32161 Mon Sep 17 00:00:00 2001 From: dirk Date: Tue, 9 Aug 2016 20:26:44 +0200 Subject: [PATCH 2/3] Guard that the name of a property can not be null or empty. Make sure that Equals and GetHashCode can handle a null value. Former-commit-id: 8a982497a7ec7182a34d41c44d9dc6658ce4de7c Former-commit-id: 7c973c93aca38357e5e2b35cebeac9c10c93296b Former-commit-id: ed06603bf43c266c6619bf81c3a65393f2bf4b2e --- src/ImageProcessorCore/Image/ImageProperty.cs | 17 ++++++----- .../Image/ImagePropertyTests.cs | 30 ++++++++++++++++++- 2 files changed, 38 insertions(+), 9 deletions(-) diff --git a/src/ImageProcessorCore/Image/ImageProperty.cs b/src/ImageProcessorCore/Image/ImageProperty.cs index fee4ab516d..7e447c5fca 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 bee588e944..edd3479afa 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); + } } } From fa90043db8e04a048a3fe64cf7696f446cfc1e42 Mon Sep 17 00:00:00 2001 From: dirk Date: Tue, 9 Aug 2016 20:54:02 +0200 Subject: [PATCH 3/3] Changed throw asserts. Former-commit-id: 85e3d74449bb2b1e4da0c0e523b1449be5f0be08 Former-commit-id: 7a12c94c42fd9dfac0fde12d816170c2de1390e6 Former-commit-id: 4014fcf534e4d21080101ae158bc86c27f2342cc --- tests/ImageProcessorCore.Tests/Image/ImagePropertyTests.cs | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/tests/ImageProcessorCore.Tests/Image/ImagePropertyTests.cs b/tests/ImageProcessorCore.Tests/Image/ImagePropertyTests.cs index edd3479afa..5a82f139de 100644 --- a/tests/ImageProcessorCore.Tests/Image/ImagePropertyTests.cs +++ b/tests/ImageProcessorCore.Tests/Image/ImagePropertyTests.cs @@ -56,11 +56,9 @@ namespace ImageProcessorCore.Tests [Fact] public void ConstructorThrowsWhenNameIsNullOrEmpty() { - Exception ex = Record.Exception(() => new ImageProperty(null, "Foo")); - Assert.IsType(ex); + Assert.Throws(() => new ImageProperty(null, "Foo")); - ex = Record.Exception(() => new ImageProperty(string.Empty, "Foo")); - Assert.IsType(ex); + Assert.Throws(() => new ImageProperty(string.Empty, "Foo")); } ///