Skip to content

Commit 82482ed

Browse files
authored
[cecil-tests] Improve these tests a bit. (#14994)
1 parent 8b9cd60 commit 82482ed

File tree

12 files changed

+52
-67
lines changed

12 files changed

+52
-67
lines changed

tests/cecil-tests/AttributeTest.cs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,6 @@ public class AttributeTest {
4242
public void ChildElementsListAvailabilityForAllPlatformsOnParent (string assemblyPath)
4343
{
4444
var assembly = Helper.GetAssembly (assemblyPath);
45-
if (assembly is null) {
46-
Assert.Ignore ("{assemblyPath} could not be found (might be disabled in build)");
47-
return;
48-
}
4945

5046
HashSet<string> found = new HashSet<string> ();
5147
foreach (var prop in Helper.FilterProperties (assembly, a => HasAnyAvailabilityAttribute (a))) {
@@ -498,10 +494,6 @@ IEnumerable<IMemberDefinition> GetAllTypeMembers (TypeDefinition type)
498494
public void AllAttributedItemsMustIncludeCurrentPlatform (string assemblyPath)
499495
{
500496
var assembly = Helper.GetAssembly (assemblyPath);
501-
if (assembly is null) {
502-
Assert.Ignore ("{assemblyPath} could not be found (might be disabled in build)");
503-
return;
504-
}
505497

506498
string platformName = AssemblyToAttributeName (assemblyPath);
507499

tests/cecil-tests/ConstructorTest.cs

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using System;
22
using System.Collections.Generic;
3-
using System.Linq;
43

54
using Mono.Cecil;
65
using Mono.Cecil.Cil;
@@ -29,7 +28,7 @@ static bool IsMatch (MethodDefinition ctor, params (string Namespace, string Nam
2928
return true;
3029
}
3130

32-
static MethodDefinition GetConstructor (TypeDefinition type, params (string Namespace, string Name) [] parameterTypes)
31+
static MethodDefinition? GetConstructor (TypeDefinition type, params (string Namespace, string Name) [] parameterTypes)
3332
{
3433
foreach (var ctor in type.Methods) {
3534
if (IsMatch (ctor, parameterTypes))
@@ -38,9 +37,9 @@ static MethodDefinition GetConstructor (TypeDefinition type, params (string Name
3837
return null;
3938
}
4039

41-
static string GetLocation (MethodDefinition method)
40+
static string GetLocation (MethodDefinition? method)
4241
{
43-
if (method.DebugInformation.HasSequencePoints) {
42+
if (method?.DebugInformation?.HasSequencePoints == true) {
4443
var seq = method.DebugInformation.SequencePoints [0];
4544
return seq.Document.Url + ":" + seq.StartLine + ": ";
4645
}
@@ -58,7 +57,7 @@ static bool IsFunctionEnd (IList<Instruction> instructions, int index)
5857
return false;
5958
}
6059

61-
static bool VerifyInstructions (MethodDefinition method, IList<Instruction> instructions, out string reason)
60+
static bool VerifyInstructions (MethodDefinition method, IList<Instruction> instructions, out string? reason)
6261
{
6362
reason = null;
6463

@@ -70,7 +69,7 @@ static bool VerifyInstructions (MethodDefinition method, IList<Instruction> inst
7069
instructions [0].OpCode == OpCodes.Ldarg_0 &&
7170
instructions [1].OpCode == OpCodes.Ldarg_1 &&
7271
instructions [2].OpCode == OpCodes.Call) {
73-
var targetMethod = (instructions [2].Operand as MethodReference).Resolve ();
72+
var targetMethod = (instructions [2].Operand as MethodReference)!.Resolve ();
7473
if (!targetMethod.IsConstructor) {
7574
reason = $"Calls another method which is not a constructor: {targetMethod.FullName}";
7675
return false;
@@ -85,7 +84,7 @@ static bool VerifyInstructions (MethodDefinition method, IList<Instruction> inst
8584
return true;
8685

8786
if (instructions [3].OpCode == OpCodes.Ldarg_0 && instructions [4].OpCode == OpCodes.Ldc_I4_0 && instructions [5].OpCode == OpCodes.Call) {
88-
targetMethod = (instructions [5].Operand as MethodReference).Resolve ();
87+
targetMethod = (instructions [5].Operand as MethodReference)!.Resolve ();
8988
if (targetMethod.Name != "set_IsDirectBinding") {
9089
reason = $"Calls unknown method: {targetMethod.FullName}";
9190
return false;
@@ -96,7 +95,7 @@ static bool VerifyInstructions (MethodDefinition method, IList<Instruction> inst
9695
}
9796

9897
if (instructions [3].OpCode == OpCodes.Ldarg_0 && instructions [4].OpCode == OpCodes.Call) {
99-
targetMethod = (instructions [4].Operand as MethodReference).Resolve ();
98+
targetMethod = (instructions [4].Operand as MethodReference)!.Resolve ();
10099
if (targetMethod.Name != "MarkDirtyIfDerived") {
101100
reason = $"Calls unknown method: {targetMethod.FullName}";
102101
return false;
@@ -113,7 +112,7 @@ static bool VerifyInstructions (MethodDefinition method, IList<Instruction> inst
113112
instructions [1].OpCode == OpCodes.Ldarg_1 &&
114113
(instructions [2].OpCode == OpCodes.Ldarg_2 || instructions [2].OpCode == OpCodes.Ldc_I4_0) &&
115114
instructions [3].OpCode == OpCodes.Call) {
116-
var targetMethod = (instructions [3].Operand as MethodReference).Resolve ();
115+
var targetMethod = (instructions [3].Operand as MethodReference)!.Resolve ();
117116
if (!targetMethod.IsConstructor) {
118117
reason = $"Calls another method which is not a constructor (2): {targetMethod.FullName}";
119118
return false;
@@ -135,7 +134,7 @@ static bool VerifyInstructions (MethodDefinition method, IList<Instruction> inst
135134
(instructions [2].OpCode == OpCodes.Ldarg_2 || instructions [2].OpCode == OpCodes.Ldc_I4_0) &&
136135
(instructions [3].OpCode == OpCodes.Ldc_I4_0 || instructions [3].OpCode == OpCodes.Ldc_I4_1) &&
137136
instructions [4].OpCode == OpCodes.Call) {
138-
var targetMethod = (instructions [4].Operand as MethodReference).Resolve ();
137+
var targetMethod = (instructions [4].Operand as MethodReference)!.Resolve ();
139138
if (!targetMethod.IsConstructor) {
140139
reason = $"Calls another method which is not a constructor (2): {targetMethod.FullName}";
141140
return false;
@@ -156,7 +155,7 @@ static bool VerifyInstructions (MethodDefinition method, IList<Instruction> inst
156155
return false;
157156
}
158157

159-
static bool VerifyConstructor (MethodDefinition ctor, out string failureReason)
158+
static bool VerifyConstructor (MethodDefinition? ctor, out string? failureReason)
160159
{
161160
failureReason = null;
162161
// There's nothing wrong with a constructor that doesn't exist
@@ -175,7 +174,7 @@ static bool VerifyConstructor (MethodDefinition ctor, out string failureReason)
175174
return true;
176175
}
177176

178-
public static bool ImplementsINativeObject (TypeDefinition type)
177+
public static bool ImplementsINativeObject (TypeDefinition? type)
179178
{
180179
if (type is null)
181180
return false;
@@ -189,7 +188,7 @@ public static bool ImplementsINativeObject (TypeDefinition type)
189188
return ImplementsINativeObject (type.BaseType?.Resolve ());
190189
}
191190

192-
public static bool SubclassesNSObject (TypeDefinition type)
191+
public static bool SubclassesNSObject (TypeDefinition? type)
193192
{
194193
if (type is null)
195194
return false;
@@ -236,7 +235,6 @@ public void INativeObjectIntPtrConstructorDoesNotOwnHandle (ApplePlatform platfo
236235

237236
var failures = new List<string> ();
238237
foreach (var dll in Configuration.GetBaseLibraryImplementations (platform)) {
239-
Console.WriteLine (dll);
240238
using (var ad = AssemblyDefinition.ReadAssembly (dll, new ReaderParameters (ReadingMode.Deferred) { ReadSymbols = true })) {
241239
foreach (var type in ad.MainModule.Types) {
242240
// Skip classes we know aren't (properly) reference counted.

tests/cecil-tests/Helper.cs

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,9 @@ public class Helper {
1919
static Dictionary<string, AssemblyDefinition> cache = new Dictionary<string, AssemblyDefinition> ();
2020

2121
// make sure we load assemblies only once into memory
22-
public static AssemblyDefinition? GetAssembly (string assembly, ReaderParameters? parameters = null, bool readSymbols = false)
22+
public static AssemblyDefinition GetAssembly (string assembly, ReaderParameters? parameters = null, bool readSymbols = false)
2323
{
24-
if (!File.Exists (assembly))
25-
return null;
24+
Assert.That (assembly, Does.Exist, "Assembly existence");
2625
if (!cache.TryGetValue (assembly, out var ad)) {
2726
if (parameters == null) {
2827
var resolver = new DefaultAssemblyResolver ();
@@ -164,17 +163,26 @@ public static string GetBCLDirectory (string assembly)
164163

165164
public static IEnumerable PlatformAssemblies {
166165
get {
167-
// we want to process 32/64 bits individually since their content can differ
168-
yield return new TestCaseData (Path.Combine (Configuration.MonoTouchRootDirectory, "lib", "32bits", "iOS", "Xamarin.iOS.dll"));
169-
yield return new TestCaseData (Path.Combine (Configuration.MonoTouchRootDirectory, "lib", "64bits", "iOS", "Xamarin.iOS.dll"));
166+
if (Configuration.include_ios) {
167+
// we want to process 32/64 bits individually since their content can differ
168+
yield return new TestCaseData (Path.Combine (Configuration.MonoTouchRootDirectory, "lib", "32bits", "iOS", "Xamarin.iOS.dll"));
169+
yield return new TestCaseData (Path.Combine (Configuration.MonoTouchRootDirectory, "lib", "64bits", "iOS", "Xamarin.iOS.dll"));
170+
}
170171

171-
// XamarinWatchOSDll is stripped of its IL
172-
yield return new TestCaseData (Path.Combine (Configuration.MonoTouchRootDirectory, "lib", "32bits", "watchOS", "Xamarin.WatchOS.dll"));
173-
// XamarinTVOSDll is stripped of it's IL
174-
yield return new TestCaseData (Path.Combine (Configuration.MonoTouchRootDirectory, "lib", "64bits", "tvOS", "Xamarin.TVOS.dll"));
172+
if (Configuration.include_watchos) {
173+
// XamarinWatchOSDll is stripped of its IL
174+
yield return new TestCaseData (Path.Combine (Configuration.MonoTouchRootDirectory, "lib", "32bits", "watchOS", "Xamarin.WatchOS.dll"));
175+
}
175176

176-
yield return new TestCaseData (Configuration.XamarinMacMobileDll);
177-
yield return new TestCaseData (Configuration.XamarinMacFullDll);
177+
if (Configuration.include_tvos) {
178+
// XamarinTVOSDll is stripped of it's IL
179+
yield return new TestCaseData (Path.Combine (Configuration.MonoTouchRootDirectory, "lib", "64bits", "tvOS", "Xamarin.TVOS.dll"));
180+
}
181+
182+
if (Configuration.include_mac) {
183+
yield return new TestCaseData (Configuration.XamarinMacMobileDll);
184+
yield return new TestCaseData (Configuration.XamarinMacFullDll);
185+
}
178186
}
179187
}
180188

@@ -184,8 +192,10 @@ public static IEnumerable PlatformAssemblies {
184192

185193
public static IEnumerable TaskAssemblies {
186194
get {
187-
yield return CreateTestFixtureDataFromPath (Path.Combine (Configuration.SdkRootXI, "lib", "msbuild", "iOS", "Xamarin.iOS.Tasks.dll"));
188-
yield return CreateTestFixtureDataFromPath (Path.Combine (Configuration.SdkRootXM, "lib", "msbuild", "Xamarin.Mac.Tasks.dll"));
195+
if (Configuration.include_ios)
196+
yield return CreateTestFixtureDataFromPath (Path.Combine (Configuration.SdkRootXI, "lib", "msbuild", "iOS", "Xamarin.iOS.Tasks.dll"));
197+
if (Configuration.include_mac)
198+
yield return CreateTestFixtureDataFromPath (Path.Combine (Configuration.SdkRootXM, "lib", "msbuild", "Xamarin.Mac.Tasks.dll"));
189199
}
190200
}
191201

tests/cecil-tests/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ build:
88
$(SYSTEM_MSBUILD) /r
99

1010
run-tests: build
11-
$(TOP)/tools/nunit3-console-3.11.1 $(abspath $(TOP)/tests/cecil-tests/bin/Debug/net472/cecil-tests.dll) $(TEST_NAME)
11+
$(TOP)/tools/nunit3-console-3.11.1 $(abspath $(TOP)/tests/cecil-tests/bin/Debug/net472/cecil-tests.dll) $(TEST_NAME) -labels=After
1212

1313
clean:
1414
rm -rf bin/ obj/ TestResult.xml

tests/cecil-tests/MarshalAsTest.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,10 @@ namespace Cecil.Tests {
1515

1616
[TestFixture]
1717
public class MarshalAsTest {
18-
[TestCaseSource (typeof (Helper), "PlatformAssemblies")]
18+
[TestCaseSource (typeof (Helper), nameof (Helper.PlatformAssemblies))]
1919
public void TestAssembly (string assemblyPath)
2020
{
2121
var assembly = Helper.GetAssembly (assemblyPath);
22-
if (assembly == null)
23-
Assert.Ignore ($"{assemblyPath} could not be found (might be disabled in build)");
24-
2522
var failedMethods = new List<string> ();
2623
List<string>? failures = null;
2724
var checkedTypes = new List<TypeReference> ();

tests/cecil-tests/ObsoleteTest.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@ public class ObsoleteTest {
1616
[TestCaseSource (typeof (Helper), nameof (Helper.NetPlatformImplementationAssemblies))] // call this method with every .net6 library
1717
public void GetAllObsoletedThings (string assemblyPath)
1818
{
19-
var assembly = Helper.GetAssembly (assemblyPath, readSymbols: true)!;
20-
Assert.That (assembly, Is.Not.Null, "Must find the assembly");
19+
var assembly = Helper.GetAssembly (assemblyPath, readSymbols: true);
2120

2221
// Make a list of Obsolete things
2322
var found = new HashSet<string> ();

tests/cecil-tests/OpenTKTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public class OpenTKTest {
1414
// https://github.com/xamarin/xamarin-macios/issues/9724
1515
public void BeGone (string assemblyPath)
1616
{
17-
var assembly = Helper.GetAssembly (assemblyPath)!;
17+
var assembly = Helper.GetAssembly (assemblyPath);
1818
var found = new HashSet<string> ();
1919
foreach (var type in assembly.MainModule.Types) {
2020
if (type.Namespace?.StartsWith ("OpenTK", StringComparison.Ordinal) == true) {

tests/cecil-tests/Test.cs

Lines changed: 5 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,11 @@ namespace Cecil.Tests {
1414
[TestFixture]
1515
public class Test {
1616

17-
[TestCaseSource (typeof(Helper), "PlatformAssemblies")]
17+
[TestCaseSource (typeof (Helper), nameof (Helper.PlatformAssemblies))]
1818
// ref: https://github.com/xamarin/xamarin-macios/pull/7760
1919
public void IdentifyBackingFieldAssignation (string assemblyPath)
2020
{
2121
var assembly = Helper.GetAssembly (assemblyPath);
22-
if (assembly == null)
23-
Assert.Ignore ($"{assemblyPath} could not be found (might be disabled in build)");
2422
// look inside all .cctor (static constructor) inside `assemblyName`
2523
foreach (var m in Helper.FilterMethods (assembly!, (m) => m.IsStatic && m.IsConstructor)) {
2624
foreach (var ins in m.Body.Instructions) {
@@ -51,15 +49,11 @@ public void IdentifyBackingFieldAssignation (string assemblyPath)
5149
}
5250
}
5351

54-
[TestCaseSource (typeof (Helper), "PlatformAssemblies")]
52+
[TestCaseSource (typeof (Helper), nameof (Helper.PlatformAssemblies))]
5553
// ref: https://github.com/xamarin/xamarin-macios/issues/8249
5654
public void EnsureUIThreadOnInit (string assemblyPath)
5755
{
5856
var assembly = Helper.GetAssembly (assemblyPath);
59-
if (assembly == null) {
60-
Assert.Ignore ($"{assemblyPath} could not be found (might be disabled in build)");
61-
return; // just to help nullability
62-
}
6357

6458
// `CNContactsUserDefaults` is `[ThreadSafe (false)]` and part of iOS and macOS
6559
var t = assembly.MainModule.GetType ("Contacts.CNContactsUserDefaults");
@@ -89,17 +83,13 @@ public void EnsureUIThreadOnInit (string assemblyPath)
8983
}
9084
}
9185

92-
[TestCaseSource (typeof (Helper), "PlatformAssemblies")]
86+
[TestCaseSource (typeof (Helper), nameof (Helper.PlatformAssemblies))]
9387
public void NoSystemConsoleReference (string assemblyPath)
9488
{
9589
if (Path.GetFileName (assemblyPath) == "Xamarin.Mac.dll")
9690
Assert.Ignore ("Xamarin.Mac has a workaround for Sierra bug w/NSLog");
9791

9892
var assembly = Helper.GetAssembly (assemblyPath);
99-
if (assembly == null) {
100-
Assert.Ignore ($"{assemblyPath} could not be found (might be disabled in build)");
101-
return; // just to help nullability
102-
}
10393
// this has a quite noticeable impact on (small) app size
10494
if (assembly.MainModule.TryGetTypeReference ("System.Console", out var _))
10595
Assert.Fail ($"{assemblyPath} has a reference to `System.Console`. Please use `Runtime.NSLog` inside the platform assemblies");
@@ -135,14 +125,10 @@ public void NoSystemConsoleReference (string assemblyPath)
135125
"wvsprintfa", "wvsprintfw"
136126
};
137127

138-
[TestCaseSource (typeof (Helper), "PlatformAssemblies")]
128+
[TestCaseSource (typeof (Helper), nameof (Helper.PlatformAssemblies))]
139129
public void NoBannedApi (string assemblyPath)
140130
{
141131
var assembly = Helper.GetAssembly (assemblyPath);
142-
if (assembly == null) {
143-
Assert.Ignore ($"{assemblyPath} could not be found (might be disabled in build)");
144-
return; // just to help nullability
145-
}
146132
List<string> found = new List<string> ();
147133
foreach (var m in Helper.FilterMethods (assembly!, (m) => m.IsPInvokeImpl)) {
148134
var symbol = m.PInvokeInfo.EntryPoint;
@@ -164,15 +150,11 @@ public enum PlatformName : byte {
164150
MacCatalyst,
165151
}
166152

167-
[TestCaseSource (typeof (Helper), "PlatformAssemblies")]
153+
[TestCaseSource (typeof (Helper), nameof (Helper.PlatformAssemblies))]
168154
// ref: https://github.com/xamarin/xamarin-macios/issues/4835
169155
public void Unavailable (string assemblyPath)
170156
{
171157
var assembly = Helper.GetAssembly (assemblyPath);
172-
if (assembly == null) {
173-
Assert.Ignore ($"{assemblyPath} could not be found (might be disabled in build)");
174-
return; // just to help nullability
175-
}
176158

177159
var platform = PlatformName.None;
178160
switch (assembly.Name.Name) {

tests/cecil-tests/cecil-tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
<RootNamespace>ceciltests</RootNamespace>
66
<AssemblyName>cecil-tests</AssemblyName>
77
<LangVersion>latest</LangVersion>
8+
<Nullable>enable</Nullable>
89
</PropertyGroup>
910
<ItemGroup>
1011
<Reference Include="System" />

tests/common/Configuration.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99

1010
using Xamarin.Utils;
1111

12+
#nullable disable // until we get around to fixing this file
13+
1214
namespace Xamarin.Tests
1315
{
1416
static partial class Configuration

0 commit comments

Comments
 (0)