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

luttje / Key2Joy / 6599541470

21 Oct 2023 08:35PM UTC coverage: 44.926% (-7.6%) from 52.519%
6599541470

Pull #50

github

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

760 of 2323 branches covered (0.0%)

Branch coverage included in aggregate %.

2833 of 2833 new or added lines in 98 files covered. (100.0%)

3888 of 8023 relevant lines covered (48.46%)

22519.45 hits per line

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

84.62
/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
    private PropertyInfo[] GetProperties()
16
    {
17
        var type = this.GetType();
371✔
18
        var properties = type.GetProperties();
371✔
19

20
        return properties.Where(p => p.GetCustomAttributes(typeof(JsonIgnoreAttribute), true).Length == 0).ToArray();
2,555✔
21
    }
22

23
    public virtual MappingAspectOptions SaveOptions()
24
    {
25
        MappingAspectOptions options = new();
98✔
26
        var properties = this.GetProperties();
98✔
27

28
        foreach (var property in properties)
588✔
29
        {
30
            this.SaveOptionsGetProperty(options, property);
196✔
31
        }
32

33
        return options;
98✔
34
    }
35

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

45
        switch (value)
46
        {
47
            case DateTime dateTime:
48
                value = dateTime.Ticks;
×
49
                break;
50

51
            default:
52
                break;
53
        }
54

55
        options.Add(property.Name, value);
196✔
56
    }
196✔
57

58
    public virtual void LoadOptions(MappingAspectOptions options)
59
    {
60
        var properties = this.GetProperties();
273✔
61

62
        foreach (var property in properties)
2,266✔
63
        {
64
            if (!options.ContainsKey(property.Name))
860✔
65
            {
66
                continue;
67
            }
68

69
            this.LoadOptionSetProperty(options, property);
860✔
70
        }
71
    }
273✔
72

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

84
        propertyType = Nullable.GetUnderlyingType(propertyType) ?? propertyType;
860✔
85

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

108
            if (value is List<object> list)
4!
109
            {
110
                var addMethod = constructedListType.GetMethod("Add");
4✔
111

112
                foreach (var item in list)
24✔
113
                {
114
                    addMethod.Invoke(instance, new object[] { item });
8✔
115
                }
116

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

129
        property.SetValue(this, value);
860✔
130
    }
860✔
131

132
    public virtual int CompareTo(AbstractMappingAspect other) => this.ToString().CompareTo(other.ToString());
×
133

134
    public static bool operator ==(AbstractMappingAspect a, AbstractMappingAspect b)
135
    {
136
        if (ReferenceEquals(a, b))
135!
137
        {
138
            return true;
×
139
        }
140

141
        if (a is null || b is null)
135!
142
        {
143
            return false;
135✔
144
        }
145

146
        return a.Equals(b);
×
147
    }
148

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

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