• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

luttje / Key2Joy / 6557152774

18 Oct 2023 06:36AM UTC coverage: 52.519% (+39.8%) from 12.718%
6557152774

push

github

web-flow
Adding more tests, fixing bugs in the process (#48)

* Add config manager tests

* legacy config and mapping profile tests (should fix #42)

* remove comment, problem was caused by transfer across appdomain (or to/fro scripting environment)

* Test core functionality #48 + includes minor refactoring to be able to test + added docs

* Add interop tests + implement and test async test utility (refactors away from singletons)

* fix not all tests running in workflow

* config and interop tests

* Refactor and allow mocking global input hook class

* add capture action test

* Make Execute override optional for script only methods

* add dependency injection + refactor and try test gamepad service

* Refactor config singleton to using dependency injection

* add tests for scripting

* add tests for plugin set + fix plugin showing as loaded even if checksum match failed

* fix tests failing because it relied on config exist (I guess the test order was accidentally correct earlier, this means we should really fix cleanup so we catch this sooner)

* refactor docs code + fix wrong enum summary

* refactor docs builder and start testing it a bit

* fix cmd project structure

* ignore designer files in tests

* cleanup and refactor UI code + show latest version in help

* truncate listview action column

* allow user config to minimize app when pressing X (defaults to shut down app) resolves #45

696 of 1757 branches covered (0.0%)

Branch coverage included in aggregate %.

4597 of 4597 new or added lines in 138 files covered. (100.0%)

3619 of 6459 relevant lines covered (56.03%)

17089.01 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

59.13
/Core/Key2Joy.Core/Mapping/Actions/Scripting/LuaScriptAction.cs
1
using System;
2
using System.Collections;
3
using System.Collections.Generic;
4
using System.Diagnostics;
5
using System.Linq;
6
using System.Text;
7
using System.Threading.Tasks;
8
using Key2Joy.Contracts;
9
using Key2Joy.Contracts.Mapping;
10
using Key2Joy.Contracts.Mapping.Actions;
11
using Key2Joy.Contracts.Mapping.Triggers;
12
using Key2Joy.Contracts.Plugins;
13
using NLua;
14

15
namespace Key2Joy.Mapping.Actions.Scripting;
16

17
[Action(
18
    Description = "Lua Script Action",
19
    NameFormat = "Lua Script: {0}",
20
    GroupName = "Scripting",
21
    GroupImage = "script_code"
22
)]
23
public class LuaScriptAction : BaseScriptActionWithEnvironment<Lua>
24
{
25
    public LuaScriptAction(string name)
26
        : base(name) => this.ImageResource = "Lua";
16✔
27

28
    public override async Task Execute(AbstractInputBag inputBag)
29
    {
30
        try
31
        {
32
            var source = "Key2Joy.Script.Inline";
33
            if (this.IsScriptPath)
1!
34
            {
35
                source = this.Script;
×
36
            }
37

38
            lock (LockObject)
1✔
39
            {
40
                if (this.Environment.State == null)
1!
41
                {
42
                    Debugger.Break(); // This really shouldn't happen.
×
43
                }
44

45
                this.Environment.DoString(this.GetExecutableScript(), this.Script);
1✔
46
            }
1✔
47
        }
1✔
48
        catch (NLua.Exceptions.LuaScriptException ex)
×
49
        {
50
            Output.WriteLine(ex.Message + ex.StackTrace);
×
51
            Debugger.Break();
×
52
            if (ex.InnerException != null)
×
53
            {
54
                throw ex.InnerException;
×
55
            }
56
        }
×
57

58
        await base.Execute(inputBag);
1✔
59
    }
1✔
60

61
    public override void RegisterScriptingEnum(ExposedEnumeration enumeration)
62
    {
63
        // TODO: Use https://github.com/NLua/NLua/blob/3aaff863c78e89a009c21ff3aef94502018f2566/src/LuaRegistrationHelper.cs#LL76C28-L76C39
64
        this.Environment.NewTable(enumeration.Name);
630✔
65

66
        foreach (var kvp in enumeration.KeyValues)
40,320✔
67
        {
68
            var enumKey = kvp.Key;
19,530✔
69
            var enumValue = kvp.Value;
19,530✔
70

71
            var path = enumeration.Name + "." + enumKey;
19,530✔
72
            this.Environment.SetObjectToPath(path, enumValue);
19,530✔
73
        }
74
    }
630✔
75

76
    public override void RegisterScriptingMethod(ExposedMethod exposedMethod, AbstractAction scriptActionInstance)
77
    {
78
        exposedMethod.Prepare(scriptActionInstance);
87✔
79

80
        var functionName = exposedMethod.FunctionName;
87✔
81
        var parents = functionName.Split('.');
87✔
82

83
        if (parents.Length > 1)
87✔
84
        {
85
            StringBuilder currentPath = new();
60✔
86

87
            for (var i = 0; i < parents.Length - 1; i++)
240✔
88
            {
89
                if (i > 0)
60!
90
                {
91
                    currentPath.Append('.');
×
92
                }
93

94
                currentPath.Append(parents[i]);
60✔
95
            }
96

97
            var path = currentPath.ToString();
60✔
98

99
            if (this.Environment.GetTable(path) == null)
60✔
100
            {
101
                this.Environment.NewTable(path);
27✔
102
            }
103
        }
104

105
        exposedMethod.RegisterParameterTransformer<LuaTable>((luaTable, expectedType) =>
87✔
106
        {
87✔
107
            var dictionary = new Dictionary<object, object>();
×
108
            var areKeysSequential = true;
×
109
            var sequentialKey = 1L;
×
110

87✔
111
            foreach (var key in luaTable.Keys)
×
112
            {
87✔
113
                var keyAsLong = key as long?;
×
114
                var value = luaTable[key];
×
115

87✔
116
                if (areKeysSequential
×
117
                && keyAsLong != null
×
118
                && keyAsLong == sequentialKey)
×
119
                {
87✔
120
                    dictionary.Add(sequentialKey, value);
×
121
                    sequentialKey++;
×
122
                }
87✔
123
                else
87✔
124
                {
87✔
125
                    areKeysSequential = false;
×
126
                    dictionary.Add(key, value);
×
127
                }
87✔
128
            }
87✔
129

87✔
130
            if (areKeysSequential)
×
131
            {
87✔
132
                return dictionary.Values.ToArray();
×
133
            }
87✔
134

87✔
135
            return dictionary;
×
136
        });
87✔
137
        exposedMethod.RegisterParameterTransformer<LuaFunction>((luaFunction, expectedType) => new WrappedPluginType(luaFunction.Call));
87✔
138
        this.Environment.RegisterFunction(functionName, exposedMethod, exposedMethod.GetExecutorMethodInfo());
87✔
139
    }
87✔
140

141
    public override Lua MakeEnvironment() => new Lua();
4✔
142

143
    public override void RegisterEnvironmentObjects()
144
    {
145
        this.Environment.RegisterFunction("print", this, typeof(LuaScriptAction).GetMethod(nameof(Print), new[] { typeof(object[]) }));
3✔
146
        this.Environment.RegisterFunction("Print", this, typeof(LuaScriptAction).GetMethod(nameof(Print), new[] { typeof(object[]) }));
3✔
147
        this.Environment.RegisterFunction("collection", this, typeof(LuaScriptAction).GetMethod(nameof(CollectionIterator), new[] { typeof(ICollection) }));
3✔
148

149
        base.RegisterEnvironmentObjects();
3✔
150
    }
3✔
151

152
    public override void OnStartListening(AbstractTriggerListener listener, ref IList<AbstractAction> otherActions)
153
        => base.OnStartListening(listener, ref otherActions);
×
154

155
    public override void OnStopListening(AbstractTriggerListener listener)
156
        => base.OnStopListening(listener);// environment.Dispose(); // Uncomment this to cause NullReferenceException problem on NLua state described here: https://github.com/luttje/Key2Joy/pull/39#issuecomment-1581537603
×
157

158
    /// <summary>
159
    /// Returns a function that, when called, will return the next value in the collection.
160
    /// </summary>
161
    /// <param name="data"></param>
162
    /// <returns></returns>
163
    public LuaFunction CollectionIterator(ICollection data)
164
    {
165
        LuaIterator iterator = new(data);
×
166

167
        return this.Environment.RegisterFunction("__iterator", iterator, iterator.GetType().GetMethod(nameof(LuaIterator.Next)));
×
168
    }
169

170
    public override bool Equals(object obj)
171
    {
172
        if (obj is not LuaScriptAction action)
1!
173
        {
174
            return false;
×
175
        }
176

177
        return action.Name == this.Name
1!
178
            && action.Script == this.Script;
1✔
179
    }
180
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc