aboutsummaryrefslogtreecommitdiffziptar.gz
path: root/src/Godosa.Core.Tests/Input/InputScriptTests.cs
blob: d208240a27edbc654802031916925ff7ecd34566 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
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);
    }
}