1 // -----------------------------------------------------------------------
\r
2 // Copyright (c) Microsoft Corporation. All rights reserved.
\r
3 // -----------------------------------------------------------------------
\r
5 using System.ComponentModel.Composition.Diagnostics;
\r
6 using System.ComponentModel.Composition.Factories;
\r
7 using System.Diagnostics;
\r
9 using System.UnitTesting;
\r
11 using Microsoft.VisualStudio.TestTools.UnitTesting;
\r
12 using System.Collections.Generic;
\r
13 using System.ComponentModel.Composition.Extensibility;
\r
15 namespace System.ComponentModel.Composition.AttributedModel
\r
18 public class AttributedModelServicesTests
\r
21 public void CreatePartDefinition1_NullAsType_ShouldThrowArgumentNull()
\r
23 var origin = ElementFactory.Create();
\r
25 ExceptionAssert.ThrowsArgumentNull("type", () =>
\r
27 AttributedModelServices.CreatePartDefinition((Type)null, origin);
\r
32 public void CreatePartDefinition2_NullAsType_ShouldThrowArgumentNull()
\r
34 var origin = ElementFactory.Create();
\r
36 ExceptionAssert.ThrowsArgumentNull("type", () =>
\r
38 AttributedModelServices.CreatePartDefinition((Type)null, origin, false);
\r
45 public void CreatePartDefinition2_TypeMarkedWithPartNotDiscoverableAttribute_ShouldTraceInformation()
\r
47 var types = GetTypesMarkedWithPartNotDiscoverableAttribute();
\r
49 foreach (Type type in types)
\r
51 using (TraceContext context = new TraceContext(SourceLevels.Information))
\r
53 AttributedModelServices.CreatePartDefinition(type, null, true);
\r
55 Assert.IsNotNull(context.LastTraceEvent);
\r
56 Assert.AreEqual(context.LastTraceEvent.EventType, TraceEventType.Information);
\r
57 Assert.AreEqual(context.LastTraceEvent.Id, TraceId.Discovery_DefinitionMarkedWithPartNotDiscoverableAttribute);
\r
63 public void CreatePartDefinition2_OpenGenericType_ShouldTraceInformation()
\r
65 var types = GetOpenGenericTypes();
\r
67 foreach (Type type in types)
\r
69 using (TraceContext context = new TraceContext(SourceLevels.Information))
\r
71 AttributedModelServices.CreatePartDefinition(type, null, true);
\r
73 Assert.IsNotNull(context.LastTraceEvent);
\r
74 Assert.AreEqual(context.LastTraceEvent.EventType, TraceEventType.Information);
\r
75 Assert.AreEqual(context.LastTraceEvent.Id, TraceId.Discovery_DefinitionContainsGenericParameters);
\r
81 public void CreatePartDefinition2_TypeWithNoExports_ShouldTraceInformation()
\r
83 var types = GetTypesWithNoExports();
\r
85 foreach (Type type in types)
\r
87 using (TraceContext context = new TraceContext(SourceLevels.Information))
\r
89 var result = AttributedModelServices.CreatePartDefinition(type, null, true);
\r
91 Assert.IsNotNull(context.LastTraceEvent);
\r
92 Assert.AreEqual(context.LastTraceEvent.EventType, TraceEventType.Information);
\r
93 Assert.AreEqual(context.LastTraceEvent.Id, TraceId.Discovery_DefinitionContainsNoExports);
\r
98 private static IEnumerable<Type> GetTypesMarkedWithPartNotDiscoverableAttribute()
\r
100 yield return typeof(ClassMarkedWithPartNotDiscoverableAttribute);
\r
101 yield return typeof(ClassMarkedWithPartNotDiscoverableAttribute<>);
\r
104 private static IEnumerable<Type> GetOpenGenericTypes()
\r
106 yield return typeof(OpenGenericType<>);
\r
109 private static IEnumerable<Type> GetTypesWithNoExports()
\r
111 yield return typeof(ClassWithNoExports);
\r
112 yield return typeof(ClassWithOnlyFieldImport);
\r
113 yield return typeof(ClassWithOnlyPropertyImport);
\r
114 yield return typeof(ClassWithOnlyParameterImport);
\r
117 public class ClassWithNoExports
\r
121 public class ClassWithOnlyFieldImport
\r
124 public string Field;
\r
127 public class ClassWithOnlyPropertyImport
\r
130 public string Property
\r
137 public class ClassWithOnlyParameterImport
\r
139 [ImportingConstructor]
\r
140 public ClassWithOnlyParameterImport(string parameter)
\r
145 public class OpenGenericType<T>
\r
149 [PartNotDiscoverable]
\r
150 public class ClassMarkedWithPartNotDiscoverableAttribute
\r
154 [PartNotDiscoverable]
\r
155 public class ClassMarkedWithPartNotDiscoverableAttribute<T>
\r