update MEF to preview 9
[mcs.git] / class / System.ComponentModel.Composition / Tests / ComponentModelUnitTest / System / ComponentModel / Composition / CompositionServicesTests.cs
blobb8ee4c4bd6d33187c6a73c31c4494db3d5523a73
1 // -----------------------------------------------------------------------
2 // Copyright (c) Microsoft Corporation. All rights reserved.
3 // -----------------------------------------------------------------------
4 using System;
5 using System.Collections.Generic;
6 using System.ComponentModel.Composition;
7 using System.ComponentModel.Composition.Hosting;
8 using System.Text;
9 using Microsoft.Internal;
10 using Microsoft.VisualStudio.TestTools.UnitTesting;
11 using System.UnitTesting;
12 using Tests.Integration;
13 using System.Reflection;
14 using System.Reflection.Emit;
16 namespace System.ComponentModel.Composition
18 [TestClass]
19 public class CompositionServicesTests
21 private static readonly Type NonGenericType = typeof(int);
23 [TestMethod]
24 public void GetContractName_NullAsTypeArgument_ThrowsArgumentNull()
26 ExceptionAssert.ThrowsArgument<ArgumentNullException>("type", () =>
28 AttributedModelServices.GetContractName((Type)null);
29 });
32 [TestMethod]
33 [Description("Verifies adding custom modifiers to contract name.")]
34 public void ContractNameServicesAddCustomModifiersTest()
36 Type[] modifiers = new Type[] { typeof(int), typeof(List<int>), typeof(double) };
37 StringBuilder typeName = new StringBuilder();
38 ContractNameServices.WriteCustomModifiers(typeName, "test", modifiers);
39 Assert.AreEqual<string>(
40 string.Format(" {0}(System.Int32,System.Collections.Generic.List(System.Int32),System.Double)", "test"),
41 typeName.ToString(),
42 "Expecting correct modifiers text.");
45 [TestMethod]
46 [Description("Verifies CompositionServices.GetDefaultContractName method.")]
47 public void GetDefaultContractNameTest()
49 ExpectationCollection<Type, string> expectations = new ExpectationCollection<Type, string>();
51 expectations.Add(typeof(string), "System.String");
52 expectations.Add(typeof(int?), "System.Nullable(System.Int32)");
53 expectations.Add(typeof(NestedParent.NestedChild), "System.ComponentModel.Composition.NestedParent+NestedChild");
54 expectations.Add(typeof(int[,]), "System.Int32[,]");
55 expectations.Add(typeof(int[,][][]), "System.Int32[,][][]");
56 expectations.Add(typeof(Array[,][][]).MakePointerType(), "System.Array[,][][]*");
57 expectations.Add(typeof(int).MakePointerType(), "System.Int32*");
58 expectations.Add(typeof(int).MakePointerType().MakeArrayType(3).MakeArrayType().MakePointerType(), "System.Int32*[][,,]*");
59 expectations.Add(typeof(int).MakeByRefType(), "System.Int32&");
60 expectations.Add(typeof(int).MakePointerType().MakeArrayType(4).MakeArrayType().MakeByRefType(), "System.Int32*[][,,,]&");
61 expectations.Add(typeof(List<>), "System.Collections.Generic.List()");
62 expectations.Add(typeof(Dictionary<int,double>), "System.Collections.Generic.Dictionary(System.Int32,System.Double)");
63 expectations.Add(typeof(Dictionary<Dictionary<int,double>,double>), "System.Collections.Generic.Dictionary(System.Collections.Generic.Dictionary(System.Int32,System.Double),System.Double)");
64 expectations.Add(typeof(GenericContract1<int>.GenericContract2.GenericContract3<double>), "System.ComponentModel.Composition.GenericContract1(System.Int32)+GenericContract2+GenericContract3(System.Double)");
65 expectations.Add(typeof(GenericContract4<int,double>.GenericContract5<double,int>.GenericContract6<int,double>), "System.ComponentModel.Composition.GenericContract4(System.Int32,System.Double)+GenericContract5(System.Double,System.Int32)+GenericContract6(System.Int32,System.Double)");
66 expectations.Add(typeof(GenericContract4<,>.GenericContract5<,>.GenericContract6<,>), "System.ComponentModel.Composition.GenericContract4(,)+GenericContract5(,)+GenericContract6(,)");
67 expectations.Add(typeof(GenericContract7), "System.ComponentModel.Composition.GenericContract7");
68 expectations.Add(typeof(GenericContract8<int>), "System.ComponentModel.Composition.GenericContract8(System.Int32)");
69 expectations.Add(typeof(GenericContract4<int,double>.GenericContract5<double,int>[][,,,,]), "System.ComponentModel.Composition.GenericContract4(System.Int32,System.Double)+GenericContract5(System.Double,System.Int32)[][,,,,]");
70 expectations.Add(typeof(GenericContract4<int,double>.GenericContract5<double,int>).MakePointerType(), "System.ComponentModel.Composition.GenericContract4(System.Int32,System.Double)+GenericContract5(System.Double,System.Int32)*");
71 expectations.Add(typeof(GenericContract4<int,double>.GenericContract5<double,int>[][,]).MakePointerType().MakeArrayType(4), "System.ComponentModel.Composition.GenericContract4(System.Int32,System.Double)+GenericContract5(System.Double,System.Int32)[][,]*[,,,]"); expectations.Add(typeof(OuterClassWithGenericNested.GenericNested<int>), "System.ComponentModel.Composition.OuterClassWithGenericNested+GenericNested(System.Int32)");
72 expectations.Add(typeof(Delegate), "System.Delegate");
73 expectations.Add(typeof(MulticastDelegate), "System.MulticastDelegate");
74 expectations.Add(typeof(Action), "System.Void()");
75 expectations.Add(typeof(Func<Func<string>>), "System.Func(System.String)()");
76 expectations.Add(typeof(DelegateCompositionTests.DoWorkDelegate), "System.Object(System.Int32,System.Object&,System.String&)");
77 expectations.Add(typeof(DelegateCompositionTests.DelegateOneArg), "System.Int32(System.Int32)");
78 expectations.Add(typeof(DelegateCompositionTests.DelegateTwoArgs), "System.Int32(System.Int32,System.Int32)");
79 expectations.Add(typeof(DelegateCompositionTests.SimpleDelegate), "System.Int32()");
80 expectations.Add(typeof(DelegateCompositionTests.ActionWith9Args), "System.Void(System.Int32,System.Int32,System.Int32,System.Int32,System.Int32,System.Int32,System.Int32,System.Int32,System.Int32)");
81 expectations.Add(typeof(DelegateCompositionTests.FuncWith9Args), "System.Int32(System.Int32,System.Int32,System.Int32,System.Int32,System.Int32,System.Int32,System.Int32,System.Int32,System.Int32)");
82 expectations.Add(EmitGenericType("MyGeneratedType", typeof(int)), "MyGeneratedType(System.Int32)");
83 expectations.Add(EmitGenericType("MyGeneratedType`1", typeof(string)), "MyGeneratedType(System.String)");
84 expectations.Add(EmitGenericType("_name_", typeof(List<>)), "_name_(System.Collections.Generic.List())");
86 // This particular case can clash with the name generation for methods because we use () for both generics and methods
87 // we may want to fix this clash by changing the generic markers but it is likely to be very rare.
88 expectations.Add(EmitGenericType("System.Void", typeof(int)), "System.Void(System.Int32)");
90 foreach (var e in expectations)
92 string typeIdentity = AttributedModelServices.GetTypeIdentity(e.Input);
93 Assert.AreEqual(e.Output, typeIdentity);
96 // Do it again to excerise the cache.
97 foreach (var e in expectations)
99 string typeIdentity = AttributedModelServices.GetTypeIdentity(e.Input);
100 Assert.AreEqual(e.Output, typeIdentity);
104 public Type EmitGenericType(string typeName, Type genericArgumentType)
106 var assemblyName = new AssemblyName("EmittedTestAssembly");
107 var assemblyBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.Run);
108 var moduleBuilder = assemblyBuilder.DefineDynamicModule("EmittedTestModule");
109 var typeBuilder = moduleBuilder.DefineType(typeName, TypeAttributes.Public);
111 GenericTypeParameterBuilder T = typeBuilder.DefineGenericParameters(new string[] { "T" })[0];
112 typeBuilder.SetCustomAttribute(new CustomAttributeBuilder(typeof(ExportAttribute).GetConstructor(Type.EmptyTypes), new object[0]));
114 return typeBuilder.CreateType().MakeGenericType(genericArgumentType);