1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
|
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
using Mono.Linker.Tests.Cases.Expectations.Assertions;
using Mono.Linker.Tests.Cases.Expectations.Helpers;
using Mono.Linker.Tests.Cases.Expectations.Metadata;
namespace Mono.Linker.Tests.Cases.RequiresCapability
{
[SkipKeptItemsValidation]
[ExpectedNoWarnings]
public class BasicRequires
{
public static void Main ()
{
TestRequiresWithMessageOnlyOnMethod ();
TestRequiresWithMessageAndUrlOnMethod ();
TestRequiresOnConstructor ();
TestRequiresOnPropertyGetterAndSetter ();
TestThatTrailingPeriodIsAddedToMessage ();
TestThatTrailingPeriodIsNotDuplicatedInWarningMessage ();
TestRequiresFromNameOf ();
OnEventMethod.Test ();
RequiresOnGenerics.Test ();
}
[ExpectedWarning ("IL2026", "Message for --RequiresWithMessageOnly--.")]
[ExpectedWarning ("IL3002", "Message for --RequiresWithMessageOnly--.", ProducedBy = ProducedBy.Analyzer)]
[ExpectedWarning ("IL3050", "Message for --RequiresWithMessageOnly--.", ProducedBy = ProducedBy.Analyzer)]
static void TestRequiresWithMessageOnlyOnMethod ()
{
RequiresWithMessageOnly ();
}
[RequiresUnreferencedCode ("Message for --RequiresWithMessageOnly--")]
[RequiresAssemblyFiles ("Message for --RequiresWithMessageOnly--")]
[RequiresDynamicCode ("Message for --RequiresWithMessageOnly--")]
static void RequiresWithMessageOnly ()
{
}
[ExpectedWarning ("IL2026", "Message for --RequiresWithMessageAndUrl--.", "https://helpurl")]
[ExpectedWarning ("IL3002", "Message for --RequiresWithMessageAndUrl--.", "https://helpurl", ProducedBy = ProducedBy.Analyzer)]
[ExpectedWarning ("IL3050", "Message for --RequiresWithMessageAndUrl--.", "https://helpurl", ProducedBy = ProducedBy.Analyzer)]
static void TestRequiresWithMessageAndUrlOnMethod ()
{
RequiresWithMessageAndUrl ();
}
[RequiresUnreferencedCode ("Message for --RequiresWithMessageAndUrl--", Url = "https://helpurl")]
[RequiresAssemblyFiles ("Message for --RequiresWithMessageAndUrl--", Url = "https://helpurl")]
[RequiresDynamicCode ("Message for --RequiresWithMessageAndUrl--", Url = "https://helpurl")]
static void RequiresWithMessageAndUrl ()
{
}
[ExpectedWarning ("IL2026", "Message for --ConstructorRequires--.")]
[ExpectedWarning ("IL3002", "Message for --ConstructorRequires--.", ProducedBy = ProducedBy.Analyzer)]
[ExpectedWarning ("IL3050", "Message for --ConstructorRequires--.", ProducedBy = ProducedBy.Analyzer)]
static void TestRequiresOnConstructor ()
{
new ConstructorRequires ();
}
class ConstructorRequires
{
[RequiresUnreferencedCode ("Message for --ConstructorRequires--")]
[RequiresAssemblyFiles ("Message for --ConstructorRequires--")]
[RequiresDynamicCode ("Message for --ConstructorRequires--")]
public ConstructorRequires ()
{
}
}
[ExpectedWarning ("IL2026", "Message for --getter PropertyRequires--.")]
[ExpectedWarning ("IL2026", "Message for --setter PropertyRequires--.")]
[ExpectedWarning ("IL3002", "Message for --getter PropertyRequires--.", ProducedBy = ProducedBy.Analyzer)]
[ExpectedWarning ("IL3002", "Message for --setter PropertyRequires--.", ProducedBy = ProducedBy.Analyzer)]
[ExpectedWarning ("IL3050", "Message for --getter PropertyRequires--.", ProducedBy = ProducedBy.Analyzer)]
[ExpectedWarning ("IL3050", "Message for --setter PropertyRequires--.", ProducedBy = ProducedBy.Analyzer)]
static void TestRequiresOnPropertyGetterAndSetter ()
{
_ = PropertyRequires;
PropertyRequires = 0;
}
static int PropertyRequires {
[RequiresUnreferencedCode ("Message for --getter PropertyRequires--")]
[RequiresAssemblyFiles ("Message for --getter PropertyRequires--")]
[RequiresDynamicCode ("Message for --getter PropertyRequires--")]
get { return 42; }
[RequiresUnreferencedCode ("Message for --setter PropertyRequires--")]
[RequiresAssemblyFiles ("Message for --setter PropertyRequires--")]
[RequiresDynamicCode ("Message for --setter PropertyRequires--")]
set { }
}
[RequiresUnreferencedCode ("Linker adds a trailing period to this message")]
[RequiresAssemblyFiles ("Linker adds a trailing period to this message")]
[RequiresDynamicCode ("Linker adds a trailing period to this message")]
static void WarningMessageWithoutEndingPeriod ()
{
}
[ExpectedWarning ("IL2026", "Linker adds a trailing period to this message.")]
[ExpectedWarning ("IL3002", "Linker adds a trailing period to this message.", ProducedBy = ProducedBy.Analyzer)]
[ExpectedWarning ("IL3050", "Linker adds a trailing period to this message.", ProducedBy = ProducedBy.Analyzer)]
static void TestThatTrailingPeriodIsAddedToMessage ()
{
WarningMessageWithoutEndingPeriod ();
}
[RequiresUnreferencedCode ("Linker does not add a period to this message.")]
[RequiresAssemblyFiles ("Linker does not add a period to this message.")]
[RequiresDynamicCode ("Linker does not add a period to this message.")]
static void WarningMessageEndsWithPeriod ()
{
}
[LogDoesNotContain ("Linker does not add a period to this message..")]
[ExpectedWarning ("IL2026", "Linker does not add a period to this message.")]
[ExpectedWarning ("IL3002", "Linker does not add a period to this message.", ProducedBy = ProducedBy.Analyzer)]
[ExpectedWarning ("IL3050", "Linker does not add a period to this message.", ProducedBy = ProducedBy.Analyzer)]
static void TestThatTrailingPeriodIsNotDuplicatedInWarningMessage ()
{
WarningMessageEndsWithPeriod ();
}
static void TestRequiresFromNameOf ()
{
_ = nameof (BasicRequires.RequiresWithMessageOnly);
}
class OnEventMethod
{
[ExpectedWarning ("IL2026", "--EventToTestRemove.remove--", ProducedBy = ProducedBy.Trimmer)]
[ExpectedWarning ("IL2026", "--EventToTestRemove.remove--", ProducedBy = ProducedBy.Trimmer)]
static event EventHandler EventToTestRemove {
add { }
[RequiresUnreferencedCode ("Message for --EventToTestRemove.remove--")]
[RequiresAssemblyFiles ("Message for --EventToTestRemove.remove--")]
[RequiresDynamicCode ("Message for --EventToTestRemove.remove--")]
remove { }
}
[ExpectedWarning ("IL2026", "--EventToTestAdd.add--", ProducedBy = ProducedBy.Trimmer)]
[ExpectedWarning ("IL2026", "--EventToTestAdd.add--", ProducedBy = ProducedBy.Trimmer)]
static event EventHandler EventToTestAdd {
[RequiresUnreferencedCode ("Message for --EventToTestAdd.add--")]
[RequiresAssemblyFiles ("Message for --EventToTestAdd.add--")]
[RequiresDynamicCode ("Message for --EventToTestAdd.add--")]
add { }
remove { }
}
[ExpectedWarning ("IL2026", "--EventToTestRemove.remove--")]
[ExpectedWarning ("IL3002", "--EventToTestRemove.remove--", ProducedBy = ProducedBy.Analyzer)]
[ExpectedWarning ("IL3050", "--EventToTestRemove.remove--", ProducedBy = ProducedBy.Analyzer)]
[ExpectedWarning ("IL2026", "--EventToTestAdd.add--")]
[ExpectedWarning ("IL3002", "--EventToTestAdd.add--", ProducedBy = ProducedBy.Analyzer)]
[ExpectedWarning ("IL3050", "--EventToTestAdd.add--", ProducedBy = ProducedBy.Analyzer)]
public static void Test ()
{
EventToTestRemove -= (sender, e) => { };
EventToTestAdd += (sender, e) => { };
}
}
class RequiresOnGenerics
{
class GenericWithStaticMethod<T>
{
[RequiresUnreferencedCode ("Message for --GenericTypeWithStaticMethodWhichRequires--")]
[RequiresAssemblyFiles ("Message for --GenericTypeWithStaticMethodWhichRequires--")]
[RequiresDynamicCode ("Message for --GenericTypeWithStaticMethodWhichRequires--")]
public static void GenericTypeWithStaticMethodWhichRequires () { }
}
[ExpectedWarning ("IL2026", "--GenericTypeWithStaticMethodWhichRequires--")]
[ExpectedWarning ("IL3002", "--GenericTypeWithStaticMethodWhichRequires--", ProducedBy = ProducedBy.Analyzer)]
[ExpectedWarning ("IL3050", "--GenericTypeWithStaticMethodWhichRequires--", ProducedBy = ProducedBy.Analyzer)]
public static void GenericTypeWithStaticMethodViaLdftn ()
{
var _ = new Action (GenericWithStaticMethod<TestType>.GenericTypeWithStaticMethodWhichRequires);
}
class TestType { }
static T MakeNew<T> () where T : new() => new T ();
static T MakeNew2<T> () where T : new() => MakeNew<T> ();
public static void Test ()
{
GenericTypeWithStaticMethodViaLdftn ();
MakeNew2<TestType> ();
}
}
}
}
|