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(() => 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(); 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); } }