Skip to content

Commit a60ec4e

Browse files
Fix TrimmedEquals logic and add negative test cases
Correct TrimmedEquals null/empty handling for StringSegment overloads. Update tests to use correct overloads and add negative cases for both char and span trimming.
1 parent 2e2212a commit a60ec4e

2 files changed

Lines changed: 25 additions & 4 deletions

File tree

Source/Extensions.Equals.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -146,9 +146,8 @@ public static bool TrimmedEquals(this string? source, ReadOnlySpan<char> other,
146146
/// <inheritdoc cref="TrimmedEquals(string?, StringSegment, ReadOnlySpan{char}, StringComparison)"/>
147147
public static bool TrimmedEquals(this string? source, StringSegment other, char trimChar, StringComparison comparisonType = StringComparison.Ordinal)
148148
{
149-
bool otherHasValue = !other.HasValue;
150-
if (source is null) return otherHasValue;
151-
if (otherHasValue) return false;
149+
if (source is null) return !other.HasValue;
150+
if (!other.HasValue) return false;
152151
int slen = source.Length, olen = other.Length;
153152
if (slen < olen) return false;
154153
ReadOnlySpan<char> span = source.AsSpan().Trim(trimChar);

Tests/EqualityTests.cs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ public void TrimmedEquals(string value, string? other, StringComparison comparis
102102
public void TrimmedEqualsChar(string value, string? other, StringComparison comparison = StringComparison.Ordinal)
103103
{
104104
Assert.True(value.TrimmedEquals(other, ' ', comparison));
105-
Assert.True(value.TrimmedEquals(other!.AsSegment(), comparison));
105+
Assert.True(value.TrimmedEquals(other!.AsSegment(), ' ', comparison));
106106
Assert.True(value.TrimmedEquals(other.AsSpan(), ' ', comparison));
107107
}
108108

@@ -115,6 +115,28 @@ public void TrimmedEqualsChar(string value, string? other, StringComparison comp
115115
public void TrimmedEqualsChars(string? value, string? other, StringComparison comparison = StringComparison.Ordinal)
116116
{
117117
Assert.True(value.TrimmedEquals(other, Chars.Span, comparison));
118+
Assert.True(value.TrimmedEquals(other!.AsSegment(), Chars.Span, comparison));
118119
Assert.True(value.TrimmedEquals(other.AsSpan(), Chars.Span, comparison));
119120
}
121+
122+
[Fact]
123+
public void TrimmedEqualsCharNegativeCases()
124+
{
125+
Assert.False(" hello ".TrimmedEquals("world", ' '));
126+
Assert.False(" hello ".TrimmedEquals("world".AsSegment(), ' '));
127+
Assert.False(" hello ".TrimmedEquals("hell", ' '));
128+
Assert.False(" hello ".TrimmedEquals("hell".AsSegment(), ' '));
129+
Assert.False("x".TrimmedEquals("y".AsSegment(), ' '));
130+
Assert.False(" x ".TrimmedEquals("y".AsSegment(), ' '));
131+
}
132+
133+
[Fact]
134+
public void TrimmedEqualsCharsNegativeCases()
135+
{
136+
Assert.False(" hello ".TrimmedEquals("world", Chars.Span));
137+
Assert.False(" hello ".TrimmedEquals("world".AsSegment(), Chars.Span));
138+
Assert.False(" hello ".TrimmedEquals("hell", Chars.Span));
139+
Assert.False(" hello ".TrimmedEquals("hell".AsSegment(), Chars.Span));
140+
Assert.False(" x ".TrimmedEquals("y".AsSegment(), Chars.Span));
141+
}
120142
}

0 commit comments

Comments
 (0)