Browse Source

Add OpenIddictParameter.Count

pull/1018/head
Kévin Chalet 6 years ago
parent
commit
6e3394be1a
  1. 16
      src/OpenIddict.Abstractions/Primitives/OpenIddictParameter.cs
  2. 77
      test/OpenIddict.Abstractions.Tests/Primitives/OpenIddictParameterTests.cs

16
src/OpenIddict.Abstractions/Primitives/OpenIddictParameter.cs

@ -77,6 +77,22 @@ namespace OpenIddict.Abstractions
/// <returns>An <see cref="OpenIddictParameter"/> instance containing the item value.</returns> /// <returns>An <see cref="OpenIddictParameter"/> instance containing the item value.</returns>
public OpenIddictParameter? this[string name] => GetNamedParameter(name); public OpenIddictParameter? this[string name] => GetNamedParameter(name);
/// <summary>
/// Gets the number of unnamed child items contained in the current parameter or
/// <c>0</c> if the parameter doesn't represent an array of strings or a JSON array.
/// </summary>
public int Count => Value switch
{
// If the parameter is a primitive array of strings, return its length.
string[] value => value.Length,
// If the parameter is a JSON array, return its length.
JsonElement value when value.ValueKind == JsonValueKind.Array => value.GetArrayLength(),
// Otherwise, return 0.
_ => 0
};
/// <summary> /// <summary>
/// Gets the associated value, that can be either a primitive CLR type /// Gets the associated value, that can be either a primitive CLR type
/// (e.g bool, string, long), an array of strings or a complex JSON object. /// (e.g bool, string, long), an array of strings or a complex JSON object.

77
test/OpenIddict.Abstractions.Tests/Primitives/OpenIddictParameterTests.cs

@ -9,7 +9,6 @@ using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Text.Encodings.Web;
using System.Text.Json; using System.Text.Json;
using Xunit; using Xunit;
@ -17,6 +16,82 @@ namespace OpenIddict.Abstractions.Tests.Primitives
{ {
public class OpenIddictParameterTests public class OpenIddictParameterTests
{ {
[Fact]
public void Count_ReturnsZeroForNullValue()
{
// Arrange
var parameter = new OpenIddictParameter();
// Act and assert
Assert.Equal(0, parameter.Count);
}
[Fact]
public void Count_ReturnsZeroForBoolean()
{
// Arrange
var parameter = new OpenIddictParameter(true);
// Act and assert
Assert.Equal(0, parameter.Count);
}
[Fact]
public void Count_ReturnsZeroForInteger()
{
// Arrange
var parameter = new OpenIddictParameter(42);
// Act and assert
Assert.Equal(0, parameter.Count);
}
[Fact]
public void Count_ReturnsZeroForString()
{
// Arrange
var parameter = new OpenIddictParameter("Fabrikam");
// Act and assert
Assert.Equal(0, parameter.Count);
}
[Fact]
public void Count_ReturnsExpectedValueForArray()
{
// Arrange
var parameter = new OpenIddictParameter(new[]
{
"Fabrikam",
"Contoso"
});
// Act and assert
Assert.Equal(2, parameter.Count);
}
[Fact]
public void Count_ReturnsExpectedValueForJsonArray()
{
// Arrange
var parameter = new OpenIddictParameter(
JsonSerializer.Deserialize<JsonElement>(@"[""Fabrikam"",""Contoso""]"));
// Act and assert
Assert.Equal(2, parameter.Count);
}
[Fact]
public void Count_ReturnsZeroForJsonObjects()
{
// Arrange
var parameter = new OpenIddictParameter(
JsonSerializer.Deserialize<JsonElement>(@"{""parameter"":""value""}"));
// Act and assert
Assert.Equal(0, parameter.Count);
}
[Fact] [Fact]
public void Equals_ReturnsTrueWhenBothParametersAreNull() public void Equals_ReturnsTrueWhenBothParametersAreNull()
{ {

Loading…
Cancel
Save