-
Notifications
You must be signed in to change notification settings - Fork 723
/
DictionaryContainsKeyValuePairConstraint.cs
93 lines (79 loc) · 3.36 KB
/
DictionaryContainsKeyValuePairConstraint.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
// Copyright (c) Charlie Poole, Rob Prouse and Contributors. MIT License - see LICENSE.txt
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using NUnit.Framework.Internal;
namespace NUnit.Framework.Constraints
{
/// <summary>
/// DictionaryContainsKeyValuePairConstraint is used to test whether a dictionary
/// contains an expected object as a key-value-pair.
/// </summary>
public sealed class DictionaryContainsKeyValuePairConstraint : CollectionItemsEqualConstraint
{
private readonly DictionaryEntry _expected;
/// <summary>
/// Construct a DictionaryContainsKeyValuePairConstraint
/// </summary>
public DictionaryContainsKeyValuePairConstraint(object key, object? value)
{
_expected = new DictionaryEntry(key, value);
}
/// <summary>
/// The display name of this Constraint for use by ToString().
/// The default value is the name of the constraint with
/// trailing "Constraint" removed. Derived classes may set
/// this to another name in their constructors.
/// </summary>
public override string DisplayName => "ContainsKeyValuePair";
/// <summary>
/// The Description of what this constraint tests, for
/// use in messages and in the ConstraintResult.
/// </summary>
public override string Description => "dictionary containing entry " + MsgUtils.FormatValue(_expected);
private bool Matches(object? actual)
{
if (actual is null)
throw new ArgumentException("Expected: IDictionary But was: null", nameof(actual));
if (TypeHelper.TryCast<IDictionary>(actual, out var dictionary))
{
foreach (var entry in dictionary)
{
if (ItemsEqual(entry, _expected))
return true;
}
return false;
}
// If 'actual' implements IDictionary<TKey, TValue>, construct an 'expected' KeyValuePair<TKey, TValue>
// and look it up by iterating using IEnumerable
if (actual.GetType().GetInterfaces().Any(i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(IDictionary<,>)))
{
var expected = new KeyValuePair<object, object?>(_expected.Key, _expected.Value);
var enumerable = (IEnumerable)actual;
foreach (var item in enumerable)
{
if (ItemsEqual(item, expected))
return true;
}
return false;
}
throw new ArgumentException($"Expected: IDictionary But was: {actual.GetType()}", nameof(actual));
}
/// <summary>
/// Test whether the constraint is satisfied by a given value
/// </summary>
/// <param name="actual">The value to be tested</param>
public override ConstraintResult ApplyTo<TActual>(TActual actual)
{
return new ConstraintResult(this, actual, Matches(actual));
}
/// <summary>
/// Test whether the expected key is contained in the dictionary
/// </summary>
protected override bool Matches(IEnumerable collection)
{
return Matches(collection);
}
}
}