diff --git a/src/SendGrid/Helpers/Mail/Model/EmailAddress.cs b/src/SendGrid/Helpers/Mail/Model/EmailAddress.cs index a8e67f551..cbf7baafd 100644 --- a/src/SendGrid/Helpers/Mail/Model/EmailAddress.cs +++ b/src/SendGrid/Helpers/Mail/Model/EmailAddress.cs @@ -1,46 +1,131 @@ -// -// Copyright (c) Twilio SendGrid. All rights reserved. -// Licensed under the MIT license. See LICENSE file in the project root for full license information. -// - -using Newtonsoft.Json; - -namespace SendGrid.Helpers.Mail -{ - /// - /// An email object containing the email address and name of the sender or recipient. - /// - [JsonObject(IsReference = false)] - public class EmailAddress - { - /// - /// Initializes a new instance of the class. - /// - public EmailAddress() - { - } - - /// - /// Initializes a new instance of the class. - /// - /// The email address of the sender or recipient. - /// The name of the sender or recipient. - public EmailAddress(string email, string name = null) - { - this.Email = email; - this.Name = name; - } - - /// - /// Gets or sets the name of the sender or recipient. - /// - [JsonProperty(PropertyName = "name")] - public string Name { get; set; } - - /// - /// Gets or sets the email address of the sender or recipient. - /// - [JsonProperty(PropertyName = "email")] - public string Email { get; set; } - } -} +// +// Copyright (c) Twilio SendGrid. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. +// + +using Newtonsoft.Json; +using System; + +namespace SendGrid.Helpers.Mail +{ + /// + /// An email object containing the email address and name of the sender or recipient. + /// + [JsonObject(IsReference = false)] + public class EmailAddress : IEquatable + { + /// + /// Initializes a new instance of the class. + /// + public EmailAddress() + { + } + + /// + /// Initializes a new instance of the class. + /// + /// The email address of the sender or recipient. + /// The name of the sender or recipient. + public EmailAddress(string email, string name = null) + { + this.Email = email; + this.Name = name; + } + + /// + /// Gets or sets the name of the sender or recipient. + /// + [JsonProperty(PropertyName = "name")] + public string Name { get; set; } + + /// + /// Gets or sets the email address of the sender or recipient. + /// + [JsonProperty(PropertyName = "email")] + public string Email { get; set; } + + /// + /// Determines whether the two specified operands are equal. + /// + /// The left hand operand in the equation. + /// The right hand operand in the equation. + /// True if equal, false if not. + public static bool operator ==(EmailAddress left, EmailAddress right) + { + if (left is null && right is null) + { + return true; + } + + return left?.Equals(right) ?? false; + } + + /// + /// Determines whether the two specified operands are not equal. + /// + /// The left hand operand in the equation. + /// The right hand operand in the equation. + /// True if the two operands are not equal, and false if they are. + public static bool operator !=(EmailAddress left, EmailAddress right) + { + return !(left == right); + } + + /// + /// Gets a value indicating whether this is equal to the specified EmailAddress. + /// + /// The comparand email address. + /// true if the objects are equal, false if they're not. + 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); + } + + /// + /// Gets a value indicating whether this is equal to the specified object. + /// + /// The comparand object. + /// true if the objects are equal, false if they're not. + 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); + } + + /// + /// Gets a hash code representing this object. + /// + /// A hash code representing this instance. + public override int GetHashCode() + { + unchecked + { + return (this.Email != null ? this.Email.ToLower().GetHashCode() : 0); + } + } + } +} diff --git a/tests/SendGrid.Tests/Helpers/Mail/EmailAddressTests.cs b/tests/SendGrid.Tests/Helpers/Mail/EmailAddressTests.cs new file mode 100644 index 000000000..bbccf4f19 --- /dev/null +++ b/tests/SendGrid.Tests/Helpers/Mail/EmailAddressTests.cs @@ -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)); + } + } +}