1
// -----------------------------------------------------------------------
2 // Copyright (c) Microsoft Corporation. All rights reserved.
3 // -----------------------------------------------------------------------
5 using System
.Collections
;
6 using System
.Collections
.Generic
;
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
20 public class GenerationServicesTests
22 // Has to be public, otherwise the dynamic method doesn't see it
29 public static class DelegateTestClass
31 public static int Method(int 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
>()));
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);
120 public void StringType()
122 this.TestSuccessfulValueGeneration("42");
126 public void EnumType()
128 this.TestSuccessfulValueGeneration(TestEnum
.Second
);
133 public void TypeType()
135 this.TestSuccessfulValueGeneration(typeof(TestEnum
));
139 public void PrimitiveTypeEnumerable()
141 int[] enumerable
= new int[] { 1, 2, 3, 4, 5 }
;
142 this.TestSuccessfulEnumerableGeneration
<int>(enumerable
);
146 public void StringTypeEnumerable()
148 string[] enumerable
= new string[] { "1", "2", "3", "4", "5" }
;
149 this.TestSuccessfulEnumerableGeneration
<string>(enumerable
);
155 public void EnumTypeEnumerable()
157 TestEnum
[] enumerable
= new TestEnum
[] { TestEnum.First, TestEnum.Second }
;
158 this.TestSuccessfulEnumerableGeneration
<string>(enumerable
);
163 public void MixedEnumerable()
165 List
<object> list
= new List
<object>();
168 list
.Add(typeof(TestEnum
));
169 list
.Add(TestEnum
.Second
);
172 this.TestSuccessfulEnumerableGeneration
<object>(list
);