Class EqualConstraint
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.
Inherited Members
Namespace: NUnit.Framework.Constraints
Assembly: nunit.framework.dll
Syntax
public class EqualConstraint : Constraint, IConstraint, IResolveConstraint
Constructors
View SourceEqualConstraint(object?)
Initializes a new instance of the EqualConstraint class.
Declaration
public EqualConstraint(object? expected)
Parameters
| Type | Name | Description |
|---|---|---|
| object | expected | The expected value. |
Properties
View SourceAsCollection
Flag the constraint to compare arrays as collections and return self.
Declaration
public EqualConstraint AsCollection { get; }
Property Value
| Type | Description |
|---|---|
| EqualConstraint |
CaseInsensitive
Gets a value indicating whether to compare case insensitive.
Declaration
public bool CaseInsensitive { get; }
Property Value
| Type | Description |
|---|---|
| bool |
ClipStrings
Gets a value indicating whether or not to clip strings.
Declaration
public bool ClipStrings { get; }
Property Value
| Type | Description |
|---|---|
| bool |
Comparer
The underlying comparer used.
Declaration
protected NUnitEqualityComparer Comparer { get; }
Property Value
| Type | Description |
|---|---|
| NUnitEqualityComparer |
ComparingProperties
Gets a value indicating whether to compare separate properties.
Declaration
public bool ComparingProperties { get; }
Property Value
| Type | Description |
|---|---|
| bool |
Days
Causes the tolerance to be interpreted as a TimeSpan in days.
Declaration
public EqualConstraint Days { get; }
Property Value
| Type | Description |
|---|---|
| EqualConstraint | Self |
Description
The Description of what this constraint tests, for use in messages and in the ConstraintResult.
Declaration
public override string Description { get; }
Property Value
| Type | Description |
|---|---|
| string |
Overrides
View SourceFailurePoints
Gets the failure points.
Declaration
public IList<NUnitEqualityComparer.FailurePoint> FailurePoints { get; }
Property Value
| Type | Description |
|---|---|
| IList<NUnitEqualityComparer.FailurePoint> | The failure points. |
HasFailurePoints
Gets a value indicating whether there is any additional Failure Information.
Declaration
public bool HasFailurePoints { get; }
Property Value
| Type | Description |
|---|---|
| bool |
Hours
Causes the tolerance to be interpreted as a TimeSpan in hours.
Declaration
public EqualConstraint Hours { get; }
Property Value
| Type | Description |
|---|---|
| EqualConstraint | Self |
IgnoreCase
Flag the constraint to ignore case and return self.
Declaration
public EqualConstraint IgnoreCase { get; }
Property Value
| Type | Description |
|---|---|
| EqualConstraint |
IgnoreLineEndingFormat
Flag the constraint to ignore line ending format (\r vs. \n vs. \r\n) and return self.
Declaration
public EqualConstraint IgnoreLineEndingFormat { get; }
Property Value
| Type | Description |
|---|---|
| EqualConstraint |
IgnoreWhiteSpace
Flag the constraint to ignore white space and return self.
Declaration
public EqualConstraint IgnoreWhiteSpace { get; }
Property Value
| Type | Description |
|---|---|
| EqualConstraint |
IgnoringLineEndingFormat
Gets a value indicating whether to compare ignoring line ending format (\r vs. \n vs. \r\n).
Declaration
public bool IgnoringLineEndingFormat { get; }
Property Value
| Type | Description |
|---|---|
| bool | true if comparing ignoring line ending format (\r vs. \n vs. \r\n); otherwise, false. |
IgnoringWhiteSpace
Gets a value indicating whether to compare ignoring white space.
Declaration
public bool IgnoringWhiteSpace { get; }
Property Value
| Type | Description |
|---|---|
| bool |
Milliseconds
Causes the tolerance to be interpreted as a TimeSpan in milliseconds.
Declaration
public EqualConstraint Milliseconds { get; }
Property Value
| Type | Description |
|---|---|
| EqualConstraint | Self |
Minutes
Causes the tolerance to be interpreted as a TimeSpan in minutes.
Declaration
public EqualConstraint Minutes { get; }
Property Value
| Type | Description |
|---|---|
| EqualConstraint | Self |
NoClip
Flag the constraint to suppress string clipping and return self.
Declaration
public EqualConstraint NoClip { get; }
Property Value
| Type | Description |
|---|---|
| EqualConstraint |
Percent
Switches the .Within() modifier to interpret its tolerance as a percentage that the actual values is allowed to deviate from the expected value.
Declaration
public EqualConstraint Percent { get; }
Property Value
| Type | Description |
|---|---|
| EqualConstraint | Self |
Seconds
Causes the tolerance to be interpreted as a TimeSpan in seconds.
Declaration
public EqualConstraint Seconds { get; }
Property Value
| Type | Description |
|---|---|
| EqualConstraint | Self |
Ticks
Causes the tolerance to be interpreted as a TimeSpan in clock ticks.
Declaration
public EqualConstraint Ticks { get; }
Property Value
| Type | Description |
|---|---|
| EqualConstraint | Self |
Tolerance
Gets the tolerance for this comparison.
Declaration
public Tolerance Tolerance { get; }
Property Value
| Type | Description |
|---|---|
| Tolerance | The tolerance. |
Ulps
Switches the .Within() modifier to interpret its tolerance as a distance in representable values (see remarks).
Declaration
public EqualConstraint Ulps { get; }
Property Value
| Type | Description |
|---|---|
| EqualConstraint | Self. |
Remarks
Ulp stands for "unit in the last place" and describes the minimum amount a given value can change. For any integers, an ulp is 1 whole digit. For floating point values, the accuracy of which is better for smaller numbers and worse for larger numbers, an ulp depends on the size of the number. Using ulps for comparison of floating point results instead of fixed tolerances is safer because it will automatically compensate for the added inaccuracy of larger numbers.
WithSameOffset
Flags the constraint to include Offset property in comparison of two DateTimeOffset values.
Declaration
public EqualConstraint WithSameOffset { get; }
Property Value
| Type | Description |
|---|---|
| EqualConstraint |
Remarks
Using this modifier does not allow to use the Within(object) constraint modifier.
Methods
View SourceApplyTo<TActual>(TActual)
Test whether the constraint is satisfied by a given value
Declaration
public override ConstraintResult ApplyTo<TActual>(TActual actual)
Parameters
| Type | Name | Description |
|---|---|---|
| TActual | actual | The value to be tested |
Returns
| Type | Description |
|---|---|
| ConstraintResult | True for success, false for failure |
Type Parameters
| Name | Description |
|---|---|
| TActual |
Overrides
View SourceUsing(IComparer)
Flag the constraint to use the supplied IComparer object.
Declaration
public EqualConstraint Using(IComparer comparer)
Parameters
| Type | Name | Description |
|---|---|---|
| IComparer | comparer | The IComparer object to use. |
Returns
| Type | Description |
|---|---|
| EqualConstraint | Self. |
Using(IEqualityComparer)
Flag the constraint to use the supplied IEqualityComparer object.
Declaration
public EqualConstraint Using(IEqualityComparer comparer)
Parameters
| Type | Name | Description |
|---|---|---|
| IEqualityComparer | comparer | The IComparer object to use. |
Returns
| Type | Description |
|---|---|
| EqualConstraint | Self. |
UsingPropertiesComparer()
Enables comparing of instance properties.
Declaration
public EqualConstraint UsingPropertiesComparer()
Returns
| Type | Description |
|---|---|
| EqualConstraint |
Remarks
This allows comparing classes that don't implement IEquatable<T> without having to compare each property separately in own code.
UsingPropertiesComparer(Func<PropertiesComparerConfigurationUntyped, PropertiesComparerConfigurationUntyped>)
Enables comparing of instance properties.
Declaration
public EqualConstraint UsingPropertiesComparer(Func<PropertiesComparerConfigurationUntyped, PropertiesComparerConfigurationUntyped> configure)
Parameters
| Type | Name | Description |
|---|---|---|
| Func<PropertiesComparerConfigurationUntyped, PropertiesComparerConfigurationUntyped> | configure | Function to configure the PropertiesComparerConfiguration |
Returns
| Type | Description |
|---|---|
| EqualConstraint |
Remarks
This allows comparing classes that don't implement IEquatable<T> without having to compare each property separately in own code.
Using<T>(IComparer<T>)
Flag the constraint to use the supplied IComparer object.
Declaration
public EqualConstraint Using<T>(IComparer<T> comparer)
Parameters
| Type | Name | Description |
|---|---|---|
| IComparer<T> | comparer | The IComparer object to use. |
Returns
| Type | Description |
|---|---|
| EqualConstraint | Self. |
Type Parameters
| Name | Description |
|---|---|
| T |
Using<T>(IEqualityComparer<T>)
Flag the constraint to use the supplied IEqualityComparer object.
Declaration
public EqualConstraint Using<T>(IEqualityComparer<T> comparer)
Parameters
| Type | Name | Description |
|---|---|---|
| IEqualityComparer<T> | comparer | The IComparer object to use. |
Returns
| Type | Description |
|---|---|
| EqualConstraint | Self. |
Type Parameters
| Name | Description |
|---|---|
| T |
Using<T>(Comparison<T>)
Flag the constraint to use the supplied Comparison object.
Declaration
public EqualConstraint Using<T>(Comparison<T> comparer)
Parameters
| Type | Name | Description |
|---|---|---|
| Comparison<T> | comparer | The IComparer object to use. |
Returns
| Type | Description |
|---|---|
| EqualConstraint | Self. |
Type Parameters
| Name | Description |
|---|---|
| T |
Using<T>(Func<T, T, bool>)
Flag the constraint to use the supplied boolean-returning delegate.
Declaration
public EqualConstraint Using<T>(Func<T, T, bool> comparer)
Parameters
| Type | Name | Description |
|---|---|---|
| Func<T, T, bool> | comparer | The boolean-returning delegate to use. |
Returns
| Type | Description |
|---|---|
| EqualConstraint | Self. |
Type Parameters
| Name | Description |
|---|---|
| T |
Using<TActual, TExpected>(Func<TActual, TExpected, bool>)
Flag the constraint to use the supplied predicate function
Declaration
public EqualConstraint Using<TActual, TExpected>(Func<TActual, TExpected, bool> comparison)
Parameters
| Type | Name | Description |
|---|---|---|
| Func<TActual, TExpected, bool> | comparison | The comparison function to use. |
Returns
| Type | Description |
|---|---|
| EqualConstraint | Self. |
Type Parameters
| Name | Description |
|---|---|
| TActual | The type of the actual value. Note for collection comparisons this is the element type. |
| TExpected | The type of the expected value. Note for collection comparisons this is the element type. |
Within(object)
Flag the constraint to use a tolerance when determining equality.
Declaration
public EqualConstraint Within(object amount)
Parameters
| Type | Name | Description |
|---|---|---|
| object | amount | Tolerance value to be used |
Returns
| Type | Description |
|---|---|
| EqualConstraint | Self. |