aboutsummaryrefslogtreecommitdiffziptar.gz
path: root/src/Godosa.Core.Tests/Input/InputScriptTests.cs
diff options
context:
space:
mode:
Diffstat (limited to 'src/Godosa.Core.Tests/Input/InputScriptTests.cs')
-rw-r--r--src/Godosa.Core.Tests/Input/InputScriptTests.cs73
1 files changed, 73 insertions, 0 deletions
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);
+ }
+}