PageRenderTime 52ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/UnitTests/Reflection/ReflectionHelper_UnitTests.cs

http://github.com/fredericaltorres/DynamicSugarNet
C# | 316 lines | 252 code | 64 blank | 0 comment | 0 complexity | 150e4273ef412e102a6b18a239619d1d MD5 | raw file
  1. using System;
  2. using System.Text;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using Microsoft.VisualStudio.TestTools.UnitTesting;
  6. using DynamicSugar;
  7. using System.Dynamic;
  8. namespace DynamicSugarSharp_UnitTests {
  9. [TestClass]
  10. public class ReflectionHelper_UnitTests {
  11. public class FredPropertyClass {
  12. public string FieldString { get; set; }
  13. public DateTime FieldDate { get; set; }
  14. public double FieldDouble { get; set; }
  15. public bool FieldBool { get; set; }
  16. public FredPropertyClass() {
  17. FieldString = "Hello World";
  18. FieldDate = DateTime.Parse("12/13/2013 11:15:35 PM");
  19. FieldDouble = 12.34;
  20. FieldBool = true;
  21. }
  22. public int this[int index] {
  23. get {
  24. return index*2;
  25. }
  26. }
  27. }
  28. public class IndexerTestClass {
  29. private int _index;
  30. public int this[int index] {
  31. get{
  32. return this._index;
  33. }
  34. set {
  35. this._index = index*value;
  36. }
  37. }
  38. }
  39. class ConsTest1 {
  40. public string Name = "Toto";
  41. }
  42. class ConsTest2 {
  43. public string Name = "Toto";
  44. public ConsTest2() {
  45. }
  46. public ConsTest2(string name) {
  47. this.Name = name;
  48. }
  49. }
  50. [TestMethod]
  51. public void Indexer_Set() {
  52. var f = new IndexerTestClass();
  53. ReflectionHelper.SetIndexer(f, 2, 123);
  54. Assert.AreEqual(2*123, ReflectionHelper.GetIndexer(f, 1));
  55. }
  56. [TestMethod]
  57. public void Indexer_Get() {
  58. var f = new FredPropertyClass();
  59. Assert.AreEqual(2, ReflectionHelper.GetIndexer(f, 1));
  60. Assert.AreEqual(16, ReflectionHelper.GetIndexer(f, 8));
  61. }
  62. [TestMethod, ExpectedException(typeof(ArgumentException))]
  63. public void Indexer_Get_NoIndexerDefined() {
  64. var f = new ConsTest2();
  65. ReflectionHelper.GetIndexer(f, 1);
  66. }
  67. [TestMethod, ExpectedException(typeof(ArgumentException))]
  68. public void Indexer_Get_FromNull() {
  69. ReflectionHelper.GetIndexer(null, 1);
  70. }
  71. [TestMethod]
  72. public void Constructor_MultipleConstructor_GenericMethod() {
  73. var consTestInstance = ReflectionHelper.Constructor<ConsTest2>(typeof(ConsTest2));
  74. Assert.AreEqual("Toto", consTestInstance.Name);
  75. var consTestInstance2 = ReflectionHelper.Constructor<ConsTest2>(typeof(ConsTest2), "Tata");
  76. Assert.AreEqual("Tata", consTestInstance2.Name);
  77. }
  78. [TestMethod]
  79. public void Constructor_MultipleConstructor() {
  80. var consTestInstance = ReflectionHelper.Constructor(typeof(ConsTest2)) as ConsTest2;
  81. Assert.AreEqual("Toto", consTestInstance.Name);
  82. var consTestInstance2 = ReflectionHelper.Constructor(typeof(ConsTest2), "Tata") as ConsTest2;
  83. Assert.AreEqual("Tata", consTestInstance2.Name);
  84. }
  85. [TestMethod, ExpectedException(typeof(MissingMethodException))]
  86. public void Constructor_ConstructorWithInvalidParameters() {
  87. var consTestInstance = ReflectionHelper.Constructor(typeof(ConsTest2), "Tata", true);
  88. }
  89. [TestMethod]
  90. public void Constructor_DefaultConstructor() {
  91. var consTestInstance = ReflectionHelper.Constructor(typeof(ConsTest1)) as ConsTest1;
  92. Assert.AreEqual("Toto", consTestInstance.Name);
  93. }
  94. private Dictionary<string, ParameterMetadata> MyMethod_ParameterMetadata(int i, double d, string s){
  95. var dic = ReflectionHelper.GetLocalsEx(i, d, s);
  96. Assert.AreEqual(1 , dic["i"].Value);
  97. Assert.AreEqual(2.0, dic["d"].Value);
  98. Assert.AreEqual("A", dic["s"].Value);
  99. return dic;
  100. }
  101. [TestMethod]
  102. public void GetLocalsEx() {
  103. var d = MyMethod_ParameterMetadata(1, 2.0, "A");
  104. }
  105. private Dictionary<string, object> MyMethod(int i, double d, string s){
  106. var dic = ReflectionHelper.GetLocals(i, d, s);
  107. return dic;
  108. }
  109. [TestMethod]
  110. public void GetLocals() {
  111. var dic = MyMethod(1, 2.0, "A");
  112. Assert.AreEqual(@"{ i:1, d:2, s:""A"" }", dic.Format());
  113. Assert.AreEqual(1 , dic["i"]);
  114. Assert.AreEqual(2.0, dic["d"]);
  115. Assert.AreEqual("A", dic["s"]);
  116. }
  117. [TestMethod]
  118. public void CloneDictionary() {
  119. var d = new Dictionary<string, object>() {
  120. { "A", 1 },
  121. { "B", 2.0 },
  122. { "C", "3" }
  123. };
  124. var d2 = ReflectionHelper.CloneDictionary<string, object>(d);
  125. Assert.AreEqual(1 , d2["A"]);
  126. Assert.AreEqual(2.0, d2["B"]);
  127. Assert.AreEqual("3", d2["C"]);
  128. }
  129. [TestMethod]
  130. public void DictionaryViaReflection() {
  131. var d = new Dictionary<string, int>() {
  132. { "A", 1 },
  133. { "B", 2 },
  134. { "C", 3 }
  135. };
  136. }
  137. [TestMethod]
  138. public void GetListType() {
  139. var li = new List<int>();
  140. Assert.AreEqual(typeof(int),ReflectionHelper.GetListType(li.GetType()));
  141. var ls = new List<string>();
  142. Assert.AreEqual(typeof(string),ReflectionHelper.GetListType(ls.GetType()));
  143. var aString = "";
  144. Assert.AreNotEqual(typeof(string),ReflectionHelper.GetListType(aString.GetType()));
  145. }
  146. [TestMethod]
  147. public void GetDictionaryType_StringInt() {
  148. var dsi = new Dictionary<string, int>();
  149. Type keyType, valueType;
  150. ReflectionHelper.GetDictionaryType(dsi.GetType(), out keyType, out valueType);
  151. Assert.AreEqual(typeof(string), keyType);
  152. Assert.AreEqual(typeof(int), valueType);
  153. }
  154. [TestMethod]
  155. public void GetDictionaryType_DoubleDateTime() {
  156. var dsi = new Dictionary<double, DateTime>();
  157. Type keyType, valueType;
  158. ReflectionHelper.GetDictionaryType(dsi.GetType(), out keyType, out valueType);
  159. Assert.AreEqual(typeof(double), keyType);
  160. Assert.AreEqual(typeof(DateTime), valueType);
  161. }
  162. [TestMethod]
  163. public void IsTypeListOfT() {
  164. var o = 1;
  165. Assert.IsFalse(ReflectionHelper.IsTypeListOfT(o.GetType()));
  166. var li = new List<int>();
  167. Assert.IsTrue(ReflectionHelper.IsTypeListOfT(li.GetType()));
  168. var ld = new List<double>();
  169. Assert.IsTrue(ReflectionHelper.IsTypeListOfT(ld.GetType()));
  170. }
  171. [TestMethod]
  172. public void IsDictionaryOfKV() {
  173. var o = 1;
  174. Assert.IsFalse(ReflectionHelper.IsDictionaryOfKV(o.GetType()));
  175. var li = new List<int>();
  176. Assert.IsFalse(ReflectionHelper.IsDictionaryOfKV(li.GetType()));
  177. var dsi = new Dictionary<string, int>();
  178. Assert.IsTrue(ReflectionHelper.IsDictionaryOfKV(dsi.GetType()));
  179. }
  180. [TestMethod]
  181. public void GetDictionary() {
  182. var dic = DynamicSugar.ReflectionHelper.GetDictionary(TestDataInstanceManager.TestPersonInstance);
  183. Assert.AreEqual("TORRES" , dic["LastName"]);
  184. Assert.AreEqual("Frederic" , dic["FirstName"]);
  185. Assert.AreEqual(45 , dic["Age"]);
  186. Assert.AreEqual(new DateTime(1964, 12, 11) , dic["BirthDay"]);
  187. dic = TestDataInstanceManager.TestPersonInstance.Dictionary();
  188. Assert.AreEqual("TORRES" , dic["LastName"]);
  189. Assert.AreEqual("Frederic" , dic["FirstName"]);
  190. Assert.AreEqual(45 , dic["Age"]);
  191. Assert.AreEqual(new DateTime(1964, 12, 11) , dic["BirthDay"]);
  192. }
  193. [TestMethod]
  194. public void GetDictionary_GetSubList() {
  195. var dic = DynamicSugar.ReflectionHelper.GetDictionary(TestDataInstanceManager.TestPersonInstance, DS.List("LastName","Age"));
  196. Assert.AreEqual("TORRES" , dic["LastName"]);
  197. Assert.AreEqual(45 , dic["Age"]);
  198. Assert.IsFalse(dic.ContainsKey("FirstName"));
  199. Assert.IsFalse(dic.ContainsKey("BirthDay"));
  200. dic = TestDataInstanceManager.TestPersonInstance.Dictionary(DS.List("LastName","Age"));
  201. Assert.AreEqual("TORRES" , dic["LastName"]);
  202. Assert.AreEqual(45 , dic["Age"]);
  203. Assert.IsFalse(dic.ContainsKey("FirstName"));
  204. Assert.IsFalse(dic.ContainsKey("BirthDay"));
  205. }
  206. [TestMethod]
  207. public void GetProperties_WithExpandoObject() {
  208. dynamic ex = new ExpandoObject();
  209. ex.LastName = "TORRES";
  210. ex.FirstName = "Frederic";
  211. ex.Age = 45;
  212. ex.BirthDay = new DateTime(1964, 12, 11);
  213. var dic = DynamicSugar.ReflectionHelper.GetDictionary(ex);
  214. Assert.AreEqual("TORRES" , dic["LastName"]);
  215. Assert.AreEqual("Frederic" , dic["FirstName"]);
  216. Assert.AreEqual(45 , dic["Age"]);
  217. Assert.AreEqual(new DateTime(1964, 12, 11) , dic["BirthDay"]);
  218. }
  219. [TestMethod]
  220. public void PropertyExist() {
  221. Assert.IsTrue(DynamicSugar.ReflectionHelper.PropertyExist(TestDataInstanceManager.TestPersonInstance, "LastName"));
  222. Assert.IsTrue(DynamicSugar.ReflectionHelper.PropertyExist(TestDataInstanceManager.TestPersonInstance, "FirstName"));
  223. Assert.IsTrue(DynamicSugar.ReflectionHelper.PropertyExist(TestDataInstanceManager.TestPersonInstance, "Age"));
  224. Assert.IsTrue(DynamicSugar.ReflectionHelper.PropertyExist(TestDataInstanceManager.TestPersonInstance, "BirthDay"));
  225. Assert.IsFalse(DynamicSugar.ReflectionHelper.PropertyExist(TestDataInstanceManager.TestPersonInstance, "lastname"));
  226. Assert.IsFalse(DynamicSugar.ReflectionHelper.PropertyExist(TestDataInstanceManager.TestPersonInstance, "NotAvailableProperty"));
  227. }
  228. [TestMethod]
  229. public void MethodExist()
  230. {
  231. Assert.IsTrue(DynamicSugar.ReflectionHelper.MethodExist(TestDataInstanceManager.TestPersonInstance, "GetLastName"));
  232. Assert.IsFalse(DynamicSugar.ReflectionHelper.MethodExist(TestDataInstanceManager.TestPersonInstance, "InvalidMethod"));
  233. }
  234. [TestMethod]
  235. public void GetProperty() {
  236. Assert.AreEqual(TestDataInstanceManager.LASTNAME, DynamicSugar.ReflectionHelper.GetProperty(TestDataInstanceManager.TestPersonInstance, "LastName").ToString());
  237. Assert.AreEqual(TestDataInstanceManager.FIRSTNAME, DynamicSugar.ReflectionHelper.GetProperty(TestDataInstanceManager.TestPersonInstance, "FirstName").ToString());
  238. Assert.AreEqual(TestDataInstanceManager.AGE, (int)DynamicSugar.ReflectionHelper.GetProperty(TestDataInstanceManager.TestPersonInstance, "Age"));
  239. Assert.AreEqual(TestDataInstanceManager.BIRTH_DAY, (DateTime)DynamicSugar.ReflectionHelper.GetProperty(TestDataInstanceManager.TestPersonInstance, "BirthDay"));
  240. Assert.AreEqual(null, DynamicSugar.ReflectionHelper.GetProperty(TestDataInstanceManager.TestPersonInstance, "lastname"));
  241. Assert.AreEqual(null, DynamicSugar.ReflectionHelper.GetProperty(TestDataInstanceManager.TestPersonInstance, "NotAvailableProperty"));
  242. }
  243. [TestMethod]
  244. public void GetPropertyForObject() {
  245. var streetValue = "41 Lakewood road";
  246. var p = TestDataInstanceManager.TestPersonInstance;
  247. p.Address.Street = streetValue;
  248. dynamic address = DynamicSugar.ReflectionHelper.GetProperty(p, "Address");
  249. Assert.AreEqual(streetValue, address.Street);
  250. }
  251. }
  252. }