diff --git a/.globalconfig b/.globalconfig index 31939ab4f57f..50b667a8b39f 100644 --- a/.globalconfig +++ b/.globalconfig @@ -154,6 +154,9 @@ dotnet_diagnostic.IDE0051.severity = warning # MSTEST0004: Public types should be test classes # dotnet_diagnostic.MSTEST0004.severity = warning +# MSTEST0006: Avoid '[ExpectedException]' +dotnet_diagnostic.MSTEST0006.severity = warning + # MSTEST0007: Use test attributes only on test methods dotnet_diagnostic.MSTEST0007.severity = warning diff --git a/src/Uno.UI.RuntimeTests/Tests/Windows_Data/Pdf/Given_PdfDocument.cs b/src/Uno.UI.RuntimeTests/Tests/Windows_Data/Pdf/Given_PdfDocument.cs index 4784bdcd4847..7dbf10710efc 100644 --- a/src/Uno.UI.RuntimeTests/Tests/Windows_Data/Pdf/Given_PdfDocument.cs +++ b/src/Uno.UI.RuntimeTests/Tests/Windows_Data/Pdf/Given_PdfDocument.cs @@ -121,31 +121,33 @@ public async Task When_LoadFromStreamAsync() } [TestMethod] -#if XAMARIN_ANDROID - [ExpectedException(typeof(NotImplementedException))] -#endif public async Task When_LoadFromStreamAsync_With_Valid_Password() { var stream = GetSreamFromResource(PdfDocument_Name_Protected)?.AsRandomAccessStream(); Assert.IsNotNull(stream, "Not valid stream"); - PdfDocument? pdfDocument = await PdfDocument.LoadFromStreamAsync(stream, PdfDocument_Password_Valid); +#if __ANDROID__ + await Assert.ThrowsAsync(() => await PdfDocument.LoadFromStreamAsync(stream, PdfDocument_Password_Valid)); +#else + var pdfDocument = await PdfDocument.LoadFromStreamAsync(stream, PdfDocument_Password_Valid) await CheckDocumentAsync(pdfDocument, ReferencePageImage_ProtectedUri, hasPassword: true); +#endif } [TestMethod] -#if XAMARIN_ANDROID - [ExpectedException(typeof(NotImplementedException))] -#elif !HAS_UNO - [ExpectedException(typeof(Exception))] -#endif public async Task When_LoadFromStreamAsync_With_Wrong_Password() { var stream = GetSreamFromResource(PdfDocument_Name_Protected)?.AsRandomAccessStream(); Assert.IsNotNull(stream, "Not valid stream"); - PdfDocument? pdfDocument = await PdfDocument.LoadFromStreamAsync(stream, PdfDocument_Password_Invalid); +#if __ANDROID__ + await Assert.ThrowsAsync(() => await PdfDocument.LoadFromStreamAsync(stream, PdfDocument_Password_Invalid)); +#elif !HAS_UNO + await Assert.ThrowsAsync(() => await PdfDocument.LoadFromStreamAsync(stream, PdfDocument_Password_Invalid)); +#else + var pdfDocument = await PdfDocument.LoadFromStreamAsync(stream, PdfDocument_Password_Valid); await CheckDocumentAsync(pdfDocument, ReferencePageImage_ProtectedUri, hasPassword: true); +#endif } @@ -166,9 +168,6 @@ public async Task When_LoadFromFileAsync() } [TestMethod] -#if XAMARIN_ANDROID - [ExpectedException(typeof(NotImplementedException))] -#endif public async Task When_LoadFromFileAsync_Valid_Password() { var testFolderName = Guid.NewGuid().ToString(); @@ -180,16 +179,16 @@ public async Task When_LoadFromFileAsync_Valid_Password() Assert.IsNotNull(source); await source.CopyToAsync(ws); } + +#if __ANDROID__ + await Assert.ThrowsAsync(() => await PdfDocument.LoadFromStreamAsync(stream, PdfDocument_Password_Valid)); +#else var pdfDocument = await PdfDocument.LoadFromFileAsync(file, PdfDocument_Password_Valid); await CheckDocumentAsync(pdfDocument, ReferencePageImage_ProtectedUri, hasPassword: true); +#endif } [TestMethod] -#if XAMARIN_ANDROID - [ExpectedException(typeof(NotImplementedException))] -#elif !HAS_UNO - [ExpectedException(typeof(Exception))] -#endif public async Task When_LoadFromFileAsync_Wrong_Password() { var testFolderName = Guid.NewGuid().ToString(); @@ -201,8 +200,15 @@ public async Task When_LoadFromFileAsync_Wrong_Password() Assert.IsNotNull(source); await source.CopyToAsync(ws); } + +#if __ANDROID__ + await Assert.ThrowsAsync(() => await PdfDocument.LoadFromFileAsync(file, PdfDocument_Password_Invalid)); +#elif !HAS_UNO + await Assert.ThrowsAsync(() => await PdfDocument.LoadFromFileAsync(file, PdfDocument_Password_Invalid)); +#else var pdfDocument = await PdfDocument.LoadFromFileAsync(file, PdfDocument_Password_Invalid); await CheckDocumentAsync(pdfDocument, ReferencePageImage_ProtectedUri, hasPassword: true); +#endif } [TestMethod] diff --git a/src/Uno.UI.RuntimeTests/Tests/Windows_Storage/Given_StorageFile.cs b/src/Uno.UI.RuntimeTests/Tests/Windows_Storage/Given_StorageFile.cs index ecf792d9a329..b091c9dd6d54 100644 --- a/src/Uno.UI.RuntimeTests/Tests/Windows_Storage/Given_StorageFile.cs +++ b/src/Uno.UI.RuntimeTests/Tests/Windows_Storage/Given_StorageFile.cs @@ -118,11 +118,12 @@ public async Task When_Open_Read_AndGetInputStream() } [TestMethod] - [ExpectedException(typeof(NotSupportedException))] public async Task When_Open_Read_AndGetOutputStream() { var path = GetRandomFilePath(); - var file = (await (await GetFile(path)).OpenAsync(FileAccessMode.Read)).GetOutputStreamAt(0); + var storageFile = await GetFile(path); + var stream = storageFile.OpenAsync(FileAccessMode.Read); + Assert.Throws(() => stream.GetOutputStreamAt(0)); } [TestMethod] diff --git a/src/Uno.UI.RuntimeTests/Tests/Windows_Storage/Streams/Given_Buffer.cs b/src/Uno.UI.RuntimeTests/Tests/Windows_Storage/Streams/Given_Buffer.cs index 35df8ebae482..ec1d325d253d 100644 --- a/src/Uno.UI.RuntimeTests/Tests/Windows_Storage/Streams/Given_Buffer.cs +++ b/src/Uno.UI.RuntimeTests/Tests/Windows_Storage/Streams/Given_Buffer.cs @@ -25,12 +25,11 @@ public void When_Create_Then_LengthIsZero() } [TestMethod] - [ExpectedException(typeof(ArgumentException))] public void When_SetLengthGreaterThanCapacity() { var sut = new Buffer(42); - sut.Length = 43; + Assert.Throws(() => sut.Length = 43); } [TestMethod] @@ -110,12 +109,11 @@ public void When_GetByteGreaterThanLength() } [TestMethod] - [ExpectedException(typeof(ArgumentException))] public void When_GetByteGreaterThanCapacity() { var sut = new Buffer(42); - sut.GetByte(42); + Assert.Throws(() => sut.GetByte(42)); } [TestMethod] diff --git a/src/Uno.UI.RuntimeTests/Tests/Windows_System/Given_DispatcherQueueTimer.cs b/src/Uno.UI.RuntimeTests/Tests/Windows_System/Given_DispatcherQueueTimer.cs index 8966fe6577b1..98098e16c72d 100644 --- a/src/Uno.UI.RuntimeTests/Tests/Windows_System/Given_DispatcherQueueTimer.cs +++ b/src/Uno.UI.RuntimeTests/Tests/Windows_System/Given_DispatcherQueueTimer.cs @@ -154,12 +154,11 @@ public async Task When_StartAndStopFromBackgroundThread() } [TestMethod] - [ExpectedException(typeof(ArgumentException))] public void When_SetNegativeInterval() { var timer = DispatcherQueue.GetForCurrentThread().CreateTimer(); - - timer.Interval = TimeSpan.FromMilliseconds(-100); + var negativeTimeSpan = TimeSpan.FromMilliseconds(-100); + Assert.Throws(() => timer.Interval = negativeTimeSpan); } [TestMethod] diff --git a/src/Uno.UI.RuntimeTests/Tests/Windows_UI_Xaml/Given_FrameworkElement.cs b/src/Uno.UI.RuntimeTests/Tests/Windows_UI_Xaml/Given_FrameworkElement.cs index 3a8c9970aae8..edc700c4aad0 100644 --- a/src/Uno.UI.RuntimeTests/Tests/Windows_UI_Xaml/Given_FrameworkElement.cs +++ b/src/Uno.UI.RuntimeTests/Tests/Windows_UI_Xaml/Given_FrameworkElement.cs @@ -396,7 +396,6 @@ public void When_Using_Nan_And_Auto_Sizes(string w, string h, double expectedW, [DataRow(" Infinity")] [DataRow("-Infinity ")] [DataRow(" Infinity")] - [ExpectedException(typeof(ArgumentException))] #if !WINAPPSDK [Ignore] #endif @@ -405,10 +404,8 @@ public void When_Setting_Sizes_To_Invalid_Values_Then_Should_Throw(string varian using var _ = new AssertionScope(); var sut = new ContentControl { Tag = variant }; - - sut.SetBinding( - FrameworkElement.WidthProperty, - new Binding { Source = sut, Path = new PropertyPath("Tag") }); + var binding = new Binding { Source = sut, Path = new PropertyPath("Tag") }; + Assert.Throws(() => sut.SetBinding(FrameworkElement.WidthProperty, binding)); } private sealed partial class MyPanel : Panel diff --git a/src/Uno.UI.RuntimeTests/Tests/Windows_UI_Xaml_Media/Given_Geometry.cs b/src/Uno.UI.RuntimeTests/Tests/Windows_UI_Xaml_Media/Given_Geometry.cs index 6dcaa1e3bee5..8d57c2603eca 100644 --- a/src/Uno.UI.RuntimeTests/Tests/Windows_UI_Xaml_Media/Given_Geometry.cs +++ b/src/Uno.UI.RuntimeTests/Tests/Windows_UI_Xaml_Media/Given_Geometry.cs @@ -134,10 +134,10 @@ public void EmptyGeometryGroup_CheckBounds() } [TestMethod] - [ExpectedException(typeof(Exception))] // Catastrophic Failure on UWP public void EmptyGeometry_CheckBounds() { - Console.WriteLine(Geometry.Empty.Bounds); + // Catastrophic Failure on UWP + Assert.Throws(() => _ = Geometry.Empty.Bounds); } #if __SKIA__