[cecil-tests] Improve these tests a bit. (#14994)

This commit is contained in:
Rolf Bjarne Kvinge 2022-05-13 20:50:02 +02:00 коммит произвёл GitHub
Родитель 8b9cd60dc6
Коммит 82482edc70
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
12 изменённых файлов: 52 добавлений и 67 удалений

Просмотреть файл

@ -42,10 +42,6 @@ namespace Cecil.Tests {
public void ChildElementsListAvailabilityForAllPlatformsOnParent (string assemblyPath)
{
var assembly = Helper.GetAssembly (assemblyPath);
if (assembly is null) {
Assert.Ignore ("{assemblyPath} could not be found (might be disabled in build)");
return;
}
HashSet<string> found = new HashSet<string> ();
foreach (var prop in Helper.FilterProperties (assembly, a => HasAnyAvailabilityAttribute (a))) {
@ -498,10 +494,6 @@ namespace Cecil.Tests {
public void AllAttributedItemsMustIncludeCurrentPlatform (string assemblyPath)
{
var assembly = Helper.GetAssembly (assemblyPath);
if (assembly is null) {
Assert.Ignore ("{assemblyPath} could not be found (might be disabled in build)");
return;
}
string platformName = AssemblyToAttributeName (assemblyPath);

Просмотреть файл

@ -1,6 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Mono.Cecil;
using Mono.Cecil.Cil;
@ -29,7 +28,7 @@ namespace Cecil.Tests {
return true;
}
static MethodDefinition GetConstructor (TypeDefinition type, params (string Namespace, string Name) [] parameterTypes)
static MethodDefinition? GetConstructor (TypeDefinition type, params (string Namespace, string Name) [] parameterTypes)
{
foreach (var ctor in type.Methods) {
if (IsMatch (ctor, parameterTypes))
@ -38,9 +37,9 @@ namespace Cecil.Tests {
return null;
}
static string GetLocation (MethodDefinition method)
static string GetLocation (MethodDefinition? method)
{
if (method.DebugInformation.HasSequencePoints) {
if (method?.DebugInformation?.HasSequencePoints == true) {
var seq = method.DebugInformation.SequencePoints [0];
return seq.Document.Url + ":" + seq.StartLine + ": ";
}
@ -58,7 +57,7 @@ namespace Cecil.Tests {
return false;
}
static bool VerifyInstructions (MethodDefinition method, IList<Instruction> instructions, out string reason)
static bool VerifyInstructions (MethodDefinition method, IList<Instruction> instructions, out string? reason)
{
reason = null;
@ -70,7 +69,7 @@ namespace Cecil.Tests {
instructions [0].OpCode == OpCodes.Ldarg_0 &&
instructions [1].OpCode == OpCodes.Ldarg_1 &&
instructions [2].OpCode == OpCodes.Call) {
var targetMethod = (instructions [2].Operand as MethodReference).Resolve ();
var targetMethod = (instructions [2].Operand as MethodReference)!.Resolve ();
if (!targetMethod.IsConstructor) {
reason = $"Calls another method which is not a constructor: {targetMethod.FullName}";
return false;
@ -85,7 +84,7 @@ namespace Cecil.Tests {
return true;
if (instructions [3].OpCode == OpCodes.Ldarg_0 && instructions [4].OpCode == OpCodes.Ldc_I4_0 && instructions [5].OpCode == OpCodes.Call) {
targetMethod = (instructions [5].Operand as MethodReference).Resolve ();
targetMethod = (instructions [5].Operand as MethodReference)!.Resolve ();
if (targetMethod.Name != "set_IsDirectBinding") {
reason = $"Calls unknown method: {targetMethod.FullName}";
return false;
@ -96,7 +95,7 @@ namespace Cecil.Tests {
}
if (instructions [3].OpCode == OpCodes.Ldarg_0 && instructions [4].OpCode == OpCodes.Call) {
targetMethod = (instructions [4].Operand as MethodReference).Resolve ();
targetMethod = (instructions [4].Operand as MethodReference)!.Resolve ();
if (targetMethod.Name != "MarkDirtyIfDerived") {
reason = $"Calls unknown method: {targetMethod.FullName}";
return false;
@ -113,7 +112,7 @@ namespace Cecil.Tests {
instructions [1].OpCode == OpCodes.Ldarg_1 &&
(instructions [2].OpCode == OpCodes.Ldarg_2 || instructions [2].OpCode == OpCodes.Ldc_I4_0) &&
instructions [3].OpCode == OpCodes.Call) {
var targetMethod = (instructions [3].Operand as MethodReference).Resolve ();
var targetMethod = (instructions [3].Operand as MethodReference)!.Resolve ();
if (!targetMethod.IsConstructor) {
reason = $"Calls another method which is not a constructor (2): {targetMethod.FullName}";
return false;
@ -135,7 +134,7 @@ namespace Cecil.Tests {
(instructions [2].OpCode == OpCodes.Ldarg_2 || instructions [2].OpCode == OpCodes.Ldc_I4_0) &&
(instructions [3].OpCode == OpCodes.Ldc_I4_0 || instructions [3].OpCode == OpCodes.Ldc_I4_1) &&
instructions [4].OpCode == OpCodes.Call) {
var targetMethod = (instructions [4].Operand as MethodReference).Resolve ();
var targetMethod = (instructions [4].Operand as MethodReference)!.Resolve ();
if (!targetMethod.IsConstructor) {
reason = $"Calls another method which is not a constructor (2): {targetMethod.FullName}";
return false;
@ -156,7 +155,7 @@ namespace Cecil.Tests {
return false;
}
static bool VerifyConstructor (MethodDefinition ctor, out string failureReason)
static bool VerifyConstructor (MethodDefinition? ctor, out string? failureReason)
{
failureReason = null;
// There's nothing wrong with a constructor that doesn't exist
@ -175,7 +174,7 @@ namespace Cecil.Tests {
return true;
}
public static bool ImplementsINativeObject (TypeDefinition type)
public static bool ImplementsINativeObject (TypeDefinition? type)
{
if (type is null)
return false;
@ -189,7 +188,7 @@ namespace Cecil.Tests {
return ImplementsINativeObject (type.BaseType?.Resolve ());
}
public static bool SubclassesNSObject (TypeDefinition type)
public static bool SubclassesNSObject (TypeDefinition? type)
{
if (type is null)
return false;
@ -236,7 +235,6 @@ namespace Cecil.Tests {
var failures = new List<string> ();
foreach (var dll in Configuration.GetBaseLibraryImplementations (platform)) {
Console.WriteLine (dll);
using (var ad = AssemblyDefinition.ReadAssembly (dll, new ReaderParameters (ReadingMode.Deferred) { ReadSymbols = true })) {
foreach (var type in ad.MainModule.Types) {
// Skip classes we know aren't (properly) reference counted.

Просмотреть файл

@ -19,10 +19,9 @@ namespace Cecil.Tests {
static Dictionary<string, AssemblyDefinition> cache = new Dictionary<string, AssemblyDefinition> ();
// make sure we load assemblies only once into memory
public static AssemblyDefinition? GetAssembly (string assembly, ReaderParameters? parameters = null, bool readSymbols = false)
public static AssemblyDefinition GetAssembly (string assembly, ReaderParameters? parameters = null, bool readSymbols = false)
{
if (!File.Exists (assembly))
return null;
Assert.That (assembly, Does.Exist, "Assembly existence");
if (!cache.TryGetValue (assembly, out var ad)) {
if (parameters == null) {
var resolver = new DefaultAssemblyResolver ();
@ -164,17 +163,26 @@ namespace Cecil.Tests {
public static IEnumerable PlatformAssemblies {
get {
// we want to process 32/64 bits individually since their content can differ
yield return new TestCaseData (Path.Combine (Configuration.MonoTouchRootDirectory, "lib", "32bits", "iOS", "Xamarin.iOS.dll"));
yield return new TestCaseData (Path.Combine (Configuration.MonoTouchRootDirectory, "lib", "64bits", "iOS", "Xamarin.iOS.dll"));
if (Configuration.include_ios) {
// we want to process 32/64 bits individually since their content can differ
yield return new TestCaseData (Path.Combine (Configuration.MonoTouchRootDirectory, "lib", "32bits", "iOS", "Xamarin.iOS.dll"));
yield return new TestCaseData (Path.Combine (Configuration.MonoTouchRootDirectory, "lib", "64bits", "iOS", "Xamarin.iOS.dll"));
}
// XamarinWatchOSDll is stripped of its IL
yield return new TestCaseData (Path.Combine (Configuration.MonoTouchRootDirectory, "lib", "32bits", "watchOS", "Xamarin.WatchOS.dll"));
// XamarinTVOSDll is stripped of it's IL
yield return new TestCaseData (Path.Combine (Configuration.MonoTouchRootDirectory, "lib", "64bits", "tvOS", "Xamarin.TVOS.dll"));
if (Configuration.include_watchos) {
// XamarinWatchOSDll is stripped of its IL
yield return new TestCaseData (Path.Combine (Configuration.MonoTouchRootDirectory, "lib", "32bits", "watchOS", "Xamarin.WatchOS.dll"));
}
yield return new TestCaseData (Configuration.XamarinMacMobileDll);
yield return new TestCaseData (Configuration.XamarinMacFullDll);
if (Configuration.include_tvos) {
// XamarinTVOSDll is stripped of it's IL
yield return new TestCaseData (Path.Combine (Configuration.MonoTouchRootDirectory, "lib", "64bits", "tvOS", "Xamarin.TVOS.dll"));
}
if (Configuration.include_mac) {
yield return new TestCaseData (Configuration.XamarinMacMobileDll);
yield return new TestCaseData (Configuration.XamarinMacFullDll);
}
}
}
@ -184,8 +192,10 @@ namespace Cecil.Tests {
public static IEnumerable TaskAssemblies {
get {
yield return CreateTestFixtureDataFromPath (Path.Combine (Configuration.SdkRootXI, "lib", "msbuild", "iOS", "Xamarin.iOS.Tasks.dll"));
yield return CreateTestFixtureDataFromPath (Path.Combine (Configuration.SdkRootXM, "lib", "msbuild", "Xamarin.Mac.Tasks.dll"));
if (Configuration.include_ios)
yield return CreateTestFixtureDataFromPath (Path.Combine (Configuration.SdkRootXI, "lib", "msbuild", "iOS", "Xamarin.iOS.Tasks.dll"));
if (Configuration.include_mac)
yield return CreateTestFixtureDataFromPath (Path.Combine (Configuration.SdkRootXM, "lib", "msbuild", "Xamarin.Mac.Tasks.dll"));
}
}

Просмотреть файл

@ -8,7 +8,7 @@ build:
$(SYSTEM_MSBUILD) /r
run-tests: build
$(TOP)/tools/nunit3-console-3.11.1 $(abspath $(TOP)/tests/cecil-tests/bin/Debug/net472/cecil-tests.dll) $(TEST_NAME)
$(TOP)/tools/nunit3-console-3.11.1 $(abspath $(TOP)/tests/cecil-tests/bin/Debug/net472/cecil-tests.dll) $(TEST_NAME) -labels=After
clean:
rm -rf bin/ obj/ TestResult.xml

Просмотреть файл

@ -15,13 +15,10 @@ namespace Cecil.Tests {
[TestFixture]
public class MarshalAsTest {
[TestCaseSource (typeof (Helper), "PlatformAssemblies")]
[TestCaseSource (typeof (Helper), nameof (Helper.PlatformAssemblies))]
public void TestAssembly (string assemblyPath)
{
var assembly = Helper.GetAssembly (assemblyPath);
if (assembly == null)
Assert.Ignore ($"{assemblyPath} could not be found (might be disabled in build)");
var failedMethods = new List<string> ();
List<string>? failures = null;
var checkedTypes = new List<TypeReference> ();

Просмотреть файл

@ -16,8 +16,7 @@ namespace Cecil.Tests {
[TestCaseSource (typeof (Helper), nameof (Helper.NetPlatformImplementationAssemblies))] // call this method with every .net6 library
public void GetAllObsoletedThings (string assemblyPath)
{
var assembly = Helper.GetAssembly (assemblyPath, readSymbols: true)!;
Assert.That (assembly, Is.Not.Null, "Must find the assembly");
var assembly = Helper.GetAssembly (assemblyPath, readSymbols: true);
// Make a list of Obsolete things
var found = new HashSet<string> ();

Просмотреть файл

@ -14,7 +14,7 @@ namespace Cecil.Tests {
// https://github.com/xamarin/xamarin-macios/issues/9724
public void BeGone (string assemblyPath)
{
var assembly = Helper.GetAssembly (assemblyPath)!;
var assembly = Helper.GetAssembly (assemblyPath);
var found = new HashSet<string> ();
foreach (var type in assembly.MainModule.Types) {
if (type.Namespace?.StartsWith ("OpenTK", StringComparison.Ordinal) == true) {

Просмотреть файл

@ -14,13 +14,11 @@ namespace Cecil.Tests {
[TestFixture]
public class Test {
[TestCaseSource (typeof(Helper), "PlatformAssemblies")]
[TestCaseSource (typeof (Helper), nameof (Helper.PlatformAssemblies))]
// ref: https://github.com/xamarin/xamarin-macios/pull/7760
public void IdentifyBackingFieldAssignation (string assemblyPath)
{
var assembly = Helper.GetAssembly (assemblyPath);
if (assembly == null)
Assert.Ignore ($"{assemblyPath} could not be found (might be disabled in build)");
// look inside all .cctor (static constructor) inside `assemblyName`
foreach (var m in Helper.FilterMethods (assembly!, (m) => m.IsStatic && m.IsConstructor)) {
foreach (var ins in m.Body.Instructions) {
@ -51,15 +49,11 @@ namespace Cecil.Tests {
}
}
[TestCaseSource (typeof (Helper), "PlatformAssemblies")]
[TestCaseSource (typeof (Helper), nameof (Helper.PlatformAssemblies))]
// ref: https://github.com/xamarin/xamarin-macios/issues/8249
public void EnsureUIThreadOnInit (string assemblyPath)
{
var assembly = Helper.GetAssembly (assemblyPath);
if (assembly == null) {
Assert.Ignore ($"{assemblyPath} could not be found (might be disabled in build)");
return; // just to help nullability
}
// `CNContactsUserDefaults` is `[ThreadSafe (false)]` and part of iOS and macOS
var t = assembly.MainModule.GetType ("Contacts.CNContactsUserDefaults");
@ -89,17 +83,13 @@ namespace Cecil.Tests {
}
}
[TestCaseSource (typeof (Helper), "PlatformAssemblies")]
[TestCaseSource (typeof (Helper), nameof (Helper.PlatformAssemblies))]
public void NoSystemConsoleReference (string assemblyPath)
{
if (Path.GetFileName (assemblyPath) == "Xamarin.Mac.dll")
Assert.Ignore ("Xamarin.Mac has a workaround for Sierra bug w/NSLog");
var assembly = Helper.GetAssembly (assemblyPath);
if (assembly == null) {
Assert.Ignore ($"{assemblyPath} could not be found (might be disabled in build)");
return; // just to help nullability
}
// this has a quite noticeable impact on (small) app size
if (assembly.MainModule.TryGetTypeReference ("System.Console", out var _))
Assert.Fail ($"{assemblyPath} has a reference to `System.Console`. Please use `Runtime.NSLog` inside the platform assemblies");
@ -135,14 +125,10 @@ namespace Cecil.Tests {
"wvsprintfa", "wvsprintfw"
};
[TestCaseSource (typeof (Helper), "PlatformAssemblies")]
[TestCaseSource (typeof (Helper), nameof (Helper.PlatformAssemblies))]
public void NoBannedApi (string assemblyPath)
{
var assembly = Helper.GetAssembly (assemblyPath);
if (assembly == null) {
Assert.Ignore ($"{assemblyPath} could not be found (might be disabled in build)");
return; // just to help nullability
}
List<string> found = new List<string> ();
foreach (var m in Helper.FilterMethods (assembly!, (m) => m.IsPInvokeImpl)) {
var symbol = m.PInvokeInfo.EntryPoint;
@ -164,15 +150,11 @@ namespace Cecil.Tests {
MacCatalyst,
}
[TestCaseSource (typeof (Helper), "PlatformAssemblies")]
[TestCaseSource (typeof (Helper), nameof (Helper.PlatformAssemblies))]
// ref: https://github.com/xamarin/xamarin-macios/issues/4835
public void Unavailable (string assemblyPath)
{
var assembly = Helper.GetAssembly (assemblyPath);
if (assembly == null) {
Assert.Ignore ($"{assemblyPath} could not be found (might be disabled in build)");
return; // just to help nullability
}
var platform = PlatformName.None;
switch (assembly.Name.Name) {

Просмотреть файл

@ -5,6 +5,7 @@
<RootNamespace>ceciltests</RootNamespace>
<AssemblyName>cecil-tests</AssemblyName>
<LangVersion>latest</LangVersion>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />

Просмотреть файл

@ -9,6 +9,8 @@ using NUnit.Framework;
using Xamarin.Utils;
#nullable disable // until we get around to fixing this file
namespace Xamarin.Tests
{
static partial class Configuration

Просмотреть файл

@ -8,6 +8,8 @@ using System.Text.RegularExpressions;
using NUnit.Framework;
using Xamarin.Utils;
#nullable disable // until we get around to fixing this file
namespace Xamarin.Tests
{
class XBuild

Просмотреть файл

@ -10,6 +10,8 @@ using System;
using System.Collections.Generic;
using System.Linq;
#nullable disable // until we get around to fixing this file
namespace Xamarin.Utils
{
public struct TargetFramework : IEquatable<TargetFramework>