Skip to content

Commit

Permalink
Added Caller* Attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
MarkMichaelis committed Sep 20, 2023
1 parent 3d5c927 commit a64605f
Show file tree
Hide file tree
Showing 6 changed files with 168 additions and 34 deletions.
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
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
30 changes: 0 additions & 30 deletions src/Chapter18/Listing18 .07a.GenericException.cs

This file was deleted.

41 changes: 41 additions & 0 deletions src/Chapter18/Listing18.25a.GenericException.cs
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
59 changes: 59 additions & 0 deletions src/Chapter18/Listing18.25b.CallerArgumentExpression.cs
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
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ public class Program
public static void Main()
{
#region INCLUDE
// ...
dynamic data =
"Hello! My name is Inigo Montoya";
Console.WriteLine(data);
Expand Down

0 comments on commit a64605f

Please sign in to comment.