mirror of https://github.com/icsharpcode/ILSpy.git
Browse Source
TestPlugin was still WPF (net10.0-windows, UseWpf, System.Windows, TomsToolbox, the old ICSharpCode.ILSpy namespace), so it could not load into the cross-platform app. Re-target it to net10.0 and map each extension point onto the port's contracts: the custom Language, About-page addition, context-menu entry, main-menu/toolbar command, and an Avalonia options page whose view the ViewLocator resolves by naming convention. The command needs [ImportingConstructor] for System.Composition to satisfy its service dependency. Reference it build-only from the tests (no DLL copied into the test output) so a composition test can load it without the headless app auto-loading the plugin into every other test. Assisted-by: Claude:claude-opus-4-8:Claude Codepull/3755/head
13 changed files with 229 additions and 138 deletions
@ -0,0 +1,106 @@
@@ -0,0 +1,106 @@
|
||||
// Copyright (c) 2026 AlphaSierraPapa for the SharpDevelop Team
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
|
||||
// software and associated documentation files (the "Software"), to deal in the Software
|
||||
// without restriction, including without limitation the rights to use, copy, modify, merge,
|
||||
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
|
||||
// to whom the Software is furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in all copies or
|
||||
// substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
|
||||
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
|
||||
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
|
||||
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
||||
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
// DEALINGS IN THE SOFTWARE.
|
||||
|
||||
using System; |
||||
using System.Composition.Hosting; |
||||
using System.IO; |
||||
using System.Linq; |
||||
using System.Reflection; |
||||
|
||||
using AwesomeAssertions; |
||||
|
||||
using ILSpy.Commands; |
||||
using ILSpy.Languages; |
||||
using ILSpy.Options; |
||||
|
||||
using NUnit.Framework; |
||||
|
||||
namespace ICSharpCode.ILSpy.Tests.Plugins; |
||||
|
||||
/// <summary>
|
||||
/// The ported sample plugin (Test.Plugin.dll) exposes its extension points through the same MEF
|
||||
/// contracts the app composes at startup. This loads the built plugin assembly (not referenced into
|
||||
/// this test's output, to avoid polluting the headless app's *.Plugin.dll scan) and asserts its
|
||||
/// exports resolve against the Avalonia app's extension-point types.
|
||||
/// </summary>
|
||||
[TestFixture] |
||||
public class TestPluginCompositionTests |
||||
{ |
||||
static Assembly LoadPlugin() |
||||
{ |
||||
// AppContext.BaseDirectory is .../ILSpy.Tests/bin/<Config>/net11.0/. The plugin targets net10.0
|
||||
// (the host loads it as a library, so its TFM need not track the test project's), and builds to
|
||||
// the sibling TestPlugin/bin/<Config>/net10.0/ (the ProjectReference guarantees it's built first).
|
||||
var baseDir = AppContext.BaseDirectory.TrimEnd(Path.DirectorySeparatorChar); |
||||
var config = Path.GetFileName(Path.GetDirectoryName(baseDir)!); |
||||
var repo = Path.GetFullPath(Path.Combine(baseDir, "..", "..", "..", "..")); |
||||
var dll = Path.Combine(repo, "TestPlugin", "bin", config, "net10.0", "Test.Plugin.dll"); |
||||
|
||||
File.Exists(dll).Should().BeTrue($"the sample plugin must be built at {dll}"); |
||||
return Assembly.LoadFrom(dll); |
||||
} |
||||
|
||||
[Test] |
||||
public void Plugin_Contributes_A_Custom_Language() |
||||
{ |
||||
using var container = new ContainerConfiguration().WithAssembly(LoadPlugin()).CreateContainer(); |
||||
|
||||
container.GetExports<Language>().Should().Contain(l => l.Name == "Custom", |
||||
"the plugin exports a Language named 'Custom'"); |
||||
} |
||||
|
||||
[Test] |
||||
public void Plugin_Contributes_An_Options_Page() |
||||
{ |
||||
using var container = new ContainerConfiguration().WithAssembly(LoadPlugin()).CreateContainer(); |
||||
|
||||
// [ExportOptionPage] exports under the named "OptionPages" contract, not the default one.
|
||||
container.GetExports<IOptionPage>("OptionPages").Should().Contain(p => p.Title == "TestPlugin", |
||||
"the plugin exports an [ExportOptionPage] titled 'TestPlugin'"); |
||||
} |
||||
|
||||
[Test] |
||||
public void Plugin_Contributes_An_About_Page_Addition() |
||||
{ |
||||
using var container = new ContainerConfiguration().WithAssembly(LoadPlugin()).CreateContainer(); |
||||
|
||||
container.GetExports<IAboutPageAddition>().Should().NotBeEmpty( |
||||
"the plugin exports an IAboutPageAddition"); |
||||
} |
||||
|
||||
[Test] |
||||
public void Commands_With_A_DI_Constructor_Mark_It_Importing() |
||||
{ |
||||
// System.Composition can only instantiate a parameterized command constructor when it is
|
||||
// marked [ImportingConstructor]. A command export whose ctor takes services but lacks the
|
||||
// attribute throws when the menu/toolbar builder materialises it -- which previously took the
|
||||
// whole menu bar down. Guard every exported command in the plugin.
|
||||
var commands = LoadPlugin().GetTypes() |
||||
.Where(t => typeof(System.Windows.Input.ICommand).IsAssignableFrom(t) && !t.IsAbstract); |
||||
|
||||
foreach (var type in commands) |
||||
{ |
||||
var ctors = type.GetConstructors(); |
||||
if (ctors.Any(c => c.GetParameters().Length == 0)) |
||||
continue; // a parameterless ctor is always fine
|
||||
ctors.Should().Contain( |
||||
c => c.GetCustomAttribute<System.Composition.ImportingConstructorAttribute>() != null, |
||||
$"{type.Name} has only parameterized constructors, so one must be [ImportingConstructor]"); |
||||
} |
||||
} |
||||
} |
||||
@ -1,13 +0,0 @@
@@ -1,13 +0,0 @@
|
||||
<UserControl x:Class="TestPlugin.CustomOptionPage" |
||||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" |
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" |
||||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" d:DesignHeight="500" d:DesignWidth="500" |
||||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" |
||||
xmlns:testPlugin="clr-namespace:TestPlugin" |
||||
d:DataContext="{d:DesignInstance testPlugin:CustomOptionsViewModel}" |
||||
> |
||||
<StackPanel> |
||||
<CheckBox IsChecked="{Binding Options.UselessOption1}">Useless option 1</CheckBox> |
||||
<Slider Minimum="0" Maximum="100" Value="{Binding Options.UselessOption2}"/> |
||||
</StackPanel> |
||||
</UserControl> |
||||
@ -0,0 +1,15 @@
@@ -0,0 +1,15 @@
|
||||
<UserControl xmlns="https://github.com/avaloniaui" |
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" |
||||
xmlns:vm="using:TestPlugin" |
||||
x:Class="TestPlugin.CustomOptionsView" |
||||
x:DataType="vm:CustomOptionsViewModel"> |
||||
<StackPanel Spacing="6" Margin="6"> |
||||
<CheckBox IsChecked="{Binding Options.UselessOption1, Mode=TwoWay}" |
||||
Content="A completely useless option" /> |
||||
<StackPanel Orientation="Horizontal" Spacing="6"> |
||||
<TextBlock Text="Another useless option:" VerticalAlignment="Center" /> |
||||
<Slider Minimum="0" Maximum="100" Width="200" |
||||
Value="{Binding Options.UselessOption2, Mode=TwoWay}" /> |
||||
</StackPanel> |
||||
</StackPanel> |
||||
</UserControl> |
||||
@ -0,0 +1,15 @@
@@ -0,0 +1,15 @@
|
||||
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
|
||||
// This code is distributed under MIT X11 license (for details please see \doc\license.txt)
|
||||
|
||||
using Avalonia.Controls; |
||||
|
||||
namespace TestPlugin |
||||
{ |
||||
public partial class CustomOptionsView : UserControl |
||||
{ |
||||
public CustomOptionsView() |
||||
{ |
||||
InitializeComponent(); |
||||
} |
||||
} |
||||
} |
||||
@ -1,18 +0,0 @@
@@ -1,18 +0,0 @@
|
||||
#region Using directives
|
||||
|
||||
using System.Reflection; |
||||
using System.Runtime.InteropServices; |
||||
|
||||
#endregion
|
||||
|
||||
// General Information about an assembly is controlled through the following
|
||||
// set of attributes. Change these attribute values to modify the information
|
||||
// associated with an assembly.
|
||||
[assembly: AssemblyDescription("")] |
||||
[assembly: AssemblyCopyright("Copyright 2011")] |
||||
[assembly: AssemblyTrademark("")] |
||||
[assembly: AssemblyCulture("")] |
||||
|
||||
// This sets the default COM visibility of types in the assembly to invisible.
|
||||
// If you need to expose a type to COM, use [ComVisible(true)] on that type.
|
||||
[assembly: ComVisible(false)] |
||||
Loading…
Reference in new issue