Skip to content
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
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Copy link

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.

Copy link
Contributor Author

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?

{
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 src/Qowaiv.Validation.Abstractions/Diagnostics/DebuggerWrapper.cs
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;
}
9 changes: 9 additions & 0 deletions src/Qowaiv.Validation.Abstractions/Result.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,15 @@ internal Result(FixedMessages messages)
[Pure]
public IEnumerable<IValidationMessage> Infos => Messages.GetInfos();

/// <summary>Applies <see cref="Debugger.Break"/> when not valid and with <see cref="Debugger.IsAttached"/>.</summary>
internal void BreakIfInvalid()
{
if (!IsValid && DebuggerWrapper.IsAttached())
{
DebuggerWrapper.Break();
}
}

/// <summary>Represents an OK <see cref="Result"/>.</summary>
public static readonly Result OK = new(FixedMessages.Empty);

Expand Down
2 changes: 2 additions & 0 deletions src/Qowaiv.Validation.Abstractions/Result_TModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ public Result<TOut> Act<TOut>(Func<TModel, Result<TOut>> action)
if (IsValid)
{
var outcome = action(Value);
outcome.BreakIfInvalid();
return new(outcome.IsValid
? outcome.Value
: default,
Expand All @@ -105,6 +106,7 @@ public Result<TModel> Act(Func<TModel, Result> action)
if (IsValid)
{
var outcome = action(Value);
outcome.BreakIfInvalid();
return For(Value, ((FixedMessages)Messages).AddRange(outcome.Messages));
}
else return WithMessages<TModel>(Messages);
Expand Down