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

[Azure.Communication.Common] - Overridden operators for the CommunicationIdentifier #30075

Merged
merged 10 commits into from
Jul 29, 2022
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ protected CommunicationIdentifier() { }
public override bool Equals(object obj) { throw null; }
public static Azure.Communication.CommunicationIdentifier FromRawId(string rawId) { throw null; }
public override int GetHashCode() { throw null; }
public static bool operator ==(Azure.Communication.CommunicationIdentifier left, Azure.Communication.CommunicationIdentifier right) { throw null; }
public static implicit operator Azure.Communication.CommunicationIdentifier (string value) { throw null; }
public static bool operator !=(Azure.Communication.CommunicationIdentifier left, Azure.Communication.CommunicationIdentifier right) { throw null; }
}
public sealed partial class CommunicationTokenCredential : System.IDisposable
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,31 @@ public abstract class CommunicationIdentifier : IEquatable<CommunicationIdentifi
public override bool Equals(object obj)
=> obj is CommunicationIdentifier other && Equals(other);

/// <summary>
/// Overrides the equality operator.
/// </summary>
/// <param name="left">The first identifier to compare.</param>
/// <param name="right">The second identifier to compare.</param>
/// <returns>True if the types and <see cref="RawId"/> match.</returns>
public static bool operator ==(CommunicationIdentifier left, CommunicationIdentifier right)
{
return Equals(left, right) && left.GetType() == right.GetType();
}

/// <summary>
/// Overrides the non-equality operator.
/// </summary>
/// <param name="left">The first identifier to compare.</param>
/// <param name="right">The second identifier to compare.</param>
/// <returns>True if the types or <see cref="RawId"/> values are different.</returns>
public static bool operator !=(CommunicationIdentifier left, CommunicationIdentifier right)
{
return !Equals(left, right) || left.GetType() != right.GetType();
}

/// <summary> Converts a string to a <see cref="CommunicationIdentifier"/>. </summary>
public static implicit operator CommunicationIdentifier(string value) => FromRawId(value);

/// <summary>
/// Creates a <see cref="CommunicationIdentifier"/> from a given rawId.
/// When storing rawIds, use this function to restore the identifier that was encoded in the rawId.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ static void AssertIdentifier(string rawId, CommunicationIdentifier expectedIdent
{
Assert.AreEqual(CommunicationIdentifier.FromRawId(rawId), expectedIdentifier);
Assert.AreEqual(CommunicationIdentifier.FromRawId(rawId).GetHashCode(), expectedIdentifier.GetHashCode());
Assert.IsTrue(CommunicationIdentifier.FromRawId(rawId) == expectedIdentifier);
Assert.IsFalse(CommunicationIdentifier.FromRawId(rawId) != expectedIdentifier);
}

AssertIdentifier("8:acs:bbbcbc1e-9f06-482a-b5d8-20e3f26ef0cd_45ab2481-1c1c-4005-be24-0ffb879b1130", new CommunicationUserIdentifier("8:acs:bbbcbc1e-9f06-482a-b5d8-20e3f26ef0cd_45ab2481-1c1c-4005-be24-0ffb879b1130"));
Expand Down Expand Up @@ -99,6 +101,17 @@ public void RawIdStaysTheSameAfterConversionToIdentifierAndBack()
AssertRoundtrip("28:45ab2481-1c1c-4005-be24-0ffb879b1130");
}

[Test]
public void TestImplicitConversionOperator()
{
CommunicationIdentifier communicationUserIdentifier = "8:acs:bbbcbc1e-9f06-482a-b5d8-20e3f26ef0cd_45ab2481-1c1c-4005-be24-0ffb879b1130";
CommunicationIdentifier microsoftTeamsUserIdentifier = "8:orgid:45ab2481-1c1c-4005-be24-0ffb879b1130";
CommunicationIdentifier phoneNumberIdentifier = "4:112345556789";
Assert.IsInstanceOf(typeof(CommunicationUserIdentifier), communicationUserIdentifier);
Assert.IsInstanceOf(typeof(MicrosoftTeamsUserIdentifier), microsoftTeamsUserIdentifier);
Assert.IsInstanceOf(typeof(PhoneNumberIdentifier), phoneNumberIdentifier);
}

[Test]
public void RawIdIsOverriddenBySubTypes()
{
Expand Down