Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Implement IEquatable in EmailAddress #905

Merged
merged 6 commits into from
Apr 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
177 changes: 131 additions & 46 deletions src/SendGrid/Helpers/Mail/Model/EmailAddress.cs
Original file line number Diff line number Diff line change
@@ -1,46 +1,131 @@
// <copyright file="EmailAddress.cs" company="Twilio SendGrid">
// Copyright (c) Twilio SendGrid. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
// </copyright>

using Newtonsoft.Json;

namespace SendGrid.Helpers.Mail
{
/// <summary>
/// An email object containing the email address and name of the sender or recipient.
/// </summary>
[JsonObject(IsReference = false)]
public class EmailAddress
{
/// <summary>
/// Initializes a new instance of the <see cref="EmailAddress"/> class.
/// </summary>
public EmailAddress()
{
}

/// <summary>
/// Initializes a new instance of the <see cref="EmailAddress"/> class.
/// </summary>
/// <param name="email">The email address of the sender or recipient.</param>
/// <param name="name">The name of the sender or recipient.</param>
public EmailAddress(string email, string name = null)
{
this.Email = email;
this.Name = name;
}

/// <summary>
/// Gets or sets the name of the sender or recipient.
/// </summary>
[JsonProperty(PropertyName = "name")]
public string Name { get; set; }

/// <summary>
/// Gets or sets the email address of the sender or recipient.
/// </summary>
[JsonProperty(PropertyName = "email")]
public string Email { get; set; }
}
}
// <copyright file="EmailAddress.cs" company="Twilio SendGrid">
// Copyright (c) Twilio SendGrid. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
// </copyright>

using Newtonsoft.Json;
using System;

namespace SendGrid.Helpers.Mail
{
/// <summary>
/// An email object containing the email address and name of the sender or recipient.
/// </summary>
[JsonObject(IsReference = false)]
public class EmailAddress : IEquatable<EmailAddress>
{
/// <summary>
/// Initializes a new instance of the <see cref="EmailAddress"/> class.
/// </summary>
public EmailAddress()
{
}

/// <summary>
/// Initializes a new instance of the <see cref="EmailAddress"/> class.
/// </summary>
/// <param name="email">The email address of the sender or recipient.</param>
/// <param name="name">The name of the sender or recipient.</param>
public EmailAddress(string email, string name = null)
{
this.Email = email;
this.Name = name;
}

/// <summary>
/// Gets or sets the name of the sender or recipient.
/// </summary>
[JsonProperty(PropertyName = "name")]
public string Name { get; set; }

/// <summary>
/// Gets or sets the email address of the sender or recipient.
/// </summary>
[JsonProperty(PropertyName = "email")]
public string Email { get; set; }

/// <summary>
/// Determines whether the two specified operands are equal.
/// </summary>
/// <param name="left">The left hand operand in the equation.</param>
/// <param name="right">The right hand operand in the equation.</param>
/// <returns>True if equal, false if not.</returns>
public static bool operator ==(EmailAddress left, EmailAddress right)
{
if (left is null && right is null)
{
return true;
}

return left?.Equals(right) ?? false;
}

/// <summary>
/// Determines whether the two specified operands are not equal.
/// </summary>
/// <param name="left">The left hand operand in the equation.</param>
/// <param name="right">The right hand operand in the equation.</param>
/// <returns>True if the two operands are not equal, and false if they are.</returns>
public static bool operator !=(EmailAddress left, EmailAddress right)
{
return !(left == right);
}

/// <summary>
/// Gets a value indicating whether this <see cref="EmailAddress"/> is equal to the specified EmailAddress.
/// </summary>
/// <param name="other">The comparand email address.</param>
/// <returns>true if the objects are equal, false if they're not.</returns>
public bool Equals(EmailAddress other)
{
if (other is null)
{
return false;
}

if (ReferenceEquals(this, other))
{
return true;
}

return string.Equals(this.Email, other.Email, StringComparison.OrdinalIgnoreCase);
}

/// <summary>
/// Gets a value indicating whether this <see cref="EmailAddress"/> is equal to the specified object.
/// </summary>
/// <param name="obj">The comparand object.</param>
/// <returns>true if the objects are equal, false if they're not.</returns>
public override bool Equals(object obj)
{
if (obj is null)
{
return false;
}

if (ReferenceEquals(this, obj))
{
return true;
}

if (obj.GetType() != this.GetType())
{
return false;
}

return this.Equals((EmailAddress)obj);
}

/// <summary>
/// Gets a hash code representing this object.
/// </summary>
/// <returns>A hash code representing this instance.</returns>
public override int GetHashCode()
{
unchecked
{
return (this.Email != null ? this.Email.ToLower().GetHashCode() : 0);
}
}
}
}
22 changes: 22 additions & 0 deletions tests/SendGrid.Tests/Helpers/Mail/EmailAddressTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using SendGrid.Helpers.Mail;
using Xunit;

namespace SendGrid.Tests.Helpers.Mail
{
public class EmailAddressTests
{
[Fact]
public void TestEmailAddressEquality()
{
var left = new EmailAddress("test1@sendgrid.com", "test");
var right = new EmailAddress("test1@sendGrid.com", "Test");
var up = new EmailAddress("test2@sendgrid.com", "test");
var down = new EmailAddress("test2@sendgrid.com", "Test");

Assert.True(left.Equals(left));
Assert.True(left.Equals(right));
Assert.False(left.Equals(up));
Assert.False(left.Equals(down));
}
}
}