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: 13 additions & 0 deletions Dummies.UnitTests/AnyCollectionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,19 @@ public void DictionaryContainingKeyInsideDomainDoesNotInflate() {
Check.That(conflict.Message).Contains("2 distinct value");
}

[Fact(DisplayName = "ContainingAnyKey: a key drawn from a generator is forced into the dictionary; null is rejected (issue #287).")]
public void DictionaryContainingAnyKeyForcesADrawnKey() {
// The drawn key (4242) lies outside the key generator's own {1..9} domain, so it is supplied directly and
// extends the effective cardinality — the ContainingAny path, now reaching dictionary keys.
for (int i = 0; i < SampleCount; i++) {
Dictionary<int, string> dictionary =
Any.DictionaryOf(Any.Int32().Between(1, 9), Any.String().NonEmpty()).NonEmpty().ContainingAnyKey(Any.Int32().OneOf(4242)).Generate();
Check.That(dictionary.ContainsKey(4242)).IsTrue();
}

Check.ThatCode(() => Any.DictionaryOf(Any.Int32(), Any.Int32()).ContainingAnyKey(null!)).Throws<ArgumentNullException>();
}

[Fact(DisplayName = "PairOf and TripleOf assemble value tuples from constrained parts.")]
public void PairAndTriple() {
for (int i = 0; i < SampleCount; i++) {
Expand Down
17 changes: 17 additions & 0 deletions Dummies/AnyDictionary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,23 @@ public AnyDictionary<TKey, TValue> ContainingKey(TKey key) {
return With(_keys.WithContaining(key, $"ContainingKey({AnyDerivation.Display(key)})"));
}

/// <summary>
/// Requires the dictionary to contain an entry whose key is drawn from <paramref name="generator" /> at
/// generation time — the key analogue of a collection's <c>ContainingAny</c>. Named apart from
/// <see cref="ContainingKey" /> to keep the two cases legible: <see cref="ContainingKey" /> pins a concrete
/// key known now, whereas this draws one from a generator when the dictionary is built. The drawn key takes
/// one entry's room and its value is generated like any other.
/// </summary>
/// <param name="generator">The generator whose drawn key the dictionary must contain.</param>
/// <returns>A new generator carrying the added constraint.</returns>
/// <exception cref="ArgumentNullException">Thrown when <paramref name="generator" /> is <c>null</c>.</exception>
/// <exception cref="ConflictingAnyConstraintException">Thrown when the constraint contradicts a constraint already declared.</exception>
public AnyDictionary<TKey, TValue> ContainingAnyKey(IAny<TKey> generator) {
if (generator is null) { throw new ArgumentNullException(nameof(generator)); }

return With(_keys.WithContaining(generator, "ContainingAnyKey(<generator>)"));
}

/// <inheritdoc />
public Dictionary<TKey, TValue> Generate() {
List<TKey> keys = _keys.Materialize(_source ?? AmbientRandomSource.Instance);
Expand Down
1 change: 1 addition & 0 deletions Dummies/PublicAPI/net8.0/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ Dummies.AnyDecimal.OneOf(params decimal[]! values) -> Dummies.AnyDecimal!
Dummies.AnyDecimal.Positive() -> Dummies.AnyDecimal!
Dummies.AnyDecimal.Zero() -> Dummies.AnyDecimal!
Dummies.AnyDictionary<TKey, TValue>
Dummies.AnyDictionary<TKey, TValue>.ContainingAnyKey(Dummies.IAny<TKey>! generator) -> Dummies.AnyDictionary<TKey, TValue>!
Dummies.AnyDictionary<TKey, TValue>.ContainingKey(TKey key) -> Dummies.AnyDictionary<TKey, TValue>!
Dummies.AnyDictionary<TKey, TValue>.Empty() -> Dummies.AnyDictionary<TKey, TValue>!
Dummies.AnyDictionary<TKey, TValue>.Generate() -> System.Collections.Generic.Dictionary<TKey, TValue>!
Expand Down
1 change: 1 addition & 0 deletions Dummies/PublicAPI/netstandard2.0/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ Dummies.AnyDecimal.OneOf(params decimal[]! values) -> Dummies.AnyDecimal!
Dummies.AnyDecimal.Positive() -> Dummies.AnyDecimal!
Dummies.AnyDecimal.Zero() -> Dummies.AnyDecimal!
Dummies.AnyDictionary<TKey, TValue>
Dummies.AnyDictionary<TKey, TValue>.ContainingAnyKey(Dummies.IAny<TKey>! generator) -> Dummies.AnyDictionary<TKey, TValue>!
Dummies.AnyDictionary<TKey, TValue>.ContainingKey(TKey key) -> Dummies.AnyDictionary<TKey, TValue>!
Dummies.AnyDictionary<TKey, TValue>.Empty() -> Dummies.AnyDictionary<TKey, TValue>!
Dummies.AnyDictionary<TKey, TValue>.Generate() -> System.Collections.Generic.Dictionary<TKey, TValue>!
Expand Down