-
Notifications
You must be signed in to change notification settings - Fork 421
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6418 from smoogipoo/improve-test-messages
Refactor step buttons + improve assertion messages
- Loading branch information
Showing
11 changed files
with
247 additions
and
220 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -1,8 +1,7 @@ | ||
// Copyright (c) ppy Pty Ltd <[email protected]>. Licensed under the MIT Licence. | ||
// See the LICENCE file in the repository root for full licence text. | ||
|
||
#nullable disable | ||
|
||
using System.Diagnostics; | ||
using osu.Framework.Graphics; | ||
using osu.Framework.Graphics.Containers; | ||
using osu.Framework.Testing.Drawables.Steps; | ||
|
@@ -22,12 +21,43 @@ public TestSceneStepButton() | |
Spacing = new Vector2(5), | ||
Children = new Drawable[] | ||
{ | ||
new LabelStep { Text = nameof(LabelStep) }, | ||
new AssertButton { Text = nameof(AssertButton), Assertion = () => true }, | ||
new SingleStepButton { Text = nameof(SingleStepButton) }, | ||
new RepeatStepButton(null) { Text = nameof(RepeatStepButton) }, | ||
new ToggleStepButton(null) { Text = nameof(ToggleStepButton) }, | ||
new UntilStepButton(() => true) { Text = nameof(UntilStepButton) }, | ||
new LabelStep | ||
{ | ||
Text = nameof(LabelStep), | ||
IsSetupStep = false, | ||
Action = _ => { }, | ||
}, | ||
new AssertButton | ||
{ | ||
Text = nameof(AssertButton), | ||
IsSetupStep = false, | ||
Assertion = () => true, | ||
CallStack = new StackTrace() | ||
}, | ||
new SingleStepButton | ||
{ | ||
Text = nameof(SingleStepButton), | ||
IsSetupStep = false, | ||
Action = () => { } | ||
}, | ||
new RepeatStepButton | ||
{ | ||
Text = nameof(RepeatStepButton), | ||
IsSetupStep = false | ||
}, | ||
new ToggleStepButton | ||
{ | ||
Text = nameof(ToggleStepButton), | ||
IsSetupStep = false, | ||
Action = _ => { } | ||
}, | ||
new UntilStepButton | ||
{ | ||
Text = nameof(UntilStepButton), | ||
IsSetupStep = false, | ||
Assertion = () => true, | ||
CallStack = new StackTrace() | ||
}, | ||
new StepSlider<int>(nameof(StepSlider<int>), 0, 10, 5), | ||
} | ||
}; | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,9 @@ | ||
// Copyright (c) ppy Pty Ltd <[email protected]>. Licensed under the MIT Licence. | ||
// See the LICENCE file in the repository root for full licence text. | ||
|
||
#nullable disable | ||
|
||
using System; | ||
using System.Diagnostics; | ||
using System.Runtime.ExceptionServices; | ||
using System.Text; | ||
using NUnit.Framework; | ||
using osuTK.Graphics; | ||
|
@@ -13,15 +12,14 @@ namespace osu.Framework.Testing.Drawables.Steps | |
{ | ||
public partial class AssertButton : StepButton | ||
{ | ||
public Func<bool> Assertion; | ||
public string ExtendedDescription; | ||
public StackTrace CallStack; | ||
private readonly Func<string> getFailureMessage; | ||
public required StackTrace CallStack { get; init; } | ||
public required Func<bool> Assertion { get; init; } | ||
public Func<string>? GetFailureMessage { get; init; } | ||
|
||
public string? ExtendedDescription { get; init; } | ||
|
||
public AssertButton(bool isSetupStep = false, Func<string> getFailureMessage = null) | ||
: base(isSetupStep) | ||
public AssertButton() | ||
{ | ||
this.getFailureMessage = getFailureMessage; | ||
Action += checkAssert; | ||
LightColour = Color4.OrangeRed; | ||
} | ||
|
@@ -39,26 +37,13 @@ private void checkAssert() | |
if (!string.IsNullOrEmpty(ExtendedDescription)) | ||
builder.Append($" {ExtendedDescription}"); | ||
|
||
if (getFailureMessage != null) | ||
builder.Append($": {getFailureMessage()}"); | ||
if (GetFailureMessage != null) | ||
builder.Append($": {GetFailureMessage()}"); | ||
|
||
throw new TracedException(builder.ToString(), CallStack); | ||
throw ExceptionDispatchInfo.SetRemoteStackTrace(new AssertionException(builder.ToString()), CallStack.ToString()); | ||
} | ||
} | ||
|
||
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(); | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -1,12 +1,15 @@ | ||
// 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; | ||
using osuTK.Graphics; | ||
|
||
namespace osu.Framework.Testing.Drawables.Steps | ||
{ | ||
public partial class LabelStep : StepButton | ||
{ | ||
public new required Action<LabelStep> Action { get; init; } | ||
|
||
protected override Color4 IdleColour => new Color4(77, 77, 77, 255); | ||
|
||
protected override Color4 RunningColour => new Color4(128, 128, 128, 255); | ||
|
@@ -15,6 +18,9 @@ public LabelStep() | |
{ | ||
Light.Hide(); | ||
Height = 30; | ||
base.Action = clickAction; | ||
} | ||
|
||
private void clickAction() => Action(this); | ||
} | ||
} |
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 |
---|---|---|
@@ -1,45 +1,39 @@ | ||
// Copyright (c) ppy Pty Ltd <[email protected]>. Licensed under the MIT Licence. | ||
// See the LICENCE file in the repository root for full licence text. | ||
|
||
#nullable disable | ||
|
||
using System; | ||
|
||
namespace osu.Framework.Testing.Drawables.Steps | ||
{ | ||
public partial class RepeatStepButton : StepButton | ||
{ | ||
private readonly int count; | ||
private int invocations; | ||
public int Count { get; init; } = 1; | ||
|
||
public override int RequiredRepetitions => count; | ||
public override int RequiredRepetitions => Count; | ||
|
||
private string text; | ||
private readonly string text = string.Empty; | ||
private int invocations; | ||
|
||
public new string Text | ||
public RepeatStepButton() | ||
{ | ||
get => text; | ||
set => base.Text = text = value; | ||
updateText(); | ||
} | ||
|
||
public RepeatStepButton(Action action, int count = 1, bool isSetupStep = false) | ||
: base(isSetupStep) | ||
public new string Text | ||
{ | ||
this.count = count; | ||
Action = action; | ||
|
||
updateText(); | ||
get => text; | ||
init => base.Text = text = value; | ||
} | ||
|
||
public override void PerformStep(bool userTriggered = false) | ||
{ | ||
if (invocations == count && !userTriggered) throw new InvalidOperationException("Repeat step was invoked too many times"); | ||
if (invocations == Count && !userTriggered) throw new InvalidOperationException("Repeat step was invoked too many times"); | ||
|
||
invocations++; | ||
|
||
base.PerformStep(userTriggered); | ||
|
||
if (invocations >= count) // Allows for manual execution beyond the invocation limit. | ||
if (invocations >= Count) // Allows for manual execution beyond the invocation limit. | ||
Success(); | ||
|
||
updateText(); | ||
|
@@ -53,7 +47,7 @@ public override void Reset() | |
updateText(); | ||
} | ||
|
||
private void updateText() => base.Text = $@"{Text} {invocations}/{count}"; | ||
private void updateText() => base.Text = $@"{Text} {invocations}/{Count}"; | ||
|
||
public override string ToString() => "Repeat: " + base.ToString(); | ||
} | ||
|
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 |
---|---|---|
@@ -1,24 +1,23 @@ | ||
// Copyright (c) ppy Pty Ltd <[email protected]>. Licensed under the MIT Licence. | ||
// See the LICENCE file in the repository root for full licence text. | ||
|
||
#nullable disable | ||
|
||
using System; | ||
|
||
namespace osu.Framework.Testing.Drawables.Steps | ||
{ | ||
public partial class SingleStepButton : StepButton | ||
{ | ||
public new Action Action; | ||
public new required Action Action { get; init; } | ||
|
||
public SingleStepButton() | ||
{ | ||
base.Action = clickAction; | ||
} | ||
|
||
public SingleStepButton(bool isSetupStep = false) | ||
: base(isSetupStep) | ||
private void clickAction() | ||
{ | ||
base.Action = () => | ||
{ | ||
Action?.Invoke(); | ||
Success(); | ||
}; | ||
Action(); | ||
Success(); | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -1,8 +1,6 @@ | ||
// Copyright (c) ppy Pty Ltd <[email protected]>. Licensed under the MIT Licence. | ||
// See the LICENCE file in the repository root for full licence text. | ||
|
||
#nullable disable | ||
|
||
using System; | ||
using osu.Framework.Graphics; | ||
using osu.Framework.Graphics.Containers; | ||
|
@@ -16,42 +14,20 @@ namespace osu.Framework.Testing.Drawables.Steps | |
{ | ||
public abstract partial class StepButton : CompositeDrawable | ||
{ | ||
public virtual int RequiredRepetitions => 1; | ||
|
||
protected Box Light; | ||
protected Box Background; | ||
protected SpriteText SpriteText; | ||
|
||
public Action Action { get; set; } | ||
|
||
public LocalisableString Text | ||
{ | ||
get => SpriteText.Text; | ||
set => SpriteText.Text = value; | ||
} | ||
|
||
private Color4 lightColour = Color4.BlueViolet; | ||
|
||
public Color4 LightColour | ||
{ | ||
get => lightColour; | ||
set | ||
{ | ||
lightColour = value; | ||
if (IsLoaded) Reset(); | ||
} | ||
} | ||
public required bool IsSetupStep { get; init; } | ||
public Action? Action { get; set; } | ||
|
||
public readonly bool IsSetupStep; | ||
public virtual int RequiredRepetitions => 1; | ||
|
||
protected virtual Color4 IdleColour => new Color4(0.15f, 0.15f, 0.15f, 1); | ||
|
||
protected virtual Color4 RunningColour => new Color4(0.5f, 0.5f, 0.5f, 1); | ||
|
||
protected StepButton(bool isSetupStep = false) | ||
{ | ||
IsSetupStep = isSetupStep; | ||
protected readonly Box Light; | ||
protected readonly Box Background; | ||
protected readonly SpriteText SpriteText; | ||
|
||
protected StepButton() | ||
{ | ||
InternalChildren = new Drawable[] | ||
{ | ||
Background = new Box | ||
|
@@ -85,6 +61,24 @@ protected StepButton(bool isSetupStep = false) | |
Masking = true; | ||
} | ||
|
||
public LocalisableString Text | ||
{ | ||
get => SpriteText.Text; | ||
set => SpriteText.Text = value; | ||
} | ||
|
||
private Color4 lightColour = Color4.BlueViolet; | ||
|
||
public Color4 LightColour | ||
{ | ||
get => lightColour; | ||
set | ||
{ | ||
lightColour = value; | ||
if (IsLoaded) Reset(); | ||
} | ||
} | ||
|
||
protected override void LoadComplete() | ||
{ | ||
base.LoadComplete(); | ||
|
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
Oops, something went wrong.