Add ParameterCollection.GetValueOrDefault methods
This commit is contained in:
parent
a88ab0db49
commit
d921705881
|
|
@ -64,6 +64,27 @@ namespace Microsoft.AspNetCore.Blazor.Components
|
|||
return false;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the value of the parameter with the specified name, or a default value
|
||||
/// if no such parameter exists in the collection.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of the value.</typeparam>
|
||||
/// <param name="parameterName">The name of the parameter.</param>
|
||||
/// <returns>The parameter value if found; otherwise the default value for the specified type.</returns>
|
||||
public T GetValueOrDefault<T>(string parameterName)
|
||||
=> GetValueOrDefault<T>(parameterName, default);
|
||||
|
||||
/// <summary>
|
||||
/// Gets the value of the parameter with the specified name, or a specified default value
|
||||
/// if no such parameter exists in the collection.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of the value.</typeparam>
|
||||
/// <param name="parameterName">The name of the parameter.</param>
|
||||
/// <param name="defaultValue">The default value to return if no such parameter exists in the collection.</param>
|
||||
/// <returns>The parameter value if found; otherwise <paramref name="defaultValue"/>.</returns>
|
||||
public T GetValueOrDefault<T>(string parameterName, T defaultValue)
|
||||
=> TryGetValue<T>(parameterName, out T result) ? result : defaultValue;
|
||||
|
||||
/// <summary>
|
||||
/// Returns a dictionary populated with the contents of the <see cref="ParameterCollection"/>.
|
||||
/// </summary>
|
||||
|
|
|
|||
|
|
@ -121,6 +121,60 @@ namespace Microsoft.AspNetCore.Blazor.Test
|
|||
Assert.Equal("hello", value);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void CanGetValueOrDefault_WithExistingValue()
|
||||
{
|
||||
// Arrange
|
||||
var myEntryValue = new object();
|
||||
var parameterCollection = new ParameterCollection(new[]
|
||||
{
|
||||
RenderTreeFrame.Element(0, "some element").WithElementSubtreeLength(2),
|
||||
RenderTreeFrame.Attribute(1, "my entry", myEntryValue),
|
||||
RenderTreeFrame.Attribute(1, "my other entry", new object())
|
||||
}, 0);
|
||||
|
||||
// Act
|
||||
var result = parameterCollection.GetValueOrDefault<object>("my entry");
|
||||
|
||||
// Assert
|
||||
Assert.Same(myEntryValue, result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void CanGetValueOrDefault_WithNonExistingValue()
|
||||
{
|
||||
// Arrange
|
||||
var parameterCollection = new ParameterCollection(new[]
|
||||
{
|
||||
RenderTreeFrame.Element(0, "some element").WithElementSubtreeLength(2),
|
||||
RenderTreeFrame.Attribute(1, "some other entry", new object())
|
||||
}, 0);
|
||||
|
||||
// Act
|
||||
var result = parameterCollection.GetValueOrDefault<DateTime>("nonexisting entry");
|
||||
|
||||
// Assert
|
||||
Assert.Equal(default, result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void CanGetValueOrDefault_WithNonExistingValueAndExplicitDefault()
|
||||
{
|
||||
// Arrange
|
||||
var explicitDefaultValue = new DateTime(2018, 3, 20);
|
||||
var parameterCollection = new ParameterCollection(new[]
|
||||
{
|
||||
RenderTreeFrame.Element(0, "some element").WithElementSubtreeLength(2),
|
||||
RenderTreeFrame.Attribute(1, "some other entry", new object())
|
||||
}, 0);
|
||||
|
||||
// Act
|
||||
var result = parameterCollection.GetValueOrDefault("nonexisting entry", explicitDefaultValue);
|
||||
|
||||
// Assert
|
||||
Assert.Equal(explicitDefaultValue, result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ThrowsIfTryGetExistingValueWithIncorrectType()
|
||||
{
|
||||
|
|
|
|||
Loading…
Reference in New Issue