Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[pull] main from nunit:main #263

Merged
merged 6 commits into from
Nov 26, 2024
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
11 changes: 11 additions & 0 deletions src/NUnitFramework/framework/Constraints/ConstraintBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,17 @@ public void Append(Constraint constraint)
constraint.Builder = this;
}

/// <summary>
/// Replaces the last pushed constraint with the specified constraint.
/// </summary>
/// <param name="constraint">The constraint to replace the lastPushed with.</param>
public void Replace(Constraint constraint)
{
_constraints.Pop();
_lastPushed = _ops.Top;
Append(constraint);
}

/// <summary>
/// Sets the top operator right context.
/// </summary>
Expand Down
43 changes: 43 additions & 0 deletions src/NUnitFramework/framework/Constraints/ConstraintExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,49 @@ public EqualConstraint EqualTo(object? expected)
return Append(new EqualConstraint(expected));
}

/// <summary>
/// Returns a constraint that tests two strings for equality
/// </summary>
public EqualStringConstraint EqualTo(string? expected)
{
return Append(new EqualStringConstraint(expected));
}

/// <summary>
/// Returns a constraint that tests two date time offset instances for equality
/// </summary>
public EqualDateTimeOffsetConstraint EqualTo(DateTimeOffset expected)
{
return Append(new EqualDateTimeOffsetConstraint(expected));
}

/// <summary>
/// Returns a constraint that tests two date time instances for equality
/// </summary>
public EqualTimeBaseConstraint<DateTime> EqualTo(DateTime expected)
{
return Append(new EqualTimeBaseConstraint<DateTime>(expected, x => x.Ticks));
}

/// <summary>
/// Returns a constraint that tests two timespan instances for equality
/// </summary>
public EqualTimeBaseConstraint<TimeSpan> EqualTo(TimeSpan expected)
{
return Append(new EqualTimeBaseConstraint<TimeSpan>(expected, x => x.Ticks));
}

/// <summary>
/// Returns a constraint that tests two numbers for equality
/// </summary>
#pragma warning disable CS3024 // Constraint type is not CLS-compliant
public EqualNumericConstraint<T> EqualTo<T>(T expected)
where T : unmanaged, IConvertible, IEquatable<T>
{
return Append(new EqualNumericConstraint<T>(expected));
}
#pragma warning restore CS3024 // Constraint type is not CLS-compliant

#endregion

#region SameAs
Expand Down
45 changes: 45 additions & 0 deletions src/NUnitFramework/framework/Constraints/EqualConstraintResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,51 @@ public EqualConstraintResult(EqualConstraint constraint, object? actual, bool ha
_failurePoints = constraint.HasFailurePoints ? constraint.FailurePoints : Array.Empty<NUnitEqualityComparer.FailurePoint>();
}

/// <summary>
/// Construct an EqualConstraintResult
/// </summary>
public EqualConstraintResult(Constraint constraint, object? actual, Tolerance tolerance, bool hasSucceeded)
: base(constraint, actual, hasSucceeded)
{
_expectedValue = constraint.Arguments[0];
_tolerance = tolerance;
_comparingProperties = false;
_caseInsensitive = false;
_ignoringWhiteSpace = false;
_clipStrings = false;
_failurePoints = Array.Empty<NUnitEqualityComparer.FailurePoint>();
}

/// <summary>
/// Construct an EqualConstraintResult
/// </summary>
public EqualConstraintResult(Constraint constraint, object? actual, bool hasSucceeded)
: base(constraint, actual, hasSucceeded)
{
_expectedValue = constraint.Arguments[0];
_tolerance = Tolerance.Default;
_comparingProperties = false;
_caseInsensitive = false;
_ignoringWhiteSpace = false;
_clipStrings = false;
_failurePoints = Array.Empty<NUnitEqualityComparer.FailurePoint>();
}

/// <summary>
/// Construct an EqualConstraintResult
/// </summary>
public EqualConstraintResult(EqualStringWithoutUsingConstraint constraint, object? actual, bool caseInsensitive, bool ignoringWhiteSpace, bool clipStrings, bool hasSucceeded)
: base(constraint, actual, hasSucceeded)
{
_expectedValue = constraint.Arguments[0];
_tolerance = Tolerance.Exact;
_comparingProperties = false;
_caseInsensitive = caseInsensitive;
_ignoringWhiteSpace = ignoringWhiteSpace;
_clipStrings = clipStrings;
_failurePoints = Array.Empty<NUnitEqualityComparer.FailurePoint>();
}

/// <summary>
/// Write a failure message. Overridden to provide custom
/// failure messages for EqualConstraint.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Copyright (c) Charlie Poole, Rob Prouse and Contributors. MIT License - see LICENSE.txt

using System;

namespace NUnit.Framework.Constraints
{
/// <summary>
/// EqualConstraint is able to compare an actual value with the
/// expected value provided in its constructor. Two objects are
/// considered equal if both are null, or if both have the same
/// value. NUnit has special semantics for some object types.
/// </summary>
public class EqualDateTimeOffsetConstraint : EqualTimeBaseConstraint<DateTimeOffset>
{
#region Constructor

/// <summary>
/// Initializes a new instance of the <see cref="EqualConstraint"/> class.
/// </summary>
/// <param name="expected">The expected value.</param>
public EqualDateTimeOffsetConstraint(DateTimeOffset expected)
: base(expected, x => x.UtcTicks)
{
}

#endregion

#region Constraint Modifiers

/// <summary>
/// Flags the constraint to include <see cref="DateTimeOffset.Offset"/>
/// property in comparison of two <see cref="DateTimeOffset"/> values.
/// </summary>
/// <remarks>
/// Using this modifier does not allow to use the <see cref="EqualConstraint.Within"/>
/// constraint modifier.
/// </remarks>
public EqualDateTimeOffsetConstraintWithSameOffset WithSameOffset
{
get
{
var constraint = new EqualDateTimeOffsetConstraintWithSameOffset(Expected);
Builder?.Replace(constraint);
return constraint;
}
}

#endregion
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
// Copyright (c) Charlie Poole, Rob Prouse and Contributors. MIT License - see LICENSE.txt

using System;
using System.Text;

namespace NUnit.Framework.Constraints
{
/// <summary>
/// EqualConstraint is able to compare an actual value with the
/// expected value provided in its constructor. Two objects are
/// considered equal if both are null, or if both have the same
/// value. NUnit has special semantics for some object types.
/// </summary>
public class EqualDateTimeOffsetConstraintWithSameOffset : Constraint
{
private readonly DateTimeOffset _expected;

#region Constructor

/// <summary>
/// Initializes a new instance of the <see cref="EqualConstraint"/> class.
/// </summary>
/// <param name="expected">The expected value.</param>
public EqualDateTimeOffsetConstraintWithSameOffset(DateTimeOffset expected)
: base(expected)
{
_expected = expected;
}

#endregion

#region Public Methods

/// <summary>
/// Test whether the constraint is satisfied by a given value
/// </summary>
/// <param name="actual">The value to be tested</param>
/// <returns>True for success, false for failure</returns>
public ConstraintResult ApplyTo(DateTimeOffset actual)
{
bool hasSucceeded = _expected.Equals(actual) && _expected.Offset == actual.Offset;

return new ConstraintResult(this, actual, hasSucceeded);
}

/// <summary>
/// Test whether the constraint is satisfied by a given value
/// </summary>
/// <param name="actual">The value to be tested</param>
/// <returns>True for success, false for failure</returns>
public override ConstraintResult ApplyTo<TActual>(TActual actual)
{
if (actual is DateTimeOffset dateTimeOffset)
{
return ApplyTo(dateTimeOffset);
}

return new ConstraintResult(this, actual, false);
}

/// <summary>
/// The Description of what this constraint tests, for
/// use in messages and in the ConstraintResult.
/// </summary>
public override string Description
{
get
{
var sb = new StringBuilder(MsgUtils.FormatValue(_expected));

sb.Append(" with the same offset");

return sb.ToString();
}
}

#endregion
}
}
31 changes: 31 additions & 0 deletions src/NUnitFramework/framework/Constraints/EqualNumericConstraint.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright (c) Charlie Poole, Rob Prouse and Contributors. MIT License - see LICENSE.txt

using System;

namespace NUnit.Framework.Constraints
{
/// <summary>
/// EqualNumericConstraint is able to compare an actual value with the
/// expected value provided in its constructor. Two objects are
/// considered equal if both are null, or if both have the same
/// value. NUnit has special semantics for some object types.
/// </summary>
#pragma warning disable CS3024 // Constraint type is not CLS-compliant
public class EqualNumericConstraint<T> : EqualNumericWithoutUsingConstraint<T>, IEqualWithUsingConstraint<T>
where T : unmanaged, IConvertible, IEquatable<T>
#pragma warning restore CS3024 // Constraint type is not CLS-compliant
{
#region Constructor

/// <summary>
/// Initializes a new instance of the <see cref="EqualConstraint"/> class.
/// </summary>
/// <param name="expected">The expected value.</param>
public EqualNumericConstraint(T expected)
: base(expected)
{
}

#endregion
}
}
Loading
Loading