Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make sure to await async test asserts (2) #20506

Merged
merged 4 commits into from
Apr 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<TargetFrameworks Condition=" '$(OS)' == 'Windows_NT' ">net47;</TargetFrameworks>
<TargetFrameworks Condition=" '$(OS)' != 'Windows_NT' ">netcoreapp2.1</TargetFrameworks>
<IsPackable>false</IsPackable>
<NoWarn>0114;0672;0108;0067;0168;0169;0219;0612;0618;1998;4014</NoWarn>
<NoWarn>0114;0672;0108;0067;0168;0169;0219;0612;0618;1998</NoWarn>
</PropertyGroup>


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<TargetFramework>$(_MauiDotNetTfm)</TargetFramework>
<AssemblyName>Microsoft.Maui.Controls.Core.UnitTests</AssemblyName>
<IsPackable>false</IsPackable>
<NoWarn>0114;0672;0108;0067;0168;0169;0219;0612;0618;1998;4014</NoWarn>
<NoWarn>0114;0672;0108;0067;0168;0169;0219;0612;0618;1998</NoWarn>
<!--
NOTE: Keep this project on C# 9 to avoid changes in overload resolution:
src/Controls/tests/Core.UnitTests/TemplatedItemsListTests.cs(543,11): error CS0121: The call is ambiguous between the following methods or properties: 'Assert.That(TestDelegate, IResolveConstraint)' and 'Assert.That<TActual>(TActual, IResolveConstraint)'
Expand Down
7 changes: 3 additions & 4 deletions src/Controls/tests/Core.UnitTests/DeviceUnitTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using System.Threading.Tasks;
using Microsoft.Maui.Dispatching;
using Microsoft.Maui.UnitTests;
using Xunit;

Expand Down Expand Up @@ -108,7 +107,7 @@ async Task<bool> boom()
Assert.False(invoked, "Action invoked early.");

async Task MethodThatThrows() => await task;
Assert.ThrowsAsync<ApplicationException>(MethodThatThrows);
await Assert.ThrowsAsync<ApplicationException>(MethodThatThrows);
});

[Fact]
Expand Down Expand Up @@ -144,10 +143,10 @@ async Task boom()
Assert.False(invoked, "Action invoked early.");

async Task MethodThatThrows() => await task;
Assert.ThrowsAsync<ApplicationException>(MethodThatThrows);
await Assert.ThrowsAsync<ApplicationException>(MethodThatThrows);
});

private void MockPlatformServices(Action onInvokeOnMainThread, Action<Action> invokeOnMainThread = null)
private static void MockPlatformServices(Action onInvokeOnMainThread, Action<Action> invokeOnMainThread = null)
{
DispatcherProviderStubOptions.InvokeOnMainThread =
action =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,11 @@ public async Task TextPackageCorrectlySetsOnCompatibleTarget(Type fieldType, str

[Theory]
[InlineData(typeof(DatePicker), "12/12/2020 12:00:00 AM")]
public void DateTextPackageCorrectlySetsOnCompatibleTarget(Type fieldType, string result)
public async Task DateTextPackageCorrectlySetsOnCompatibleTarget(Type fieldType, string result)
{
var date = DateTime.Parse(result);
result = date.ToString();
TextPackageCorrectlySetsOnCompatibleTarget(fieldType, result);
await TextPackageCorrectlySetsOnCompatibleTarget(fieldType, result);
}

[Fact]
Expand Down
18 changes: 10 additions & 8 deletions src/Controls/tests/Core.UnitTests/NavigationUnitTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Maui.Handlers;
using Xunit;

namespace Microsoft.Maui.Controls.Core.UnitTests
Expand Down Expand Up @@ -556,7 +555,7 @@ public async Task DoesNotSendBackEventToNonCurrentPage(bool useMaui)
{
var current = new BackButtonPage();
var navPage = new TestNavigationPage(useMaui, current);
navPage.PushAsync(new ContentPage());
await navPage.PushAsync(new ContentPage());

var emitted = false;
current.BackPressed += (sender, args) => emitted = true;
Expand Down Expand Up @@ -836,7 +835,7 @@ public async Task PopModalWithEmptyStackThrows()
var window = new TestWindow(new ContentPage());
var contentPage1 = new ContentPage();
var navigationPage = new TestNavigationPage(true, contentPage1);
Assert.ThrowsAsync<InvalidOperationException>(() => window.Navigation.PopModalAsync());
await Assert.ThrowsAsync<InvalidOperationException>(() => window.Navigation.PopModalAsync());
}

[Fact]
Expand All @@ -846,9 +845,9 @@ public async Task InvalidOperationExceptionIsThrownWhenNavigatingOutsideNavigati
var contentPage1 = new ContentPage();
var contentPage2 = new ContentPage();

Assert.ThrowsAsync<InvalidOperationException>(() => window.Navigation.PushAsync(contentPage1));
Assert.ThrowsAsync<InvalidOperationException>(() => window.Navigation.PopAsync());
Assert.ThrowsAsync<InvalidOperationException>(() => window.Navigation.PopToRootAsync());
await Assert.ThrowsAsync<InvalidOperationException>(() => window.Navigation.PushAsync(contentPage1));
await Assert.ThrowsAsync<InvalidOperationException>(() => window.Navigation.PopAsync());
await Assert.ThrowsAsync<InvalidOperationException>(() => window.Navigation.PopToRootAsync());
Assert.Throws<InvalidOperationException>(() => window.Navigation.InsertPageBefore(contentPage1, contentPage2));
Assert.Throws<InvalidOperationException>(() => window.Navigation.RemovePage(contentPage1));
}
Expand All @@ -859,7 +858,7 @@ public async Task RemoveWrappingIntoNavigationPage()
var window = new TestWindow(new ContentPage());
var contentPage1 = new ContentPage();
var navigationPage = new TestNavigationPage(true, contentPage1);
Assert.ThrowsAsync<InvalidOperationException>(() => window.Navigation.PushAsync(contentPage1));
await Assert.ThrowsAsync<InvalidOperationException>(() => window.Navigation.PushAsync(contentPage1));
}

[Fact]
Expand Down Expand Up @@ -935,8 +934,11 @@ internal class BackButtonPage : ContentPage

protected override bool OnBackButtonPressed()
{
if (BackPressed != null)
if (BackPressed is not null)
{
BackPressed(this, EventArgs.Empty);
}

return Handle;
}
}
Expand Down
10 changes: 4 additions & 6 deletions src/Controls/tests/Core.UnitTests/ScrollViewUnitTests.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Maui.Graphics;
using System.Threading.Tasks;
using NSubstitute;
using Xunit;

Expand Down Expand Up @@ -267,12 +265,12 @@ public void TestScrollToElementNotAnimated()
}

[Fact]
public void TestScrollToInvalid()
public async Task TestScrollToInvalid()
{
var scrollView = new ScrollView();

Assert.ThrowsAsync<ArgumentException>(() => scrollView.ScrollToAsync(new VisualElement(), ScrollToPosition.Center, true));
Assert.ThrowsAsync<ArgumentException>(() => scrollView.ScrollToAsync(null, (ScrollToPosition)500, true));
await Assert.ThrowsAsync<ArgumentException>(() => scrollView.ScrollToAsync(new VisualElement(), ScrollToPosition.Center, true));
await Assert.ThrowsAsync<ArgumentException>(() => scrollView.ScrollToAsync(null, (ScrollToPosition)500, true));
}

[Fact]
Expand Down
4 changes: 3 additions & 1 deletion src/Controls/tests/Core.UnitTests/ShellLifeCycleTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,9 @@ public void EnsureOnAppearingFiresAfterParentIsSet(bool templated)
page.Appearing += (_, __) =>
{
if (page.Parent == null || !parentSet)
{
throw new Exception("Appearing firing before parent set is called");
}

pageAppearing = true;
};
Expand Down Expand Up @@ -185,7 +187,7 @@ public async Task EnsureOnAppearingFiresForPushedPage()
{
Shell shell = new TestShell();
shell.Items.Add(CreateShellItem());
shell.Navigation.PushAsync(new LifeCyclePage());
await shell.Navigation.PushAsync(new LifeCyclePage());
var page = (LifeCyclePage)shell.GetVisiblePage();
Assert.True(page.Appearing);
Assert.True(page.ParentSet);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -682,7 +682,7 @@ public async Task AbsoluteRoutingToPage()

Routing.RegisterRoute("catdetails", typeof(ContentPage));

Assert.ThrowsAnyAsync<Exception>(async () => await shell.GoToAsync($"//catdetails"));
await Assert.ThrowsAnyAsync<Exception>(async () => await shell.GoToAsync($"//catdetails"));
}

[Fact]
Expand Down
6 changes: 3 additions & 3 deletions src/Controls/tests/Core.UnitTests/ShellTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -620,10 +620,10 @@ public async Task UriNavigationTests()
shell.Items.Add(item1);
shell.Items.Add(item2);

shell.GoToAsync("//rootlevelcontent2");
await shell.GoToAsync("//rootlevelcontent2");
Assert.Equal(shell.CurrentItem, item2);

shell.GoToAsync("//rootlevelcontent1");
await shell.GoToAsync("//rootlevelcontent1");
Assert.Equal(shell.CurrentItem, item1);
}

Expand Down Expand Up @@ -1292,7 +1292,7 @@ public async Task GetCurrentPageBetweenSections()
page = shell.CurrentPage;
};

shell.GoToAsync(new ShellNavigationState("//two/tabfour/"));
await shell.GoToAsync(new ShellNavigationState("//two/tabfour/"));
Assert.NotNull(page);
Assert.IsType<ShellTestPage>(page);
Assert.Equal((tabfour as IShellSectionController).PresentedPage, page);
Expand Down
4 changes: 2 additions & 2 deletions src/Controls/tests/Core.UnitTests/ShellUriHandlerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ public async Task RelativeNavigationToShellElementThrows()
shell.Items.Add(item1);
shell.Items.Add(item2);

Assert.ThrowsAnyAsync<Exception>(async () => await shell.GoToAsync($"domestic"));
await Assert.ThrowsAnyAsync<Exception>(async () => await shell.GoToAsync($"domestic"));
}


Expand All @@ -266,7 +266,7 @@ public async Task RelativeNavigationWithRoute()
shell.Items.Add(item2);

Routing.RegisterRoute("catdetails", typeof(ContentPage));
Assert.ThrowsAnyAsync<Exception>(async () => await shell.GoToAsync($"cats/catdetails?name=domestic"));
await Assert.ThrowsAnyAsync<Exception>(async () => await shell.GoToAsync($"cats/catdetails?name=domestic"));

// once relative routing with a stack is fixed then we can remove the above exception check and add below back in
// await shell.GoToAsync($"cats/catdetails?name=domestic")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@ public static async Task Run(Func<Task> asyncMethod)
// Invoke the function and alert the context when it's complete
var task = asyncMethod() ?? throw new InvalidOperationException("No task provided.");

task.ContinueWith(async (t, o) => await context.Complete(), TaskScheduler.Default);
_ = task.ContinueWith(async (t, o) => await context.Complete(), TaskScheduler.Default);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure about this one.


// Start working through the queue
// Start working through the queue
context.RunOnCurrentThread();

await task;
Expand Down
2 changes: 1 addition & 1 deletion src/Controls/tests/Core.UnitTests/ToolbarTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public async Task RemoveRootPageHidesBackButton()
IToolbarElement toolbarElement = window;
var startingPage = new TestNavigationPage(true, new ContentPage());
window.Page = startingPage;
startingPage.Navigation.PushAsync(new ContentPage());
await startingPage.Navigation.PushAsync(new ContentPage());
startingPage.Navigation.RemovePage(startingPage.RootPage);
await Task.Delay(50);
Assert.False(toolbarElement.Toolbar.BackButtonVisible);
Expand Down
18 changes: 13 additions & 5 deletions src/Controls/tests/Core.UnitTests/ViewUnitTests.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
using System;
using System.Threading.Tasks;
using Microsoft.Maui.Controls.Internals;
using Microsoft.Maui.Controls.Shapes;
using Microsoft.Maui.Devices;
using Microsoft.Maui.Graphics;
using Microsoft.Maui.Handlers;
using NSubstitute;
using Xunit;

namespace Microsoft.Maui.Controls.Core.UnitTests
Expand All @@ -23,7 +20,10 @@ public ViewUnitTests()
MockPlatformSizeService.Current.GetPlatformSizeFunc = (ve, widthConstraint, heightConstraint) =>
{
if (widthConstraint < 30)
{
return new SizeRequest(new Size(40, 50));
}

return new SizeRequest(new Size(20, 100));
};
}
Expand Down Expand Up @@ -538,7 +538,7 @@ public async Task StartTimerSimple()
{
var task = new TaskCompletionSource<bool>();

Task.Factory.StartNew(() => Device.StartTimer(TimeSpan.FromMilliseconds(200), () =>
_ = Task.Factory.StartNew(() => Device.StartTimer(TimeSpan.FromMilliseconds(200), () =>
{
task.SetResult(false);
return false;
Expand All @@ -554,11 +554,14 @@ public async Task StartTimerMultiple()
var task = new TaskCompletionSource<int>();

int steps = 0;
Task.Factory.StartNew(() => Device.StartTimer(TimeSpan.FromMilliseconds(200), () =>
_ = Task.Factory.StartNew(() => Device.StartTimer(TimeSpan.FromMilliseconds(200), () =>
{
steps++;
if (steps < 2)
{
return true;
}

task.SetResult(steps);
return false;
}));
Expand Down Expand Up @@ -604,7 +607,9 @@ public void MockBounds()
args.PropertyName == View.YProperty.PropertyName ||
args.PropertyName == View.WidthProperty.PropertyName ||
args.PropertyName == View.HeightProperty.PropertyName)
{
changed = true;
}
};

view.SizeChanged += (sender, args) => changed = true;
Expand Down Expand Up @@ -671,7 +676,10 @@ public void HeightRequestEffectsGetSizeRequest()
MockPlatformSizeService.Current.GetPlatformSizeFunc = (ve, widthConstraint, heightConstraint) =>
{
if (heightConstraint < 30)
{
return new SizeRequest(new Size(40, 50));
}

return new SizeRequest(new Size(20, 100));
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<TargetFramework>$(_MauiDotNetTfm)</TargetFramework>
<AssemblyName>Microsoft.Maui.Controls.Xaml.UnitTests.ExternalAssembly</AssemblyName>
<IsPackable>false</IsPackable>
<NoWarn>0114;0672;0108;0067;0168;0169;0219;0612;0618;1998;4014</NoWarn>
<NoWarn>0114;0672;0108;0067;0168;0169;0219;0612;0618;1998</NoWarn>
<RootNamespace>Controls.Xaml.UnitTests.ExternalAssembly</RootNamespace>
</PropertyGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<TargetFramework>$(_MauiDotNetTfm)</TargetFramework>
<AssemblyName>Microsoft.Maui.Controls.Xaml.UnitTests.InternalsHiddenAssembly</AssemblyName>
<IsPackable>false</IsPackable>
<NoWarn>0114;0672;0108;0067;0168;0169;0219;0612;0618;1998;4014</NoWarn>
<NoWarn>0114;0672;0108;0067;0168;0169;0219;0612;0618;1998</NoWarn>
<RootNamespace>Microsoft.Maui.Controls.Xaml.UnitTests.InternalsHiddenAssembly</RootNamespace>
</PropertyGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<TargetFramework>$(_MauiDotNetTfm)</TargetFramework>
<AssemblyName>Microsoft.Maui.Controls.Xaml.UnitTests.InternalsVisibleAssembly</AssemblyName>
<IsPackable>false</IsPackable>
<NoWarn>0114;0672;0108;0067;0168;0169;0219;0612;0618;1998;4014</NoWarn>
<NoWarn>0114;0672;0108;0067;0168;0169;0219;0612;0618;1998</NoWarn>
<RootNamespace>Microsoft.Maui.Controls.Xaml.UnitTests.InternalsVisibleAssembly</RootNamespace>
</PropertyGroup>

Expand Down
9 changes: 5 additions & 4 deletions src/Essentials/test/UnitTests/AppActions_Tests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.Maui.ApplicationModel;
using Xunit;

Expand All @@ -7,12 +8,12 @@ namespace Tests
public class AppActions_Tests
{
[Fact]
public void AppActions_SetActions() =>
Assert.ThrowsAsync<NotImplementedInReferenceAssemblyException>(() => AppActions.SetAsync(new List<AppAction>()));
public async Task AppActions_SetActions() =>
await Assert.ThrowsAsync<NotImplementedInReferenceAssemblyException>(() => AppActions.SetAsync(new List<AppAction>()));

[Fact]
public void AppActions_GetActions() =>
Assert.ThrowsAsync<NotImplementedInReferenceAssemblyException>(() => AppActions.GetAsync());
public async Task AppActions_GetActions() =>
await Assert.ThrowsAsync<NotImplementedInReferenceAssemblyException>(() => AppActions.GetAsync());

[Fact]
public void AppActions_IsSupported() =>
Expand Down
4 changes: 2 additions & 2 deletions src/Essentials/test/UnitTests/Sms_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace Tests
public class Sms_Tests
{
[Fact]
public Task Sms_Fail_On_NetStandard() =>
Assert.ThrowsAsync<NotImplementedInReferenceAssemblyException>(() => Sms.ComposeAsync());
public async Task Sms_Fail_On_NetStandard() =>
await Assert.ThrowsAsync<NotImplementedInReferenceAssemblyException>(() => Sms.ComposeAsync());
}
}
5 changes: 3 additions & 2 deletions src/Essentials/test/UnitTests/TextToSpeech_Tests.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Threading.Tasks;
using Microsoft.Maui.ApplicationModel;
using Microsoft.Maui.Media;
using Xunit;
Expand All @@ -7,8 +8,8 @@ namespace Tests
public class TextToSpeech_Tests
{
[Fact]
public void TextToSpeech_Speak_Fail_On_NetStandard() =>
Assert.ThrowsAsync<NotImplementedInReferenceAssemblyException>(() => TextToSpeech.SpeakAsync("Xamarin Essentials!"));
public async Task TextToSpeech_Speak_Fail_On_NetStandard() =>
await Assert.ThrowsAsync<NotImplementedInReferenceAssemblyException>(() => TextToSpeech.SpeakAsync("Xamarin Essentials!"));

[Fact]
public void TextToSpeech_Slit_Text()
Expand Down
Loading