Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions Source/aweXpect/Results/ContainsKeyResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,29 @@ public class ContainsKeyResult<TCollection, TThat, TKey, TValue>
: AndOrResult<TCollection, TThat>
{
private readonly TKey _key;
private readonly Func<TCollection, TValue> _memberAccessor;
private readonly Func<TCollection, TKey, TValue> _valueAccessor;

/// <remarks>
/// The <paramref name="valueAccessor" /> gets the <paramref name="key" /> as its argument, so that the accessor of
/// the value is only bound to the key when <see cref="WhoseValue" /> is used.
/// </remarks>
internal ContainsKeyResult(
ExpectationBuilder expectationBuilder,
TThat returnValue,
TKey key,
Func<TCollection, TValue> memberAccessor)
Func<TCollection, TKey, TValue> valueAccessor)
: base(expectationBuilder, returnValue)
{
_key = key;
_memberAccessor = memberAccessor;
_valueAccessor = valueAccessor;
}

/// <summary>
/// Further expectations on the selected value of the dictionary.
/// </summary>
public IThat<TValue> WhoseValue
=> new ThatSubject<TValue>(ExpectationBuilder.ForWhich(_memberAccessor, " whose value ",
=> new ThatSubject<TValue>(ExpectationBuilder.ForWhich((TCollection collection) => _valueAccessor(collection, _key),
" whose value ",
$"value of key {Formatter.Format(_key)}",
grammars => (grammars & ~ExpectationGrammars.Plural) | ExpectationGrammars.Introduced));
}
12 changes: 8 additions & 4 deletions Source/aweXpect/Results/ContainsKeysResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,29 @@ public class ContainsKeysResult<TCollection, TThat, TKey, TValue>
: AndOrResult<TCollection, TThat>
{
private readonly TKey[] _keys;
private readonly Func<TCollection, IEnumerable<TValue>> _memberAccessor;
private readonly Func<TCollection, TKey[], IEnumerable<TValue>> _valuesAccessor;

/// <remarks>
/// The <paramref name="valuesAccessor" /> gets the <paramref name="keys" /> as its argument, so that the
/// accessor of the values is only bound to the keys when <see cref="WhoseValues" /> is used.
/// </remarks>
internal ContainsKeysResult(
ExpectationBuilder expectationBuilder,
TThat returnValue,
TKey[] keys,
Func<TCollection, IEnumerable<TValue>> memberAccessor)
Func<TCollection, TKey[], IEnumerable<TValue>> valuesAccessor)
: base(expectationBuilder, returnValue)
{
_keys = keys;
_memberAccessor = memberAccessor;
_valuesAccessor = valuesAccessor;
}

/// <summary>
/// Further expectations on the selected values of the dictionary.
/// </summary>
public IThat<IEnumerable<TValue>> WhoseValues
=> new ThatSubject<IEnumerable<TValue>>(ExpectationBuilder
.ForWhich(_memberAccessor, " whose values ", $"values of keys {Formatter.Format(_keys)}",
.ForWhich((TCollection collection) => _valuesAccessor(collection, _keys), " whose values ", $"values of keys {Formatter.Format(_keys)}",
grammars => grammars | ExpectationGrammars.Nested | ExpectationGrammars.Plural |
ExpectationGrammars.Introduced));
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public static partial class ThatDictionary
.InvertIf(state.Negated)),
subject,
expected,
f => TryLookUp(GetLookup(f), expected, out TValue? value) ? value : default
static (f, key) => TryLookUp(GetLookup(f), key, out TValue? value) ? value : default
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public static partial class ThatDictionary
.InvertIf(state.Negated)),
subject,
keys,
dictionary => new KeyedValues<TKey, TValue?>(keys
static (dictionary, keys) => new KeyedValues<TKey, TValue?>(keys
.Where(key => ContainsKey(dictionary, key))
.Select(key => new KeyValuePair<TKey, TValue?>(key,
TryLookUp(GetLookup(dictionary), key, out TValue? value) ? value : default)))
Expand Down
Loading