Skip to content

Commit

Permalink
test: Avoid using ExpectedExceptionAttribute
Browse files Browse the repository at this point in the history
  • Loading branch information
Youssef1313 committed Jan 18, 2025
1 parent 4c8b326 commit 154cbec
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 34 deletions.
3 changes: 3 additions & 0 deletions .globalconfig
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
42 changes: 24 additions & 18 deletions src/Uno.UI.RuntimeTests/Tests/Windows_Data/Pdf/Given_PdfDocument.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<NotImplementedException>(() => 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<NotImplementedException>(() => await PdfDocument.LoadFromStreamAsync(stream, PdfDocument_Password_Invalid));
#elif !HAS_UNO
await Assert.ThrowsAsync<Exception>(() => 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
}


Expand All @@ -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();
Expand All @@ -180,16 +179,16 @@ public async Task When_LoadFromFileAsync_Valid_Password()
Assert.IsNotNull(source);
await source.CopyToAsync(ws);
}

#if __ANDROID__
await Assert.ThrowsAsync<NotImplementedException>(() => 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();
Expand All @@ -201,8 +200,15 @@ public async Task When_LoadFromFileAsync_Wrong_Password()
Assert.IsNotNull(source);
await source.CopyToAsync(ws);
}

#if __ANDROID__
await Assert.ThrowsAsync<NotImplementedException>(() => await PdfDocument.LoadFromFileAsync(file, PdfDocument_Password_Invalid));
#elif !HAS_UNO
await Assert.ThrowsAsync<Exception>(() => 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]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<NotSuppotedException>(() => stream.GetOutputStreamAt(0));
}

[TestMethod]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<ArgumentException>(() => sut.Length = 43);
}

[TestMethod]
Expand Down Expand Up @@ -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<ArgumentException>(() => sut.GetByte(42));
}

[TestMethod]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<ArgumentException>(() => timer.Interval = negativeTimeSpan);
}

[TestMethod]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<ArgumentException>(() => sut.SetBinding(FrameworkElement.WidthProperty, binding));
}

private sealed partial class MyPanel : Panel
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Exception>(() => _ = Geometry.Empty.Bounds);
}

#if __SKIA__
Expand Down

0 comments on commit 154cbec

Please sign in to comment.