• 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

48.57
/Core/Key2Joy.Core/Mapping/Actions/ActionsRepository.cs
1
using System;
2
using System.Collections.Generic;
3
using System.Linq;
4
using System.Reflection;
5
using Key2Joy.Contracts.Mapping;
6
using Key2Joy.Contracts.Mapping.Actions;
7
using Key2Joy.Mapping.Actions.Scripting;
8
using Key2Joy.Plugins;
9

10
namespace Key2Joy.Mapping.Actions;
11

12
public static class ActionsRepository
13
{
14
    private static Dictionary<string, MappingTypeFactory<AbstractAction>> actions;
15

16
    /// <summary>
17
    /// Loads all actions in the assembly, optionally merging it with additional action types.
18
    /// </summary>
19
    /// <param name="additionalActionTypeFactories"></param>
20
    public static void Buffer(IReadOnlyList<MappingTypeFactory<AbstractAction>> additionalActionTypeFactories = null, bool discoverActions = true)
21
    {
22
        if (discoverActions)
18✔
23
        {
24
            static TypeExposedMethod MethodInfoToTypeExposed(MethodInfo m, ExposesScriptingMethodAttribute a)
25
                => new(a.FunctionName, m.Name, m.DeclaringType);
203✔
26

27
            actions = Assembly.GetExecutingAssembly()
11✔
28
                .GetTypes()
11✔
29
                .Where(t => t.GetCustomAttribute(typeof(ActionAttribute), false) != null)
2,541✔
30
                .ToDictionary(
11✔
31
                    t => t.FullName,
330✔
32
                    t => new MappingTypeFactory<AbstractAction>(
330✔
33
                        t.FullName,
330✔
34
                        t.GetCustomAttribute<ActionAttribute>(),
330✔
35
                        t.GetMethods()
330✔
36
                            .Where(m => m.GetCustomAttributes(typeof(ExposesScriptingMethodAttribute), false).Length > 0)
5,544✔
37
                            .SelectMany(m => m.GetCustomAttributes<ExposesScriptingMethodAttribute>()
175✔
38
                                .Select(a => MethodInfoToTypeExposed(m, a))
378✔
39
                            )
330✔
40
                    )
330✔
41
                );
11✔
42
        }
43
        else
44
        {
45
            actions = new Dictionary<string, MappingTypeFactory<AbstractAction>>();
7✔
46
        }
47

48
        if (additionalActionTypeFactories == null)
18✔
49
        {
50
            return;
10✔
51
        }
52

53
        foreach (var actionFactory in additionalActionTypeFactories)
40✔
54
        {
55
            if (actions.ContainsKey(actionFactory.FullTypeName))
12!
56
            {
57
                Console.WriteLine("Action {0} already exists in the action buffer. Overwriting.", actionFactory.FullTypeName);
×
58
            }
59

60
            actions.Add(actionFactory.FullTypeName, actionFactory);
12✔
61
        }
62
    }
8✔
63

64
    /// <summary>
65
    /// Gets all action type factories
66
    /// </summary>
67
    /// <returns></returns>
68
    public static IDictionary<string, MappingTypeFactory<AbstractAction>> GetAllActions() => actions;
21✔
69

70
    /// <summary>
71
    /// Gets all action attributes
72
    /// </summary>
73
    /// <returns></returns>
74
    public static IList<ActionAttribute> GetAllActionAttributes() => actions.Values.Select(x => x.Attribute as ActionAttribute).ToList();
3✔
75

76
    /// <summary>
77
    /// Gets the attribute for the provided action
78
    /// </summary>
79
    /// <param name="action"></param>
80
    /// <returns></returns>
81
    public static ActionAttribute GetAttributeForAction(AbstractAction action)
82
    {
83
        var realTypeName = MappingTypeHelper.GetTypeFullName(actions, action);
×
84
        realTypeName = MappingTypeHelper.EnsureSimpleTypeName(realTypeName);
×
85
        return actions[realTypeName].Attribute as ActionAttribute;
×
86
    }
87

88
    /// <summary>
89
    /// Gets a specific action factory by its type
90
    /// </summary>
91
    /// <param name="type"></param>
92
    /// <returns></returns>
93
    public static MappingTypeFactory<AbstractAction> GetAction(Type type) => actions[type.FullName];
1✔
94

95
    /// <summary>
96
    /// Gets all action types and their attribute annotations depending on the specified visibility
97
    /// </summary>
98
    /// <param name="forTopLevel"></param>
99
    /// <returns></returns>
100
    public static SortedDictionary<ActionAttribute, MappingTypeFactory<AbstractAction>> GetAllActions(bool forTopLevel)
101
        => new(
×
102
            actions.Where(kvp =>
×
103
                {
×
104
                    if (kvp.Value.Attribute is not ActionAttribute actionAttribute
×
105
                    || actionAttribute.Visibility == MappingMenuVisibility.Never)
×
106
                    {
×
107
                        return false;
×
108
                    }
×
109

×
110
                    if (forTopLevel)
×
111
                    {
×
112
                        return actionAttribute.Visibility is MappingMenuVisibility.Always
×
113
                            or MappingMenuVisibility.OnlyTopLevel;
×
114
                    }
×
115

×
116
                    return actionAttribute.Visibility is MappingMenuVisibility.Always or MappingMenuVisibility.UnlessTopLevel;
×
117
                })
×
118
                .ToDictionary(t => t.Value.Attribute as ActionAttribute, t => t.Value)
×
119
            );
×
120
}
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