-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow Debugger.Break() on failing Result.Act()'s when the debugger is attachted #65
Open
Corniel
wants to merge
1
commit into
main
Choose a base branch
from
act-failed-break
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
120 changes: 120 additions & 0 deletions
120
specs/Qowaiv.Validation.Specs/Abstractions/Result_Act_Debugger_specs.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,120 @@ | ||
using Qowaiv.Validation.Abstractions; | ||
using System.Diagnostics; | ||
|
||
namespace Abstractions.Result_Act_Debugger_specs; | ||
|
||
[NonParallelizable] | ||
public class When_debugger_is_attached | ||
{ | ||
[SetUp] | ||
public void SetUp() | ||
{ | ||
SetDebugger.IsAttached(true); | ||
SetDebugger.Break(true); | ||
} | ||
|
||
[TearDown] | ||
public void TearDown() | ||
{ | ||
SetDebugger.IsAttached(null); | ||
SetDebugger.Break(false); | ||
} | ||
|
||
[Test] | ||
public void Break_on_Act_when_invalid() | ||
{ | ||
var result = Result.For(42); | ||
result.Invoking(res => res.Act(r => Result.WithMessages(ValidationMessage.Error("Break")))) | ||
.Should().Throw<DebuggerBreaks>(); | ||
} | ||
[Test] | ||
public void Break_on_Act_T_when_invalid() | ||
{ | ||
var result = Result.For(42); | ||
result.Invoking(res => res.Act(r => Result.WithMessages<int>(ValidationMessage.Error("Break")))) | ||
.Should().Throw<DebuggerBreaks>(); | ||
} | ||
|
||
[Test] | ||
public void Continue_on_Act_when_valid() | ||
{ | ||
var result = Result.For(42); | ||
result.Invoking(res => res.Act(r => Result.OK)) | ||
.Should().NotThrow(); | ||
} | ||
|
||
[Test] | ||
public void Continue_on_Act_of_T_when_valid() | ||
{ | ||
var result = Result.For(42); | ||
result.Invoking(res => res.Act(r => Result.For(17))) | ||
.Should().NotThrow(); | ||
} | ||
} | ||
|
||
[NonParallelizable] | ||
public class When_debugger_is_not_attached | ||
{ | ||
[SetUp] | ||
public void SetUp() => SetDebugger.IsAttached(false); | ||
|
||
[TearDown] | ||
public void TearDown() => SetDebugger.IsAttached(null); | ||
|
||
|
||
[Test] | ||
public void Continue_on_Act_when_invalid() | ||
{ | ||
var result = Result.For(42); | ||
result.Invoking(res => res.Act(r => Result.WithMessages(ValidationMessage.Error("Break")))) | ||
.Should().NotThrow(); | ||
} | ||
[Test] | ||
public void Continue_on_Act_T_when_invalid() | ||
{ | ||
var result = Result.For(42); | ||
result.Invoking(res => res.Act(r => Result.WithMessages<int>(ValidationMessage.Error("Break")))) | ||
.Should().NotThrow(); | ||
} | ||
|
||
[Test] | ||
public void Continue_on_Act_when_valid() | ||
{ | ||
var result = Result.For(42); | ||
result.Invoking(res => res.Act(r => Result.OK)) | ||
.Should().NotThrow(); | ||
} | ||
|
||
[Test] | ||
public void Continue_on_Act_of_T_when_valid() | ||
{ | ||
var result = Result.For(42); | ||
result.Invoking(res => res.Act(r => Result.For(17))) | ||
.Should().NotThrow(); | ||
} | ||
} | ||
|
||
static class SetDebugger | ||
{ | ||
public static void IsAttached(bool? isAttached) | ||
{ | ||
Func<bool> action = isAttached.HasValue | ||
? () => isAttached.Value | ||
: () => Debugger.IsAttached; | ||
|
||
DebuggerWrapper.GetProperty(nameof(IsAttached)).SetValue(null, action); | ||
} | ||
|
||
public static void Break(bool @throw) | ||
{ | ||
Action action = @throw | ||
? () => throw new DebuggerBreaks() | ||
: Debugger.Break; | ||
|
||
DebuggerWrapper.GetProperty(nameof(Break)).SetValue(null, action); | ||
} | ||
|
||
private static Type DebuggerWrapper = typeof(Result).Assembly.DefinedTypes.Single(t => t.Name == nameof(DebuggerWrapper)); | ||
} | ||
|
||
public sealed class DebuggerBreaks : Exception { } |
22 changes: 22 additions & 0 deletions
22
src/Qowaiv.Validation.Abstractions/Diagnostics/DebuggerWrapper.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,22 @@ | ||
namespace Qowaiv.Validation.Abstractions.Diagnostics; | ||
|
||
/// <summary> | ||
/// The <see cref="Debugger" /> class is a part of the System.Diagnostics package | ||
/// and is used for communicating with a debugger. | ||
/// </summary> | ||
/// <remarks> | ||
/// For testability reasons, this internal wrapper is added, so that under test | ||
/// the behavior can be adjusted. | ||
/// </remarks> | ||
internal static class DebuggerWrapper | ||
{ | ||
/// <summary>Returns whether or not a debugger is attached to the process.</summary> | ||
public static Func<bool> IsAttached { get; set; } = () => Debugger.IsAttached; | ||
|
||
/// <summary> | ||
/// Break causes a breakpoint to be signalled to an attached debugger.If no debugger | ||
/// is attached, the user is asked if they want to attach a debugger. If yes, then the | ||
/// debugger is launched. | ||
/// </summary> | ||
public static Action Break { get; set; } = Debugger.Break; | ||
} |
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
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So a Break(true) throws an exception. And a Break(false) does a debugger.Break.
That's confusing.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Sjaaky I see your point. Do you have a suggestion?
throwOnBreak
? Or injection the action? Or somehting else?