Skip to content

Commit 8e0783c

Browse files
EvangelinkCopilot
andauthored
[test-improver] test: add StringComparison overload and null-handling tests for Assert.StartsWith/EndsWith (#8781)
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 80540c8 commit 8e0783c

1 file changed

Lines changed: 166 additions & 0 deletions

File tree

‎test/UnitTests/TestFramework.UnitTests/Assertions/AssertTests.StartsWithEndsWith.cs‎

Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,4 +118,170 @@ Assertion failed. Expected string to not end with the specified suffix.
118118
ex.Data["assert.expected"].Should().Be(ex.ExpectedText);
119119
ex.Data["assert.actual"].Should().Be(ex.ActualText);
120120
}
121+
122+
public void StartsWith_WithOrdinalIgnoreCase_WhenMatchesCaseInsensitively_DoesNotThrow()
123+
=> Assert.StartsWith("FOO", "foobar", StringComparison.OrdinalIgnoreCase);
124+
125+
public void StartsWith_WithOrdinalIgnoreCase_WhenMatchesExactly_DoesNotThrow()
126+
=> Assert.StartsWith("foo", "foobar", StringComparison.OrdinalIgnoreCase);
127+
128+
public void StartsWith_WithOrdinalIgnoreCase_OnFailure_ShouldIncludeComparisonInMessage()
129+
{
130+
string value = "bar";
131+
Action action = () => Assert.StartsWith("foo", value, StringComparison.OrdinalIgnoreCase, "User-provided message");
132+
133+
AssertFailedException ex = action.Should().Throw<AssertFailedException>()
134+
.WithMessage(
135+
"""
136+
Assertion failed. Expected string to start with the specified prefix.
137+
User-provided message
138+
139+
expected prefix: "foo"
140+
actual: "bar"
141+
comparison: OrdinalIgnoreCase
142+
143+
Assert.StartsWith("foo", value)
144+
""")
145+
.Which;
146+
147+
ex.ExpectedText.Should().Be("\"foo\"");
148+
ex.ActualText.Should().Be("\"bar\"");
149+
}
150+
151+
public void StartsWith_WhenValueIsNull_ShouldThrow()
152+
{
153+
Action action = () => Assert.StartsWith("foo", null!);
154+
action.Should().Throw<AssertFailedException>()
155+
.WithMessage("Assert.StartsWith failed. The parameter 'value' is invalid. The value cannot be null.");
156+
}
157+
158+
public void StartsWith_WhenPrefixIsNull_ShouldThrow()
159+
{
160+
Action action = () => Assert.StartsWith(null!, "foobar");
161+
action.Should().Throw<AssertFailedException>()
162+
.WithMessage("Assert.StartsWith failed. The parameter 'expectedPrefix' is invalid. The value cannot be null.");
163+
}
164+
165+
public void DoesNotStartWith_WithOrdinalIgnoreCase_WhenDoesNotMatchCaseInsensitively_DoesNotThrow()
166+
=> Assert.DoesNotStartWith("baz", "foobar", StringComparison.OrdinalIgnoreCase);
167+
168+
public void DoesNotStartWith_WithOrdinalIgnoreCase_OnFailure_ShouldIncludeComparisonInMessage()
169+
{
170+
string value = "foobar";
171+
Action action = () => Assert.DoesNotStartWith("FOO", value, StringComparison.OrdinalIgnoreCase, "User-provided message");
172+
173+
AssertFailedException ex = action.Should().Throw<AssertFailedException>()
174+
.WithMessage(
175+
"""
176+
Assertion failed. Expected string to not start with the specified prefix.
177+
User-provided message
178+
179+
unexpected prefix: "FOO"
180+
actual: "foobar"
181+
comparison: OrdinalIgnoreCase
182+
183+
Assert.DoesNotStartWith("FOO", value)
184+
""")
185+
.Which;
186+
187+
ex.ExpectedText.Should().Be("\"FOO\"");
188+
ex.ActualText.Should().Be("\"foobar\"");
189+
}
190+
191+
public void DoesNotStartWith_WhenValueIsNull_ShouldThrow()
192+
{
193+
Action action = () => Assert.DoesNotStartWith("foo", null!);
194+
action.Should().Throw<AssertFailedException>()
195+
.WithMessage("Assert.DoesNotStartWith failed. The parameter 'value' is invalid. The value cannot be null.");
196+
}
197+
198+
public void DoesNotStartWith_WhenPrefixIsNull_ShouldThrow()
199+
{
200+
Action action = () => Assert.DoesNotStartWith(null!, "foobar");
201+
action.Should().Throw<AssertFailedException>()
202+
.WithMessage("Assert.DoesNotStartWith failed. The parameter 'notExpectedPrefix' is invalid. The value cannot be null.");
203+
}
204+
205+
public void EndsWith_WithOrdinalIgnoreCase_WhenMatchesCaseInsensitively_DoesNotThrow()
206+
=> Assert.EndsWith("BAR", "foobar", StringComparison.OrdinalIgnoreCase);
207+
208+
public void EndsWith_WithOrdinalIgnoreCase_WhenMatchesExactly_DoesNotThrow()
209+
=> Assert.EndsWith("bar", "foobar", StringComparison.OrdinalIgnoreCase);
210+
211+
public void EndsWith_WithOrdinalIgnoreCase_OnFailure_ShouldIncludeComparisonInMessage()
212+
{
213+
string value = "foobar";
214+
Action action = () => Assert.EndsWith("baz", value, StringComparison.OrdinalIgnoreCase, "User-provided message");
215+
216+
AssertFailedException ex = action.Should().Throw<AssertFailedException>()
217+
.WithMessage(
218+
"""
219+
Assertion failed. Expected string to end with the specified suffix.
220+
User-provided message
221+
222+
expected suffix: "baz"
223+
actual: "foobar"
224+
comparison: OrdinalIgnoreCase
225+
226+
Assert.EndsWith("baz", value)
227+
""")
228+
.Which;
229+
230+
ex.ExpectedText.Should().Be("\"baz\"");
231+
ex.ActualText.Should().Be("\"foobar\"");
232+
}
233+
234+
public void EndsWith_WhenValueIsNull_ShouldThrow()
235+
{
236+
Action action = () => Assert.EndsWith("bar", null!);
237+
action.Should().Throw<AssertFailedException>()
238+
.WithMessage("Assert.EndsWith failed. The parameter 'value' is invalid. The value cannot be null.");
239+
}
240+
241+
public void EndsWith_WhenSuffixIsNull_ShouldThrow()
242+
{
243+
Action action = () => Assert.EndsWith(null!, "foobar");
244+
action.Should().Throw<AssertFailedException>()
245+
.WithMessage("Assert.EndsWith failed. The parameter 'expectedSuffix' is invalid. The value cannot be null.");
246+
}
247+
248+
public void DoesNotEndWith_WithOrdinalIgnoreCase_WhenDoesNotMatchCaseInsensitively_DoesNotThrow()
249+
=> Assert.DoesNotEndWith("baz", "foobar", StringComparison.OrdinalIgnoreCase);
250+
251+
public void DoesNotEndWith_WithOrdinalIgnoreCase_OnFailure_ShouldIncludeComparisonInMessage()
252+
{
253+
string value = "foobar";
254+
Action action = () => Assert.DoesNotEndWith("BAR", value, StringComparison.OrdinalIgnoreCase, "User-provided message");
255+
256+
AssertFailedException ex = action.Should().Throw<AssertFailedException>()
257+
.WithMessage(
258+
"""
259+
Assertion failed. Expected string to not end with the specified suffix.
260+
User-provided message
261+
262+
unexpected suffix: "BAR"
263+
actual: "foobar"
264+
comparison: OrdinalIgnoreCase
265+
266+
Assert.DoesNotEndWith("BAR", value)
267+
""")
268+
.Which;
269+
270+
ex.ExpectedText.Should().Be("\"BAR\"");
271+
ex.ActualText.Should().Be("\"foobar\"");
272+
}
273+
274+
public void DoesNotEndWith_WhenValueIsNull_ShouldThrow()
275+
{
276+
Action action = () => Assert.DoesNotEndWith("bar", null!);
277+
action.Should().Throw<AssertFailedException>()
278+
.WithMessage("Assert.DoesNotEndWith failed. The parameter 'value' is invalid. The value cannot be null.");
279+
}
280+
281+
public void DoesNotEndWith_WhenSuffixIsNull_ShouldThrow()
282+
{
283+
Action action = () => Assert.DoesNotEndWith(null!, "foobar");
284+
action.Should().Throw<AssertFailedException>()
285+
.WithMessage("Assert.DoesNotEndWith failed. The parameter 'notExpectedSuffix' is invalid. The value cannot be null.");
286+
}
121287
}

0 commit comments

Comments
 (0)