Skip to content

Commit

Permalink
Merge pull request dotnet#20962 from hughbe/composition-attributemode…
Browse files Browse the repository at this point in the history
…l-tests

Add System.Composition.AttributedModel tests
  • Loading branch information
stephentoub authored Jun 14, 2017
2 parents aaeb95b + 0b907b2 commit 1476c2e
Show file tree
Hide file tree
Showing 24 changed files with 402 additions and 50 deletions.
Original file line number Diff line number Diff line change
@@ -1,26 +1,45 @@
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 14
VisualStudioVersion = 14.0.25420.1
# Visual Studio 15
VisualStudioVersion = 15.0.26430.6
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "System.Composition.AttributedModel", "src\System.Composition.AttributedModel.csproj", "{C6257381-C624-494A-A9D9-5586E60856EA}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{E107E9C1-E893-4E87-987E-04EF0DCEAEFD}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "tests", "tests", "{DB303F4D-3697-4882-B6B9-FF221B6034A1}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "System.Composition.AttributeModel.Tests", "tests\System.Composition.AttributeModel.Tests.csproj", "{853BB14F-8A5B-42B4-A053-21DE1AEBB335}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
netstandard-Debug|Any CPU = netstandard-Debug|Any CPU
netstandard-Release|Any CPU = netstandard-Release|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{C6257381-C624-494A-A9D9-5586E60856EA}.Debug|Any CPU.ActiveCfg = netstandard-Debug|Any CPU
{C6257381-C624-494A-A9D9-5586E60856EA}.Debug|Any CPU.Build.0 = netstandard-Debug|Any CPU
{C6257381-C624-494A-A9D9-5586E60856EA}.netstandard-Debug|Any CPU.ActiveCfg = netstandard-Debug|Any CPU
{C6257381-C624-494A-A9D9-5586E60856EA}.netstandard-Debug|Any CPU.Build.0 = netstandard-Debug|Any CPU
{C6257381-C624-494A-A9D9-5586E60856EA}.netstandard-Release|Any CPU.ActiveCfg = netstandard-Release|Any CPU
{C6257381-C624-494A-A9D9-5586E60856EA}.netstandard-Release|Any CPU.Build.0 = netstandard-Release|Any CPU
{C6257381-C624-494A-A9D9-5586E60856EA}.Release|Any CPU.ActiveCfg = netstandard-Release|Any CPU
{C6257381-C624-494A-A9D9-5586E60856EA}.Release|Any CPU.Build.0 = netstandard-Release|Any CPU
{853BB14F-8A5B-42B4-A053-21DE1AEBB335}.Debug|Any CPU.ActiveCfg = netstandard-Debug|Any CPU
{853BB14F-8A5B-42B4-A053-21DE1AEBB335}.Debug|Any CPU.Build.0 = netstandard-Debug|Any CPU
{853BB14F-8A5B-42B4-A053-21DE1AEBB335}.netstandard-Debug|Any CPU.ActiveCfg = netstandard-Debug|Any CPU
{853BB14F-8A5B-42B4-A053-21DE1AEBB335}.netstandard-Debug|Any CPU.Build.0 = netstandard-Debug|Any CPU
{853BB14F-8A5B-42B4-A053-21DE1AEBB335}.netstandard-Release|Any CPU.ActiveCfg = netstandard-Release|Any CPU
{853BB14F-8A5B-42B4-A053-21DE1AEBB335}.netstandard-Release|Any CPU.Build.0 = netstandard-Release|Any CPU
{853BB14F-8A5B-42B4-A053-21DE1AEBB335}.Release|Any CPU.ActiveCfg = netstandard-Release|Any CPU
{853BB14F-8A5B-42B4-A053-21DE1AEBB335}.Release|Any CPU.Build.0 = netstandard-Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{C6257381-C624-494A-A9D9-5586E60856EA} = {E107E9C1-E893-4E87-987E-04EF0DCEAEFD}
{853BB14F-8A5B-42B4-A053-21DE1AEBB335} = {DB303F4D-3697-4882-B6B9-FF221B6034A1}
EndGlobalSection
EndGlobal
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ public class ExportAttribute : Attribute
/// Initializes a new instance of the <see cref="ExportAttribute"/> class, exporting the
/// type or member marked with this attribute under the default contract name.
/// </summary>
public ExportAttribute()
: this((string)null, (Type)null)
public ExportAttribute() : this(null, null)
{
}

Expand All @@ -33,8 +32,7 @@ public ExportAttribute()
/// member marked with this attribute, under; or <see langword="null"/> to use the
/// default contract name.
/// </param>
public ExportAttribute(Type contractType)
: this((string)null, contractType)
public ExportAttribute(Type contractType) : this(null, contractType)
{
}

Expand All @@ -43,12 +41,11 @@ public ExportAttribute(Type contractType)
/// type or member marked with this attribute under the specified contract name.
/// </summary>
/// <param name="contractName">
/// A <see cref="String"/> containing the contract name to export the type or member
/// A <see cref="string"/> containing the contract name to export the type or member
/// marked with this attribute, under; or <see langword="null"/> or an empty string
/// ("") to use the default contract name.
/// </param>
public ExportAttribute(string contractName)
: this(contractName, (Type)null)
public ExportAttribute(string contractName) : this(contractName, null)
{
}

Expand All @@ -57,7 +54,7 @@ public ExportAttribute(string contractName)
/// type or member marked with this attribute under the specified contract name.
/// </summary>
/// <param name="contractName">
/// A <see cref="String"/> containing the contract name to export the type or member
/// A <see cref="string"/> containing the contract name to export the type or member
/// marked with this attribute, under; or <see langword="null"/> or an empty string
/// ("") to use the default contract name.
/// </param>
Expand All @@ -76,10 +73,10 @@ public ExportAttribute(string contractName, Type contractType)
/// Gets the contract name to export the type or member under.
/// </summary>
/// <value>
/// A <see cref="String"/> containing the contract name to export the type or member
/// A <see cref="string"/> containing the contract name to export the type or member
/// marked with this attribute, under. The default value is an empty string ("").
/// </value>
public string ContractName { get; private set; }
public string ContractName { get; }

/// <summary>
/// Get the contract type that is exported by the member that this attribute is attached to.
Expand All @@ -89,6 +86,6 @@ public ExportAttribute(string contractName, Type contractType)
/// <see langword="null"/> which means that the type will be obtained by looking at the type on
/// the member that this export is attached to.
/// </value>
public Type ContractType { get; private set; }
public Type ContractType { get; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public sealed class ExportMetadataAttribute : Attribute
/// specified name and metadata value.
/// </summary>
/// <param name="name">
/// A <see cref="String"/> containing the name of the metadata value; or
/// A <see cref="string"/> containing the name of the metadata value; or
/// <see langword="null"/> to set the <see cref="Name"/> property to an empty
/// string ("").
/// </param>
Expand All @@ -35,16 +35,16 @@ public ExportMetadataAttribute(string name, object value)
/// Gets the name of the metadata value.
/// </summary>
/// <value>
/// A <see cref="String"/> containing the name of the metadata value.
/// A <see cref="string"/> containing the name of the metadata value.
/// </value>
public string Name { get; private set; }
public string Name { get; }

/// <summary>
/// Gets the metadata value.
/// </summary>
/// <value>
/// An <see cref="object"/> containing the metadata value.
/// </value>
public object Value { get; private set; }
public object Value { get; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ public class ImportAttribute : Attribute
/// Initializes a new instance of the <see cref="ImportAttribute"/> class, importing the
/// export without a contract name.
/// </summary>
public ImportAttribute()
: this((string)null)
public ImportAttribute() : this(null)
{
}

Expand All @@ -28,7 +27,7 @@ public ImportAttribute()
/// export with the specified contract name.
/// </summary>
/// <param name="contractName">
/// A <see cref="String"/> containing the contract name of the export to import, or
/// A <see cref="string"/> containing the contract name of the export to import, or
/// <see langword="null"/> or an empty string ("") to use the default contract name.
/// </param>
public ImportAttribute(string contractName)
Expand All @@ -40,10 +39,10 @@ public ImportAttribute(string contractName)
/// Gets the contract name of the export to import.
/// </summary>
/// <value>
/// A <see cref="String"/> containing the contract name of the export to import. The
/// A <see cref="string"/> containing the contract name of the export to import. The
/// default value is null.
/// </value>
public string ContractName { get; private set; }
public string ContractName { get; }

/// <summary>
/// Gets or sets a value indicating whether the property, field or parameter will be left
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ public class ImportManyAttribute : Attribute
/// Initializes a new instance of the <see cref="ImportManyAttribute"/> class, importing the
/// set of exports without a contract name.
/// </summary>
public ImportManyAttribute()
: this((string)null)
public ImportManyAttribute() : this(null)
{
}

Expand All @@ -28,7 +27,7 @@ public ImportManyAttribute()
/// set of exports with the specified contract name.
/// </summary>
/// <param name="contractName">
/// A <see cref="String"/> containing the contract name of the exports to import, or
/// A <see cref="string"/> containing the contract name of the exports to import, or
/// <see langword="null"/>.
/// </param>
public ImportManyAttribute(string contractName)
Expand All @@ -40,9 +39,9 @@ public ImportManyAttribute(string contractName)
/// Gets the contract name of the exports to import.
/// </summary>
/// <value>
/// A <see cref="String"/> containing the contract name of the exports to import. The
/// A <see cref="string"/> containing the contract name of the exports to import. The
/// default value is null.
/// </value>
public string ContractName { get; private set; }
public string ContractName { get; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,28 +10,25 @@ namespace System.Composition
[AttributeUsage(AttributeTargets.Property, Inherited = false)]
public sealed class ImportMetadataConstraintAttribute : Attribute
{
private readonly string _name;
private readonly object _value;

/// <summary>
/// Require a specific metadata value on the exporter.
/// </summary>
/// <param name="name">The name of the metadata item to match.</param>
/// <param name="value">The value to match.</param>
public ImportMetadataConstraintAttribute(string name, object value)
{
_name = name;
_value = value;
Name = name;
Value = value;
}

/// <summary>
/// The metadata key to match.
/// </summary>
public string Name { get { return _name; } }
public string Name { get; }

/// <summary>
/// The value to match.
/// </summary>
public object Value { get { return _value; } }
public object Value { get; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class PartMetadataAttribute : Attribute
/// specified name and metadata value.
/// </summary>
/// <param name="name">
/// A <see cref="String"/> containing the name of the metadata value; or
/// A <see cref="string"/> containing the name of the metadata value; or
/// <see langword="null"/> to use an empty string ("").
/// </param>
/// <param name="value">
Expand All @@ -24,26 +24,24 @@ public class PartMetadataAttribute : Attribute
/// </param>
public PartMetadataAttribute(string name, object value)
{
if (name == null) throw new ArgumentNullException(nameof(name));

Name = name;
Name = name ?? throw new ArgumentNullException(nameof(name));
Value = value;
}

/// <summary>
/// Gets the name of the metadata value.
/// </summary>
/// <value>
/// A <see cref="String"/> containing the name of the metadata value.
/// A <see cref="string"/> containing the name of the metadata value.
/// </value>
public string Name { get; private set; }
public string Name { get; }

/// <summary>
/// Gets the metadata value.
/// </summary>
/// <value>
/// An <see cref="object"/> containing the metadata value.
/// </value>
public object Value { get; private set; }
public object Value { get; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ public class SharedAttribute : PartMetadataAttribute
/// <summary>
/// Mark a part as globally shared.
/// </summary>
public SharedAttribute()
: base(SharingBoundaryPartMetadataName, null)
public SharedAttribute() : base(SharingBoundaryPartMetadataName, null)
{
}

Expand All @@ -31,14 +30,13 @@ public SharedAttribute()
/// boundary name.
/// </summary>
/// <param name="sharingBoundaryName">The boundary outside of which this part is inaccessible.</param>
public SharedAttribute(string sharingBoundaryName)
: base(SharingBoundaryPartMetadataName, sharingBoundaryName)
public SharedAttribute(string sharingBoundaryName) : base(SharingBoundaryPartMetadataName, sharingBoundaryName)
{
}

/// <summary>
/// he boundary outside of which this part is inaccessible.
/// </summary>
public string SharingBoundary { get { return (string)base.Value; } }
public string SharingBoundary => (string)base.Value;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,12 @@ public sealed class SharingBoundaryAttribute : Attribute
/// <param name="sharingBoundaryNames">Boundaries implemented by the created ExportLifetimeContext{T}s.</param>
public SharingBoundaryAttribute(params string[] sharingBoundaryNames)
{
if (sharingBoundaryNames == null) throw new ArgumentNullException(nameof(sharingBoundaryNames));

_sharingBoundaryNames = sharingBoundaryNames;
_sharingBoundaryNames = sharingBoundaryNames ?? throw new ArgumentNullException(nameof(sharingBoundaryNames));
}

/// <summary>
/// Boundaries implemented by the created ExportLifetimeContext{T}s.
/// </summary>
public ReadOnlyCollection<string> SharingBoundaryNames { get { return new ReadOnlyCollection<string>(_sharingBoundaryNames); } }
public ReadOnlyCollection<string> SharingBoundaryNames => new ReadOnlyCollection<string>(_sharingBoundaryNames);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<BuildConfigurations>
netstandard;
</BuildConfigurations>
</PropertyGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using Xunit;

namespace System.Composition.Tests
{
public class ExportAttributeTests
{
[Fact]
public void Ctor_Default()
{
var attribute = new ExportAttribute();
Assert.Null(attribute.ContractName);
Assert.Null(attribute.ContractType);
}

[Theory]
[InlineData(null)]
[InlineData("ContractName")]
public void Ctor_ContractName(string contractName)
{
var attribute = new ExportAttribute(contractName);
Assert.Equal(contractName, attribute.ContractName);
Assert.Null(attribute.ContractType);
}

[Theory]
[InlineData(null)]
[InlineData(typeof(string))]
public void Ctor_ContractName(Type contractType)
{
var attribute = new ExportAttribute(contractType);
Assert.Null(attribute.ContractName);
Assert.Equal(contractType, attribute.ContractType);
}

[Theory]
[InlineData(null, null)]
[InlineData("ContractName", typeof(string))]
public void Ctor_ContractName_ContractType(string contractName, Type contractType)
{
var attribute = new ExportAttribute(contractName, contractType);
Assert.Equal(contractName, attribute.ContractName);
Assert.Equal(contractType, attribute.ContractType);
}
}
}
Loading

0 comments on commit 1476c2e

Please sign in to comment.