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

luttje / Key2Joy / 6602636543

22 Oct 2023 08:16AM UTC coverage: 44.104% (-8.4%) from 52.519%
6602636543

Pull #50

github

web-flow
Merge cf342a7b3 into 14b7ce9a7
Pull Request #50: Add XInput in preparation for gamepad triggers + add xmldoc

764 of 2383 branches covered (0.0%)

Branch coverage included in aggregate %.

3060 of 3060 new or added lines in 106 files covered. (100.0%)

3896 of 8183 relevant lines covered (47.61%)

15812.68 hits per line

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

82.8
/Core/Key2Joy.Contracts/Mapping/AbstractMappingAspect.cs
1
using System;
2
using System.Collections.Generic;
3
using System.Linq;
4
using System.Reflection;
5
using System.Text.Json.Serialization;
6

7
namespace Key2Joy.Contracts.Mapping;
8

9
public abstract class AbstractMappingAspect : MarshalByRefObject, ICloneable, IComparable<AbstractMappingAspect>
10
{
11
    public string Name { get; set; }
896✔
12

13
    public AbstractMappingAspect(string name) => this.Name = name;
1,046✔
14

15
    public virtual string GetNameDisplay() => this.Name;
×
16

17
    public override string ToString() => this.GetNameDisplay();
×
18

19
    private PropertyInfo[] GetProperties()
20
    {
21
        var type = this.GetType();
371✔
22
        var properties = type.GetProperties();
371✔
23

24
        return properties.Where(p => p.GetCustomAttributes(typeof(JsonIgnoreAttribute), true).Length == 0).ToArray();
2,555✔
25
    }
26

27
    public virtual MappingAspectOptions SaveOptions()
28
    {
29
        MappingAspectOptions options = new();
98✔
30
        var properties = this.GetProperties();
98✔
31

32
        foreach (var property in properties)
588✔
33
        {
34
            this.SaveOptionsGetProperty(options, property);
196✔
35
        }
36

37
        return options;
98✔
38
    }
39

40
    /// <summary>
41
    /// Can be overridden by child actions or triggers to allow saving more complex types.
42
    /// </summary>
43
    /// <param name="property"></param>
44
    /// <param name="value"></param>
45
    protected virtual void SaveOptionsGetProperty(MappingAspectOptions options, PropertyInfo property)
46
    {
47
        var value = property.GetValue(this);
196!
48

49
        switch (value)
50
        {
51
            case DateTime dateTime:
52
                value = dateTime.Ticks;
×
53
                break;
54

55
            default:
56
                break;
57
        }
58

59
        options.Add(property.Name, value);
196✔
60
    }
196✔
61

62
    public virtual void LoadOptions(MappingAspectOptions options)
63
    {
64
        var properties = this.GetProperties();
273✔
65

66
        foreach (var property in properties)
2,266✔
67
        {
68
            if (!options.ContainsKey(property.Name))
860✔
69
            {
70
                continue;
71
            }
72

73
            this.LoadOptionSetProperty(options, property);
860✔
74
        }
75
    }
273✔
76

77
    /// <summary>
78
    /// Can be overridden by child actions or triggers to allow loading more complex types.
79
    /// </summary>
80
    /// <param name="property"></param>
81
    /// <param name="value"></param>
82
    protected virtual void LoadOptionSetProperty(MappingAspectOptions options, PropertyInfo property)
83
    {
84
        var propertyType = property.PropertyType;
860✔
85
        var value = options[property.Name];
860✔
86
        var genericTypeDefinition = propertyType.IsGenericType ? propertyType.GetGenericTypeDefinition() : null;
860✔
87

88
        propertyType = Nullable.GetUnderlyingType(propertyType) ?? propertyType;
860✔
89

90
        if (propertyType.IsEnum)
860✔
91
        {
92
            value = Enum.Parse(propertyType, (string)value);
304✔
93
        }
94
        else if (propertyType == typeof(DateTime))
556!
95
        {
96
            value = new DateTime(Convert.ToInt64(value));
×
97
        }
98
        else if (propertyType == typeof(TimeSpan))
556!
99
        {
100
            value = TimeSpan.Parse((string)value);
×
101
        }
102
        else if (propertyType == typeof(short))
556✔
103
        {
104
            value = Convert.ToInt16(value);
36✔
105
        }
106
        else if (propertyType.IsGenericType
520!
107
            && (genericTypeDefinition == typeof(List<>) || genericTypeDefinition == typeof(IList<>)))
520✔
108
        {
109
            var constructedListType = typeof(List<>).MakeGenericType(propertyType.GetGenericArguments());
4✔
110
            var instance = Activator.CreateInstance(constructedListType);
4✔
111

112
            if (value is List<object> list)
4!
113
            {
114
                var addMethod = constructedListType.GetMethod("Add");
4✔
115

116
                foreach (var item in list)
24✔
117
                {
118
                    addMethod.Invoke(instance, new object[] { item });
8✔
119
                }
120

121
                value = instance;
4✔
122
            }
123
            else
124
            {
125
                throw new ArgumentException($"Expected value to be of type List<> to parse. But was: {value.GetType()}");
×
126
            }
127
        }
128
        else if (value != null)
516✔
129
        {
130
            value = Convert.ChangeType(value, propertyType);
370✔
131
        }
132

133
        property.SetValue(this, value);
860✔
134
    }
860✔
135

136
    public virtual int CompareTo(AbstractMappingAspect other) => this.ToString().CompareTo(other.ToString());
×
137

138
    public static bool operator ==(AbstractMappingAspect a, AbstractMappingAspect b)
139
    {
140
        if (ReferenceEquals(a, b))
135!
141
        {
142
            return true;
×
143
        }
144

145
        if (a is null || b is null)
135!
146
        {
147
            return false;
135✔
148
        }
149

150
        return a.Equals(b);
×
151
    }
152

153
    public static bool operator !=(AbstractMappingAspect a, AbstractMappingAspect b) => !(a == b);
131✔
154

155
    public virtual object Clone() => this.MemberwiseClone();
268✔
156
}
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