Skip to content

Commit

Permalink
Assortment of Minor Improvements (shouldly#962)
Browse files Browse the repository at this point in the history
* An assortment of minor improvements

* Set SuppressTfmSupportBuildWarnings

* Make TimeProvider TFM conditional

* Update documentation snippets

* Use common wait times everywhere

* Fix CI

* Add Polyfill

* Update LangVersion

* Fix CI detection

* Fix CI detection

* Add debugging code

* Add more fixes

* Collection expressions everywhere

* Revert unintentional change
  • Loading branch information
slang25 authored Jul 2, 2024
1 parent f7f6ba7 commit b0d088d
Show file tree
Hide file tree
Showing 60 changed files with 294 additions and 220 deletions.
6 changes: 3 additions & 3 deletions documentation/documentation/completeIn.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
<a id='snippet-ShouldCompleteInExamples.ShouldCompleteIn.codeSample.approved.cs'></a>
```cs
Should.CompleteIn(
action: () => { Thread.Sleep(TimeSpan.FromSeconds(2)); },
timeout: TimeSpan.FromSeconds(1),
action: () => { Thread.Sleep(TimeSpan.FromSeconds(15)); },
timeout: TimeSpan.FromSeconds(0.5),
customMessage: "Some additional context");
```
<sup><a href='/src/DocumentationExamples/CodeExamples/ShouldCompleteInExamples.ShouldCompleteIn.codeSample.approved.cs#L1-L4' title='Snippet source file'>snippet source</a> | <a href='#snippet-ShouldCompleteInExamples.ShouldCompleteIn.codeSample.approved.cs' title='Start of snippet'>anchor</a></sup>
Expand All @@ -19,7 +19,7 @@ Should.CompleteIn(
Delegate
should complete in
00:00:01
00:00:00.5000000
but did not
Additional Info:
Expand Down
4 changes: 2 additions & 2 deletions documentation/documentation/equality/shouldBe.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
`ShouldBeExamples` works on all types and compares using `.Equals`.

<!-- snippet: ShouldBeObjects -->
<a id='snippet-shouldbeobjects'></a>
<a id='snippet-ShouldBeObjects'></a>
```cs
var theSimpsonsCat = new Cat { Name = "Santas little helper" };
theSimpsonsCat.Name.ShouldBe("Snowball 2");
```
<sup><a href='/src/DocumentationExamples/ShouldBeExamples.cs#L14-L19' title='Snippet source file'>snippet source</a> | <a href='#snippet-shouldbeobjects' title='Start of snippet'>anchor</a></sup>
<sup><a href='/src/DocumentationExamples/ShouldBeExamples.cs#L14-L19' title='Snippet source file'>snippet source</a> | <a href='#snippet-ShouldBeObjects' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->

**Exception**
Expand Down
13 changes: 5 additions & 8 deletions documentation/documentation/exceptions/throw.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,23 +31,20 @@ System.DivideByZeroException
## ShouldThrowAsync

<!-- snippet: ShouldThrowAsync -->
<a id='snippet-shouldthrowasync'></a>
<a id='snippet-ShouldThrowAsync'></a>
```cs
Func<Task> doSomething = async () =>
{
await Task.Delay(1);
};
Task doSomething() => Task.CompletedTask;
var exception = await Should.ThrowAsync<DivideByZeroException>(() => doSomething());
```
<sup><a href='/src/Shouldly.Tests/ShouldThrowAsync/FuncOfTaskScenarioAsync.cs#L103-L109' title='Snippet source file'>snippet source</a> | <a href='#snippet-shouldthrowasync' title='Start of snippet'>anchor</a></sup>
<sup><a href='/src/Shouldly.Tests/ShouldThrowAsync/FuncOfTaskScenarioAsync.cs#L91-L95' title='Snippet source file'>snippet source</a> | <a href='#snippet-ShouldThrowAsync' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->

**Exception**

Task `doSomething()` <!-- include: FuncOfTaskScenarioAsync.ShouldThrowAsync.approved.txt -->
Task `doSomething()`<!-- include: FuncOfTaskScenarioAsync.ShouldThrowAsync.approved.txt -->
should throw
System.DivideByZeroException
but did not <!-- endInclude -->
but did not<!-- endInclude -->


## ShouldThrow Action Extension
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Should.CompleteIn(
action: () => { Thread.Sleep(TimeSpan.FromSeconds(2)); },
timeout: TimeSpan.FromSeconds(1),
action: () => { Thread.Sleep(TimeSpan.FromSeconds(15)); },
timeout: TimeSpan.FromSeconds(0.5),
customMessage: "Some additional context");
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

Delegate
should complete in
00:00:01
00:00:00.5000000
but did not

Additional Info:
Expand Down
6 changes: 3 additions & 3 deletions src/DocumentationExamples/ShouldCompleteInExamples.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@
public ShouldCompleteInExamples(ITestOutputHelper testOutputHelper) =>
_testOutputHelper = testOutputHelper;

[Fact(Skip = "Flaky test")]
[Fact]
public void ShouldCompleteIn()
{
DocExampleWriter.Document(
() =>
{
Should.CompleteIn(
action: () => { Thread.Sleep(TimeSpan.FromSeconds(2)); },
timeout: TimeSpan.FromSeconds(1),
action: () => { Thread.Sleep(TimeSpan.FromSeconds(15)); },
timeout: TimeSpan.FromSeconds(0.5),
customMessage: "Some additional context");
},
_testOutputHelper);
Expand Down
18 changes: 18 additions & 0 deletions src/Shouldly.Tests/CommonWaitDurations.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
namespace Shouldly.Tests;

internal static class CommonWaitDurations
{
private static readonly bool IsRunningOnContinuousIntegration =
Environment.GetEnvironmentVariable("CI")
?.Equals("true", StringComparison.OrdinalIgnoreCase)
?? false;

public static TimeSpan ShortWait =>
TimeSpan.FromSeconds(IsRunningOnContinuousIntegration ? 0.5 : 0.2);

public static TimeSpan LongWait =>
TimeSpan.FromSeconds(IsRunningOnContinuousIntegration ? 15 : 5);

public static TimeSpan ImmediateTaskTimeout =>
TimeSpan.FromSeconds(IsRunningOnContinuousIntegration ? 2 : 0.5);
}
2 changes: 1 addition & 1 deletion src/Shouldly.Tests/ConventionTests/ShouldlyConventions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public void VerifyItWorks()
var ex = Should.Throw<ConventionFailedException>(() =>
{
var convention = new ShouldlyMethodsShouldHaveCustomMessageOverload();
var types = Types.InCollection(new[] { typeof(TestWithMissingOverloads) }, "Sample");
var types = Types.InCollection([typeof(TestWithMissingOverloads)], "Sample");
Convention.Is(convention, types);
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public class DifferenceHighlighterHelpersTests
[Fact]
public void HighlightDifferencesBetween_IntegerListsSameExceptOne_HighlightsDifference()
{
Should.Throw<ShouldAssertException>(() => new[] { 2, 2, 3 }.ShouldBe(new[] { 1, 2, 3 }))
Should.Throw<ShouldAssertException>(() => new[] { 2, 2, 3 }.ShouldBe([1, 2, 3]))
.Message.ShouldContain("[*2*, 2, 3]");
}

Expand All @@ -21,14 +21,14 @@ public void HighlightDifferencesBetween_ListsSame_DisplaysActualWithoutHighlight
[Fact]
public void HighlightDifferencesBetween_ListsActualHasMoreElements_HighlightsMissingElements()
{
Should.Throw<ShouldAssertException>(() => new[] { 1, 2, 3 }.ShouldBe(new[] { 1 }))
Should.Throw<ShouldAssertException>(() => new[] { 1, 2, 3 }.ShouldBe([1]))
.Message.ShouldContain("[1, *2*, *3*]");
}

[Fact]
public void HighlightDifferencesBetween_ListsActualHasLessElements_HighlightsMissingElements()
{
Should.Throw<ShouldAssertException>(() => new[] { 1 }.ShouldBe(new[] { 1, 2, 3 }))
Should.Throw<ShouldAssertException>(() => new[] { 1 }.ShouldBe([1, 2, 3]))
.Message.ShouldContain("[1, *, *]");
}

Expand All @@ -49,49 +49,52 @@ public void HighlightDifferencesBetween_NotEnumerables_DisplaysActualWithoutHigh
[Fact]
public void HighlightDifferencesBetween_ListsSameElementsInDifferentOrder_HighlightAllAsDifferent()
{
Should.Throw<ShouldAssertException>(() => new[] { 2, 3, 1 }.ShouldBe(new[] { 1, 2, 3 }))
Should.Throw<ShouldAssertException>(() => new[] { 2, 3, 1 }.ShouldBe([1, 2, 3]))
.Message.ShouldContain("[*2*, *3*, *1*]");
}

[Fact]
public void HighlightDifferencesBetween_StringLists_HighlightsDifference()
{
Should.Throw<ShouldAssertException>(() => new[] { "rita", "sue", "betty" }.ShouldBe(new[] { "rita", "sue", "bob" }))
Should.Throw<ShouldAssertException>(() => new[] { "rita", "sue", "betty" }.ShouldBe(["rita", "sue", "bob"]))
.Message.ShouldContain("[\"rita\", \"sue\", *\"betty\"*]");
}

[Fact]
public void HighlightDifferencesBetween_EnumLists_HighlightsDifference()
{
Should.Throw<ShouldAssertException>(() => new[] { Weapons.Screwdriver, Weapons.Axe }.ShouldBe(new[] { Weapons.Chainsaw, Weapons.Axe }))
Should.Throw<ShouldAssertException>(() => new[] { Weapons.Screwdriver, Weapons.Axe }.ShouldBe([Weapons.Chainsaw, Weapons.Axe
]))
.Message.ShouldContain("[*Weapons.Screwdriver*, Weapons.Axe]");
}

[Fact]
public void HighlightDifferencesBetween_BoolLists_HighlightsDifference()
{
Should.Throw<ShouldAssertException>(() => new[] { true, true, false }.ShouldBe(new[] { true, false, true }))
Should.Throw<ShouldAssertException>(() => new[] { true, true, false }.ShouldBe([true, false, true]))
.Message.ShouldContain("[True, *True*, *False*]");
}

[Fact]
public void HighlightDifferencesBetween_ListOfObjectWithEqualsDefined_HighlightsDifference()
{
Should.Throw<ShouldAssertException>(() => new[] { new EqualType("t1"), new EqualType("t3") }.ShouldBe(new[] { new EqualType("t1"), new EqualType("t2") }))
Should.Throw<ShouldAssertException>(() => new[] { new EqualType("t1"), new EqualType("t3") }.ShouldBe([new EqualType("t1"), new EqualType("t2")
]))
.Message.ShouldContain("[t1, *t3*]");
}

[Fact]
public void HighlightDifferencesBetween_ListOfObjectWithoutEqualsDefined_HighlightsEverythingAsDifference()
{
Should.Throw<ShouldAssertException>(() => new[] { new NonEqualType("t1"), new NonEqualType("t2") }.ShouldBe(new[] { new NonEqualType("t1"), new NonEqualType("t2") }))
Should.Throw<ShouldAssertException>(() => new[] { new NonEqualType("t1"), new NonEqualType("t2") }.ShouldBe([new NonEqualType("t1"), new NonEqualType("t2")
]))
.Message.ShouldContain("[*t1*, *t2*]");
}

[Fact]
public void HighlightDifferencesBetween_GenericList_HighlightsDifference()
{
Should.Throw<ShouldAssertException>(() => new List<int> { 1, 4, 3 }.ShouldBe(new() { 1, 2, 3 }))
Should.Throw<ShouldAssertException>(() => new List<int> { 1, 4, 3 }.ShouldBe([1, 2, 3]))
.Message.ShouldContain("[1, *4*, 3]");
}

Expand Down Expand Up @@ -163,7 +166,7 @@ public NonEqualType(string description) =>

private class PureEnumerable : IEnumerable
{
private readonly List<int> _numbers = new();
private readonly List<int> _numbers = [];

public void Add(int number) => _numbers.Add(number);

Expand Down
13 changes: 13 additions & 0 deletions src/Shouldly.Tests/ModuleInitializer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System.Runtime.CompilerServices;
using static Shouldly.Tests.CommonWaitDurations;

namespace Shouldly.Tests;

internal static class ModuleInitializer
{
[ModuleInitializer]
internal static void Initialize()
{
ShouldlyConfiguration.DefaultTaskTimeout = LongWait;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
public class EnumerableOfComplexTypeScenario
{
private readonly IEnumerable<Widget> _aEnumerable = new Widget { Name = "Joe", Enabled = true }.ToEnumerable();
private readonly Widget[] _bArray = { new() { Name = "Joeyjojoshabadoo Jr", Enabled = true } };
private readonly Widget[] _bArray = [new() { Name = "Joeyjojoshabadoo Jr", Enabled = true }];

[Fact]
public void EnumerableOfComplexTypeScenarioShouldFail()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ public class EnumerableOfStringIsInsensitiveScenario
public void EnumerableOfStringIsInsensitiveScenarioShouldFail()
{
Verify.ShouldFail(() =>
new[] { "foo" }.ShouldBe(new[] { "different" }, Case.Insensitive, "Some additional context"),
new[] { "foo" }.ShouldBe(["different"], Case.Insensitive, "Some additional context"),

errorWithSource:
"""
Expand Down Expand Up @@ -39,6 +39,6 @@ Some additional context
[Fact]
public void ShouldPass()
{
new[] { "foo" }.ShouldBe(new[] { "FOo" }, Case.Insensitive);
new[] { "foo" }.ShouldBe(["FOo"], Case.Insensitive);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ public class EnumerableOfStringIsSensitiveScenario
public void EnumerableOfStringIsSensitiveScenarioShouldFail()
{
Verify.ShouldFail(() =>
new[] { "foo" }.ShouldBe(new[] { "FoO" }, Case.Sensitive, "Some additional context"),
new[] { "foo" }.ShouldBe(["FoO"], Case.Sensitive, "Some additional context"),

errorWithSource:
"""
Expand Down Expand Up @@ -39,6 +39,6 @@ Some additional context
[Fact]
public void ShouldPass()
{
new[] { "foo" }.ShouldBe(new[] { "foo" }, Case.Sensitive);
new[] { "foo" }.ShouldBe(["foo"], Case.Sensitive);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ public class EnumerableOfStringScenario
public void EnumerableOfStringScenarioShouldFail()
{
Verify.ShouldFail(() =>
new[] { "foo" }.ShouldBe(new[] { "foo2" }, "Some additional context"),
new[] { "foo" }.ShouldBe(["foo2"], "Some additional context"),

errorWithSource:
"""
Expand Down Expand Up @@ -39,6 +39,6 @@ Some additional context
[Fact]
public void ShouldPass()
{
new[] { "foo" }.ShouldBe(new[] { "foo" });
new[] { "foo" }.ShouldBe(["foo"]);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ public class IgnoreOrderFalseIEnumerableMethodYieldBreak
public void IgnoreOrderFalseIEnumerableMethodYieldBreakShouldFail()
{
Verify.ShouldFail(() =>
GetEmptyEnumerable().ShouldBe(new[] { 2, 4 }, false, "Some additional context"),
GetEmptyEnumerable().ShouldBe([2, 4], false, "Some additional context"),

errorWithSource:
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ public class IgnoreOrderFalseIEnumerableMethodYieldReturn
public void IgnoreOrderFalseIEnumerableMethodYieldReturnShouldFail()
{
Verify.ShouldFail(() =>
GetEnumerable().ShouldBe(new[] { 1, 2 }, false, "Some additional context"),
GetEnumerable().ShouldBe([1, 2], false, "Some additional context"),

errorWithSource:
"""
Expand Down Expand Up @@ -39,7 +39,7 @@ Some additional context
[Fact]
public void ShouldPass()
{
GetEnumerable().ShouldBe(new[] { 1 });
GetEnumerable().ShouldBe([1]);
}

private static IEnumerable<int> GetEnumerable()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ public class IgnoreOrderFalseScenario
public void IgnoreOrderFalseScenarioShouldFail()
{
Verify.ShouldFail(() =>
new List<int> { 1, 4, 2 }.ShouldBe(new[] { 1, 2, 3 }, false, "Some additional context"),
new List<int> { 1, 4, 2 }.ShouldBe([1, 2, 3], false, "Some additional context"),

errorWithSource:
"""
Expand Down Expand Up @@ -39,6 +39,6 @@ Some additional context
[Fact]
public void ShouldPass()
{
new List<int> { 1, 2, 3 }.ShouldBe(new[] { 1, 2, 3 }, ignoreOrder: false);
new List<int> { 1, 2, 3 }.ShouldBe([1, 2, 3], ignoreOrder: false);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ public class IgnoreOrderFalseScenario2
public void IgnoreOrderFalseScenario2ShouldFail()
{
Verify.ShouldFail(() =>
new List<int> { 1, 3, 2 }.ShouldBe(new[] { 1, 2, 3 }, false, "Some additional context"),
new List<int> { 1, 3, 2 }.ShouldBe([1, 2, 3], false, "Some additional context"),

errorWithSource:
"""
Expand Down Expand Up @@ -39,6 +39,6 @@ Some additional context
[Fact]
public void ShouldPass()
{
new List<int> { 1, 2, 3 }.ShouldBe(new[] { 1, 2, 3 }, ignoreOrder: false);
new List<int> { 1, 2, 3 }.ShouldBe([1, 2, 3], ignoreOrder: false);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ public class IgnoreOrderIEnumerableMethodYieldBreak
public void IgnoreOrderIEnumerableMethodYieldBreakShouldFail()
{
Verify.ShouldFail(() =>
GetEmptyEnumerable().ShouldBe(new[] { 2, 4 }, true, "Some additional context"),
GetEmptyEnumerable().ShouldBe([2, 4], true, "Some additional context"),

errorWithSource:
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ public class IgnoreOrderIEnumerableMethodYieldReturn
public void IgnoreOrderIEnumerableMethodYieldReturnShouldFail()
{
Verify.ShouldFail(() =>
GetEnumerable().ShouldBe(new[] { 1, 2 }, true, "Some additional context"),
GetEnumerable().ShouldBe([1, 2], true, "Some additional context"),

errorWithSource:
"""
Expand Down Expand Up @@ -40,7 +40,7 @@ Some additional context
[Fact]
public void ShouldPass()
{
GetEnumerable().ShouldBe(new[] { 1 }, true);
GetEnumerable().ShouldBe([1], true);
}

private static IEnumerable<int> GetEnumerable()
Expand Down
Loading

0 comments on commit b0d088d

Please sign in to comment.