Skip to content

Commit

Permalink
Add test project and solution fpr ILSpy plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
charlesbetros committed Feb 12, 2020
1 parent da440ff commit 19b186b
Show file tree
Hide file tree
Showing 12 changed files with 251 additions and 104 deletions.
31 changes: 31 additions & 0 deletions Tools/ILSpyPlugAddIn/ILSpyPlugAddIn.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.29709.97
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ILSpyPlugAddIn", "ILSpyPlugAddIn\ILSpyPlugAddIn.csproj", "{08FE791B-5864-4E6F-AF7B-A02742C9C764}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ILSpyPlugAddin.Tests", "ILSpyPlugAddin.Tests\ILSpyPlugAddin.Tests\ILSpyPlugAddin.Tests.csproj", "{573518BB-28C4-47DC-81A1-1C1FECDD87D3}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{08FE791B-5864-4E6F-AF7B-A02742C9C764}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{08FE791B-5864-4E6F-AF7B-A02742C9C764}.Debug|Any CPU.Build.0 = Debug|Any CPU
{08FE791B-5864-4E6F-AF7B-A02742C9C764}.Release|Any CPU.ActiveCfg = Release|Any CPU
{08FE791B-5864-4E6F-AF7B-A02742C9C764}.Release|Any CPU.Build.0 = Release|Any CPU
{573518BB-28C4-47DC-81A1-1C1FECDD87D3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{573518BB-28C4-47DC-81A1-1C1FECDD87D3}.Debug|Any CPU.Build.0 = Debug|Any CPU
{573518BB-28C4-47DC-81A1-1C1FECDD87D3}.Release|Any CPU.ActiveCfg = Release|Any CPU
{573518BB-28C4-47DC-81A1-1C1FECDD87D3}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {DFF6D834-D4FF-4981-A1E0-E087501A6E36}
EndGlobalSection
EndGlobal
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net46</TargetFramework>

<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="nunit" Version="3.12.0" />
<PackageReference Include="NUnit3TestAdapter" Version="3.15.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.4.0" />
</ItemGroup>

<ItemGroup>
<ILSpyReference Include="ICSharpCode.TreeView.dll" />
<ILSpyReference Include="ILSpy.exe" />
<ILSpyReference Include="Mono.Cecil.dll" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\ILSpyPlugAddIn\ILSpyPlugAddIn.csproj" />
</ItemGroup>

<ItemGroup>
<Reference Include="@(ILSpyReference)" HintPath="$(RepoRoot)Resources\Dependencies\ILSpy\%(Identity)" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Cosmos.ILSpyPlugs.Plugin;
using ICSharpCode.ILSpy;
using Mono.Cecil;
using NUnit.Framework;

namespace ILSpyPlugAddin.Tests
{
public class UtilitiesTests
{
[Test]
public void ReturnsExpectedFieldAccessString()
{
var moduleDefinition = CreateTestModuleDefinition();
var typeDefinition = CreateTestTypeDefinition();
var fieldDefinition = CreateTestFieldDefinition();
typeDefinition.Fields.Add(fieldDefinition);
moduleDefinition.Types.Add(typeDefinition);

string expected = "[FieldAccess(Name = \"TestNamespace.TestFieldType TestNamespace.TestType.TestField\")] ref [TestAssembly]TestNamespace.TestFieldType fieldTestField";
string actual = Utilities.GenerateFieldAccessPlugEntry(fieldDefinition);
Assert.AreEqual(expected, actual);
}

[Test]
public void ReturnsExpectedTypePlugEntry()
{
var typeDefinition = new TypeDefinition("TestNamespace", "TestType", TypeAttributes.Public);

var moduleDefinition = ModuleDefinition.CreateModule("TestModule", ModuleKind.Dll);
moduleDefinition.Types.Add(typeDefinition);

string expected = "[Plug(Target = typeof(global::TestNamespace.TestType))]\r\npublic static class TestTypeImpl\r\n{\r\n}\r\n";
string actual = Utilities.GenerateTypePlugEntry(typeDefinition);
Assert.AreEqual(expected, actual);
}

[Test]
public void ReturnsExpectedMethodPlugEntry()
{
var moduleDefinition = CreateTestModuleDefinition();
var typeDefinition = CreateTestTypeDefinition();
var methodDefinition = CreateTestMethodDefinition();
typeDefinition.Methods.Add(methodDefinition);
moduleDefinition.Types.Add(typeDefinition);

string expected = "public static [TestAssembly]TestNamespace.TestFieldType TestMethod(TestNamespace.TestType aThis)\r\n{\r\n}\r\n";
string actual = Utilities.GenerateMethodPlugEntry(methodDefinition);
Assert.AreEqual(expected, actual);
}

private static ModuleDefinition CreateTestModuleDefinition()
{
var definition = ModuleDefinition.CreateModule("TestModule", ModuleKind.Dll);
return definition;
}

private static TypeDefinition CreateTestTypeDefinition()
{
var definition = new TypeDefinition("TestNamespace", "TestType", TypeAttributes.Public);
return definition;
}

private static TypeReference CreateTestTypeReference()
{
var module = CreateTestModuleDefinition();
var reference = new TypeReference("TestNamespace", "TestFieldType", module, new AssemblyNameDefinition("TestAssembly", new Version()));
return reference;
}

private static FieldDefinition CreateTestFieldDefinition()
{
var typeReference = CreateTestTypeReference();
var definition = new FieldDefinition("TestField", FieldAttributes.Public, typeReference);
return definition;
}

public static MethodDefinition CreateTestMethodDefinition()
{
var returnType = CreateTestTypeReference();
var definition = new MethodDefinition("TestMethod", MethodAttributes.Public, returnType);
return definition;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public override void Execute(TextViewContext context)
var xCurrentField = node as FieldTreeNode;
if (xCurrentField != null)
{
xString.Append(GenerateField(xCurrentField.FieldDefinition));
xString.Append(Utilities.GenerateFieldAccessPlugEntry(xCurrentField.FieldDefinition));
xString.AppendLine();
}
}
Expand All @@ -51,12 +51,5 @@ public override void Execute(TextViewContext context)

MessageBox.Show("Done", "Cosmos Plug tool");
}

public string GenerateField(FieldDefinition field)
{
StringBuilder xString = new StringBuilder();
xString.Append($"[FieldAccess(Name = \"{field.FieldType.FullName} {field.DeclaringType.FullName}.{field.Name}\")] ref {Utilities.GetCSharpTypeName(field.FieldType)} field{field.Name}");
return xString.ToString();
}
}
}
53 changes: 53 additions & 0 deletions Tools/ILSpyPlugAddIn/ILSpyPlugAddin/GenerateMethodPlugEntry.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using ICSharpCode.ILSpy;
using ICSharpCode.ILSpy.TreeNodes;
using Mono.Cecil;

namespace Cosmos.ILSpyPlugs.Plugin
{
[ExportContextMenuEntry(Header = "Cosmos Plug: Generate method plug")]
public class GenerateMethodPlugEntry: BaseContextMenuEntry
{
public override bool IsVisible(TextViewContext context)
{
if (context?.SelectedTreeNodes != null)
{
foreach (var node in context.SelectedTreeNodes)
{
var xCurrentMethod = node as MethodTreeNode;
if (xCurrentMethod != null)
{
return true;
}
}
}
return false;
}

public override void Execute(TextViewContext context)
{
if (MessageBox.Show("Do you want to generate plug code to your clipboard?", "Cosmos Plug tool", MessageBoxButton.YesNo) == MessageBoxResult.No)
{
return;
}

StringBuilder xString = new StringBuilder();
foreach (var node in context.SelectedTreeNodes)
{
var xCurrentMethod = node as MethodTreeNode;
xString.Append(Utilities.GenerateMethodPlugEntry(xCurrentMethod.MethodDefinition));
xString.AppendLine();
}

Clipboard.SetText(xString.ToString());

MessageBox.Show("Done", "Cosmos Plug tool");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,15 @@ public string GenerateProperty(PropertyDefinition property)
StringBuilder xString = new StringBuilder();
if (property.GetMethod != null)
{
xString.AppendLine(GenerateMethodPlugEntry.GenerateMethod(property.GetMethod));
xString.AppendLine(Utilities.GenerateMethodPlugEntry(property.GetMethod));
xString.AppendLine();
}
if (property.SetMethod != null)
{
xString.AppendLine(GenerateMethodPlugEntry.GenerateMethod(property.SetMethod));
xString.AppendLine(Utilities.GenerateMethodPlugEntry(property.SetMethod));
xString.AppendLine();
}
return xString.ToString();
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public override void Execute(TextViewContext context)
var xCurrentType = node as TypeTreeNode;
if (xCurrentType != null)
{
xString.Append(GenerateType(xCurrentType.TypeDefinition));
xString.Append(Utilities.GenerateTypePlugEntry(xCurrentType.TypeDefinition));
xString.AppendLine();
}
}
Expand All @@ -51,20 +51,5 @@ public override void Execute(TextViewContext context)

MessageBox.Show("Done", "Cosmos Plug tool");
}

public string GenerateType(TypeDefinition type)
{
var xString = new StringBuilder();
xString.AppendFormat(
type.IsPublic
? "[Plug(Target = typeof(global::{0}))]"
: "[Plug(TargetName = \"{0}, {1}\")]", Utilities.GetCSharpTypeName(type), type.Module.Assembly.Name);
xString.AppendLine();
xString.AppendFormat("public static class {0}Impl", type.Name);
xString.AppendLine();
xString.AppendLine("{");
xString.AppendLine("}");
return xString.ToString();
}
}
}
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,59 +1,34 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using ICSharpCode.ILSpy;
using ICSharpCode.ILSpy.TreeNodes;
using Mono.Cecil;

namespace Cosmos.ILSpyPlugs.Plugin
{
[ExportContextMenuEntry(Header = "Cosmos Plug: Generate method plug")]
public class GenerateMethodPlugEntry: BaseContextMenuEntry
public static class Utilities
{
public override bool IsVisible(TextViewContext context)
public static string GenerateTypePlugEntry(TypeDefinition type)
{
if (context?.SelectedTreeNodes != null)
{
foreach (var node in context.SelectedTreeNodes)
{
var xCurrentMethod = node as MethodTreeNode;
if (xCurrentMethod != null)
{
return true;
}
}
}
return false;
var xString = new StringBuilder();
xString.AppendFormat(
type.IsPublic
? "[Plug(Target = typeof(global::{0}))]"
: "[Plug(TargetName = \"{0}, {1}\")]", Utilities.GetCSharpTypeName(type), type.Module.Assembly.Name);
xString.AppendLine();
xString.AppendFormat("public static class {0}Impl", type.Name);
xString.AppendLine();
xString.AppendLine("{");
xString.AppendLine("}");
return xString.ToString();
}

public override void Execute(TextViewContext context)
{
if (MessageBox.Show("Do you want to generate plug code to your clipboard?", "Cosmos Plug tool", MessageBoxButton.YesNo) == MessageBoxResult.No)
{
return;
}

StringBuilder xString = new StringBuilder();
foreach (var node in context.SelectedTreeNodes)
{
var xCurrentMethod = node as MethodTreeNode;
xString.Append(GenerateMethod(xCurrentMethod.MethodDefinition));
xString.AppendLine();
}

Clipboard.SetText(xString.ToString());

MessageBox.Show("Done", "Cosmos Plug tool");
}

public static string GenerateMethod(MethodDefinition method)
public static string GenerateMethodPlugEntry(MethodDefinition method)
{
var xSB = new StringBuilder();

xSB.Append($"public static {Utilities.GetCSharpTypeName(method.ReturnType)} {Utilities.GetMethodName(method)}(");
var xAddComma = false;

Expand Down Expand Up @@ -102,5 +77,35 @@ public static string GenerateMethod(MethodDefinition method)
xSB.AppendLine("}");
return xSB.ToString();
}

public static string GenerateFieldAccessPlugEntry(FieldDefinition field)
{
StringBuilder xString = new StringBuilder();
xString.Append($"[FieldAccess(Name = \"{field.FieldType.FullName} {field.DeclaringType.FullName}.{field.Name}\")] ref {Utilities.GetCSharpTypeName(field.FieldType)} field{field.Name}");
return xString.ToString();
}

public static string GetCSharpTypeName(TypeReference reference)
{
var xCSharp = Languages.GetLanguage("C#");

return xCSharp.TypeToString(reference, true);
}

public static string GetMethodName(MethodDefinition method)
{
if (method.IsConstructor)
{
if (method.IsStatic)
{
return "Cctor";
}
else
{
return "Ctor";
}
}
return method.Name;
}
}
}
}
Loading

0 comments on commit 19b186b

Please sign in to comment.