Skip to content

Commit

Permalink
Issue3918 white space (#4664)
Browse files Browse the repository at this point in the history
* Remove need for casts when using ConstraintExpression.Append

* Fix nullability of StringContraint.Matches

* Add `WhiteSpace` Constraint

* Add support for `.IgnoreWhiteSpace` modifier

to:
- AnyOfConstraint
- CollectionItemsEqualConstaint
- ContainsConstraint
- EqualConstraint
- UniqueItemsConstraint

Updates to Clip String
Requires individual clipping when white-space is ignored.
We then also need to show two different ^ to indicate the mismatched
location.

* Code review changes

* Make it clear that what the extra size is for when creating StringBuilder
  • Loading branch information
manfred-brands committed Mar 27, 2024
1 parent 0d3c43c commit bb18e96
Show file tree
Hide file tree
Showing 36 changed files with 793 additions and 215 deletions.
1 change: 1 addition & 0 deletions nuget/framework/nunit.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
<dependencies>
<group targetFramework="net462">
<dependency id="System.Threading.Tasks.Extensions" version="4.5.4" exclude="Build,Analyzers" />
<dependency id="System.ValueTuple" version="4.5.0" exclude="Build,Analyzers" />
</group>
<group targetFramework="net6.0" />
</dependencies>
Expand Down
12 changes: 12 additions & 0 deletions src/NUnitFramework/framework/Constraints/AnyOfConstraint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,18 @@ public AnyOfConstraint IgnoreCase
}
}

/// <summary>
/// Flag the constraint to ignore white space and return self.
/// </summary>
public AnyOfConstraint IgnoreWhiteSpace
{
get
{
_comparer.IgnoreWhiteSpace = true;
return this;
}
}

/// <summary>
/// Flag the constraint to use the supplied IComparer object.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ protected CollectionItemsEqualConstraint(object? arg) : base(arg)
/// </summary>
protected bool IgnoringCase => _comparer.IgnoreCase;

/// <summary>
/// Get a flag indicating whether the user requested us to ignore white space.
/// </summary>
protected bool IgnoringWhiteSpace => _comparer.IgnoreWhiteSpace;

/// <summary>
/// Get a flag indicating whether any external comparers are in use.
/// </summary>
Expand All @@ -61,6 +66,18 @@ public CollectionItemsEqualConstraint IgnoreCase
}
}

/// <summary>
/// Flag the constraint to ignore white space and return self.
/// </summary>
public CollectionItemsEqualConstraint IgnoreWhiteSpace
{
get
{
_comparer.IgnoreWhiteSpace = true;
return this;
}
}

/// <summary>
/// Flag the constraint to use the supplied IComparer object.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,22 @@ public static EqualMethodResult Equal(object x, object y, ref Tolerance toleranc
if (tolerance.HasVariance)
return EqualMethodResult.ToleranceNotSupported;

var stringComparison = equalityComparer.IgnoreCase ? StringComparison.CurrentCultureIgnoreCase : StringComparison.Ordinal;
return xString.Equals(yString, stringComparison) ?
EqualMethodResult.ComparedEqual : EqualMethodResult.ComparedNotEqual;
return Equals(xString, yString, equalityComparer.IgnoreCase, equalityComparer.IgnoreWhiteSpace) ?
EqualMethodResult.ComparedEqual :
EqualMethodResult.ComparedNotEqual;
}

public static bool Equals(string x, string y, bool ignoreCase, bool ignoreWhiteSpace)
{
if (ignoreWhiteSpace)
{
(int mismatchExpected, int mismatchActual) = MsgUtils.FindMismatchPosition(x, y, ignoreCase, true);
return mismatchExpected == -1 && mismatchActual == -1;
}
else
{
return x.Equals(y, ignoreCase ? StringComparison.CurrentCultureIgnoreCase : StringComparison.Ordinal);
}
}
}
}

0 comments on commit bb18e96

Please sign in to comment.