1 // -----------------------------------------------------------------------
\r
2 // Copyright (c) Microsoft Corporation. All rights reserved.
\r
3 // -----------------------------------------------------------------------
\r
5 using System.Collections.Generic;
\r
6 using System.ComponentModel.Composition;
\r
7 using Microsoft.VisualStudio.TestTools.UnitTesting;
\r
8 using System.UnitTesting;
\r
9 using System.ComponentModel.Composition.Factories;
\r
10 using System.ComponentModel.Composition.Primitives;
\r
12 namespace System.ComponentModel.Composition
\r
15 public class ExportDefinitionTests
\r
18 public void Constructor1_ShouldSetMetadataPropertyToEmptyDictionary()
\r
20 var definition = new DerivedExportDefinition();
\r
22 EnumerableAssert.IsEmpty(definition.Metadata);
\r
26 public void Constructor1_ShouldSetMetadataPropertyToReadOnlyDictionary()
\r
28 var definition = new DerivedExportDefinition();
\r
30 ExceptionAssert.Throws<NotSupportedException>(() =>
\r
32 definition.Metadata["Value"] = "Value";
\r
37 public void Constructor2_NullAsContractNameArgument_ShouldThrowArgumentNull()
\r
39 ExceptionAssert.ThrowsArgument<ArgumentNullException>("contractName", () =>
\r
41 new ExportDefinition((string)null, new Dictionary<string, object>());
\r
46 public void Constructor2_EmptyStringAsContractNameArgument_ShouldThrowArgument()
\r
48 ExceptionAssert.ThrowsArgument<ArgumentException>("contractName", () =>
\r
50 new ExportDefinition(string.Empty, new Dictionary<string, object>());
\r
55 public void Constructor2_ValueAsContractNameArgument_ShouldSetContractNameProperty()
\r
57 var expectations = Expectations.GetContractNames();
\r
59 foreach (var e in expectations)
\r
61 var definition = new ExportDefinition(e, new Dictionary<string, object>());
\r
63 Assert.AreEqual(e, definition.ContractName);
\r
68 public void Constructor2_NullAsMetadataArgument_ShouldSetMetadataPropertyToEmptyDictionary()
\r
70 var definition = new ExportDefinition("Contract", (IDictionary<string, object>)null); ;
\r
72 EnumerableAssert.IsEmpty(definition.Metadata);
\r
76 public void Constructor2_NullAsMetadataArgument_ShouldSetMetadataPropertyToReadOnlyDictionary()
\r
78 var definition = new ExportDefinition("Contract", (IDictionary<string, object>)null);
\r
80 ExceptionAssert.Throws<NotSupportedException>(() =>
\r
82 definition.Metadata["Value"] = "Value";
\r
87 public void Constructor2_WritableDictionaryAsMetadataArgument_ShouldSetMetadataPropertyToReadOnlyDictionary()
\r
89 var definition = new ExportDefinition("Contract", new Dictionary<string, object>());
\r
91 ExceptionAssert.Throws<NotSupportedException>(() =>
\r
93 definition.Metadata["Value"] = "Value";
\r
98 public void Constructor2_DictionaryAsMetadataArgument_ShouldSetMetadataProperty()
\r
100 var expectations = Expectations.GetMetadata();
\r
102 foreach (var e in expectations)
\r
104 var definition = new ExportDefinition("Contract", e);
\r
106 EnumerableAssert.AreEqual(e, definition.Metadata);
\r
111 public void ContractName_WhenNotOverridden_ShouldThrowNotImplemented()
\r
113 var definition = new DerivedExportDefinition();
\r
115 ExceptionAssert.Throws<NotImplementedException>(() =>
\r
117 var contractName = definition.ContractName;
\r
122 public void ToString_WhenContractNameNotOverridden_ShouldThrowNotImplemented()
\r
124 var definition = new DerivedExportDefinition();
\r
126 ExceptionAssert.Throws<NotImplementedException>(() =>
\r
128 definition.ToString();
\r
133 public void ToString_ShouldReturnContractNameProperty()
\r
135 var expectations = Expectations.GetContractNames();
\r
137 foreach (var e in expectations)
\r
139 var definition = new ExportDefinition(e, new Dictionary<string, object>());
\r
141 Assert.AreEqual(e, definition.ToString());
\r
146 public void ToString_ShouldReturnOverriddenContractNameProperty()
\r
148 var expectations = Expectations.GetContractNamesWithEmpty();
\r
150 foreach (var e in expectations)
\r
152 var definition = new DerivedExportDefinition(() => e);
\r
154 Assert.AreEqual(e, definition.ToString());
\r
158 private class DerivedExportDefinition : ExportDefinition
\r
160 private readonly Func<string> _contractNameGetter;
\r
162 public DerivedExportDefinition()
\r
166 public DerivedExportDefinition(Func<string> contractNameGetter)
\r
168 _contractNameGetter = contractNameGetter;
\r
171 public override string ContractName
\r
175 if (_contractNameGetter != null)
\r
177 return _contractNameGetter();
\r
180 return base.ContractName;
\r