Skip to content

Commit

Permalink
Remove StringUtil class and update tests references nunit#4819 (nunit…
Browse files Browse the repository at this point in the history
…#4827)

* Remove StringUtil class and update test references

- Removed the legacy internal StringUtil class, including Compare and
  StringEqual methods.
- Updated references in tests to use System.StringComparison instead.

* refactor: Reduce code duplication and replace StringComparison type

Changes suggested after PR review.
- Replacing path-related comparisons from CurrentCulture to Ordinal.
- Encapsulation of case-sensitivity check to reduce code duplication.
  • Loading branch information
mr-sergito authored Sep 21, 2024
1 parent fb51025 commit af7b941
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 50 deletions.
16 changes: 14 additions & 2 deletions src/NUnitFramework/framework/Constraints/PathConstraint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

using System;
using System.IO;
using NUnit.Framework.Internal;

namespace NUnit.Framework.Constraints
{
Expand Down Expand Up @@ -101,6 +100,19 @@ protected string Canonicalize(string path)
return leadingSeparators + string.Join(Path.DirectorySeparatorChar.ToString(), parts, 0, count);
}

/// <summary>
/// Determines the <see cref="StringComparison"/> value based on the
/// <see cref="StringConstraint.caseInsensitive"/> field.
/// If <c>caseInsensitive</c> is true, it returns <see cref="StringComparison.OrdinalIgnoreCase"/>;
/// otherwise, it returns <see cref="StringComparison.Ordinal"/>.
/// </summary>
protected StringComparison DetermineComparisonType()
{
return caseInsensitive
? StringComparison.OrdinalIgnoreCase
: StringComparison.Ordinal;
}

/// <summary>
/// Test whether one path in canonical form is a subpath of another path
/// </summary>
Expand All @@ -117,7 +129,7 @@ protected bool IsSubPath(string path1, string path2)
return false;

// path 2 is longer than path 1: see if initial parts match
if (!StringUtil.StringsEqual(path1, path2.Substring(0, length1), caseInsensitive))
if (!string.Equals(path1, path2.Substring(0, length1), DetermineComparisonType()))
return false;

// must match through or up to a directory separator boundary
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
// Copyright (c) Charlie Poole, Rob Prouse and Contributors. MIT License - see LICENSE.txt

using NUnit.Framework.Internal;

namespace NUnit.Framework.Constraints
{
/// <summary>
Expand Down Expand Up @@ -30,7 +28,7 @@ public SamePathConstraint(string expected) : base(expected)
/// <returns>True for success, false for failure</returns>
protected override bool Matches(string? actual)
{
return actual is not null && StringUtil.StringsEqual(Canonicalize(expected), Canonicalize(actual), caseInsensitive);
return actual is not null && string.Equals(Canonicalize(expected), Canonicalize(actual), DetermineComparisonType());
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
// Copyright (c) Charlie Poole, Rob Prouse and Contributors. MIT License - see LICENSE.txt

using NUnit.Framework.Internal;

namespace NUnit.Framework.Constraints
{
/// <summary>
Expand Down Expand Up @@ -35,7 +33,8 @@ protected override bool Matches(string? actual)

string path1 = Canonicalize(expected);
string path2 = Canonicalize(actual);
return StringUtil.StringsEqual(path1, path2, caseInsensitive) || IsSubPath(path1, path2);

return string.Equals(path1, path2, DetermineComparisonType()) || IsSubPath(path1, path2);
}
}
}
38 changes: 0 additions & 38 deletions src/NUnitFramework/framework/Internal/StringUtil.cs

This file was deleted.

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

using System;
using System.Collections;
using System.Collections.Generic;
using NUnit.Framework.Constraints;
Expand Down Expand Up @@ -197,7 +198,7 @@ public void UsingIsHonored()
var dictionary = new Dictionary<string, string> { { "Hello", "World" }, { "Hi", "Universe" }, { "Hola", "Mundo" } };

Assert.That(dictionary,
new DictionaryContainsKeyValuePairConstraint("HI", "UNIVERSE").Using<string>((x, y) => StringUtil.Compare(x, y, true)));
new DictionaryContainsKeyValuePairConstraint("HI", "UNIVERSE").Using<string>((x, y) => string.Compare(x, y, StringComparison.CurrentCultureIgnoreCase)));
}

[Test]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Copyright (c) Charlie Poole, Rob Prouse and Contributors. MIT License - see LICENSE.txt

using System;
using System.Collections;
using System.Collections.Generic;
using NUnit.Framework.Constraints;
Expand Down Expand Up @@ -84,7 +85,7 @@ public void UsingIsHonored()
var dictionary = new Dictionary<string, string> { { "Hello", "World" }, { "Hi", "Universe" }, { "Hola", "Mundo" } };

Assert.That(dictionary,
new DictionaryContainsValueConstraint("UNIVERSE").Using<string>((x, y) => StringUtil.Compare(x, y, true)));
new DictionaryContainsValueConstraint("UNIVERSE").Using<string>((x, y) => string.Compare(x, y, StringComparison.CurrentCultureIgnoreCase)));
}

[Test]
Expand Down
4 changes: 2 additions & 2 deletions src/NUnitFramework/tests/Constraints/EqualConstraintTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -949,7 +949,7 @@ public void UsesProvidedLambda_IntArgs()
[Test, SetCulture("en-US")]
public void UsesProvidedLambda_StringArgs()
{
Assert.That("hello", Is.EqualTo("HELLO").Using<string>((x, y) => StringUtil.Compare(x, y, true)));
Assert.That("hello", Is.EqualTo("HELLO").Using<string>((x, y) => string.Compare(x, y, StringComparison.CurrentCultureIgnoreCase)));
}

[Test]
Expand Down Expand Up @@ -1027,7 +1027,7 @@ public void UsesProvidedPredicateForItemComparison()
var expected = new[] { "yeti", "łysy", "rysiu" };
var actual = new[] { "YETI", "Łysy", "RySiU" };

Assert.That(actual, Is.EqualTo(expected).Using<string>((x, y) => StringUtil.StringsEqual(x, y, true)));
Assert.That(actual, Is.EqualTo(expected).Using<string>((x, y) => string.Equals(x, y, StringComparison.CurrentCultureIgnoreCase)));
}

[Test]
Expand Down

0 comments on commit af7b941

Please sign in to comment.