Clean ActorId (#121)

* Remove unused using

* Updated parameter names

* Summary spacing and extra text

* Checking for null

* Added ActorId tests and convert to xUnit test framework
This commit is contained in:
Greg Ingram 2019-10-21 22:25:18 -04:00 committed by Aman Bhardwaj
parent 02dd7eb814
commit eba332e911
5 changed files with 231 additions and 41 deletions

View File

@ -6,7 +6,6 @@
namespace Dapr.Actors namespace Dapr.Actors
{ {
using System; using System;
using System.Globalization;
using System.Runtime.Serialization; using System.Runtime.Serialization;
/// <summary> /// <summary>
@ -20,50 +19,49 @@ namespace Dapr.Actors
private readonly string stringId; private readonly string stringId;
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="ActorId"/> class with Id value of type <see cref="string"/>. /// Initializes a new instance of the <see cref="ActorId"/> class with id value of type <see cref="string"/>.
/// </summary> /// </summary>
/// <param name="id">Value for actor id.</param> /// <param name="id">Value for actor id.</param>
public ActorId(string id) public ActorId(string id)
{ {
this.stringId = id ?? throw new ArgumentNullException("id"); this.stringId = id ?? throw new ArgumentNullException(nameof(id));
} }
/// <summary> /// <summary>
/// Determines whether two specified actorIds have the same id. /// Determines whether two specified actorIds have the same id.
/// </summary> /// </summary>
/// <param name="x">The first actorId to compare, or null. </param> /// <param name="id1">The first actorId to compare, or null.</param>
/// <param name="y">The second actorId to compare, or null. </param> /// <param name="id2">The second actorId to compare, or null.</param>
/// <returns>true if the id is same for both objects; otherwise, false.</returns> /// <returns>true if the id is same for both objects; otherwise, false.</returns>
public static bool operator ==(ActorId x, ActorId y) public static bool operator ==(ActorId id1, ActorId id2)
{ {
if (ReferenceEquals(x, null) && ReferenceEquals(y, null)) if (id1 is null && id2 is null)
{ {
return true; return true;
} }
else if (ReferenceEquals(x, null) || ReferenceEquals(y, null)) else if (id1 is null || id2 is null)
{ {
return false; return false;
} }
else else
{ {
return EqualsContents(x, y); return EqualsContents(id1, id2);
} }
} }
/// <summary> /// <summary>
/// Determines whether two specified actorIds have different values for id./>. /// Determines whether two specified actorIds have different values for id./>.
/// </summary> /// </summary>
/// <param name="x">The first actorId to compare, or null. </param> /// <param name="id1">The first actorId to compare, or null.</param>
/// <param name="y">The second actorId to compare, or null. </param> /// <param name="id2">The second actorId to compare, or null.</param>
/// <returns>true if the id is different for both objects; otherwise, true.</returns> /// <returns>true if the id is different for both objects; otherwise, true.</returns>
public static bool operator !=(ActorId x, ActorId y) public static bool operator !=(ActorId id1, ActorId id2)
{ {
return !(x == y); return !(id1 == id2);
} }
/// <summary> /// <summary>
/// Create a new instance of the <see cref="ActorId"/>./> /// Create a new instance of the <see cref="ActorId"/> with a random <see cref="long"/> id value.
/// with a random <see cref="long"/> id value.
/// </summary> /// </summary>
/// <returns>A new ActorId object.</returns> /// <returns>A new ActorId object.</returns>
/// <remarks>This method is thread-safe and generates a new random <see cref="ActorId"/> every time it is called.</remarks> /// <remarks>This method is thread-safe and generates a new random <see cref="ActorId"/> every time it is called.</remarks>
@ -79,7 +77,7 @@ namespace Dapr.Actors
} }
/// <summary> /// <summary>
/// Gets id./>. /// Gets id.
/// </summary> /// </summary>
/// <returns><see cref="string"/>The id value for ActorId.</returns> /// <returns><see cref="string"/>The id value for ActorId.</returns>
public string GetId() public string GetId()
@ -109,16 +107,12 @@ namespace Dapr.Actors
/// Determines whether this instance and a specified object, which must also be a <see cref="ActorId"/> object, /// Determines whether this instance and a specified object, which must also be a <see cref="ActorId"/> object,
/// have the same value. Overrides <see cref="object.Equals(object)"/>. /// have the same value. Overrides <see cref="object.Equals(object)"/>.
/// </summary> /// </summary>
/// <param name="obj">The actorId to compare to this instance. </param> /// <param name="obj">The actorId to compare to this instance.</param>
/// <returns>true if obj is a <see cref="ActorId"/> and its value is the same as this instance; /// <returns>true if obj is a <see cref="ActorId"/> and its value is the same as this instance;
/// otherwise, false. If obj is null, the method returns false.</returns> /// otherwise, false. If obj is null, the method returns false.</returns>
public override bool Equals(object obj) public override bool Equals(object obj)
{ {
if (ReferenceEquals(obj, null)) if (obj is null || obj.GetType() != typeof(ActorId))
{
return false;
}
else if (obj.GetType() != typeof(ActorId))
{ {
return false; return false;
} }
@ -131,13 +125,12 @@ namespace Dapr.Actors
/// <summary> /// <summary>
/// Determines whether this instance and another specified <see cref="ActorId"/> object have the same value. /// Determines whether this instance and another specified <see cref="ActorId"/> object have the same value.
/// </summary> /// </summary>
/// <param name="other">The actorId to compare to this instance. </param> /// <param name="other">The actorId to compare to this instance.</param>
/// <returns>true if the id of the other parameter is the same as the /// <returns>true if the id of the other parameter is the same as the id of this instance; otherwise, false.
/// id of this instance; otherwise, false.
/// If other is null, the method returns false.</returns> /// If other is null, the method returns false.</returns>
public bool Equals(ActorId other) public bool Equals(ActorId other)
{ {
if (ReferenceEquals(other, null)) if (other is null)
{ {
return false; return false;
} }
@ -151,23 +144,23 @@ namespace Dapr.Actors
/// Compares this instance with a specified <see cref="ActorId"/> object and indicates whether this /// Compares this instance with a specified <see cref="ActorId"/> object and indicates whether this
/// instance precedes, follows, or appears in the same position in the sort order as the specified actorId. /// instance precedes, follows, or appears in the same position in the sort order as the specified actorId.
/// </summary> /// </summary>
/// <param name="other">The actorId to compare with this instance. </param> /// <param name="other">The actorId to compare with this instance.</param>
/// <returns>A 32-bit signed integer that indicates whether this instance precedes, follows, or appears /// <returns>A 32-bit signed integer that indicates whether this instance precedes, follows, or appears
/// in the same position in the sort order as the other parameter.</returns> /// in the same position in the sort order as the other parameter.</returns>
/// <remarks>The comparison is done based on the id if both the instances.</remarks> /// <remarks>The comparison is done based on the id if both the instances.</remarks>
public int CompareTo(ActorId other) public int CompareTo(ActorId other)
{ {
return ReferenceEquals(other, null) ? 1 : CompareContents(this, other); return other is null ? 1 : CompareContents(this, other);
} }
private static bool EqualsContents(ActorId x, ActorId y) private static bool EqualsContents(ActorId id1, ActorId id2)
{ {
return string.Equals(x.stringId, y.stringId, StringComparison.OrdinalIgnoreCase); return string.Equals(id1.stringId, id2.stringId, StringComparison.OrdinalIgnoreCase);
} }
private static int CompareContents(ActorId x, ActorId y) private static int CompareContents(ActorId id1, ActorId id2)
{ {
return string.Compare(x.stringId, y.stringId, StringComparison.OrdinalIgnoreCase); return string.Compare(id1.stringId, id2.stringId, StringComparison.OrdinalIgnoreCase);
} }
} }
} }

View File

@ -6,18 +6,17 @@
namespace Dapr.Actors.Test namespace Dapr.Actors.Test
{ {
using Dapr.Actors.Builder; using Dapr.Actors.Builder;
using Microsoft.VisualStudio.TestTools.UnitTesting; using Xunit;
/// <summary> /// <summary>
/// Test class for Actor Code builder. /// Test class for Actor Code builder.
/// </summary> /// </summary>
[TestClass]
public class ActorCodeBuilderTests public class ActorCodeBuilderTests
{ {
/// <summary> /// <summary>
/// Tests Proxy Generation. /// Tests Proxy Generation.
/// </summary> /// </summary>
[TestMethod] [Fact]
public void TestBuildActorProxyGenerator() public void TestBuildActorProxyGenerator()
{ {
ActorProxyGenerator proxyGenerator = ActorCodeBuilder.GetOrCreateProxyGenerator(typeof(ITestActor)); ActorProxyGenerator proxyGenerator = ActorCodeBuilder.GetOrCreateProxyGenerator(typeof(ITestActor));

View File

@ -0,0 +1,196 @@
// ------------------------------------------------------------
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
// ------------------------------------------------------------
namespace Dapr.Actors.Test
{
using System;
using System.Collections.Generic;
using Xunit;
/// <summary>
/// Contains tests for Actor ID.
/// </summary>
public class ActorIdTests
{
public static readonly IEnumerable<object[]> CompareToValues = new List<object[]>
{
new object[]
{
new ActorId("1"),
null,
1,
},
new object[]
{
new ActorId("1"),
new ActorId("1"),
0,
},
new object[]
{
new ActorId("1"),
new ActorId("2"),
-1,
},
new object[]
{
new ActorId("2"),
new ActorId("1"),
1,
},
};
public static readonly IEnumerable<object[]> EqualsValues = new List<object[]>
{
new object[]
{
new ActorId("1"),
null,
false,
},
new object[]
{
new ActorId("1"),
new ActorId("1"),
true,
},
new object[]
{
new ActorId("1"),
new ActorId("2"),
false,
},
};
public static readonly IEnumerable<object[]> EqualsOperatorValues = new List<object[]>
{
new object[]
{
null,
null,
true,
},
new object[]
{
new ActorId("1"),
null,
false,
},
new object[]
{
null,
new ActorId("1"),
false,
},
new object[]
{
new ActorId("1"),
new ActorId("1"),
true,
},
new object[]
{
new ActorId("1"),
new ActorId("2"),
false,
},
};
/// <summary>
/// Throw exception if id is null.
/// </summary>
[Fact]
public void Initialize_New_ActorId_Object_With_Null_Id()
{
Assert.Throws<ArgumentNullException>(() =>
{
ActorId actorId = new ActorId(null);
});
}
[Theory]
[InlineData("one")]
[InlineData("123")]
public void Get_Id(string id)
{
ActorId actorId = new ActorId(id);
Assert.Equal(id, actorId.GetId());
}
[Theory]
[InlineData("one")]
[InlineData("123")]
public void Verify_ToString(string id)
{
ActorId actorId = new ActorId(id);
Assert.Equal(id, actorId.ToString());
}
/// <summary>
/// Verify Equals method by comparing two actorIds.
/// </summary>
/// <param name="id1">The first actorId to compare.</param>
/// <param name="id2">The second actorId to compare, or null.</param>
/// <param name="expectedValue">Expected value from comparison.</param>
[Theory]
[MemberData(nameof(EqualsValues))]
public void Verify_Equals_By_Object(object id1, object id2, bool expectedValue)
{
Assert.Equal(expectedValue, id1.Equals(id2));
}
/// <summary>
/// Verify Equals method by comparing two actorIds.
/// </summary>
/// <param name="id1">The first actorId to compare.</param>
/// <param name="id2">The second actorId to compare, or null.</param>
/// <param name="expectedValue">Expected value from comparison.</param>
[Theory]
[MemberData(nameof(EqualsValues))]
public void Verify_Equals_By_ActorId(ActorId id1, ActorId id2, bool expectedValue)
{
Assert.Equal(expectedValue, id1.Equals(id2));
}
/// <summary>
/// Verify equals operator by comparing two actorIds.
/// </summary>
/// <param name="id1">The first actorId to compare, or null.</param>
/// <param name="id2">The second actorId to compare, or null.</param>
/// <param name="expectedValue">Expected value from comparison.</param>
[Theory]
[MemberData(nameof(EqualsOperatorValues))]
public void Verify_Equals_Operator(ActorId id1, ActorId id2, bool expectedValue)
{
Assert.Equal(expectedValue, id1 == id2);
}
/// <summary>
/// Verify not equals operator by comparing two actorIds.
/// </summary>
/// <param name="id1">The first actorId to compare, or null.</param>
/// <param name="id2">The second actorId to compare, or null.</param>
/// <param name="expectedValue">Expected value from comparison.</param>
[Theory]
[MemberData(nameof(EqualsOperatorValues))]
public void Verify_Not_Equals_Operator(ActorId id1, ActorId id2, bool expectedValue)
{
Assert.Equal(!expectedValue, id1 != id2);
}
/// <summary>
/// Verify CompareTo method by comparing two actorIds.
/// </summary>
/// <param name="id1">The first actorId to compare.</param>
/// <param name="id2">The second actorId to compare, or null.</param>
/// <param name="expectedValue">Expected value from comparison.</param>
[Theory]
[MemberData(nameof(CompareToValues))]
public void Verify_CompareTo(ActorId id1, ActorId id2, int expectedValue)
{
Assert.Equal(expectedValue, id1.CompareTo(id2));
}
}
}

View File

@ -9,12 +9,11 @@ namespace Dapr.Actors.Test
using System.IO; using System.IO;
using Dapr.Actors.Communication; using Dapr.Actors.Communication;
using FluentAssertions; using FluentAssertions;
using Microsoft.VisualStudio.TestTools.UnitTesting; using Xunit;
/// <summary> /// <summary>
/// Contains tests for Actor method invocation exceptions. /// Contains tests for Actor method invocation exceptions.
/// </summary> /// </summary>
[TestClass]
public class ActorMethodInvocationExceptionTests public class ActorMethodInvocationExceptionTests
{ {
/// <summary> /// <summary>
@ -22,7 +21,7 @@ namespace Dapr.Actors.Test
/// 1) the path for serialization and deserialization of the remote exception /// 1) the path for serialization and deserialization of the remote exception
/// 2) and validating the inner exception. /// 2) and validating the inner exception.
/// </summary> /// </summary>
[TestMethod] [Fact]
public void TestThrowActorMethodInvocationException() public void TestThrowActorMethodInvocationException()
{ {
// Create Remote Actor Method test Exception // Create Remote Actor Method test Exception

View File

@ -5,9 +5,12 @@
<ItemGroup> <ItemGroup>
<PackageReference Include="FluentAssertions" Version="5.9.0" /> <PackageReference Include="FluentAssertions" Version="5.9.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.9.0" /> <PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.3.0" />
<PackageReference Include="MSTest.TestAdapter" Version="1.3.2" /> <PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="MSTest.TestFramework" Version="1.3.2" /> <PackageReference Include="xunit.runner.visualstudio" Version="2.4.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>