diff --git a/Tests/FluentAssertions.Specs/Exceptions/AsyncFunctionExceptionAssertionSpecs.cs b/Tests/FluentAssertions.Specs/Exceptions/AsyncFunctionExceptionAssertionSpecs.cs index f454271169..0b686b1da3 100644 --- a/Tests/FluentAssertions.Specs/Exceptions/AsyncFunctionExceptionAssertionSpecs.cs +++ b/Tests/FluentAssertions.Specs/Exceptions/AsyncFunctionExceptionAssertionSpecs.cs @@ -84,17 +84,21 @@ public async Task When_async_method_throws_an_empty_AggregateException_it_should await act2.Should().ThrowAsync(); } - [UIFact] - public async Task When_async_method_throws_an_empty_AggregateException_on_UI_thread_it_should_fail() + [Collection("UIFacts")] + public partial class UIFacts { - // Arrange - Func act = () => throw new AggregateException(); + [UIFact] + public async Task When_async_method_throws_an_empty_AggregateException_on_UI_thread_it_should_fail() + { + // Arrange + Func act = () => throw new AggregateException(); - // Act - Func act2 = () => act.Should().NotThrowAsync(); + // Act + Func act2 = () => act.Should().NotThrowAsync(); - // Assert - await act2.Should().ThrowAsync(); + // Assert + await act2.Should().ThrowAsync(); + } } [Fact] @@ -107,14 +111,17 @@ public async Task When_async_method_throws_a_nested_AggregateException_it_should await act.Should().ThrowAsync().WithMessage("That was wrong."); } - [UIFact] - public async Task When_async_method_throws_a_nested_AggregateException_on_UI_thread_it_should_provide_the_message() + public partial class UIFacts { - // Arrange - Func act = () => throw new AggregateException(new ArgumentException("That was wrong.")); + [UIFact] + public async Task When_async_method_throws_a_nested_AggregateException_on_UI_thread_it_should_provide_the_message() + { + // Arrange + Func act = () => throw new AggregateException(new ArgumentException("That was wrong.")); - // Act & Assert - await act.Should().ThrowAsync().WithMessage("That was wrong."); + // Act & Assert + await act.Should().ThrowAsync().WithMessage("That was wrong."); + } } [Fact] @@ -499,17 +506,20 @@ public async Task When_async_method_does_not_throw_async_exception_and_that_was_ await action.Should().NotThrowAsync(); } - [UIFact] - public async Task When_async_method_does_not_throw_async_exception_on_UI_thread_and_that_was_expected_it_should_succeed() + public partial class UIFacts { - // Arrange - var asyncObject = new AsyncClass(); + [UIFact] + public async Task When_async_method_does_not_throw_async_exception_on_UI_thread_and_that_was_expected_it_should_succeed() + { + // Arrange + var asyncObject = new AsyncClass(); - // Act - Func action = () => asyncObject.SucceedAsync(); + // Act + Func action = () => asyncObject.SucceedAsync(); - // Assert - await action.Should().NotThrowAsync(); + // Assert + await action.Should().NotThrowAsync(); + } } [Fact] @@ -605,17 +615,20 @@ public async Task When_subject_throws_expected_async_exact_exception_it_should_s await action.Should().ThrowExactlyAsync("because {0} should do that", "IFoo.Do"); } - [UIFact] - public async Task When_subject_throws_on_UI_thread_expected_async_exact_exception_it_should_succeed() + public partial class UIFacts { - // Arrange - var asyncObject = new AsyncClass(); + [UIFact] + public async Task When_subject_throws_on_UI_thread_expected_async_exact_exception_it_should_succeed() + { + // Arrange + var asyncObject = new AsyncClass(); - // Act - Func action = () => asyncObject.ThrowAsync(); + // Act + Func action = () => asyncObject.ThrowAsync(); - // Assert - await action.Should().ThrowExactlyAsync("because {0} should do that", "IFoo.Do"); + // Assert + await action.Should().ThrowExactlyAsync("because {0} should do that", "IFoo.Do"); + } } [Fact] @@ -1205,34 +1218,37 @@ await action.Should().ThrowAsync() .WithMessage("Did not expect any exceptions after 2s because we passed valid arguments*"); } - [UIFact] - public async Task When_no_exception_should_be_thrown_on_UI_thread_for_async_func_after_wait_time_but_it_was_it_should_throw() + public partial class UIFacts { - // Arrange - var waitTime = 2.Seconds(); - var pollInterval = 10.Milliseconds(); + [UIFact] + public async Task When_no_exception_should_be_thrown_on_UI_thread_for_async_func_after_wait_time_but_it_was_it_should_throw() + { + // Arrange + var waitTime = 2.Seconds(); + var pollInterval = 10.Milliseconds(); - var clock = new FakeClock(); - var timer = clock.StartTimer(); - clock.CompleteAfter(waitTime); + var clock = new FakeClock(); + var timer = clock.StartTimer(); + clock.CompleteAfter(waitTime); - Func throwLongerThanWaitTime = async () => - { - if (timer.Elapsed <= waitTime.Multiply(1.5)) + Func throwLongerThanWaitTime = async () => { - throw new ArgumentException("An exception was forced"); - } + if (timer.Elapsed <= waitTime.Multiply(1.5)) + { + throw new ArgumentException("An exception was forced"); + } - await Task.Yield(); - }; + await Task.Yield(); + }; - // Act - Func action = () => throwLongerThanWaitTime.Should(clock) - .NotThrowAfterAsync(waitTime, pollInterval, "we passed valid arguments"); + // Act + Func action = () => throwLongerThanWaitTime.Should(clock) + .NotThrowAfterAsync(waitTime, pollInterval, "we passed valid arguments"); - // Assert - await action.Should().ThrowAsync() - .WithMessage("Did not expect any exceptions after 2s because we passed valid arguments*"); + // Assert + await action.Should().ThrowAsync() + .WithMessage("Did not expect any exceptions after 2s because we passed valid arguments*"); + } } [Fact] @@ -1263,32 +1279,35 @@ public async Task When_no_exception_should_be_thrown_for_async_func_after_wait_t await act.Should().NotThrowAsync(); } - [UIFact] - public async Task When_no_exception_should_be_thrown_on_UI_thread_for_async_func_after_wait_time_and_none_was_it_should_not_throw() + public partial class UIFacts { - // Arrange - var waitTime = 6.Seconds(); - var pollInterval = 10.Milliseconds(); + [UIFact] + public async Task When_no_exception_should_be_thrown_on_UI_thread_for_async_func_after_wait_time_and_none_was_it_should_not_throw() + { + // Arrange + var waitTime = 6.Seconds(); + var pollInterval = 10.Milliseconds(); - var clock = new FakeClock(); - var timer = clock.StartTimer(); - clock.Delay(waitTime); + var clock = new FakeClock(); + var timer = clock.StartTimer(); + clock.Delay(waitTime); - Func throwShorterThanWaitTime = async () => - { - if (timer.Elapsed <= waitTime.Divide(12)) + Func throwShorterThanWaitTime = async () => { - throw new ArgumentException("An exception was forced"); - } + if (timer.Elapsed <= waitTime.Divide(12)) + { + throw new ArgumentException("An exception was forced"); + } - await Task.Yield(); - }; + await Task.Yield(); + }; - // Act - Func act = () => throwShorterThanWaitTime.Should(clock).NotThrowAfterAsync(waitTime, pollInterval); + // Act + Func act = () => throwShorterThanWaitTime.Should(clock).NotThrowAfterAsync(waitTime, pollInterval); - // Assert - await act.Should().NotThrowAsync(); + // Assert + await act.Should().NotThrowAsync(); + } } #endregion } diff --git a/Tests/FluentAssertions.Specs/Execution/CallerIdentifierSpecs.cs b/Tests/FluentAssertions.Specs/Execution/CallerIdentifierSpecs.cs index 61dbaac438..b463e4c6b9 100644 --- a/Tests/FluentAssertions.Specs/Execution/CallerIdentifierSpecs.cs +++ b/Tests/FluentAssertions.Specs/Execution/CallerIdentifierSpecs.cs @@ -455,20 +455,24 @@ public void When_the_method_has_Should_prefix_it_should_read_whole_method() .WithMessage("Expected foo.ShouldReturnSomeBool() to be false*"); } - [UIFact] - public async Task Caller_identification_should_also_work_for_statements_following_async_code() + [Collection("UIFacts")] + public partial class UIFacts { - // Arrange - const string someText = "Hello"; - Func task = async () => await Task.Yield(); - - // Act - await task.Should().NotThrowAsync(); - Action act = () => someText.Should().Be("Hi"); - - // Assert - act.Should().Throw() - .WithMessage("*someText*", "it should capture the variable name"); + [UIFact] + public async Task Caller_identification_should_also_work_for_statements_following_async_code() + { + // Arrange + const string someText = "Hello"; + Func task = async () => await Task.Yield(); + + // Act + await task.Should().NotThrowAsync(); + Action act = () => someText.Should().Be("Hi"); + + // Assert + act.Should().Throw() + .WithMessage("*someText*", "it should capture the variable name"); + } } [Fact] diff --git a/Tests/FluentAssertions.Specs/Specialized/TaskAssertionSpecs.cs b/Tests/FluentAssertions.Specs/Specialized/TaskAssertionSpecs.cs index 3f6f5237f9..defe801a43 100644 --- a/Tests/FluentAssertions.Specs/Specialized/TaskAssertionSpecs.cs +++ b/Tests/FluentAssertions.Specs/Specialized/TaskAssertionSpecs.cs @@ -79,20 +79,24 @@ public async Task Sync_work_in_async_method_is_taken_into_account() await action.Should().ThrowAsync(); } - [UIFact] - public async Task When_task_completes_on_UI_thread_fast_async_it_should_succeed() + [Collection("UIFacts")] + public partial class UIFacts { - // Arrange - var timer = new FakeClock(); - var taskFactory = new TaskCompletionSource(); + [UIFact] + public async Task When_task_completes_on_UI_thread_fast_async_it_should_succeed() + { + // Arrange + var timer = new FakeClock(); + var taskFactory = new TaskCompletionSource(); - // Act - Func action = () => taskFactory.Awaiting(t => (Task)t.Task).Should(timer).CompleteWithinAsync(100.Milliseconds()); - taskFactory.SetResult(true); - timer.Complete(); + // Act + Func action = () => taskFactory.Awaiting(t => (Task)t.Task).Should(timer).CompleteWithinAsync(100.Milliseconds()); + taskFactory.SetResult(true); + timer.Complete(); - // Assert - await action.Should().NotThrowAsync(); + // Assert + await action.Should().NotThrowAsync(); + } } [Fact] @@ -110,37 +114,40 @@ public async Task When_task_completes_slow_async_it_should_fail() await action.Should().ThrowAsync(); } - [UIFact] - public async Task When_task_completes_on_UI_thread_slow_async_it_should_fail() + public partial class UIFacts { - // Arrange - var timer = new FakeClock(); - var taskFactory = new TaskCompletionSource(); + [UIFact] + public async Task When_task_completes_on_UI_thread_slow_async_it_should_fail() + { + // Arrange + var timer = new FakeClock(); + var taskFactory = new TaskCompletionSource(); - // Act - Func action = () => taskFactory.Awaiting(t => (Task)t.Task).Should(timer).CompleteWithinAsync(100.Milliseconds()); - timer.Complete(); + // Act + Func action = () => taskFactory.Awaiting(t => (Task)t.Task).Should(timer).CompleteWithinAsync(100.Milliseconds()); + timer.Complete(); - // Assert - await action.Should().ThrowAsync(); - } + // Assert + await action.Should().ThrowAsync(); + } - [UIFact] - public async Task When_task_is_checking_synchronization_context_on_UI_thread_it_should_succeed() - { - // Arrange - Func task = CheckContextAsync; + [UIFact] + public async Task When_task_is_checking_synchronization_context_on_UI_thread_it_should_succeed() + { + // Arrange + Func task = CheckContextAsync; - // Act - Func action = () => this.Awaiting(x => task()).Should().CompleteWithinAsync(1.Seconds()); + // Act + Func action = () => this.Awaiting(x => task()).Should().CompleteWithinAsync(1.Seconds()); - // Assert - await action.Should().NotThrowAsync(); + // Assert + await action.Should().NotThrowAsync(); - async Task CheckContextAsync() - { - await Task.Delay(1); - SynchronizationContext.Current.Should().NotBeNull(); + async Task CheckContextAsync() + { + await Task.Delay(1); + SynchronizationContext.Current.Should().NotBeNull(); + } } } } diff --git a/Tests/FluentAssertions.Specs/Specialized/TaskOfTAssertionSpecs.cs b/Tests/FluentAssertions.Specs/Specialized/TaskOfTAssertionSpecs.cs index 64291955ee..d713c177f0 100644 --- a/Tests/FluentAssertions.Specs/Specialized/TaskOfTAssertionSpecs.cs +++ b/Tests/FluentAssertions.Specs/Specialized/TaskOfTAssertionSpecs.cs @@ -190,27 +190,31 @@ public async Task When_task_does_not_throw_async_it_should_succeed() await action.Should().NotThrowAsync(); } - [UIFact] - public async Task When_task_does_not_throw_async_on_UI_thread_it_should_succeed() + [Collection("UIFacts")] + public partial class UIFacts { - // Arrange - var timer = new FakeClock(); - var taskFactory = new TaskCompletionSource(); - - // Act - Func action = async () => + [UIFact] + public async Task When_task_does_not_throw_async_on_UI_thread_it_should_succeed() { - Func> func = () => taskFactory.Task; + // Arrange + var timer = new FakeClock(); + var taskFactory = new TaskCompletionSource(); - (await func.Should(timer).NotThrowAsync()) - .Which.Should().Be(42); - }; + // Act + Func action = async () => + { + Func> func = () => taskFactory.Task; - taskFactory.SetResult(42); - timer.Complete(); + (await func.Should(timer).NotThrowAsync()) + .Which.Should().Be(42); + }; - // Assert - await action.Should().NotThrowAsync(); + taskFactory.SetResult(42); + timer.Complete(); + + // Assert + await action.Should().NotThrowAsync(); + } } [Fact] @@ -231,22 +235,25 @@ public async Task When_task_throws_async_it_should_fail() await action.Should().ThrowAsync(); } - [UIFact] - public async Task When_task_throws_async_on_UI_thread_it_should_fail() + public partial class UIFacts { - // Arrange - var timer = new FakeClock(); - - // Act - Func action = () => + [UIFact] + public async Task When_task_throws_async_on_UI_thread_it_should_fail() { - Func> func = () => throw new AggregateException(); + // Arrange + var timer = new FakeClock(); - return func.Should(timer).NotThrowAsync(); - }; + // Act + Func action = () => + { + Func> func = () => throw new AggregateException(); - // Assert - await action.Should().ThrowAsync(); + return func.Should(timer).NotThrowAsync(); + }; + + // Assert + await action.Should().ThrowAsync(); + } } [Fact] @@ -351,35 +358,38 @@ await action.Should().ThrowAsync() .WithMessage("Did not expect any exceptions after 2s because we passed valid arguments*"); } - [UIFact] - public async Task When_no_exception_should_be_thrown_async_on_UI_thread_after_wait_time_but_it_was_it_should_throw() + public partial class UIFacts { - // Arrange - var waitTime = 2.Seconds(); - var pollInterval = 10.Milliseconds(); + [UIFact] + public async Task When_no_exception_should_be_thrown_async_on_UI_thread_after_wait_time_but_it_was_it_should_throw() + { + // Arrange + var waitTime = 2.Seconds(); + var pollInterval = 10.Milliseconds(); - var clock = new FakeClock(); - var timer = clock.StartTimer(); - clock.CompleteAfter(waitTime); + var clock = new FakeClock(); + var timer = clock.StartTimer(); + clock.CompleteAfter(waitTime); - Func> throwLongerThanWaitTime = async () => - { - if (timer.Elapsed <= waitTime.Multiply(1.5)) + Func> throwLongerThanWaitTime = async () => { - throw new ArgumentException("An exception was forced"); - } + if (timer.Elapsed <= waitTime.Multiply(1.5)) + { + throw new ArgumentException("An exception was forced"); + } - await Task.Yield(); - return 42; - }; + await Task.Yield(); + return 42; + }; - // Act - Func action = () => throwLongerThanWaitTime.Should(clock) - .NotThrowAfterAsync(waitTime, pollInterval, "we passed valid arguments"); + // Act + Func action = () => throwLongerThanWaitTime.Should(clock) + .NotThrowAfterAsync(waitTime, pollInterval, "we passed valid arguments"); - // Assert - await action.Should().ThrowAsync() - .WithMessage("Did not expect any exceptions after 2s because we passed valid arguments*"); + // Assert + await action.Should().ThrowAsync() + .WithMessage("Did not expect any exceptions after 2s because we passed valid arguments*"); + } } [Fact] @@ -415,37 +425,40 @@ public async Task When_no_exception_should_be_thrown_async_after_wait_time_and_n await act.Should().NotThrowAsync(); } - [UIFact] - public async Task When_no_exception_should_be_thrown_async_on_UI_thread_after_wait_time_and_none_was_it_should_not_throw() + public partial class UIFacts { - // Arrange - var waitTime = 6.Seconds(); - var pollInterval = 10.Milliseconds(); + [UIFact] + public async Task When_no_exception_should_be_thrown_async_on_UI_thread_after_wait_time_and_none_was_it_should_not_throw() + { + // Arrange + var waitTime = 6.Seconds(); + var pollInterval = 10.Milliseconds(); - var clock = new FakeClock(); - var timer = clock.StartTimer(); - clock.Delay(waitTime); + var clock = new FakeClock(); + var timer = clock.StartTimer(); + clock.Delay(waitTime); - Func> throwShorterThanWaitTime = async () => - { - if (timer.Elapsed <= waitTime.Divide(12)) + Func> throwShorterThanWaitTime = async () => { - throw new ArgumentException("An exception was forced"); - } + if (timer.Elapsed <= waitTime.Divide(12)) + { + throw new ArgumentException("An exception was forced"); + } - await Task.Yield(); - return 42; - }; + await Task.Yield(); + return 42; + }; - // Act - Func act = async () => - { - (await throwShorterThanWaitTime.Should(clock).NotThrowAfterAsync(waitTime, pollInterval)) - .Which.Should().Be(42); - }; + // Act + Func act = async () => + { + (await throwShorterThanWaitTime.Should(clock).NotThrowAfterAsync(waitTime, pollInterval)) + .Which.Should().Be(42); + }; - // Assert - await act.Should().NotThrowAsync(); + // Assert + await act.Should().NotThrowAsync(); + } } #endregion diff --git a/Tests/FluentAssertions.Specs/UIFactsDefinition.cs b/Tests/FluentAssertions.Specs/UIFactsDefinition.cs new file mode 100644 index 0000000000..40bbd58db4 --- /dev/null +++ b/Tests/FluentAssertions.Specs/UIFactsDefinition.cs @@ -0,0 +1,8 @@ +using Xunit; + +namespace FluentAssertions.Specs +{ + // Try to stabilize UIFact tests + [CollectionDefinition("UIFacts", DisableParallelization = true)] + public class UIFactsDefinition { } +}