• 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

90.54
/Support/Key2Joy.Tests/Core/Mapping/Actions/Scripting/ExposedMethodTests.cs
1
using Key2Joy.Mapping.Actions.Scripting;
2
using Microsoft.VisualStudio.TestTools.UnitTesting;
3
using Moq;
4
using System;
5
using System.Collections.Generic;
6
using System.Linq;
7

8
namespace Key2Joy.Tests.Core.Mapping.Actions.Scripting;
9

10
public class MockExposedMethod : ExposedMethod
11
{
12
    public MockExposedMethod()
13
        : base("functionName", "methodName")
7✔
14
    { }
7✔
15

16
    public MockExposedMethod(string functionName, string methodName)
17
        : base(functionName, methodName)
×
18
    { }
×
19

20
    public override IList<Type> GetParameterTypes()
21
        => throw new NotImplementedException();
×
22

23
    public override object InvokeMethod(object[] transformedParameters)
24
        => transformedParameters;
5✔
25

26
    public object GetInstance() => this.Instance;
1✔
27
}
28

29
public class MockType
30
{
31
    public void MethodWithNoParameters()
32
    { }
×
33

34
    public string MethodThatConcatsParameters(string p1, int p2)
35
        => p1 + p2;
1✔
36
}
37

38
[TestClass]
39
public class ExposedMethodTests
40
{
41
    [TestMethod]
42
    public void Test_Prepare_SetsInstanceAndParameterTypes()
43
    {
44
        var exposedMethod = new Mock<MockExposedMethod>() { CallBase = true };
1✔
45
        exposedMethod.Setup(m => m.GetParameterTypes()).Returns(new List<Type> { typeof(string), typeof(int) });
1✔
46

47
        var instance = new object();
1✔
48
        exposedMethod.Object.Prepare(instance);
1✔
49

50
        Assert.AreEqual(instance, exposedMethod.Object.GetInstance());
1✔
51

52
        var resultingParameters = (object[])exposedMethod.Object.TransformAndRedirect("test", 2);
1✔
53
        Assert.IsInstanceOfType(resultingParameters[0], typeof(string));
1✔
54
        Assert.IsInstanceOfType(resultingParameters[1], typeof(int));
1✔
55
    }
1✔
56

57
    [TestMethod]
58
    [ExpectedException(typeof(InvalidOperationException))]
59
    public void Test_RegisterParameterTransformer_FailsIfNotPrepared()
60
    {
61
        var exposedMethod = new Mock<MockExposedMethod>() { CallBase = true };
1✔
62

63
        exposedMethod.Object.RegisterParameterTransformer<int>((p, t) => p * 2);
1✔
64
    }
×
65

66
    [TestMethod]
67
    public void Test_RegisterParameterTransformer_ReplacesExistingTransformer()
68
    {
69
        var exposedMethod = new Mock<MockExposedMethod>() { CallBase = true };
1✔
70
        exposedMethod.Setup(m => m.GetParameterTypes()).Returns(new List<Type> { typeof(int) });
1✔
71
        var instance = new object();
1✔
72
        exposedMethod.Object.Prepare(instance);
1✔
73

74
        exposedMethod.Object.RegisterParameterTransformer<int>((p, t) => p * 2);
1✔
75
        exposedMethod.Object.RegisterParameterTransformer<int>((p, t) => p * 3);
2✔
76

77
        var resultingParameters = (object[])exposedMethod.Object.TransformAndRedirect(1);
1✔
78
        Assert.AreEqual(3, resultingParameters[0]); // Ensures the transformer was replaced, not added
1✔
79
    }
1✔
80

81
    [TestMethod]
82
    [ExpectedException(typeof(InvalidOperationException))]
83
    public void Test_TransformAndRedirect_FailsIfNotPrepared()
84
    {
85
        var exposedMethod = new Mock<MockExposedMethod>() { CallBase = true };
1✔
86
        exposedMethod.Setup(m => m.GetParameterTypes()).Returns(new List<Type> { typeof(DayOfWeek) });
1✔
87
        exposedMethod.Setup(m => m.InvokeMethod(It.IsAny<object[]>())).Returns<object[]>(p => p[0]);
1✔
88

89
        var resultingParameters = (object[])exposedMethod.Object.TransformAndRedirect(nameof(DayOfWeek.Monday));
1✔
90
    }
×
91

92
    [TestMethod]
93
    public void Test_TransformAndRedirect_EnumConversion()
94
    {
95
        var exposedMethod = new Mock<MockExposedMethod>() { CallBase = true };
1✔
96
        exposedMethod.Setup(m => m.GetParameterTypes()).Returns(new List<Type> { typeof(DayOfWeek) });
1✔
97

98
        var instance = new object();
1✔
99
        exposedMethod.Object.Prepare(instance);
1✔
100

101
        var resultingParameters = (object[])exposedMethod.Object.TransformAndRedirect(nameof(DayOfWeek.Monday));
1✔
102
        var resultingParameters2 = (object[])exposedMethod.Object.TransformAndRedirect((int)DayOfWeek.Monday);
1✔
103

104
        Assert.AreEqual(DayOfWeek.Monday, resultingParameters[0]);
1✔
105
        Assert.AreEqual(DayOfWeek.Monday, resultingParameters2[0]);
1✔
106
    }
1✔
107

108
    [TestMethod]
109
    public void Test_TransformAndRedirect_ObjectArrayConversion()
110
    {
111
        var exposedMethod = new Mock<MockExposedMethod>() { CallBase = true };
1✔
112
        exposedMethod.Setup(m => m.GetParameterTypes()).Returns(new List<Type> { typeof(string[]), typeof(int[]) });
1✔
113

114
        var instance = new object();
1✔
115
        exposedMethod.Object.Prepare(instance);
1✔
116

117
        var objectArray = new object[] { "string", "anotherString" };
1✔
118
        var intArray = new int[] { 1, 2 };
1✔
119
        var resultingParameters = (object[])exposedMethod.Object.TransformAndRedirect(objectArray, intArray);
1✔
120

121
        Assert.IsInstanceOfType<string[]>(resultingParameters[0]);
1✔
122
        Assert.AreEqual(((string[])resultingParameters[0])[1], "anotherString");
1✔
123

124
        Assert.IsInstanceOfType<int[]>(resultingParameters[1]);
1✔
125
        Assert.AreEqual(((int[])resultingParameters[1])[1], 2);
1✔
126
    }
1✔
127

128
    [TestMethod]
129
    [ExpectedException(typeof(InvalidOperationException))]
130
    public void Test_TransformAndRedirect_InvalidOperationException()
131
    {
132
        var exposedMethod = new Mock<MockExposedMethod>() { CallBase = true };
1✔
133
        exposedMethod.Setup(m => m.GetParameterTypes()).Returns(new List<Type> { typeof(MockExposedMethod) });
1✔
134

135
        exposedMethod.Object.TransformAndRedirect(new object());
1✔
136
    }
×
137

138
    [TestMethod]
139
    public void Test_TypeExposedMethod_GetParameterTypesIfNone()
140
    {
141
        var exposedMethod = new TypeExposedMethod("functionName", nameof(MockType.MethodWithNoParameters), typeof(MockType));
1✔
142

143
        var parameterTypes = exposedMethod.GetParameterTypes();
1✔
144

145
        Assert.IsFalse(parameterTypes.Any());
1✔
146
    }
1✔
147

148
    [TestMethod]
149
    public void Test_TypeExposedMethod_GetParameterTypes()
150
    {
151
        var exposedMethod = new TypeExposedMethod("functionName", nameof(MockType.MethodThatConcatsParameters), typeof(MockType));
1✔
152

153
        var parameterTypes = exposedMethod.GetParameterTypes().ToList();
1✔
154

155
        CollectionAssert.AreEqual(parameterTypes, new Type[] { typeof(string), typeof(int) });
1✔
156
    }
1✔
157

158
    [TestMethod]
159
    public void Test_TypeExposedMethod_InvokeMethod()
160
    {
161
        var exposedMethod = new TypeExposedMethod("functionName", nameof(MockType.MethodThatConcatsParameters), typeof(MockType));
1✔
162

163
        var instance = new MockType();
1✔
164
        exposedMethod.Prepare(instance);
1✔
165

166
        var result = exposedMethod.InvokeMethod(new object[] { "1", 23 });
1✔
167

168
        Assert.AreEqual("123", result);
1✔
169
    }
1✔
170
}
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

© 2026 Coveralls, Inc