Skip to content

Commit

Permalink
Add edge case with referencing enums in another assembly
Browse files Browse the repository at this point in the history
- Dependency crawler now properly finds usage of enums from other assemblies.
- Add edge case to Unit Test test app.
- Update dumper unit test because of new types added.
  • Loading branch information
josesimoes committed Apr 11, 2024
1 parent d88efb0 commit c4845d6
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 3 deletions.
11 changes: 10 additions & 1 deletion MetadataProcessor.Shared/nanoAssemblyBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -713,7 +713,16 @@ private HashSet<MetadataToken> BuildDependencyList(MetadataToken token)
{
if (v.VariableType.DeclaringType != null)
{
set.Add(v.VariableType.DeclaringType.MetadataToken);
var resolvedType = v.VariableType.Resolve();

if (resolvedType != null && resolvedType.IsEnum)
{
set.Add(v.VariableType.MetadataToken);
}
else
{
set.Add(v.VariableType.DeclaringType.MetadataToken);
}
}
else if (v.VariableType.MetadataType == MetadataType.Class)
{
Expand Down
4 changes: 2 additions & 2 deletions MetadataProcessor.Tests/Core/Utility/DumperTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ public void DumpAssemblyTest()
Assert.IsTrue(dumpFileContent.Contains("TypeRefProps [01000001]: Scope: 23000001 'System.Diagnostics.DebuggableAttribute'"));
Assert.IsTrue(dumpFileContent.Contains(": Scope: 23000002 'TestNFClassLibrary.ClassOnAnotherAssembly'"));

Assert.IsTrue(dumpFileContent.Contains(": Flags: 00001001 Extends: 0100000d Enclosed: 02000000 'TestNFApp.DummyCustomAttribute1'"));
Assert.IsTrue(dumpFileContent.Contains(": Flags: 00001001 Extends: 0100000f Enclosed: 02000000 'TestNFApp.DummyCustomAttribute1'"));

Assert.IsTrue(dumpFileContent.Contains(": Flags: 00001001 Extends: 0100000d Enclosed: 02000000 'TestNFApp.DummyCustomAttribute2'"));
Assert.IsTrue(dumpFileContent.Contains(": Flags: 00001001 Extends: 0100000f Enclosed: 02000000 'TestNFApp.DummyCustomAttribute2'"));

Assert.IsTrue(dumpFileContent.Contains(": Flags: 00001061 Extends: 01000000 Enclosed: 02000000 'TestNFApp.IOneClassOverAll'"));
Assert.IsTrue(dumpFileContent.Contains(": Flags: 000007c6 Impl: 00000000 RVA: 00000000 'get_DummyProperty' [I4( )]"));
Expand Down
21 changes: 21 additions & 0 deletions MetadataProcessor.Tests/TestNFApp/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

using System;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using TestNFClassLibrary;

Expand Down Expand Up @@ -36,6 +37,26 @@ public static void Main()
// Reflection Tests
ReflectionTests();

////////////////////////////////////////////////
// Test enum in another assembly, same namespace
var enumTest = new TestEnumInAnotherAssembly();
enumTest.CallTestEnumInAnotherAssembly();

/////////////////////////////////////
// reference enum in another assembly
var x = (IAmAClassWithAnEnum.EnumA)1;

var messageType = (IAmAClassWithAnEnum.EnumA)0;
switch (messageType)
{
case IAmAClassWithAnEnum.EnumA.Test:
Console.WriteLine("all good");
break;

default:
break;
}

Debug.WriteLine("Exiting TestNFApp");
}

Expand Down
20 changes: 20 additions & 0 deletions MetadataProcessor.Tests/TestNFApp/TestEnumInAnotherAssembly.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//
// Copyright (c) .NET Foundation and Contributors
// See LICENSE file in the project root for full license information.
//

namespace System.IO
{
public class TestEnumInAnotherAssembly
{
public void CallTestEnumInAnotherAssembly()
{
// This test checks if MDP can minimize the assembly using an enum that is defined in another assembly
// and the class calling it is in a different assembly BUT in the same namespace.
var ioException = new IOException(
string.Empty,
(int)IOException.IOExceptionErrorCode.DirectoryNotFound);
}
}

}
1 change: 1 addition & 0 deletions MetadataProcessor.Tests/TestNFApp/TestNFApp.nfproj
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
<Compile Include="IOneClassOverAll.cs" />
<Compile Include="ComplexAttribute.cs" />
<Compile Include="MaxAttribute.cs" />
<Compile Include="TestEnumInAnotherAssembly.cs" />
<Compile Include="MyAttribute.cs" />
<Compile Include="MyClass1.cs" />
<Compile Include="OneClassOverAll.cs" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//
// Copyright (c) .NET Foundation and Contributors
// See LICENSE file in the project root for full license information.
//

namespace TestNFClassLibrary
{
public class IAmAClassWithAnEnum
{
public enum EnumA
{
Test = 1
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
</PropertyGroup>
<Import Project="$(NanoFrameworkProjectSystemPath)NFProjectSystem.props" Condition="Exists('$(NanoFrameworkProjectSystemPath)NFProjectSystem.props')" />
<ItemGroup>
<Compile Include="IAmAClassWithAnEnum.cs" />
<Compile Include="ClassOnAnotherAssembly.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
Expand Down

0 comments on commit c4845d6

Please sign in to comment.