Skip to content

Commit

Permalink
Support And/Or linking with DictionaryContainsKeyValuePairConstraint (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
Falco20019 committed Mar 25, 2024
1 parent 328dc52 commit 0d3c43c
Show file tree
Hide file tree
Showing 4 changed files with 151 additions and 10 deletions.
21 changes: 21 additions & 0 deletions src/NUnitFramework/framework/Constraints/Constraint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,27 @@ public ConstraintExpression Or
}
}

/// <summary>
/// Returns a ConstraintExpression by appending Instead
/// to the current constraint.
/// </summary>
internal ConstraintExpression Instead
{
get
{
ConstraintBuilder? builder = Builder;
if (builder is null)
{
builder = new ConstraintBuilder();
builder.Append(this);
}

builder.Append(new InsteadOperator());

return new ConstraintExpression(builder);
}
}

#endregion

#region After Modifier
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,16 +53,7 @@ public DictionaryContainsKeyConstraint(object expected)
/// </summary>
public DictionaryContainsKeyValuePairConstraint WithValue(object expectedValue)
{
var builder = Builder;
if (builder is null)
{
builder = new ConstraintBuilder();
builder.Append(this);
}

var constraint = new DictionaryContainsKeyValuePairConstraint(Expected, expectedValue);
builder.Append(constraint);
return constraint;
return (DictionaryContainsKeyValuePairConstraint)Instead.Append(new DictionaryContainsKeyValuePairConstraint(Expected, expectedValue));
}

private bool Matches(object? actual)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright (c) Charlie Poole, Rob Prouse and Contributors. MIT License - see LICENSE.txt

namespace NUnit.Framework.Constraints
{
/// <summary>
/// Operator that replaces the left constraint with the right constraint
/// </summary>
public class InsteadOperator : BinaryOperator
{
/// <summary>
/// Construct an InsteadOperator
/// </summary>
public InsteadOperator()
{
left_precedence = 1;
right_precedence = 99;
}

/// <summary>
/// Apply the operator to replace the left constraint
/// </summary>
public override IConstraint ApplyOperator(IConstraint left, IConstraint right)
{
return right;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,48 @@ public void SucceedsWhenPairIsNotPresentUsingContainKeyWithValue()
Assert.That(dictionary, Does.Not.ContainKey("Hello").WithValue("NotValue"));
}

[Test]
public void SucceedsWhenPairIsPresentUsingContainKeyWithValueJoinedByAnd()
{
var dictionary = new Dictionary<string, string> { { "Hello", "World" }, { "Hola", "Mundo" } };
Assert.That(dictionary, Does.ContainKey("Hola").WithValue("Mundo").And.ContainKey("Hello").WithValue("World"));
}

[Test]
public void SucceedsWhenPairIsPresentUsingContainKeyWithValueJoinedByOrBothTrue()
{
var dictionary = new Dictionary<string, string> { { "Hello", "World" }, { "Hola", "Mundo" } };
Assert.That(dictionary, Does.ContainKey("Hola").WithValue("Mundo").Or.ContainKey("Hello").WithValue("World"));
}

[Test]
public void SucceedsWhenPairIsPresentUsingContainKeyWithValueJoinedByOrLeftKeyWrong()
{
var dictionary = new Dictionary<string, string> { { "Hello", "World" }, { "Hola", "Mundo" } };
Assert.That(dictionary, Does.ContainKey("NotKey").WithValue("Mundo").Or.ContainKey("Hello").WithValue("World"));
}

[Test]
public void SucceedsWhenPairIsPresentUsingContainKeyWithValueJoinedByOrLeftValueWrong()
{
var dictionary = new Dictionary<string, string> { { "Hello", "World" }, { "Hola", "Mundo" } };
Assert.That(dictionary, Does.ContainKey("Hola").WithValue("NotValue").Or.ContainKey("Hello").WithValue("World"));
}

[Test]
public void SucceedsWhenPairIsPresentUsingContainKeyWithValueJoinedByOrRightKeyWrong()
{
var dictionary = new Dictionary<string, string> { { "Hello", "World" }, { "Hola", "Mundo" } };
Assert.That(dictionary, Does.ContainKey("Hola").WithValue("Mundo").Or.ContainKey("NotKey").WithValue("World"));
}

[Test]
public void SucceedsWhenPairIsPresentUsingContainKeyWithValueJoinedByOrRightValueWrong()
{
var dictionary = new Dictionary<string, string> { { "Hello", "World" }, { "Hola", "Mundo" } };
Assert.That(dictionary, Does.ContainKey("Hola").WithValue("Mundo").Or.ContainKey("Hello").WithValue("NotValue"));
}

[Test]
public void FailsWhenNotUsedAgainstADictionary()
{
Expand All @@ -63,6 +105,66 @@ public void FailsWhenNotUsedAgainstADictionary()
Assert.That(act, Throws.ArgumentException.With.Message.Contains("IDictionary"));
}

[Test]
public void FailsWhenPairIsPresentUsingContainKeyWithValueJoinedByAndBothFalse()
{
var dictionary = new Dictionary<string, string> { { "Hello", "World" }, { "Hola", "Mundo" } };

var expression = (IResolveConstraint)Does.ContainKey("NotKeyLeft").WithValue("NotValueLeft").And.ContainKey("NotKeyRight").WithValue("NotValueRight");
var constraint = expression.Resolve();
var result = constraint.ApplyTo(dictionary);

Assert.That(result.IsSuccess, Is.False);
}

[Test]
public void FailsWhenPairIsPresentUsingContainKeyWithValueJoinedByAndLeftKeyWrong()
{
var dictionary = new Dictionary<string, string> { { "Hello", "World" }, { "Hola", "Mundo" } };

var expression = (IResolveConstraint)Does.ContainKey("NotKey").WithValue("Mundo").And.ContainKey("Hello").WithValue("World");
var constraint = expression.Resolve();
var result = constraint.ApplyTo(dictionary);

Assert.That(result.IsSuccess, Is.False);
}

[Test]
public void FailsWhenPairIsPresentUsingContainKeyWithValueJoinedByAndLeftValueWrong()
{
var dictionary = new Dictionary<string, string> { { "Hello", "World" }, { "Hola", "Mundo" } };

var expression = (IResolveConstraint)Does.ContainKey("Hola").WithValue("NotValue").And.ContainKey("Hello").WithValue("World");
var constraint = expression.Resolve();
var result = constraint.ApplyTo(dictionary);

Assert.That(result.IsSuccess, Is.False);
}

[Test]
public void FailsWhenPairIsPresentUsingContainKeyWithValueJoinedByAndRightKeyWrong()
{
var dictionary = new Dictionary<string, string> { { "Hello", "World" }, { "Hola", "Mundo" } };

var expression = (IResolveConstraint)Does.ContainKey("Hola").WithValue("Mundo").And.ContainKey("NotKey").WithValue("World");
var constraint = expression.Resolve();
var result = constraint.ApplyTo(dictionary);

Assert.That(result.IsSuccess, Is.False);
}

[Test]
public void FailsWhenPairIsPresentUsingContainKeyWithValueJoinedByAndRightValueWrong()
{
var dictionary = new Dictionary<string, string> { { "Hello", "World" }, { "Hola", "Mundo" } };

var expression = (IResolveConstraint)Does.ContainKey("Hola").WithValue("Mundo").And.ContainKey("Hello").WithValue("NotValue");
var constraint = expression.Resolve();
var result = constraint.ApplyTo(dictionary);

Assert.That(result.IsSuccess, Is.False);
}

[Test]
public void WorksWithNonGenericDictionary()
{
Expand Down

0 comments on commit 0d3c43c

Please sign in to comment.