Skip to content
This repository has been archived by the owner on May 9, 2020. It is now read-only.

Commit

Permalink
Add sources for MEFX
Browse files Browse the repository at this point in the history
  • Loading branch information
dsplaisted authored and terrajobst committed Aug 19, 2017
1 parent 626adfc commit 2b94cc8
Show file tree
Hide file tree
Showing 87 changed files with 5,740 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
//------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//------------------------------------------------------------

using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.ComponentModel.Composition.Hosting;
using System.ComponentModel.Composition.Primitives;

namespace Microsoft.ComponentModel.Composition.Diagnostics.Tests
{
[TestClass]
public class IntegrationTests
{
ComposablePartCatalog _catalog;
CompositionContainer _container;
CompositionInfo _analysis;

public IntegrationTests()
{
_catalog = new TypeCatalog(
typeof(Foo), typeof(Bar), typeof(Biff), typeof(MissingMeta),
typeof(WrongCreationPolicy), typeof(WrongTypeIdentity));

_container = new CompositionContainer(_catalog);

_analysis = new CompositionInfo(_catalog, _container);
}

[TestMethod]
public void FindsRootCauseOfRejection()
{
var fooInfo = _analysis.GetPartDefinitionInfo(typeof(Foo));

var rootCause = fooInfo.FindPossibleRootCauses().Single();

var barInfo = _analysis.GetPartDefinitionInfo(typeof(Bar));

Assert.AreSame(barInfo, rootCause);
}

[TestMethod]
public void FindsRootCauseOfRejectionWhenPartIsRootCause()
{
var barInfo = _analysis.GetPartDefinitionInfo(typeof(Bar));

var rootCause = barInfo.FindPossibleRootCauses().Single();

Assert.AreSame(barInfo, rootCause);
}

[TestMethod]
public void FindsThreeUnsuitableExportsForBiff()
{
var barInfo = _analysis.GetPartDefinitionInfo(typeof(Bar));

var biffImport = barInfo.ImportDefinitions.Single();

Assert.AreEqual(3, biffImport.UnsuitableExportDefinitions.Count());
}

[TestMethod]
public void DetectsMissingMetadataCase()
{
CheckReasonForBiffUnsuitability(
typeof(MissingMeta),
UnsuitableExportDefinitionReason.RequiredMetadata);
}

[TestMethod]
public void DetectsIncompatibleCreationPolicyCase()
{
CheckReasonForBiffUnsuitability(
typeof(WrongCreationPolicy),
UnsuitableExportDefinitionReason.CreationPolicy);
}

[TestMethod]
public void DetectsTypeIdentityCase()
{
CheckReasonForBiffUnsuitability(
typeof(WrongTypeIdentity),
UnsuitableExportDefinitionReason.TypeIdentity);
}

void CheckReasonForBiffUnsuitability(Type partType, UnsuitableExportDefinitionReason reason)
{
var barInfo = _analysis.GetPartDefinitionInfo(typeof(Bar));
var unsuitablePart = _analysis.GetPartDefinitionInfo(partType);

var issue = barInfo.ImportDefinitions.Single()
.UnsuitableExportDefinitions
.Where(ed => ed.PartDefinition == unsuitablePart)
.Single()
.Issues
.Single();

Assert.AreEqual(reason, issue.Reason);
}

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProductVersion>9.0.30729</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{3D2774F9-84F4-4261-BFEB-14D1F94C6E56}</ProjectGuid>
<OutputType>Library</OutputType>
<OutputPath>..\bin\CompositionDiagnostics</OutputPath>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>Microsoft.ComponentModel.Composition.Diagnostics.Tests</RootNamespace>
<AssemblyName>Microsoft.ComponentModel.Composition.Diagnostics.Tests</AssemblyName>
<FileAlignment>512</FileAlignment>
<ProjectTypeGuids>{3AC096D0-A1C2-E12C-1390-A8335801FDAB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
</PropertyGroup>
<ItemGroup>
<Reference Include="Microsoft.VisualStudio.QualityTools.UnitTestFramework" />
<Reference Include="System" />
<Reference Include="System.ComponentModel.Composition" />
<Reference Include="System.Core" />
</ItemGroup>
<ItemGroup>
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="IntegrationTests.cs" />
<Compile Include="Scenario.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Composition.Diagnostics\Microsoft.ComponentModel.Composition.Diagnostics.csproj">
<Project>{0A44C587-989D-4785-9311-8BF8069BB775}</Project>
<Name>Microsoft.ComponentModel.Composition.Diagnostics</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

// 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: AssemblyTitle("Microsoft.ComponentModel.Composition.Diagnostics.Tests")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("Microsoft")]
[assembly: AssemblyProduct("Microsoft.ComponentModel.Composition.Diagnostics.Tests")]
[assembly: AssemblyCopyright("Copyright © Microsoft 2009")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// Setting ComVisible to false makes the types in this assembly not visible
// to COM componenets. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]

// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("51737055-7531-4c89-97fd-3df748d7316f")]

// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Revision and Build Numbers
// by using the '*' as shown below:
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
52 changes: 52 additions & 0 deletions Composition.Diagnostics/Composition.Diagnostics.Tests/Scenario.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
//------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.Composition;

namespace Microsoft.ComponentModel.Composition.Diagnostics.Tests
{
public class Biff { }

public interface IBiffMetadata
{
string Name { get; }
}

public class MissingMeta
{
[Export("biff")]
public Biff Biff = new Biff();
}

[PartCreationPolicy(CreationPolicy.Shared)]
public class WrongCreationPolicy
{
[Export("biff"), ExportMetadata("Name", "BiffBiff")]
public Biff Biff = new Biff();
}

public class WrongTypeIdentity
{
[Export("biff"), ExportMetadata("Name", "Baf")]
public string Biff = "Yep";
}

[Export]
public class Bar
{
[Import("biff", RequiredCreationPolicy = CreationPolicy.NonShared)]
public Lazy<Biff, IBiffMetadata> Biff { get; set; }
}

[Export]
public class Foo
{
[Import]
public Bar Bar { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
//------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Microsoft.ComponentModel.Composition.Diagnostics
{
/// <summary>
/// Describes use of a contract within a composition.
/// </summary>
public class CompositionContractInfo
{
public CompositionContractInfo(ContractInfo contract)
{
Contract = contract;
Importers = new HashSet<PartDefinitionInfo>();
Exporters = new HashSet<PartDefinitionInfo>();
}

/// <summary>
/// The contract.
/// </summary>
public ContractInfo Contract { get; private set; }

/// <summary>
/// Importers of the contract.
/// </summary>
public ICollection<PartDefinitionInfo> Importers { get; private set; }

/// <summary>
/// Exporters of the contract.
/// </summary>
public ICollection<PartDefinitionInfo> Exporters { get; private set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
//------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.Composition.Primitives;

namespace Microsoft.ComponentModel.Composition.Diagnostics
{
/// <summary>
/// Pretty-printer for objects involved in compositoin.
/// </summary>
public static class CompositionElementTextFormatter
{
/// <summary>
/// Formats a composition element for display.
/// </summary>
/// <param name="compositionElement">Composition element, optionally
/// implementing ICompositionElement.</param>
/// <returns>String representation of compositionElement.</returns>
public static string DisplayCompositionElement(object compositionElement)
{
return compositionElement is ICompositionElement ?
((ICompositionElement)compositionElement).DisplayName :
compositionElement.ToString();
}

/// <summary>
/// Formats extended information about a composition element for display.
/// </summary>
/// <param name="compositionElement">Composition element, optionally
/// implementing ICompositionElement.</param>
/// <returns>String description of compositionElement.</returns>
public static string DescribeCompositionElement(object compositionElement)
{
var result = new StringBuilder();

var nextInChain = compositionElement;

while (nextInChain != null)
{
var nextIce = nextInChain as ICompositionElement;
ICompositionElement parentIce = null;
if (nextIce == null)
{
result.Append(nextInChain.ToString());
}
else
{
result.Append(nextIce.DisplayName);
parentIce = nextIce.Origin;
}

nextInChain = parentIce;

if (nextInChain != null)
result.Append(" from: ");
}

return result.ToString();
}
}
}
Loading

0 comments on commit 2b94cc8

Please sign in to comment.