• 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

0.0
/Core/Key2Joy.Core/Mapping/Actions/Scripting/JavascriptScriptAction.cs
1
using System;
2
using System.Collections.Generic;
3
using System.Text;
4
using System.Threading.Tasks;
5
using Jint;
6
using Jint.Native;
7
using Jint.Native.Object;
8
using Jint.Runtime.Descriptors;
9
using Jint.Runtime.Interop;
10
using Key2Joy.Contracts;
11
using Key2Joy.Contracts.Mapping;
12
using Key2Joy.Contracts.Mapping.Actions;
13
using Key2Joy.Contracts.Mapping.Triggers;
14
using Key2Joy.Util;
15

16
namespace Key2Joy.Mapping.Actions.Scripting;
17

18
[Action(
19
    Description = "Javascript Action",
20
    NameFormat = "Javascript Script: {0}",
21
    GroupName = "Scripting",
22
    GroupImage = "script_code"
23
)]
24
public class JavascriptAction : BaseScriptActionWithEnvironment<Engine>
25
{
26
    public JavascriptAction(string name)
27
        : base(name) => this.ImageResource = "JS";
×
28

29
    public override async Task Execute(AbstractInputBag inputBag)
30
    {
31
        try
32
        {
33
            lock (LockObject)
×
34
            {
35
                this.Environment.Execute(this.GetExecutableScript());
×
36
            }
×
37
        }
×
38
        catch (Jint.Runtime.JavaScriptException ex)
×
39
        {
40
            Output.WriteLine(ex);
×
41
        }
×
42

43
        await base.Execute(inputBag);
×
44
    }
×
45

46
    public override void RegisterScriptingEnum(ExposedEnumeration enumeration)
47
    {
48
        StringBuilder enumInjectScript = new();
×
49
        enumInjectScript.Append(enumeration.Name + " = {");
×
50

51
        foreach (var kvp in enumeration.KeyValues)
×
52
        {
53
            var enumKey = kvp.Key;
×
54
            var enumValue = kvp.Value;
×
55

56
            enumInjectScript.Append(enumKey);
×
57
            enumInjectScript.Append(": ");
×
58
            enumInjectScript.Append(enumValue);
×
59

60
            enumInjectScript.Append(",\n");
×
61
        }
62

63
        enumInjectScript.Append("};");
×
64

65
        var enumInjection = enumInjectScript.ToString();
×
66
        this.Environment.Execute(enumInjection);
×
67

68
        //this.environment.Execute($"Print(JSON.stringify({enumInjection}))");
69
    }
×
70

71
    public override void RegisterScriptingMethod(ExposedMethod exposedMethod, AbstractAction scriptActionInstance)
72
    {
73
        exposedMethod.Prepare(scriptActionInstance);
×
74

75
        var functionName = exposedMethod.FunctionName;
×
76
        var parents = functionName.Split('.');
×
77
        var methodInfo = exposedMethod.GetExecutorMethodInfo();
×
78
        var @delegate = new DelegateWrapper(this.Environment, methodInfo.CreateDelegate(exposedMethod));
×
79

80
        if (parents.Length > 1)
×
81
        {
82
            var currentObject = this.Environment.Realm.GlobalObject;
×
83

84
            for (var i = 0; i < parents.Length; i++)
×
85
            {
86
                if (i != parents.Length - 1)
×
87
                {
88
                    if (!currentObject.TryGetValue(parents[i], out var child))
×
89
                    {
90
                        child = new JsObject(this.Environment);
×
91
                    }
92

93
                    if (child is not ObjectInstance childObject)
×
94
                    {
95
                        throw new NotImplementedException($"Tried using a non object({parents[i]}) as object parent while registering function: {functionName}!");
×
96
                    }
97

98
                    currentObject.FastSetProperty(parents[i], new PropertyDescriptor(childObject, false, true, true));
×
99
                    currentObject = childObject;
×
100
                }
101
                else
102
                {
103
                    currentObject.FastSetProperty(parents[i], new PropertyDescriptor(@delegate, false, true, true));
×
104
                }
105
            }
106

107
            return;
×
108
        }
109

110
        this.Environment.SetValue(
×
111
            functionName,
×
112
            methodInfo);
×
113
    }
×
114

115
    public override Engine MakeEnvironment() => new Engine(options =>
×
116
    {
×
117
        options.Interop.AllowSystemReflection = true;
×
118
    });
×
119

120
    public override void RegisterEnvironmentObjects()
121
    {
122
        this.Environment.SetValue("Print", new Action<object[]>(this.Print));
×
123

124
        base.RegisterEnvironmentObjects();
×
125
    }
×
126

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

129
    public override void OnStopListening(AbstractTriggerListener listener)
130
    {
131
        base.OnStopListening(listener);
×
132

133
        this.Environment = null;
×
134
    }
×
135

136
    public override bool Equals(object obj)
137
    {
138
        if (obj is not JavascriptAction action)
×
139
        {
140
            return false;
×
141
        }
142

143
        return action.Name == this.Name
×
144
            && action.Script == this.Script;
×
145
    }
146
}
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