// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Collections.Generic;
using System.Linq;
using Xunit.Sdk;
namespace Microsoft.Extensions.Logging.Testing
{
public static class LogValuesAssert
{
///
/// Asserts that the given key and value are present in the actual values.
///
/// The key of the item to be found.
/// The value of the item to be found.
/// The actual values.
public static void Contains(
string key,
object value,
IEnumerable> actualValues)
{
Contains(new[] { new KeyValuePair(key, value) }, actualValues);
}
///
/// Asserts that all the expected values are present in the actual values by ignoring
/// the order of values.
///
/// Expected subset of values
/// Actual set of values
public static void Contains(
IEnumerable> expectedValues,
IEnumerable> actualValues)
{
if (expectedValues == null)
{
throw new ArgumentNullException(nameof(expectedValues));
}
if (actualValues == null)
{
throw new ArgumentNullException(nameof(actualValues));
}
var comparer = new LogValueComparer();
foreach (var expectedPair in expectedValues)
{
if (!actualValues.Contains(expectedPair, comparer))
{
throw new EqualException(
expected: GetString(expectedValues),
actual: GetString(actualValues));
}
}
}
private static string GetString(IEnumerable> logValues)
{
return string.Join(",", logValues.Select(kvp => $"[{kvp.Key} {kvp.Value}]"));
}
private class LogValueComparer : IEqualityComparer>
{
public bool Equals(KeyValuePair x, KeyValuePair y)
{
return string.Equals(x.Key, y.Key) && object.Equals(x.Value, y.Value);
}
public int GetHashCode(KeyValuePair obj)
{
// We are never going to put this KeyValuePair in a hash table,
// so this is ok.
throw new NotImplementedException();
}
}
}
}