update MEF to preview 9
[mcs.git] / class / System.ComponentModel.Composition / Tests / ComponentModelUnitTest / System / ComponentModel / Composition / GenerationServicesTests.cs
blobb1bfe9251e3da8e4ba171d6cd4b3ec0ef39a9851
1 // -----------------------------------------------------------------------
2 // Copyright (c) Microsoft Corporation. All rights reserved.
3 // -----------------------------------------------------------------------
4 using System;
5 using System.Collections;
6 using System.Collections.Generic;
7 using System.Linq;
8 using System.Text;
9 using System.Reflection.Emit;
10 using System.Reflection;
11 using System.ComponentModel.Composition;
12 using System.UnitTesting;
13 using Microsoft.Internal;
14 using Microsoft.VisualStudio.TestTools.UnitTesting;
15 using System.ComponentModel.Composition.UnitTesting;
17 namespace Microsoft.Internal
19 [TestClass]
20 public class GenerationServicesTests
22 // Has to be public, otherwise the dynamic method doesn't see it
23 public enum TestEnum
25 First = 1,
26 Second = 2
29 public static class DelegateTestClass
31 public static int Method(int i)
33 return i;
37 private Func<T> CreateValueGenerator<T>(T value)
39 DynamicMethod methodBuilder = new DynamicMethod(TestServices.GenerateRandomString(), typeof(T), Type.EmptyTypes);
40 // Generate the method body that simply returns the dictionary
41 ILGenerator ilGenerator = methodBuilder.GetILGenerator();
42 GenerationServices.LoadValue(ilGenerator, value);
43 ilGenerator.Emit(OpCodes.Ret);
44 return (Func<T>)methodBuilder.CreateDelegate(typeof(Func<T>));
47 private void TestSuccessfulValueGeneration<T>(T value)
49 Func<T> result = this.CreateValueGenerator<T>(value);
50 T generatedValue = result.Invoke();
51 Assert.AreEqual(value, generatedValue);
54 private void TestSuccessfulDictionaryGeneration(IDictionary<string, object> dictionary)
56 Func<IDictionary<string, object>> result = this.CreateValueGenerator<IDictionary<string, object>>(dictionary);
57 IDictionary<string, object> generatedDictionary = result.Invoke();
58 EnumerableAssert.AreEqual(dictionary, generatedDictionary);
61 private void TestSuccessfulEnumerableGeneration<T>(IEnumerable enumerable)
63 Func<IEnumerable> result = this.CreateValueGenerator<IEnumerable>(enumerable);
64 IEnumerable generatedEnumerable = result.Invoke();
65 Assert.IsTrue(generatedEnumerable.Cast<T>().SequenceEqual(enumerable.Cast<T>()));
68 [TestMethod]
69 public void PrimitiveTypes()
71 this.TestSuccessfulValueGeneration(Char.MinValue);
72 this.TestSuccessfulValueGeneration(Char.MaxValue);
73 this.TestSuccessfulValueGeneration((Char)42);
75 this.TestSuccessfulValueGeneration(true);
76 this.TestSuccessfulValueGeneration(false);
78 this.TestSuccessfulValueGeneration(Byte.MinValue);
79 this.TestSuccessfulValueGeneration(Byte.MaxValue);
80 this.TestSuccessfulValueGeneration((Byte)42);
82 this.TestSuccessfulValueGeneration(SByte.MinValue);
83 this.TestSuccessfulValueGeneration(SByte.MaxValue);
84 this.TestSuccessfulValueGeneration((SByte)42);
86 this.TestSuccessfulValueGeneration(Int16.MinValue);
87 this.TestSuccessfulValueGeneration(Int16.MaxValue);
88 this.TestSuccessfulValueGeneration((Int16)42);
90 this.TestSuccessfulValueGeneration(UInt16.MinValue);
91 this.TestSuccessfulValueGeneration(UInt16.MaxValue);
92 this.TestSuccessfulValueGeneration((UInt16)42);
94 this.TestSuccessfulValueGeneration(Int32.MinValue);
95 this.TestSuccessfulValueGeneration(Int32.MaxValue);
96 this.TestSuccessfulValueGeneration((Int32)42);
98 this.TestSuccessfulValueGeneration(UInt32.MinValue);
99 this.TestSuccessfulValueGeneration(UInt32.MaxValue);
100 this.TestSuccessfulValueGeneration((UInt32)42);
102 this.TestSuccessfulValueGeneration(Int64.MinValue);
103 this.TestSuccessfulValueGeneration(Int64.MaxValue);
104 this.TestSuccessfulValueGeneration((Int64)42);
106 this.TestSuccessfulValueGeneration(UInt64.MinValue);
107 this.TestSuccessfulValueGeneration(UInt64.MaxValue);
108 this.TestSuccessfulValueGeneration((UInt64)42);
110 this.TestSuccessfulValueGeneration(Single.MinValue);
111 this.TestSuccessfulValueGeneration(Single.MaxValue);
112 this.TestSuccessfulValueGeneration((Single)42.42);
114 this.TestSuccessfulValueGeneration(Double.MinValue);
115 this.TestSuccessfulValueGeneration(Double.MaxValue);
116 this.TestSuccessfulValueGeneration((Double)42.42);
119 [TestMethod]
120 public void StringType()
122 this.TestSuccessfulValueGeneration("42");
125 [TestMethod]
126 public void EnumType()
128 this.TestSuccessfulValueGeneration(TestEnum.Second);
132 [TestMethod]
133 public void TypeType()
135 this.TestSuccessfulValueGeneration(typeof(TestEnum));
138 [TestMethod]
139 public void PrimitiveTypeEnumerable()
141 int[] enumerable = new int[] { 1, 2, 3, 4, 5 };
142 this.TestSuccessfulEnumerableGeneration<int>(enumerable);
145 [TestMethod]
146 public void StringTypeEnumerable()
148 string[] enumerable = new string[] { "1", "2", "3", "4", "5" };
149 this.TestSuccessfulEnumerableGeneration<string>(enumerable);
152 [TestMethod]
153 [Ignore]
154 [WorkItem(507696)]
155 public void EnumTypeEnumerable()
157 TestEnum[] enumerable = new TestEnum[] { TestEnum.First, TestEnum.Second };
158 this.TestSuccessfulEnumerableGeneration<string>(enumerable);
162 [TestMethod]
163 public void MixedEnumerable()
165 List<object> list = new List<object>();
166 list.Add(42);
167 list.Add("42");
168 list.Add(typeof(TestEnum));
169 list.Add(TestEnum.Second);
170 list.Add(null);
172 this.TestSuccessfulEnumerableGeneration<object>(list);