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

JonathanCrd 5003 - comparison methods for ResourceIdentifier #20456

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
Original file line number Diff line number Diff line change
Expand Up @@ -446,5 +446,27 @@ public override int GetHashCode()
/// </summary>
/// <param name="id"> The resource identifier. </param>
public static implicit operator string(ResourceIdentifier id) => id?.ToString();

/// <summary>
/// Operator overloading for '=='.
/// </summary>
/// <param name="id1">Left ResourceIdentifier object to compare.</param>
/// <param name="id2">Right ResourceIdentifier object to compare.</param>
/// <returns></returns>
public static bool operator ==(ResourceIdentifier id1, ResourceIdentifier id2)
{
return ResourceIdentifier.Equals(id1,id2);
}

/// <summary>
/// Operator overloading for '!='.
/// </summary>
/// <param name="id1">Left ResourceIdentifier object to compare.</param>
/// <param name="id2">Right ResourceIdentifier object to compare.</param>
/// <returns></returns>
public static bool operator !=(ResourceIdentifier id1, ResourceIdentifier id2)
{
return !ResourceIdentifier.Equals(id1,id2);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ public void CheckHashCode(bool expected, string resourceId1, string resourceId2)
[TestCase(ChildResourceId, TrackedResourceId, false)]
[TestCase(TrackedResourceId, null, false)]
[TestCase(null, TrackedResourceId, false)]
public void Equals(string resourceProviderID1, string resourceProviderID2, bool expected)
public void EqualsToResourceIdentifier(string resourceProviderID1, string resourceProviderID2, bool expected)
{
ResourceIdentifier a = resourceProviderID1;
ResourceIdentifier b = resourceProviderID2;
Expand All @@ -268,6 +268,22 @@ public void Equals(string resourceProviderID1, string resourceProviderID2, bool
Assert.AreEqual(expected, ResourceIdentifier.Equals(a,b));
}

[TestCase(TrackedResourceId, TrackedResourceId, true)]
[TestCase(ChildResourceId, ChildResourceId, true)]
[TestCase(null, null, true)]
[TestCase(TrackedResourceId, ChildResourceId, false)]
[TestCase(ChildResourceId, TrackedResourceId, false)]
[TestCase(TrackedResourceId, null, false)]
[TestCase(null, TrackedResourceId, false)]
public void EqualsToString(string resourceProviderID1, string resourceProviderID2, bool expected)
{
ResourceIdentifier a = resourceProviderID1;
if (a != null)
Assert.AreEqual(expected, a.Equals(resourceProviderID2));

Assert.AreEqual(expected, ResourceIdentifier.Equals(a, resourceProviderID2));
}

[Test]
public void EqualsObj()
{
Expand Down Expand Up @@ -637,5 +653,61 @@ public void TestAppendLocationChildResource(string resourceId, string childTypeN
Assert.AreEqual(expected, resource.AppendChildResource(childTypeName, childResourceName).ToString());
}
}

[TestCase(TrackedResourceId, TrackedResourceId, true, "object")]
[TestCase(ChildResourceId, ChildResourceId, true, "object")]
[TestCase(null, null, true, "object")]
[TestCase(TrackedResourceId, ChildResourceId, false, "object")]
[TestCase(ChildResourceId, TrackedResourceId, false, "object")]
[TestCase(TrackedResourceId, null, false, "object")]
[TestCase(null, TrackedResourceId, false, "object")]
[TestCase(TrackedResourceId, TrackedResourceId, true, "string")]
[TestCase(ChildResourceId, ChildResourceId, true, "string")]
[TestCase(null, null, true, "string")]
[TestCase(TrackedResourceId, ChildResourceId, false, "string")]
[TestCase(ChildResourceId, TrackedResourceId, false, "string")]
[TestCase(TrackedResourceId, null, false, "string")]
[TestCase(null, TrackedResourceId, false, "string")]
public void EqualsOperator(string resourceProviderID1, string resourceProviderID2, bool expected, string comparisonType)
{
ResourceIdentifier a = resourceProviderID1;
if(comparisonType == "object")
{
ResourceIdentifier b = resourceProviderID2;
Assert.AreEqual(expected, a == b);
}
else
{
Assert.AreEqual(expected, a == resourceProviderID2);
}
}

[TestCase(TrackedResourceId, TrackedResourceId, false, "object")]
[TestCase(ChildResourceId, ChildResourceId, false, "object")]
[TestCase(null, null, false, "object")]
[TestCase(TrackedResourceId, ChildResourceId, true, "object")]
[TestCase(ChildResourceId, TrackedResourceId, true, "object")]
[TestCase(TrackedResourceId, null, true, "object")]
[TestCase(null, TrackedResourceId, true, "object")]
[TestCase(TrackedResourceId, TrackedResourceId, false, "string")]
[TestCase(ChildResourceId, ChildResourceId, false, "string")]
[TestCase(null, null, false, "string")]
[TestCase(TrackedResourceId, ChildResourceId, true, "string")]
[TestCase(ChildResourceId, TrackedResourceId, true, "string")]
[TestCase(TrackedResourceId, null, true, "string")]
[TestCase(null, TrackedResourceId, true, "string")]
public void NotEqualsOperator(string resourceProviderID1, string resourceProviderID2, bool expected, string comparisonType)
{
ResourceIdentifier a = resourceProviderID1;
if (comparisonType == "object")
{
ResourceIdentifier b = resourceProviderID2;
Assert.AreEqual(expected, a != b);
}
else
{
Assert.AreEqual(expected, a != resourceProviderID2);
}
}
}
}