PageRenderTime 43ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/mcs/class/System.ComponentModel.Composition/Tests/ComponentModelUnitTest/System/ComponentModel/Composition/GenerationServicesTests.cs

https://bitbucket.org/danipen/mono
C# | 175 lines | 142 code | 28 blank | 5 comment | 0 complexity | 8c1b40082d685f29b9896bf25df20356 MD5 | raw file
Possible License(s): Unlicense, Apache-2.0, LGPL-2.0, MPL-2.0-no-copyleft-exception, CC-BY-SA-3.0, GPL-2.0
  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;
  16. namespace Microsoft.Internal
  17. {
  18. [TestClass]
  19. public class GenerationServicesTests
  20. {
  21. // Has to be public, otherwise the dynamic method doesn't see it
  22. public enum TestEnum
  23. {
  24. First = 1,
  25. Second = 2
  26. }
  27. public static class DelegateTestClass
  28. {
  29. public static int Method(int i)
  30. {
  31. return i;
  32. }
  33. }
  34. private Func<T> CreateValueGenerator<T>(T value)
  35. {
  36. DynamicMethod methodBuilder = new DynamicMethod(TestServices.GenerateRandomString(), typeof(T), Type.EmptyTypes);
  37. // Generate the method body that simply returns the dictionary
  38. ILGenerator ilGenerator = methodBuilder.GetILGenerator();
  39. GenerationServices.LoadValue(ilGenerator, value);
  40. ilGenerator.Emit(OpCodes.Ret);
  41. return (Func<T>)methodBuilder.CreateDelegate(typeof(Func<T>));
  42. }
  43. private void TestSuccessfulValueGeneration<T>(T value)
  44. {
  45. Func<T> result = this.CreateValueGenerator<T>(value);
  46. T generatedValue = result.Invoke();
  47. Assert.AreEqual(value, generatedValue);
  48. }
  49. private void TestSuccessfulDictionaryGeneration(IDictionary<string, object> dictionary)
  50. {
  51. Func<IDictionary<string, object>> result = this.CreateValueGenerator<IDictionary<string, object>>(dictionary);
  52. IDictionary<string, object> generatedDictionary = result.Invoke();
  53. EnumerableAssert.AreEqual(dictionary, generatedDictionary);
  54. }
  55. private void TestSuccessfulEnumerableGeneration<T>(IEnumerable enumerable)
  56. {
  57. Func<IEnumerable> result = this.CreateValueGenerator<IEnumerable>(enumerable);
  58. IEnumerable generatedEnumerable = result.Invoke();
  59. Assert.IsTrue(generatedEnumerable.Cast<T>().SequenceEqual(enumerable.Cast<T>()));
  60. }
  61. [TestMethod]
  62. public void PrimitiveTypes()
  63. {
  64. this.TestSuccessfulValueGeneration(Char.MinValue);
  65. this.TestSuccessfulValueGeneration(Char.MaxValue);
  66. this.TestSuccessfulValueGeneration((Char)42);
  67. this.TestSuccessfulValueGeneration(true);
  68. this.TestSuccessfulValueGeneration(false);
  69. this.TestSuccessfulValueGeneration(Byte.MinValue);
  70. this.TestSuccessfulValueGeneration(Byte.MaxValue);
  71. this.TestSuccessfulValueGeneration((Byte)42);
  72. this.TestSuccessfulValueGeneration(SByte.MinValue);
  73. this.TestSuccessfulValueGeneration(SByte.MaxValue);
  74. this.TestSuccessfulValueGeneration((SByte)42);
  75. this.TestSuccessfulValueGeneration(Int16.MinValue);
  76. this.TestSuccessfulValueGeneration(Int16.MaxValue);
  77. this.TestSuccessfulValueGeneration((Int16)42);
  78. this.TestSuccessfulValueGeneration(UInt16.MinValue);
  79. this.TestSuccessfulValueGeneration(UInt16.MaxValue);
  80. this.TestSuccessfulValueGeneration((UInt16)42);
  81. this.TestSuccessfulValueGeneration(Int32.MinValue);
  82. this.TestSuccessfulValueGeneration(Int32.MaxValue);
  83. this.TestSuccessfulValueGeneration((Int32)42);
  84. this.TestSuccessfulValueGeneration(UInt32.MinValue);
  85. this.TestSuccessfulValueGeneration(UInt32.MaxValue);
  86. this.TestSuccessfulValueGeneration((UInt32)42);
  87. this.TestSuccessfulValueGeneration(Int64.MinValue);
  88. this.TestSuccessfulValueGeneration(Int64.MaxValue);
  89. this.TestSuccessfulValueGeneration((Int64)42);
  90. this.TestSuccessfulValueGeneration(UInt64.MinValue);
  91. this.TestSuccessfulValueGeneration(UInt64.MaxValue);
  92. this.TestSuccessfulValueGeneration((UInt64)42);
  93. this.TestSuccessfulValueGeneration(Single.MinValue);
  94. this.TestSuccessfulValueGeneration(Single.MaxValue);
  95. this.TestSuccessfulValueGeneration((Single)42.42);
  96. this.TestSuccessfulValueGeneration(Double.MinValue);
  97. this.TestSuccessfulValueGeneration(Double.MaxValue);
  98. this.TestSuccessfulValueGeneration((Double)42.42);
  99. }
  100. [TestMethod]
  101. public void StringType()
  102. {
  103. this.TestSuccessfulValueGeneration("42");
  104. }
  105. [TestMethod]
  106. public void EnumType()
  107. {
  108. this.TestSuccessfulValueGeneration(TestEnum.Second);
  109. }
  110. [TestMethod]
  111. public void TypeType()
  112. {
  113. this.TestSuccessfulValueGeneration(typeof(TestEnum));
  114. }
  115. [TestMethod]
  116. public void PrimitiveTypeEnumerable()
  117. {
  118. int[] enumerable = new int[] { 1, 2, 3, 4, 5 };
  119. this.TestSuccessfulEnumerableGeneration<int>(enumerable);
  120. }
  121. [TestMethod]
  122. public void StringTypeEnumerable()
  123. {
  124. string[] enumerable = new string[] { "1", "2", "3", "4", "5" };
  125. this.TestSuccessfulEnumerableGeneration<string>(enumerable);
  126. }
  127. [TestMethod]
  128. [Ignore]
  129. [WorkItem(507696)]
  130. public void EnumTypeEnumerable()
  131. {
  132. TestEnum[] enumerable = new TestEnum[] { TestEnum.First, TestEnum.Second };
  133. this.TestSuccessfulEnumerableGeneration<string>(enumerable);
  134. }
  135. [TestMethod]
  136. public void MixedEnumerable()
  137. {
  138. List<object> list = new List<object>();
  139. list.Add(42);
  140. list.Add("42");
  141. list.Add(typeof(TestEnum));
  142. list.Add(TestEnum.Second);
  143. list.Add(null);
  144. this.TestSuccessfulEnumerableGeneration<object>(list);
  145. }
  146. }
  147. }