Skip to content

Commit

Permalink
Merge pull request #93 from MASSHUU12/add-ConfirmThrowsWMessage-asser…
Browse files Browse the repository at this point in the history
…tion

Add ConfirmThrowsWMessage assertion
  • Loading branch information
MASSHUU12 authored Jun 29, 2024
2 parents 0cf17b3 + b8b5d9e commit cdd51ad
Show file tree
Hide file tree
Showing 3 changed files with 262 additions and 6 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ All notable changes to this project will be documented in this file.
- Extensions:
- ConfirmSign
- ConfirmMatchesPattern
- ConfirmThrowsWMessage
- ConfirmNotThrowsWMessage
- ConfirmDoesNotMatchPattern
- Extension classes:
- ConfirmUuidExtensions
Expand Down
129 changes: 123 additions & 6 deletions addons/confirma/src/extensions/ConfirmExceptionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ public static Func<T> ConfirmThrows<T>(this Func<T> action, Type e, string? mess
if (ex.GetType() == e) return action;

throw new ConfirmAssertException(
message
?? $"Expected exception of type '{e.Name}' but exception of type '{ex.GetType().Name}' was thrown."
message ??
$"Expected exception of type '{e.Name}' but exception of type '{ex.GetType().Name}' was thrown."
);
}

throw new ConfirmAssertException(
message
?? $"Expected exception of type '{e.Name}' but no exception was thrown."
message ??
$"Expected exception of type '{e.Name}' but no exception was thrown."
);
}

Expand Down Expand Up @@ -60,8 +60,8 @@ public static Func<T> ConfirmNotThrows<T>(this Func<T> action, Type e, string? m
if (ex.GetType() == e)
{
throw new ConfirmAssertException(
message
?? $"Expected exception of type '{e.Name}' not to be thrown but it was."
message ??
$"Expected exception of type '{e.Name}' not to be thrown but it was."
);
}
}
Expand All @@ -88,4 +88,121 @@ public static Action ConfirmNotThrows<E>(this Action action, string? message = n
return action;
}
#endregion

#region ConfirmThrowsWMessage
public static Func<T> ConfirmThrowsWMessage<T>(
this Func<T> action,
Type e,
string exMessage,
string? message = null
)
{
try
{
action();
}
catch (Exception ex)
{
if (ex.GetType() == e && ex.Message == exMessage) return action;

if (ex.GetType() != e && ex.Message != exMessage)
{
throw new ConfirmAssertException(
message ??
$"Expected exception of type '{e.Name}' with message '{exMessage}' " +
$"but exception of type '{ex.GetType().Name}' was thrown {(
string.IsNullOrEmpty(ex.Message)
? "without a message"
: $"with message '{ex.Message}'"
)}."
);
}

if (ex.GetType() != e)
{
throw new ConfirmAssertException(
message ??
$"Expected exception of type '{e.Name}' " +
$"but exception of type '{ex.GetType().Name}' was thrown."
);
}

if (ex.Message != exMessage)
{
throw new ConfirmAssertException(
message ??
$"Expected exception to be thrown with message '{exMessage}' " +
$"but was thrown with message '{ex.Message}'."
);
}
}

throw new ConfirmAssertException(
message ??
$"Expected exception of type '{e.Name}' but no exception was thrown."
);
}

public static Func<object?> ConfirmThrowsWMessage<E>(this Func<object?> action, string exMessage, string? message = null)
where E : Exception
{
return action.ConfirmThrowsWMessage(typeof(E), exMessage, message);
}

public static Action ConfirmThrowsWMessage<E>(this Action action, string exMessage, string? message = null)
where E : Exception
{
Func<object> func = () =>
{
action();
return new object();
};

func.ConfirmThrowsWMessage(typeof(E), exMessage, message);

return action;
}
#endregion

#region ConfirmNotThrowsWMessage
public static Func<T> ConfirmNotThrowsWMessage<T>(this Func<T> action, Type e, string exMessage, string? message = null)
{
try
{
action();
}
catch (Exception ex)
{
if (ex.GetType() == e)
{
throw new ConfirmAssertException(
message ??
$"Expected exception of type '{e.Name}' with message '{exMessage}' not to be thrown but it was."
);
}
}

return action;
}

public static Func<object?> ConfirmNotThrowsWMessage<E>(this Func<object?> action, string exMessage, string? message = null)
where E : Exception
{
return ConfirmNotThrowsWMessage(action, typeof(E), exMessage, message);
}

public static Action ConfirmNotThrowsWMessage<E>(this Action action, string exMessage, string? message = null)
where E : Exception
{
Func<object> func = () =>
{
action();
return new object();
};

func.ConfirmNotThrowsWMessage(typeof(E), exMessage, message);

return action;
}
#endregion
}
137 changes: 137 additions & 0 deletions tests/ConfirmExceptionTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
using System;
using Confirma.Attributes;
using Confirma.Exceptions;
using Confirma.Extensions;

namespace Confirma.Tests;

[TestClass]
[Parallelizable]
public static class ConfirmExceptionTest
{
#region ConfirmThrows
[TestCase]
public static void ConfirmThrows_WhenThrows()
{
Action action = () => throw new NotImplementedException();

action.ConfirmThrows<NotImplementedException>();
}

[TestCase]
public static void ConfirmThrows_WhenNotThrows()
{
Action action = () =>
{
Action a = () => {/* Not throws */};

a.ConfirmThrows<NotImplementedException>();
};

action.ConfirmThrows<ConfirmAssertException>();
}
#endregion

#region ConfirmThrows
[TestCase]
public static void ConfirmNotThrows_WhenNotThrows()
{
Action action = () => {/* Not throws */};

action.ConfirmNotThrows<NotImplementedException>();
}

[TestCase]
public static void ConfirmNotThrows_WhenThrows()
{
Action action = () =>
{
Action a = () => throw new NotImplementedException();

a.ConfirmNotThrows<NotImplementedException>();
};

action.ConfirmThrows<ConfirmAssertException>();
}
#endregion

#region ConfirmThrowsWMessage
[TestCase("")]
[TestCase("Lorem ipsum")]
public static void ConfirmThrowsWMessage_WhenThrows(string actual)
{
Action action = () => throw new NotImplementedException(actual);

action.ConfirmThrowsWMessage<NotImplementedException>(actual);
}

[TestCase("")]
[TestCase("Lorem ipsum")]
public static void ConfirmThrowsWMessage_WhenNotThrows(string actual)
{
Action action = () =>
{
Action a = () => {/* Not throws */};

a.ConfirmThrowsWMessage<NotImplementedException>(actual);
};

action.ConfirmThrows<ConfirmAssertException>();
}

[TestCase("", "Expected")]
[TestCase("Actual", "")]
[TestCase("Lorem ipsum", "dolor sit amet")]
public static void ConfirmThrowsWMessage_WhenThrowsWWrongMessage(string actual, string expected)
{
Action action = () =>
{
Action a = () => throw new NotImplementedException(actual);

a.ConfirmThrowsWMessage<NotImplementedException>(expected);
};

action.ConfirmThrows<ConfirmAssertException>();
}
#endregion

#region ConfirmNotThrowsWMessage
[TestCase("")]
[TestCase("Lorem ipsum")]
public static void ConfirmNotThrowsWMessage_WhenNotThrows(string actual)
{
Action action = () => {/* Not throws */};

action.ConfirmNotThrowsWMessage<NotImplementedException>(actual);
}

[TestCase("")]
[TestCase("Lorem ipsum")]
public static void ConfirmNotThrowsWMessage_WhenThrows(string actual)
{
Action action = () =>
{
Action a = () => throw new NotImplementedException(actual);

a.ConfirmNotThrowsWMessage<NotImplementedException>(actual);
};

action.ConfirmThrows<ConfirmAssertException>();
}

[TestCase("", "Expected")]
[TestCase("Actual", "")]
[TestCase("Lorem ipsum", "dolor sit amet")]
public static void ConfirmNotThrowsWMessage_WhenThrowsWWrongMessage(string actual, string expected)
{
Action action = () =>
{
Action a = () => throw new NotImplementedException(actual);

a.ConfirmNotThrowsWMessage<NotImplementedException>(expected);
};

action.ConfirmThrows<ConfirmAssertException>();
}
#endregion
}

0 comments on commit cdd51ad

Please sign in to comment.