aboutsummaryrefslogtreecommitdiffziptar.gz
path: root/src/Godosa.Core.Tests/Input
diff options
context:
space:
mode:
Diffstat (limited to 'src/Godosa.Core.Tests/Input')
-rw-r--r--src/Godosa.Core.Tests/Input/GamepadScriptTests.cs56
-rw-r--r--src/Godosa.Core.Tests/Input/InputScriptTests.cs73
-rw-r--r--src/Godosa.Core.Tests/Input/MenuCursorTests.cs42
3 files changed, 171 insertions, 0 deletions
diff --git a/src/Godosa.Core.Tests/Input/GamepadScriptTests.cs b/src/Godosa.Core.Tests/Input/GamepadScriptTests.cs
new file mode 100644
index 0000000..9c0c9dc
--- /dev/null
+++ b/src/Godosa.Core.Tests/Input/GamepadScriptTests.cs
@@ -0,0 +1,56 @@
+using System.Numerics;
+using Godosa.Core.Input;
+using Xunit;
+
+namespace Godosa.Core.Tests.Input;
+
+public class GamepadScriptTests
+{
+ [Theory]
+ [InlineData("PadA", PadButtons.A)]
+ [InlineData("PadB", PadButtons.B)]
+ [InlineData("PadX", PadButtons.X)]
+ [InlineData("PadY", PadButtons.Y)]
+ [InlineData("PadBack", PadButtons.Back)]
+ [InlineData("PadStart", PadButtons.Start)]
+ [InlineData("PadUp", PadButtons.DpadUp)]
+ [InlineData("PadDown", PadButtons.DpadDown)]
+ [InlineData("PadLeft", PadButtons.DpadLeft)]
+ [InlineData("PadRight", PadButtons.DpadRight)]
+ [InlineData("PadLB", PadButtons.LeftShoulder)]
+ [InlineData("PadRB", PadButtons.RightShoulder)]
+ public void Button_DownHolds_UpReleases(string name, PadButtons button)
+ {
+ var down = GamepadScript.Apply(default, name, true)!.Value;
+ Assert.Equal(button, down.Buttons);
+ Assert.True(down.Held(button));
+ Assert.Equal(PadButtons.None, GamepadScript.Apply(down, name, false)!.Value.Buttons);
+ }
+
+ [Fact]
+ public void Button_LeavesOtherButtonsAndSticksAlone()
+ {
+ var start = new GamepadState(new Vector2(0.5f, 0f), Vector2.Zero, 0f, 0f, PadButtons.B);
+ var next = GamepadScript.Apply(start, "PadA", true)!.Value;
+ Assert.Equal(PadButtons.A | PadButtons.B, next.Buttons);
+ Assert.Equal(new Vector2(0.5f, 0f), next.LeftStick);
+ Assert.Equal(PadButtons.A, GamepadScript.Apply(next, "PadB", false)!.Value.Buttons);
+ }
+
+ [Fact]
+ public void Triggers_GoFullOnAndOff()
+ {
+ var lt = GamepadScript.Apply(default, "PadLT", true)!.Value;
+ Assert.Equal(1f, lt.LeftTrigger);
+ Assert.Equal(0f, lt.RightTrigger);
+ Assert.Equal(0f, GamepadScript.Apply(lt, "PadLT", false)!.Value.LeftTrigger);
+ Assert.Equal(1f, GamepadScript.Apply(default, "PadRT", true)!.Value.RightTrigger);
+ }
+
+ [Fact]
+ public void Names_AreCaseInsensitive_UnknownIsNull()
+ {
+ Assert.True(GamepadScript.Apply(default, "padstart", true)!.Value.Held(PadButtons.Start));
+ Assert.Null(GamepadScript.Apply(default, "PadZ", true));
+ }
+}
diff --git a/src/Godosa.Core.Tests/Input/InputScriptTests.cs b/src/Godosa.Core.Tests/Input/InputScriptTests.cs
new file mode 100644
index 0000000..d208240
--- /dev/null
+++ b/src/Godosa.Core.Tests/Input/InputScriptTests.cs
@@ -0,0 +1,73 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using Godosa.Core.Input;
+using Xunit;
+
+namespace Godosa.Core.Tests.Input;
+
+public class InputScriptTests
+{
+ [Fact]
+ public void Parse_AbsoluteRelativeHoldPressClick_SortedByTime()
+ {
+ var script = InputScript.Parse("""
+ # walk to the Elder
+ 1.0 hold W 2.5
+ +0.5 press E
+ 5 click Accept
+ 4.0 down Space
+ 4.2 up Space
+ """);
+
+ Assert.Equal(new[]
+ {
+ new ScriptEvent(1.0, ScriptAction.Down, "W"),
+ new ScriptEvent(1.5, ScriptAction.Down, "E"),
+ new ScriptEvent(1.5 + InputScript.PressSeconds, ScriptAction.Up, "E"),
+ new ScriptEvent(3.5, ScriptAction.Up, "W"),
+ new ScriptEvent(4.0, ScriptAction.Down, "Space"),
+ new ScriptEvent(4.2, ScriptAction.Up, "Space"),
+ new ScriptEvent(5.0, ScriptAction.Click, "Accept"),
+ }, script.Events);
+ }
+
+ [Fact]
+ public void Type_KeepsTheWholeText() =>
+ Assert.Equal(new ScriptEvent(1.0, ScriptAction.Type, "Ada Lovelace"), InputScript.Parse("1 type Ada Lovelace").Events.Single());
+
+ [Fact]
+ public void Click_KeepsTheWholeLabel() =>
+ Assert.Equal("Take all", InputScript.Parse("2 click Take all").Events.Single().Arg);
+
+ [Theory]
+ [InlineData("1.0 jump W")]
+ [InlineData("abc down W")]
+ [InlineData("1.0 down")]
+ [InlineData("1.0 hold W")]
+ [InlineData("+0.5 down W")]
+ public void Parse_BadLine_ReportsItsLineNumber(string bad)
+ {
+ var ex = Assert.Throws<FormatException>(() => InputScript.Parse("# ok\n" + bad));
+ Assert.Contains("line 2", ex.Message);
+ }
+
+ [Fact]
+ public void Due_ReturnsEachEventOnceWhenItsTimeComes()
+ {
+ var script = InputScript.Parse("1 down W\n2 up W\n2 press E");
+ var got = new List<ScriptEvent>();
+
+ script.Due(0.99, got);
+ Assert.Empty(got);
+ script.Due(1.0, got);
+ Assert.Single(got);
+ script.Due(2.0, got);
+ Assert.Equal(3, got.Count);
+ script.Due(10.0, got);
+ Assert.Equal(4, got.Count);
+ script.Due(20.0, got);
+ Assert.Equal(4, got.Count);
+ Assert.True(script.Finished);
+ }
+}
diff --git a/src/Godosa.Core.Tests/Input/MenuCursorTests.cs b/src/Godosa.Core.Tests/Input/MenuCursorTests.cs
new file mode 100644
index 0000000..79a1184
--- /dev/null
+++ b/src/Godosa.Core.Tests/Input/MenuCursorTests.cs
@@ -0,0 +1,42 @@
+using Godosa.Core.Input;
+using Xunit;
+
+namespace Godosa.Core.Tests.Input;
+
+public class MenuCursorTests
+{
+ [Fact]
+ public void Move_WrapsAround()
+ {
+ var c = new MenuCursor();
+ c.Clamp(3);
+
+ c.Move(-1, 3);
+ Assert.Equal(2, c.Index);
+ c.Move(+1, 3);
+ Assert.Equal(0, c.Index);
+ c.Move(+1, 3);
+ Assert.Equal(1, c.Index);
+ }
+
+ [Fact]
+ public void Clamp_KeepsIndexInsideAShrinkingList_AndEmptyMeansNone()
+ {
+ var c = new MenuCursor();
+ c.Clamp(5);
+ c.Move(+1, 5);
+ c.Move(+1, 5);
+ c.Move(+1, 5);
+
+ c.Clamp(2);
+ Assert.Equal(1, c.Index);
+
+ c.Clamp(0);
+ Assert.Equal(-1, c.Index);
+ c.Move(+1, 0);
+ Assert.Equal(-1, c.Index);
+
+ c.Clamp(4);
+ Assert.Equal(0, c.Index);
+ }
+}