我一直在构建一个扩展库,并且我使用了在http://www.extensionmethod.net上找到的一个很好的扩展方法来包含。在我的单元测试中(使用 NUnit 1.5.2),我遇到了一个有趣的问题。首先,让我们看一下代码:
/// <summary>
/// Groups and aggregates the sequence of elements.
/// </summary>
/// <typeparam name="TSource">The source type in the sequence.</typeparam>
/// <typeparam name="TFirstKey">The first key type to group by.</typeparam>
/// <typeparam name="TSecondKey">The second key type to rotate by.</typeparam>
/// <typeparam name="TValue">The type of value that will be aggregated.</typeparam>
/// <param name="source">The source sequence.</param>
/// <param name="firstKeySelector">The first key selector.</param>
/// <param name="secondKeySelector">The second key selector.</param>
/// <param name="aggregator">The aggregating function.</param>
/// <returns>A <see cref="Dictionary{TKey,TValue}" /> representing the pivoted data.</returns>
public static Dictionary<TFirstKey, Dictionary<TSecondKey, TValue>> Pivot<TSource, TFirstKey, TSecondKey, TValue>
(this IEnumerable<TSource> source,
Func<TSource, TFirstKey> firstKeySelector,
Func<TSource, TSecondKey> secondKeySelector,
Func<IEnumerable<TSource>, TValue> aggregator)
{
return source.GroupBy(firstKeySelector).Select(
x => new
{
X = x.Key,
Y = x.GroupBy(secondKeySelector).Select(
z => new { Z = z.Key, V = aggregator(z) }).ToDictionary(e => e.Z, o => o.V)
}).ToDictionary(e => e.X, o => o.Y);
}
该函数的作用是接收一个 TSource 类型的 IEnumerable,并将项目转入字典,并使用您定义的任何函数聚合项目。我的样本数据集是一个人数组(在一个名为 Person 的类型中)。
private static readonly Person[] people =
new[]
{
new Person { Forename = "Matt", Surname = "Someone", Email = "matthew@somewhere.com", Age = 25, IsMale = true },
new Person { Forename = "Chris", Surname = "Someone", Email = "chris@somewhere.com", Age = 28, IsMale = false },
new Person { Forename = "Andy", Surname = "Someone", Email = "andy@somewhere.com", Age = 30, IsMale = true },
new Person { Forename = "Joel", Surname = "Someone", Email = "joel@somewhere.com", Age = 30, IsMale = true },
new Person { Forename = "Paul", Surname = "Someone", Email = "paul@somewhere.com", Age = 30, IsMale = true }
};
最后,我们进行测试:
/// <summary>
/// Performs a pivot function on the sample array.
/// </summary>
[Test]
public void Pivot()
{
/* Our sample data is an array of Person instances.
* Let's organise it first by gender (IsMale), and then by Age.
* Finally, we'll return a count. */
var organised = people.Pivot(p => p.IsMale, p => p.Age, l => l.Count());
Assert.IsTrue(organised.Count == 2, "More than two genders were returned.");
Assert.IsTrue(organised[true].Count == 2, "More than two ages were returned for males.");
Assert.IsTrue(organised[false].Count == 1, "More than 1 age was returned for females.");
int count = organised[true][30];
Assert.IsTrue(count == 3, "There are more than 3 male 30 year olds in our data.");
}
在这个测试用例中返回的是一个 Dictionary> 实例。该布尔值是 IsMale 分组依据的结果,在我们的示例数据中,正确返回 2 个项目,true 和 false。内部字典有一个年龄键和一个计数值。在我们的测试数据中,organized[true][30] 反映了集合中所有 30 岁的男性。
问题不在于枢轴函数本身,而是由于某种原因,当我们通过 NUnit 测试运行器和 Resharper 的单元测试运行器运行它时,测试失败,报告行“int count = Organized[true][ 30];"。当我们调试这个测试时,它正确地返回了值 3(在我们的样本数据中,我们有 3 个 30 岁的男性)。
有什么想法吗?