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

Sholtee / proxygen / 885

28 Dec 2023 02:35PM UTC coverage: 92.114% (-0.1%) from 92.258%
885

push

appveyor

Sholtee
introduce partial interface implementation support

4462 of 4844 relevant lines covered (92.11%)

0.92 hits per line

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

87.27
/SRC/Private/SyntaxFactories/ProxySyntaxFactory.EventInterceptorFactory.cs
1
/********************************************************************************
2
* ProxySyntaxFactory.EventInterceptorFactory.cs                                 *
3
*                                                                               *
4
* Author: Denes Solti                                                           *
5
********************************************************************************/
6
using System.Collections.Generic;
7
using System.Linq;
8

9
using Microsoft.CodeAnalysis.CSharp.Syntax;
10

11
using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory;
12

13
namespace Solti.Utils.Proxy.Internals
14
{
15
    internal partial class ProxySyntaxFactory
16
    {
17
        #if DEBUG
18
        internal
19
        #endif
20
        protected override ClassDeclarationSyntax ResolveEvents(ClassDeclarationSyntax cls, object context)
21
        {
1✔
22
            foreach (IEventInfo evt in InterfaceType.Events)
1✔
23
            {
1✔
24
                if (AlreadyImplemented(evt, InterceptorType.Events, SignatureEquals))
1✔
25
                    continue;
×
26

27
                cls = ResolveEvent(cls, context, evt);
1✔
28
            }
1✔
29

30
            return cls;
1✔
31

32
            static bool SignatureEquals(IEventInfo targetEvent, IEventInfo ifaceEvent)
33
            {
×
34
                if (ifaceEvent.AddMethod is not null)
×
35
                {
×
36
                    if (targetEvent.AddMethod?.SignatureEquals(ifaceEvent.AddMethod, ignoreVisibility: true) is not true)
×
37
                        return false;
×
38
                }
×
39

40
                if (ifaceEvent.RemoveMethod is not null)
×
41
                {
×
42
                    if (targetEvent.RemoveMethod?.SignatureEquals(ifaceEvent.RemoveMethod, ignoreVisibility: true) is not true)
×
43
                        return false;
×
44
                }
×
45

46
                return true;
×
47
            }
×
48
        }
1✔
49

50
        /// <summary>
51
        /// <code>
52
        /// private static readonly MethodContext FXxX = new MethodContext((ITarget target, object[] args) =>
53
        /// {                                                                                                
54
        ///     EventType _value = (EventType) args[0];                                                      
55
        ///     Target.Event += _value;                                                                      
56
        ///     return null;                                                                                  
57
        /// }, CALL_INDEX, InterfaceMap&lt;TInterface, TTarget&gt;.Value | null);                                                                                              
58
        /// private static readonly MethodContext FYyY = new MethodContext((ITarget target, object[] args) => 
59
        /// {                                                                                                 
60
        ///     EventType _value = (EventType) args[0];                                                      
61
        ///     Target.Event -= _value;                                                                       
62
        ///     return null;                                                                                 
63
        /// }, CALL_INDEX, InterfaceMap&lt;TInterface, TTarget&gt;.Value | null);                                                                                             
64
        /// event EventType IInterface.Event                                                                  
65
        /// {                                                                                                 
66
        ///     add                                                                                          
67
        ///     {                                                                                           
68
        ///         object[] args = new object[] { value };                                                
69
        ///         Invoke(new InvocationContext(args, FXxX));                                               
70
        ///     }                                                                                            
71
        ///     remove                                                                                       
72
        ///     {                                                                                          
73
        ///         object[] args = new object[] { value };                                             
74
        ///         Invoke(new InvocationContext(args, FYyY));                                              
75
        ///     }                                                                                           
76
        /// }
77
        /// </code>
78
        /// </summary>
79
        #if DEBUG
80
        internal
81
        #endif
82
        protected override ClassDeclarationSyntax ResolveEvent(ClassDeclarationSyntax cls, object context, IEventInfo evt)
83
        {
1✔
84
            //
85
            // For now, we only have call-index of 0
86
            //
87

88
            const int CALL_INDEX = 0;
89

90
            List<MemberDeclarationSyntax> members = new();
1✔
91

92
            BlockSyntax?
1✔
93
                add = null,
1✔
94
                remove = null;
1✔
95

96
            if (evt.AddMethod is not null)
1✔
97
            {
1✔
98
                FieldDeclarationSyntax addCtx = BuildField(true, evt.AddMethod);
1✔
99
                members.Add(addCtx);
1✔
100

101
                add = Block
1✔
102
                (
1✔
103
                    BuildBody(true, evt.AddMethod, addCtx)
1✔
104
                );
1✔
105
            }
1✔
106

107
            if (evt.RemoveMethod is not null)
1✔
108
            {
1✔
109
                FieldDeclarationSyntax removeCtx = BuildField(false, evt.RemoveMethod);
1✔
110
                members.Add(removeCtx);
1✔
111

112
                remove = Block
1✔
113
                (
1✔
114
                    BuildBody(false, evt.RemoveMethod, removeCtx)
1✔
115
                );
1✔
116
            }
1✔
117

118
            members.Add
1✔
119
            (
1✔
120
                ResolveEvent(evt, add, remove)
1✔
121
            );
1✔
122

123
            return cls.WithMembers
1✔
124
            (
1✔
125
                cls.Members.AddRange(members)
1✔
126
            );
1✔
127

128
            FieldDeclarationSyntax BuildField(bool add, IMethodInfo method) => ResolveMethodContext
1✔
129
            (
1✔
130
                ResolveInvokeTarget
1✔
131
                (
1✔
132
                    method,
1✔
133
                    (target, args, locals, body) =>
1✔
134
                    {
1✔
135
                        body.Add
1✔
136
                        (
1✔
137
                            ExpressionStatement
1✔
138
                            (
1✔
139
                                RegisterEvent
1✔
140
                                (
1✔
141
                                    evt,
1✔
142
                                    IdentifierName(target.Identifier),
1✔
143
                                    add,
1✔
144
                                    ResolveIdentifierName
1✔
145
                                    (
1✔
146
                                        locals.Single()
1✔
147
                                    ),
1✔
148
                                    castTargetTo: evt.DeclaringType
1✔
149
                                )
1✔
150
                            )
1✔
151
                        );
1✔
152
                        body.Add
1✔
153
                        (
1✔
154
                            ReturnNull()
1✔
155
                        );
1✔
156
                    }
1✔
157
                ),
1✔
158
                CALL_INDEX
1✔
159
            );
1✔
160

161
            IEnumerable<StatementSyntax> BuildBody(bool add, IMethodInfo method, FieldDeclarationSyntax fld)
162
            {
1✔
163
                LocalDeclarationStatementSyntax argsArray = ResolveArgumentsArray(method);
1✔
164
                yield return argsArray;
1✔
165

166
                yield return ExpressionStatement
1✔
167
                (
1✔
168
                    InvokeMethod
1✔
169
                    (
1✔
170
                        Invoke,
1✔
171
                        target: null,
1✔
172
                        castTargetTo: null,
1✔
173
                        Argument
1✔
174
                        (
1✔
175
                            ResolveObject<InvocationContext>
1✔
176
                            (
1✔
177
                                ResolveArgument(argsArray),
1✔
178
                                Argument
1✔
179
                                (
1✔
180
                                    StaticMemberAccess(cls, fld)
1✔
181
                                )
1✔
182
                            )
1✔
183
                        )
1✔
184
                    )
1✔
185
                );
1✔
186
            }
1✔
187
        }
1✔
188
    }
189
}
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