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
{
using System;
using System.Globalization;
using System.Runtime.Serialization;
/// <summary>
@ -20,50 +19,49 @@ namespace Dapr.Actors
private readonly string stringId;
/// <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>
/// <param name="id">Value for actor id.</param>
public ActorId(string id)
{
this.stringId = id ?? throw new ArgumentNullException("id");
this.stringId = id ?? throw new ArgumentNullException(nameof(id));
}
/// <summary>
/// Determines whether two specified actorIds have the same id.
/// </summary>
/// <param name="x">The first actorId to compare, or null. </param>
/// <param name="y">The second actorId to compare, or null. </param>
/// <param name="id1">The first 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>
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;
}
else if (ReferenceEquals(x, null) || ReferenceEquals(y, null))
else if (id1 is null || id2 is null)
{
return false;
}
else
{
return EqualsContents(x, y);
return EqualsContents(id1, id2);
}
}
/// <summary>
/// Determines whether two specified actorIds have different values for id./>.
/// </summary>
/// <param name="x">The first actorId to compare, or null. </param>
/// <param name="y">The second actorId to compare, or null. </param>
/// <param name="id1">The first 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>
public static bool operator !=(ActorId x, ActorId y)
public static bool operator !=(ActorId id1, ActorId id2)
{
return !(x == y);
return !(id1 == id2);
}
/// <summary>
/// Create a new instance of the <see cref="ActorId"/>./>
/// with a random <see cref="long"/> id value.
/// Create a new instance of the <see cref="ActorId"/> with a random <see cref="long"/> id value.
/// </summary>
/// <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>
@ -79,7 +77,7 @@ namespace Dapr.Actors
}
/// <summary>
/// Gets id./>.
/// Gets id.
/// </summary>
/// <returns><see cref="string"/>The id value for ActorId.</returns>
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,
/// have the same value. Overrides <see cref="object.Equals(object)"/>.
/// </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;
/// otherwise, false. If obj is null, the method returns false.</returns>
public override bool Equals(object obj)
{
if (ReferenceEquals(obj, null))
{
return false;
}
else if (obj.GetType() != typeof(ActorId))
if (obj is null || obj.GetType() != typeof(ActorId))
{
return false;
}
@ -131,13 +125,12 @@ namespace Dapr.Actors
/// <summary>
/// Determines whether this instance and another specified <see cref="ActorId"/> object have the same value.
/// </summary>
/// <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
/// id of this instance; otherwise, false.
/// <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 id of this instance; otherwise, false.
/// If other is null, the method returns false.</returns>
public bool Equals(ActorId other)
{
if (ReferenceEquals(other, null))
if (other is null)
{
return false;
}
@ -151,23 +144,23 @@ namespace Dapr.Actors
/// 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.
/// </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
/// 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>
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
{
using Dapr.Actors.Builder;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Xunit;
/// <summary>
/// Test class for Actor Code builder.
/// </summary>
[TestClass]
public class ActorCodeBuilderTests
{
/// <summary>
/// Tests Proxy Generation.
/// </summary>
[TestMethod]
[Fact]
public void TestBuildActorProxyGenerator()
{
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 Dapr.Actors.Communication;
using FluentAssertions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Xunit;
/// <summary>
/// Contains tests for Actor method invocation exceptions.
/// </summary>
[TestClass]
public class ActorMethodInvocationExceptionTests
{
/// <summary>
@ -22,7 +21,7 @@ namespace Dapr.Actors.Test
/// 1) the path for serialization and deserialization of the remote exception
/// 2) and validating the inner exception.
/// </summary>
[TestMethod]
[Fact]
public void TestThrowActorMethodInvocationException()
{
// Create Remote Actor Method test Exception

View File

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