Browse Source

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
af/merge-core
dirk 10 years ago
parent
commit
40494be867
  1. 17
      src/ImageProcessorCore/Image/ImageProperty.cs
  2. 30
      tests/ImageProcessorCore.Tests/Image/ImagePropertyTests.cs

17
src/ImageProcessorCore/Image/ImageProperty.cs

@ -21,6 +21,8 @@ namespace ImageProcessorCore
/// <param name="value">The value of the property.</param>
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);
}
/// <summary>
@ -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);
}
}
}

30
tests/ImageProcessorCore.Tests/Image/ImagePropertyTests.cs

@ -5,6 +5,7 @@
namespace ImageProcessorCore.Tests
{
using System;
using Xunit;
/// <summary>
@ -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);
}
/// <summary>
/// Tests whether the constructor throws an exception when the property name is null or empty.
/// </summary>
[Fact]
public void ConstructorThrowsWhenNameIsNullOrEmpty()
{
Exception ex = Record.Exception(() => new ImageProperty(null, "Foo"));
Assert.IsType<ArgumentNullException>(ex);
ex = Record.Exception(() => new ImageProperty(string.Empty, "Foo"));
Assert.IsType<ArgumentException>(ex);
}
/// <summary>
/// Tests whether the constructor correctly assigns properties.
/// </summary>
[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);
}
}
}

Loading…
Cancel
Save