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

Sholtee / proxygen / 980

10 Apr 2025 02:40PM UTC coverage: 91.933% (+0.2%) from 91.686%
980

push

appveyor

Sholtee
test ClassProxyGenerator against system types, related fixes III

4866 of 5293 relevant lines covered (91.93%)

0.92 hits per line

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

96.12
/SRC/Private/SyntaxFactories/ClassSyntaxFactoryBase.Method.cs
1
/********************************************************************************
2
* ClassSyntaxFactoryBase.Method.cs                                              *
3
*                                                                               *
4
* Author: Denes Solti                                                           *
5
********************************************************************************/
6
using System;
7
using System.Collections.Generic;
8
using System.Diagnostics;
9
using System.Linq;
10

11
using Microsoft.CodeAnalysis;
12
using Microsoft.CodeAnalysis.CSharp;
13
using Microsoft.CodeAnalysis.CSharp.Syntax;
14

15
using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory;
16

17
namespace Solti.Utils.Proxy.Internals
18
{
19
    internal partial class ClassSyntaxFactoryBase
20
    {
21
        private static ArgumentListSyntax ResolveArgumentList(IMethodInfo method, IEnumerable<ArgumentSyntax> arguments) => ArgumentList
1✔
22
        (
1✔
23
            arguments.ToSyntaxList
1✔
24
            (
1✔
25
                (arg, i) => method.Parameters[i].Kind switch
1✔
26
                {
1✔
27
                    ParameterKind.In => arg.WithRefKindKeyword
1✔
28
                    (
1✔
29
                        refKindKeyword: Token(SyntaxKind.InKeyword)
1✔
30
                    ),
1✔
31
                    ParameterKind.Out => arg.WithRefKindKeyword
1✔
32
                    (
1✔
33
                        refKindKeyword: Token(SyntaxKind.OutKeyword)
1✔
34
                    ),
1✔
35
                    ParameterKind.Ref => arg.WithRefKindKeyword
1✔
36
                    (
1✔
37
                        refKindKeyword: Token(SyntaxKind.RefKeyword)
1✔
38
                    ),
1✔
39
                    _ => arg
1✔
40
                }
1✔
41
            )
1✔
42
        );
1✔
43

44
        private ParameterListSyntax ResolveParameterList(IMethodInfo method) => ParameterList
1✔
45
        (
1✔
46
            method.Parameters.ToSyntaxList(param =>
1✔
47
            {
1✔
48
                ParameterSyntax parameter = Parameter
1✔
49
                (
1✔
50
                    Identifier(param.Name)
1✔
51
                )
1✔
52
                .WithType
1✔
53
                (
1✔
54
                    type: ResolveType(param.Type)
1✔
55
                );
1✔
56

1✔
57
                SyntaxKind? modifier = param.Kind switch
1✔
58
                {
1✔
59
                    ParameterKind.In => SyntaxKind.InKeyword,
1✔
60
                    ParameterKind.Out => SyntaxKind.OutKeyword,
1✔
61
                    ParameterKind.Ref => SyntaxKind.RefKeyword,
1✔
62
                    ParameterKind.Params => SyntaxKind.ParamsKeyword,
1✔
63
                    _ => null
1✔
64
                };
1✔
65

1✔
66
                return modifier is null ? parameter : parameter.WithModifiers
1✔
67
                (
1✔
68
                    TokenList
1✔
69
                    (
1✔
70
                        Token(modifier.Value)
1✔
71
                    )
1✔
72
                );
1✔
73
            })
1✔
74
        );
1✔
75

76
        /// <summary>
77
        /// <code>
78
        /// [[(Type)] target | [(Type)] this | Namespace.Type].Method&lt;...&gt;(...)
79
        /// </code>
80
        /// </summary>
81
        #if DEBUG
82
        internal
83
        #endif
84
        protected MemberAccessExpressionSyntax MethodAccess(ExpressionSyntax? target, IMethodInfo method, ITypeInfo? castTargetTo = null)
85
        {
1✔
86
            SimpleNameSyntax identifier = IdentifierName(method.Name);
1✔
87
            if (method is IGenericMethodInfo genericMethod)
1✔
88
                identifier = GenericName(identifier.Identifier).WithTypeArgumentList
1✔
89
                (
1✔
90
                    typeArgumentList: TypeArgumentList
1✔
91
                    (
1✔
92
                        arguments: genericMethod.GenericArguments.ToSyntaxList(ResolveType)
1✔
93
                    )
1✔
94
                );
1✔
95

96
            return SimpleMemberAccess
1✔
97
            (
1✔
98
                AmendTarget(target, method, castTargetTo),
1✔
99
                identifier
1✔
100
            );
1✔
101
        }
1✔
102

103
        /// <summary>
104
        /// <code>
105
        /// int IInterface.Foo&lt;...&gt;(T a, ref TT b) [where ...]
106
        /// </code>
107
        /// or
108
        /// <code>
109
        /// public override int Foo&lt;...&gt;(T a, ref TT b) [where ...]
110
        /// </code>
111
        /// </summary>
112
        #if DEBUG
113
        internal
114
        #endif
115
        protected MethodDeclarationSyntax ResolveMethod(IMethodInfo method)
116
        {
1✔
117
            TypeSyntax returnTypeSyntax = ResolveType(method.ReturnValue.Type);
1✔
118

119
            if (method.ReturnValue.Kind >= ParameterKind.Ref)
1✔
120
            {
1✔
121
                RefTypeSyntax refReturnTypeSyntax = RefType(returnTypeSyntax);
1✔
122

123
                if (method.ReturnValue.Kind is ParameterKind.RefReadonly)
1✔
124
                    refReturnTypeSyntax = refReturnTypeSyntax.WithReadOnlyKeyword
1✔
125
                    (
1✔
126
                        Token(SyntaxKind.ReadOnlyKeyword)
1✔
127
                    );
1✔
128

129
                returnTypeSyntax = refReturnTypeSyntax;
1✔
130
            }
1✔
131

132
            MethodDeclarationSyntax result = MethodDeclaration
1✔
133
            (
1✔
134
                returnType: returnTypeSyntax,
1✔
135
                identifier: Identifier(method.Name)
1✔
136
            )
1✔
137
            .WithParameterList
1✔
138
            (
1✔
139
                ResolveParameterList(method)
1✔
140
            );
1✔
141

142
            if (method.DeclaringType.IsInterface)
1✔
143
                result = result.WithExplicitInterfaceSpecifier
1✔
144
                (
1✔
145
                    explicitInterfaceSpecifier: ExplicitInterfaceSpecifier((NameSyntax) ResolveType(method.DeclaringType))
1✔
146
                );
1✔
147
            else
148
            {
1✔
149
                List<SyntaxKind> tokens = [..ResolveAccessModifiers(method)];
1✔
150

151
                tokens.Add(method.IsVirtual || method.IsAbstract ? SyntaxKind.OverrideKeyword : SyntaxKind.NewKeyword);
1✔
152

153
                result = result.WithModifiers
×
154
                (
×
155
                    TokenList
×
156
                    (
×
157
                        tokens.Select(Token)
×
158
                    )
×
159
                );
×
160
            }
1✔
161

162
            if (method is IGenericMethodInfo genericMethod)
1✔
163
            {
1✔
164
                result = result.WithTypeParameterList
1✔
165
                (
1✔
166
                    typeParameterList: TypeParameterList
1✔
167
                    (
1✔
168
                        parameters: genericMethod
1✔
169
                            .GenericArguments
1✔
170
                            .ToSyntaxList
1✔
171
                            (
1✔
172
                                type => TypeParameter
1✔
173
                                (
1✔
174
                                    ResolveType(type).ToFullString()
1✔
175
                                )
1✔
176
                            )
1✔
177
                    )
1✔
178
                );
1✔
179

180
                if (genericMethod.IsGenericDefinition)
1✔
181
                {
1✔
182
                    result = result.WithConstraintClauses
1✔
183
                    (
1✔
184
                        List
1✔
185
                        (
1✔
186
                            genericMethod
1✔
187
                                .GenericConstraints
1✔
188
                                .Where(constraint => GetConstraints(constraint).Any())
1✔
189
                                .Select
1✔
190
                                (
1✔
191
                                    constraint => TypeParameterConstraintClause
1✔
192
                                    (
1✔
193
                                        IdentifierName
1✔
194
                                        (
1✔
195
                                            constraint.Target.Name  // T, T, etc
1✔
196
                                        )
1✔
197
                                    )
1✔
198
                                    .WithConstraints
1✔
199
                                    (
1✔
200
                                        GetConstraints(constraint).ToSyntaxList()
1✔
201
                                    )
1✔
202
                                )
1✔
203
                        )
1✔
204
                    );
1✔
205

206
                    IEnumerable<TypeParameterConstraintSyntax> GetConstraints(IGenericConstraint constraint)
207
                    {
1✔
208
                        if (constraint.Struct)
1✔
209
                            yield return ClassOrStructConstraint(SyntaxKind.StructConstraint);
1✔
210
                        if (constraint.Reference)
1✔
211
                            yield return ClassOrStructConstraint(SyntaxKind.ClassConstraint);
1✔
212

213
                        //
214
                        // Explicit interface implementations must not specify type constraints
215
                        //
216

217
                        if (method.DeclaringType.IsInterface)
1✔
218
                            yield break;
1✔
219

220
                        if (constraint.DefaultConstructor)
1✔
221
                            yield return ConstructorConstraint();
1✔
222

223
                        foreach (ITypeInfo typeConstraint in constraint.ConstraintTypes)
1✔
224
                        {
1✔
225
                            yield return TypeConstraint
1✔
226
                            (
1✔
227
                                ResolveType(typeConstraint)
1✔
228
                            );
1✔
229
                        }
1✔
230
                    }
1✔
231
                }
1✔
232
            }
1✔
233

234
            return result;
1✔
235
        }
1✔
236

237
        /// <summary>
238
        /// <code>
239
        /// target.Foo(..., ref ..., ...)
240
        /// </code>
241
        /// </summary>
242
        #if DEBUG
243
        internal
244
        #endif
245
        protected InvocationExpressionSyntax InvokeMethod(IMethodInfo method, ExpressionSyntax? target = null, ITypeInfo? castTargetTo = null, params IEnumerable<ArgumentSyntax> arguments)
246
        {
1✔
247
            Debug.Assert(arguments.Count() == method.Parameters.Count);
1✔
248

249
            return InvocationExpression
1✔
250
            (
1✔
251
                expression: MethodAccess
1✔
252
                (
1✔
253
                    target,
1✔
254
                    method,
1✔
255
                    castTargetTo
1✔
256
                )
1✔
257
            )
1✔
258
            .WithArgumentList
1✔
259
            (
1✔
260
                ResolveArgumentList(method, arguments)
1✔
261
            );
1✔
262
        }
1✔
263

264
        /// <summary>
265
        /// <code>
266
        /// target.Foo(ref a, b, c)
267
        /// </code>
268
        /// </summary>
269
        #if DEBUG
270
        internal
271
        #endif
272
        protected InvocationExpressionSyntax InvokeMethod(IMethodInfo method, ExpressionSyntax? target = null, ITypeInfo? castTargetTo = null, params IReadOnlyList<string> arguments)
273
        {
1✔
274
            Debug.Assert(arguments.Count == method.Parameters.Count);
1✔
275

276
            return InvokeMethod
1✔
277
            (
1✔
278
                method,
1✔
279
                target,
1✔
280
                castTargetTo,
1✔
281
                arguments: method
1✔
282
                    .Parameters
1✔
283
                    .Select
1✔
284
                    (
1✔
285
                        (param, i) => Argument
1✔
286
                        (
1✔
287
                            expression: IdentifierName(arguments[i])
1✔
288
                        )
1✔
289
                    )
1✔
290
                    .ToArray()
1✔
291
            );
1✔
292
        }
1✔
293
    
294
        #if DEBUG
295
        internal
296
        #endif
297
        protected virtual ClassDeclarationSyntax ResolveMethods(ClassDeclarationSyntax cls, object context) => cls;
1✔
298

299
        #if DEBUG
300
        internal
301
        #endif
302
        protected virtual ClassDeclarationSyntax ResolveMethod(ClassDeclarationSyntax cls, object context, IMethodInfo method) => cls;
×
303
    }
304
}
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