-
Notifications
You must be signed in to change notification settings - Fork 162
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3d5c927
commit a64605f
Showing
6 changed files
with
168 additions
and
34 deletions.
There are no files selected for viewing
10 changes: 6 additions & 4 deletions
10
...ests/Listing18..07a..GenericAttributes.cs → ...Listing18.25a..GenericAttributes.Tests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,24 @@ | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
|
||
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter18.Listing18_07a.Tests; | ||
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter18.Listing18_25a.Tests; | ||
|
||
#if NET7_0_OR_GREATER | ||
[TestClass] | ||
public class GenericExceptionTests | ||
{ | ||
[TestMethod] | ||
public void ExpectedExceptionIsThrown() | ||
{ | ||
ExpectedException<ArgumentNullException>.AssertExceptionThrown( | ||
ExpectedException<DivideByZeroException>.AssertExceptionThrown( | ||
SampleTests.ThrowArgumentNullExceptionTest); | ||
} | ||
|
||
[TestMethod] | ||
[ExpectedException(typeof(InvalidOperationException))] | ||
public void ExpectedExceptionIsNotThrown() | ||
{ | ||
ExpectedException<ArgumentNullException>.AssertExceptionThrown( | ||
ExpectedException<DivideByZeroException>.AssertExceptionThrown( | ||
() => { }); | ||
} | ||
} | ||
} | ||
#endif // NET7_0_OR_GREATER |
61 changes: 61 additions & 0 deletions
61
src/Chapter18.Tests/Listing18.25b..CallerArgumentExpression.Tests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
|
||
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter18.Listing18_25b.Tests; | ||
|
||
#if NET7_0_OR_GREATER | ||
[TestClass] | ||
public class GenericExceptionTests | ||
{ | ||
[TestMethod] | ||
public void ExpectedExceptionIsThrown() | ||
{ | ||
ExpectedException<DivideByZeroException>.AssertExceptionThrown( | ||
SampleTests.ThrowArgumentNullExceptionTest); | ||
} | ||
|
||
[TestMethod] | ||
public void VerifyExpectedExceptionMessage() | ||
{ | ||
try | ||
{ | ||
ExpectedException<DivideByZeroException>.AssertExceptionThrown( | ||
() => { }); | ||
} | ||
catch(InvalidOperationException exception) | ||
{ | ||
Assert.IsTrue(exception.Message.Contains("'() => { }'")); | ||
} | ||
} | ||
|
||
[TestMethod] | ||
public void Method() | ||
{ | ||
try | ||
{ | ||
SampleTests.Method(); | ||
} | ||
catch (InvalidOperationException exception) | ||
{ | ||
Assert.IsTrue( | ||
exception.Message.Contains("'() => { }'") && | ||
exception.Message.Contains("'Method'") && | ||
exception.Message.Contains("'./FileName.cs'")); | ||
// The expected exception, System.DivideByZeroException, | ||
// was not thrown by the expression, 'Method' in the method, './FileName.cs', and file 'C:\Git\EssentialCSharp\src\Chapter18\Listing18.25b.CallerArgumentExpression.cs'. | ||
} | ||
} | ||
|
||
private object PassingMethodNameAndFileName() | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
|
||
[TestMethod] | ||
[ExpectedException(typeof(InvalidOperationException))] | ||
public void ExpectedExceptionIsNotThrown() | ||
{ | ||
ExpectedException<DivideByZeroException>.AssertExceptionThrown( | ||
() => { }); | ||
} | ||
} | ||
#endif // NET7_0_OR_GREATER |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter18.Listing18_25a; | ||
|
||
#if NET7_0_OR_GREATER | ||
#region INCLUDE | ||
public class SampleTests | ||
{ | ||
#region HIGHLIGHT | ||
[ExpectedException<DivideByZeroException>] | ||
#endregion HIGHLIGHT | ||
public static void ThrowArgumentNullExceptionTest() | ||
{ | ||
var result = 1/"".Length; | ||
} | ||
} | ||
|
||
[AttributeUsage(AttributeTargets.Method)] | ||
#region HIGHLIGHT | ||
public class ExpectedException<TException> : | ||
Attribute where TException : Exception | ||
#endregion HIGHLIGHT | ||
{ | ||
public static TException AssertExceptionThrown(Action testMethod) | ||
{ | ||
try | ||
{ | ||
testMethod(); | ||
throw new InvalidOperationException( | ||
$"The expected exception, { | ||
typeof(TException).FullName }, was not thrown."); | ||
} | ||
catch (TException exception) | ||
{ | ||
return exception; | ||
} | ||
} | ||
|
||
// Attribute detection | ||
// ... | ||
} | ||
#endregion INCLUDE | ||
#endif // NET7_0_OR_GREATER |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
using System.Runtime.CompilerServices; | ||
|
||
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter18.Listing18_25b; | ||
|
||
#if NET7_0_OR_GREATER | ||
#region INCLUDE | ||
public class SampleTests | ||
{ | ||
#region HIGHLIGHT | ||
[ExpectedException<DivideByZeroException>] | ||
#endregion HIGHLIGHT | ||
public static void ThrowArgumentNullExceptionTest() | ||
{ | ||
var result = 1/"".Length; | ||
} | ||
|
||
public static void Method() | ||
{ | ||
ExpectedException<DivideByZeroException>.AssertExceptionThrown( | ||
() => throw new Exception()); | ||
} | ||
} | ||
|
||
[AttributeUsage(AttributeTargets.Method)] | ||
public class ExpectedException<TException> : | ||
Attribute where TException : Exception | ||
{ | ||
#region HIGHLIGHT | ||
public static TException AssertExceptionThrown( | ||
Action testAction, | ||
[CallerArgumentExpression(nameof(testAction))] | ||
string testExpression = null!, | ||
[CallerMemberName]string testActionMemberName = null!, | ||
[CallerFilePath]string testActionFileName = null! | ||
) | ||
#endregion HIGHLIGHT | ||
{ | ||
try | ||
{ | ||
testAction(); | ||
throw new InvalidOperationException( | ||
$"The expected exception, { | ||
typeof(TException).FullName }, was not thrown" + | ||
$" by the expression '{ | ||
testExpression }' in the method '{ | ||
testActionMemberName }' and file '{ | ||
testActionFileName }'."); | ||
} | ||
catch (TException exception) | ||
{ | ||
return exception; | ||
} | ||
} | ||
|
||
// Attribute detection | ||
// ... | ||
} | ||
#endregion INCLUDE | ||
#endif // NET7_0_OR_GREATER |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters