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

Refactor step buttons + improve assertion messages #6418

Merged
merged 5 commits into from
Nov 12, 2024
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Give UntilStepButton a proper stacktrace
smoogipoo committed Nov 11, 2024

Verified

This commit was signed with the committer’s verified signature.
smoogipoo Dan Balasescu
commit 42a01b939964d1f12a60f8f866fa635ed49fcfc7
3 changes: 2 additions & 1 deletion osu.Framework.Tests/Visual/Testing/TestSceneStepButton.cs
Original file line number Diff line number Diff line change
@@ -55,7 +55,8 @@ public TestSceneStepButton()
{
Text = nameof(UntilStepButton),
IsSetupStep = false,
Assertion = () => true
Assertion = () => true,
CallStack = new StackTrace()
},
new StepSlider<int>(nameof(StepSlider<int>), 0, 10, 5),
}
14 changes: 0 additions & 14 deletions osu.Framework/Testing/Drawables/Steps/AssertButton.cs
Original file line number Diff line number Diff line change
@@ -4,7 +4,6 @@
using System;
using System.Diagnostics;
using System.Text;
using NUnit.Framework;
using osuTK.Graphics;

namespace osu.Framework.Testing.Drawables.Steps
@@ -44,18 +43,5 @@ private void checkAssert()
}

public override string ToString() => "Assert: " + base.ToString();

private class TracedException : AssertionException
{
private readonly StackTrace trace;

public TracedException(string description, StackTrace trace)
: base(description)
{
this.trace = trace;
}

public override string StackTrace => trace.ToString();
}
}
}
21 changes: 21 additions & 0 deletions osu.Framework/Testing/Drawables/Steps/TracedException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright (c) ppy Pty Ltd <[email protected]>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.

using System.Diagnostics;
using NUnit.Framework;

namespace osu.Framework.Testing.Drawables.Steps
{
internal class TracedException : AssertionException
{
private readonly StackTrace trace;

public TracedException(string description, StackTrace trace)
: base(description)
{
this.trace = trace;
}

public override string StackTrace => trace.ToString();
}
}
4 changes: 2 additions & 2 deletions osu.Framework/Testing/Drawables/Steps/UntilStepButton.cs
Original file line number Diff line number Diff line change
@@ -4,7 +4,6 @@
using System;
using System.Diagnostics;
using System.Text;
using NUnit.Framework;
using osu.Framework.Graphics;
using osuTK.Graphics;

@@ -14,6 +13,7 @@ public partial class UntilStepButton : StepButton
{
private static readonly int max_attempt_milliseconds = FrameworkEnvironment.NoTestTimeout ? int.MaxValue : 10000;

public required StackTrace CallStack { get; init; }
public required Func<bool> Assertion { get; init; }
public Func<string>? GetFailureMessage { get; init; }
public new Action? Action { get; set; }
@@ -60,7 +60,7 @@ private void checkAssert()
if (GetFailureMessage != null)
builder.Append($": {GetFailureMessage()}");

throw new AssertionException(builder.ToString());
throw new TracedException(builder.ToString(), CallStack);
}

Action?.Invoke();
2 changes: 2 additions & 0 deletions osu.Framework/Testing/TestScene.cs
Original file line number Diff line number Diff line change
@@ -331,6 +331,7 @@ protected void AddUntilStep([CanBeNull] string description, [NotNull] Func<bool>
{
Text = description ?? @"Until",
IsSetupStep = addStepsAsSetupSteps,
CallStack = new StackTrace(1),
Assertion = waitUntilTrueDelegate,
});
}
@@ -343,6 +344,7 @@ protected void AddUntilStep<T>([CanBeNull] string description, [NotNull] ActualV
{
Text = description ?? @"Until",
IsSetupStep = addStepsAsSetupSteps,
CallStack = new StackTrace(1),
Assertion = () =>
{
lastResult = constraint().Resolve().ApplyTo(actualValue());