Additional methods on ParameterCollection to simplify usage

This commit is contained in:
Steve Sanderson 2018-02-22 14:24:23 +00:00
parent 3e30655ea4
commit 58ae5dea9a
2 changed files with 119 additions and 0 deletions

View File

@ -2,6 +2,7 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Collections.Generic;
using Microsoft.AspNetCore.Blazor.RenderTree;
namespace Microsoft.AspNetCore.Blazor.Components
@ -41,6 +42,42 @@ namespace Microsoft.AspNetCore.Blazor.Components
public ParameterEnumerator GetEnumerator()
=> new ParameterEnumerator(_frames, _ownerIndex);
/// <summary>
/// Gets the value of the parameter with the specified name.
/// </summary>
/// <typeparam name="T">The type of the value.</typeparam>
/// <param name="parameterName">The name of the parameter.</param>
/// <param name="result">Receives the result, if any.</param>
/// <returns>True if a matching parameter was found; false otherwise.</returns>
public bool TryGetValue<T>(string parameterName, out T result)
{
foreach (var entry in this)
{
if (string.Equals(entry.Name, parameterName))
{
result = (T)entry.Value;
return true;
}
}
result = default;
return false;
}
/// <summary>
/// Returns a dictionary populated with the contents of the <see cref="ParameterCollection"/>.
/// </summary>
/// <returns>A dictionary populated with the contents of the <see cref="ParameterCollection"/>.</returns>
public IReadOnlyDictionary<string, object> ToDictionary()
{
var result = new Dictionary<string, object>();
foreach (var entry in this)
{
result[entry.Name] = entry.Value;
}
return result;
}
// It's internal because there isn't a known use case for user code comparing
// ParameterCollection instances, and even if there was, it's unlikely it should
// use these equality rules which are designed for their effect on rendering.

View File

@ -85,6 +85,88 @@ namespace Microsoft.AspNetCore.Blazor.Test
AssertParameter("attribute 2", attribute2Value));
}
[Fact]
public void CanTryGetNonExistingValue()
{
// Arrange
var parameterCollection = new ParameterCollection(new[]
{
RenderTreeFrame.Element(0, "some element").WithElementSubtreeLength(2),
RenderTreeFrame.Attribute(1, "some other entry", new object())
}, 0);
// Act
var didFind = parameterCollection.TryGetValue<string>("nonexisting entry", out var value);
// Assert
Assert.False(didFind);
Assert.Null(value);
}
[Fact]
public void CanTryGetExistingValueWithCorrectType()
{
// Arrange
var parameterCollection = new ParameterCollection(new[]
{
RenderTreeFrame.Element(0, "some element").WithElementSubtreeLength(2),
RenderTreeFrame.Attribute(1, "my entry", "hello")
}, 0);
// Act
var didFind = parameterCollection.TryGetValue<string>("my entry", out var value);
// Assert
Assert.True(didFind);
Assert.Equal("hello", value);
}
[Fact]
public void ThrowsIfTryGetExistingValueWithIncorrectType()
{
// Arrange
var parameterCollection = new ParameterCollection(new[]
{
RenderTreeFrame.Element(0, "some element").WithElementSubtreeLength(2),
RenderTreeFrame.Attribute(1, "my entry", "hello")
}, 0);
// Act/Assert
Assert.Throws<InvalidCastException>(() =>
{
parameterCollection.TryGetValue<bool>("my entry", out var value);
});
}
[Fact]
public void CanConvertToReadOnlyDictionary()
{
// Arrange
var entry2Value = new object();
var parameterCollection = new ParameterCollection(new[]
{
RenderTreeFrame.Element(0, "some element").WithElementSubtreeLength(3),
RenderTreeFrame.Attribute(0, "entry 1", "value 1"),
RenderTreeFrame.Attribute(0, "entry 2", entry2Value),
}, 0);
// Act
IReadOnlyDictionary<string, object> dict = parameterCollection.ToDictionary();
// Assert
Assert.Collection(dict,
entry =>
{
Assert.Equal("entry 1", entry.Key);
Assert.Equal("value 1", entry.Value);
},
entry =>
{
Assert.Equal("entry 2", entry.Key);
Assert.Same(entry2Value, entry.Value);
});
}
private Action<Parameter> AssertParameter(string expectedName, object expectedValue)
{
return parameter =>