Skip to content

Commit

Permalink
Add VerifyThrowsT
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonCropp committed Sep 14, 2024
1 parent 2a26a70 commit ee4e46b
Show file tree
Hide file tree
Showing 24 changed files with 198 additions and 484 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,15 @@ public void LoadAssemblyShouldThrowExceptionIfTheFileNameHasInvalidCharacters()
string filePath = "temp<>txt";
void A() => _fileOperations.LoadAssembly(filePath, false);

Type expectedException;
#if NETCOREAPP
expectedException = typeof(FileNotFoundException);
VerifyThrows<FileNotFoundException>(A);
#else
expectedException = typeof(ArgumentException);
VerifyThrows<ArgumentException>(A);
#endif

Exception ex = VerifyThrows(A);
Verify(ex.GetType() == expectedException);
}

public void LoadAssemblyShouldThrowExceptionIfFileIsNotFound()
{
string filePath = "temptxt";
void A() => _fileOperations.LoadAssembly(filePath, false);
Exception ex = VerifyThrows(A);
Verify(ex is FileNotFoundException);
}
public void LoadAssemblyShouldThrowExceptionIfFileIsNotFound() =>
VerifyThrows<FileNotFoundException>(() => _fileOperations.LoadAssembly("temptxt", false));

public void LoadAssemblyShouldLoadAssemblyInCurrentContext()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -224,8 +224,7 @@ public void ToSettingsShouldThrowExceptionWhenRunSettingsXmlIsWrong()

void ShouldThrowException() => MSTestAdapterSettings.ToSettings(reader);

Exception ex = VerifyThrows(ShouldThrowException);
Verify(ex is SettingsException);
VerifyThrows<SettingsException>(ShouldThrowException);
}

#endregion
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,8 @@ public void SettingsShouldReturnInitializedSettings()
Verify(!MSTestSettingsProvider.Settings.DeploymentEnabled);
}

public void LoadShouldThrowIfReaderIsNull()
{
Exception exception = VerifyThrows(() => _settingsProvider.Load(null));
Verify(exception is ArgumentNullException);
}
public void LoadShouldThrowIfReaderIsNull() =>
VerifyThrows<ArgumentNullException>(() => _settingsProvider.Load(null));

public void LoadShouldReadAndFillInSettings()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,21 +140,15 @@ public void AddResultFileShouldThrowIfFileNameIsNull()
{
_testContextImplementation = new TestContextImplementation(_testMethod.Object, new ThreadSafeStringWriter(null, "test"), _properties);

Exception exception =
VerifyThrows(() => _testContextImplementation.AddResultFile(null));

Verify(typeof(ArgumentException) == exception.GetType());
ArgumentException exception = VerifyThrows<ArgumentException>(() => _testContextImplementation.AddResultFile(null));
Verify(exception.Message.Contains(Resource.Common_CannotBeNullOrEmpty));
}

public void AddResultFileShouldThrowIfFileNameIsEmpty()
{
_testContextImplementation = new TestContextImplementation(_testMethod.Object, new ThreadSafeStringWriter(null, "test"), _properties);

Exception exception =
VerifyThrows(() => _testContextImplementation.AddResultFile(string.Empty));

Verify(typeof(ArgumentException) == exception.GetType());
ArgumentException exception = VerifyThrows<ArgumentException>(() => _testContextImplementation.AddResultFile(string.Empty));
Verify(exception.Message.Contains(Resource.Common_CannotBeNullOrEmpty));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,7 @@ public void DisposeShouldCallDisposeOnCorrespondingTraceListener()
traceListenerManager.Dispose(traceListener);

// Trying to write after closing textWriter should throw exception
void ShouldThrowException() => writer.WriteLine("Try to write something");
Exception ex = VerifyThrows(ShouldThrowException);
Verify(ex is ObjectDisposedException);
VerifyThrows<ObjectDisposedException>(() => writer.WriteLine("Try to write something"));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ public void DisposeShouldDisposeCorrespondingTextWriter()

// Trying to write after disposing textWriter should throw exception
void ShouldThrowException() => writer.WriteLine("Try to write something");
Exception ex = VerifyThrows(ShouldThrowException);
Verify(ex is ObjectDisposedException);
VerifyThrows<ObjectDisposedException>(ShouldThrowException);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@ void Action()
_testAssemblyInfo.AssemblyInitializeMethod = _dummyMethodInfo;
}

Exception ex = VerifyThrows(Action);
Verify(ex.GetType() == typeof(TypeInspectionException));
VerifyThrows<TypeInspectionException>(Action);
}

public void TestAssemblyInfoAssemblyCleanupMethodThrowsForMultipleAssemblyCleanupMethods()
Expand All @@ -54,8 +53,7 @@ void Action()
_testAssemblyInfo.AssemblyCleanupMethod = _dummyMethodInfo;
}

Exception ex = VerifyThrows(Action);
Verify(ex.GetType() == typeof(TypeInspectionException));
VerifyThrows<TypeInspectionException>(Action);
}

public void TestAssemblyHasExecutableCleanupMethodShouldReturnFalseIfAssemblyHasNoCleanupMethod() => Verify(!_testAssemblyInfo.HasExecutableCleanupMethod);
Expand Down Expand Up @@ -97,8 +95,7 @@ public void RunAssemblyInitializeShouldThrowIfTestContextIsNull()

void Action() => _testAssemblyInfo.RunAssemblyInitialize(null);

Exception ex = VerifyThrows(Action);
Verify(ex.GetType() == typeof(NullReferenceException));
VerifyThrows<NullReferenceException>(Action);
}

public void RunAssemblyInitializeShouldNotExecuteAssemblyInitializeIfItHasAlreadyExecuted()
Expand Down Expand Up @@ -151,9 +148,7 @@ public void RunAssemblyInitializeShouldThrowTestFailedExceptionOnAssertionFailur
DummyTestClass.AssemblyInitializeMethodBody = tc => UTF.Assert.Fail("Test failure");
_testAssemblyInfo.AssemblyInitializeMethod = typeof(DummyTestClass).GetMethod("AssemblyInitializeMethod");

var exception = VerifyThrows(() => _testAssemblyInfo.RunAssemblyInitialize(_testContext)) as TestFailedException;

Verify(exception is not null);
TestFailedException exception = VerifyThrows<TestFailedException>(() => _testAssemblyInfo.RunAssemblyInitialize(_testContext));
Verify(exception.Outcome == UnitTestOutcome.Failed);
Verify(
exception.Message
Expand All @@ -169,9 +164,7 @@ public void RunAssemblyInitializeShouldThrowTestFailedExceptionWithInconclusiveO
DummyTestClass.AssemblyInitializeMethodBody = tc => UTF.Assert.Inconclusive("Test Inconclusive");
_testAssemblyInfo.AssemblyInitializeMethod = typeof(DummyTestClass).GetMethod("AssemblyInitializeMethod");

var exception = VerifyThrows(() => _testAssemblyInfo.RunAssemblyInitialize(_testContext)) as TestFailedException;

Verify(exception is not null);
TestFailedException exception = VerifyThrows<TestFailedException>(() => _testAssemblyInfo.RunAssemblyInitialize(_testContext));
Verify(exception.Outcome == UnitTestOutcome.Inconclusive);
Verify(
exception.Message
Expand All @@ -187,9 +180,8 @@ public void RunAssemblyInitializeShouldThrowTestFailedExceptionWithNonAssertExce
DummyTestClass.AssemblyInitializeMethodBody = tc => throw new ArgumentException("Some actualErrorMessage message", new InvalidOperationException("Inner actualErrorMessage message"));
_testAssemblyInfo.AssemblyInitializeMethod = typeof(DummyTestClass).GetMethod("AssemblyInitializeMethod");

var exception = VerifyThrows(() => _testAssemblyInfo.RunAssemblyInitialize(_testContext)) as TestFailedException;
var exception = VerifyThrows<TestFailedException>(() => _testAssemblyInfo.RunAssemblyInitialize(_testContext));

Verify(exception is not null);
Verify(exception.Outcome == UnitTestOutcome.Failed);
Verify(
exception.Message
Expand All @@ -210,9 +202,8 @@ public void RunAssemblyInitializeShouldThrowTheInnerMostExceptionWhenThereAreMul
FailingStaticHelper.DoWork();
_testAssemblyInfo.AssemblyInitializeMethod = typeof(DummyTestClass).GetMethod("AssemblyInitializeMethod");

var exception = VerifyThrows(() => _testAssemblyInfo.RunAssemblyInitialize(_testContext)) as TestFailedException;
TestFailedException exception = VerifyThrows<TestFailedException>(() => _testAssemblyInfo.RunAssemblyInitialize(_testContext));

Verify(exception is not null);
Verify(exception.Outcome == UnitTestOutcome.Failed);
Verify(
exception.Message
Expand All @@ -230,9 +221,7 @@ public void RunAssemblyInitializeShouldThrowForAlreadyExecutedTestAssemblyInitWi
_testAssemblyInfo.AssemblyInitializeMethod = typeof(DummyTestClass).GetMethod("AssemblyInitializeMethod");
_testAssemblyInfo.AssemblyInitializationException = new TestFailedException(UnitTestOutcome.Failed, "Cached Test failure");

var exception = VerifyThrows(() => _testAssemblyInfo.RunAssemblyInitialize(_testContext)) as TestFailedException;

Verify(exception is not null);
TestFailedException exception = VerifyThrows<TestFailedException>(() => _testAssemblyInfo.RunAssemblyInitialize(_testContext));
Verify(exception.Outcome == UnitTestOutcome.Failed);
Verify(exception.Message == "Cached Test failure");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,7 @@ void Action()
_testClassInfo.ClassInitializeMethod = _testClassType.GetMethods().First();
}

Exception ex = VerifyThrows(Action);
Verify(ex.GetType() == typeof(TypeInspectionException));
VerifyThrows<TypeInspectionException>(Action);
}

public void TestClassInfoClassCleanupMethodSetShouldThrowForMultipleClassCleanupMethods()
Expand All @@ -93,8 +92,7 @@ void Action()
_testClassInfo.ClassCleanupMethod = _testClassType.GetMethods().First();
}

Exception ex = VerifyThrows(Action);
Verify(ex.GetType() == typeof(TypeInspectionException));
VerifyThrows<TypeInspectionException>(Action);
}

public void TestClassInfoClassCleanupMethodShouldNotInvokeWhenNoTestClassInitializedIsCalled()
Expand Down Expand Up @@ -174,10 +172,7 @@ public void RunClassInitializeShouldThrowIfTestContextIsNull()

_testClassInfo.ClassInitializeMethod = typeof(DummyTestClass).GetMethod("ClassInitializeMethod");

void Action() => _testClassInfo.RunClassInitialize(null);

Exception ex = VerifyThrows(Action);
Verify(ex.GetType() == typeof(NullReferenceException));
VerifyThrows<NullReferenceException>(() => _testClassInfo.RunClassInitialize(null));
}

public void RunClassInitializeShouldNotExecuteClassInitializeIfItHasAlreadyExecuted()
Expand Down Expand Up @@ -309,9 +304,8 @@ public void RunClassInitializeShouldThrowTestFailedExceptionOnBaseInitializeMeth

_testClassInfo.BaseClassInitMethods.Add(typeof(DummyBaseTestClass).GetMethod("InitBaseClassMethod"));

var exception = VerifyThrows(() => _testClassInfo.RunClassInitialize(_testContext)) as TestFailedException;
TestFailedException exception = VerifyThrows<TestFailedException>(() => _testClassInfo.RunClassInitialize(_testContext));

Verify(exception is not null);
Verify(exception.Outcome == UnitTestOutcome.Failed);
Verify(
exception.Message
Expand All @@ -328,9 +322,8 @@ public void RunClassInitializeShouldThrowTestFailedExceptionOnAssertionFailure()
DummyTestClass.ClassInitializeMethodBody = tc => UTF.Assert.Fail("Test failure");
_testClassInfo.ClassInitializeMethod = typeof(DummyTestClass).GetMethod("ClassInitializeMethod");

var exception = VerifyThrows(() => _testClassInfo.RunClassInitialize(_testContext)) as TestFailedException;
TestFailedException exception = VerifyThrows<TestFailedException>(() => _testClassInfo.RunClassInitialize(_testContext));

Verify(exception is not null);
Verify(exception.Outcome == UnitTestOutcome.Failed);
Verify(
exception.Message
Expand All @@ -346,9 +339,8 @@ public void RunClassInitializeShouldThrowTestFailedExceptionWithInconclusiveOnAs
DummyTestClass.ClassInitializeMethodBody = tc => UTF.Assert.Inconclusive("Test Inconclusive");
_testClassInfo.ClassInitializeMethod = typeof(DummyTestClass).GetMethod("ClassInitializeMethod");

var exception = VerifyThrows(() => _testClassInfo.RunClassInitialize(_testContext)) as TestFailedException;
TestFailedException exception = VerifyThrows<TestFailedException>(() => _testClassInfo.RunClassInitialize(_testContext));

Verify(exception is not null);
Verify(exception.Outcome == UnitTestOutcome.Inconclusive);
Verify(
exception.Message
Expand All @@ -364,9 +356,8 @@ public void RunClassInitializeShouldThrowTestFailedExceptionWithNonAssertExcepti
DummyTestClass.ClassInitializeMethodBody = tc => throw new ArgumentException("Argument exception");
_testClassInfo.ClassInitializeMethod = typeof(DummyTestClass).GetMethod("ClassInitializeMethod");

var exception = VerifyThrows(() => _testClassInfo.RunClassInitialize(_testContext)) as TestFailedException;
TestFailedException exception = VerifyThrows<TestFailedException>(() => _testClassInfo.RunClassInitialize(_testContext));

Verify(exception is not null);
Verify(exception.Outcome == UnitTestOutcome.Failed);
Verify(
exception.Message
Expand All @@ -382,9 +373,7 @@ public void RunClassInitializeShouldThrowForAlreadyExecutedTestClassInitWithExce
_testClassInfo.ClassInitializeMethod = typeof(DummyTestClass).GetMethod("ClassInitializeMethod");
_testClassInfo.ClassInitializationException = new TestFailedException(UnitTestOutcome.Failed, "Cached Test failure");

var exception = VerifyThrows(() => _testClassInfo.RunClassInitialize(_testContext)) as TestFailedException;

Verify(exception is not null);
TestFailedException exception = VerifyThrows<TestFailedException>(() => _testClassInfo.RunClassInitialize(_testContext));
Verify(exception.Outcome == UnitTestOutcome.Failed);
Verify(exception.Message == "Cached Test failure");
}
Expand All @@ -406,9 +395,8 @@ public void RunClassInitializeShouldThrowTheInnerMostExceptionWhenThereAreMultip
FailingStaticHelper.DoWork();
_testClassInfo.ClassInitializeMethod = typeof(DummyTestClass).GetMethod("ClassInitializeMethod");

var exception = VerifyThrows(() => _testClassInfo.RunClassInitialize(_testContext)) as TestFailedException;
var exception = VerifyThrows<TestFailedException>(() => _testClassInfo.RunClassInitialize(_testContext));

Verify(exception is not null);
Verify(exception.Outcome == UnitTestOutcome.Failed);
Verify(
exception.Message
Expand Down
Loading

0 comments on commit ee4e46b

Please sign in to comment.