PageRenderTime 53ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/UnitTests/ExtensionMethods/Dictionary_EM_UnitTests.cs

http://github.com/fredericaltorres/DynamicSugarNet
C# | 162 lines | 138 code | 23 blank | 1 comment | 0 complexity | df1e7bb80e171b9c8c79e06c1263b693 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. //TODO:Try extension method to List<T>
  10. [TestClass]
  11. public class Dictionary_EM_UnitTests {
  12. [TestMethod]
  13. public void PreProcess()
  14. {
  15. var dic1 = DS.Dictionary(new { a = 1, b = 2, date = new DateTime(1964, 12, 11) });
  16. Assert.AreEqual("a=1, b=002, date=1964-12-11", dic1.PreProcess("a={a}, b={b:000}, date={date:yyyy-MM-dd}"));
  17. }
  18. [TestMethod]
  19. public void Include_AnonymousType()
  20. {
  21. var dic1 = DS.Dictionary(new { a = 1, b = 2, c = 3, d = 4, e = 5 });
  22. Assert.IsTrue(dic1.Include(dic1));
  23. Assert.IsTrue(dic1.Include(new { a = 1, b = 2, }));
  24. Assert.IsFalse(dic1.Include(new { a = 1, b = 2, c = 33 }));
  25. Assert.IsFalse(dic1.Include(new { a = 1, b = 2, d = 3 }));
  26. }
  27. [TestMethod]
  28. public void Include_Dictionary()
  29. {
  30. var dic1 = DS.Dictionary(new { a = 1, b = 2, c = 3, d = 4, e = 5 });
  31. Assert.IsTrue(dic1.Include(dic1));
  32. Assert.IsTrue(dic1.Include(DS.Dictionary(new { a = 1, b = 2, })));
  33. Assert.IsFalse(dic1.Include(DS.Dictionary(new { a = 1, b = 2, c=33})));
  34. Assert.IsFalse(dic1.Include(DS.Dictionary(new { a = 1, b = 2, d =3 })));
  35. }
  36. [TestMethod]
  37. public void Include_List()
  38. {
  39. var dic1 = DS.Dictionary( new { a = 1, b = 2, c = 3, d = 4, e = 5 } );
  40. Assert.IsTrue(dic1.Include(DS.List("a", "b", "c")));
  41. Assert.IsTrue(dic1.Include("a", "b", "c"));
  42. Assert.IsFalse(dic1.Include(DS.List("a", "b", "c", "dd")));
  43. Assert.IsFalse(dic1.Include("a", "b", "c", "dd"));
  44. }
  45. [TestMethod]
  46. public void SystemWebRoutingRouteValueDictionary_Dictionary()
  47. {
  48. var dic1 = DS.Dictionary(new { a = 1, b = 2, c = 3 });
  49. var dic2 = DS.Dictionary(new Dictionary<string, object>() { { "a", 1 }, { "b", 2 }, { "c", 3 } } );
  50. dynamic dic3 = new ExpandoObject();
  51. dic3.a = 1;
  52. dic3.b = 2;
  53. dic3.c = 3;
  54. var dic4 = new System.Web.Routing.RouteValueDictionary(new { a = 1, b = 2, c = 3 });
  55. DS.DictionaryHelper.AssertDictionaryEqual(dic1, dic4);
  56. dic4 = new System.Web.Routing.RouteValueDictionary(new Dictionary<string, object>() { { "a", 1 }, { "b", 2 }, { "c", 3 } });
  57. DS.DictionaryHelper.AssertDictionaryEqual(dic1, dic4);
  58. dic4 = new System.Web.Routing.RouteValueDictionary(dic3);
  59. DS.DictionaryHelper.AssertDictionaryEqual(dic1, dic4);
  60. dic4 = new System.Web.Routing.RouteValueDictionary(TestDataInstanceManager.TestPersonInstance);
  61. }
  62. [TestMethod]
  63. public void Dictionary_Argument_AnonymousType_Dictionary_ExpandoObject()
  64. {
  65. var dic1 = DS.Dictionary(new { a = 1, b = 2, c = 3 });
  66. var dic2 = DS.Dictionary(
  67. new Dictionary<string, object>() { { "a",1 },{ "b",2 },{ "c",3 } }
  68. );
  69. DS.DictionaryHelper.AssertDictionaryEqual(dic1, dic2);
  70. dynamic dic3 = new ExpandoObject();
  71. dic3.a = 1;
  72. dic3.b = 2;
  73. dic3.c = 3;
  74. DS.DictionaryHelper.AssertDictionaryEqual(dic1, dic3);
  75. }
  76. [TestMethod]
  77. public void Substract_Dictionary() {
  78. var dic1 = DS.Dictionary( new { a=1, b=2, c=3, d=4, e=5 } );
  79. var dic2 = DS.Dictionary( new { a=1, c=3, e=5 } );
  80. var dic3 = dic1.Remove(dic2);
  81. var expectedDic = DS.Dictionary( new { b=2, d=4, } );
  82. DS.DictionaryHelper.AssertDictionaryEqual(expectedDic, dic3);
  83. }
  84. [TestMethod]
  85. public void Substract_list() {
  86. var dic1 = DS.Dictionary ( new { a=1, b=2, c=3, d=4, e=5 } );
  87. var dic3 = dic1.Remove( DS.List("a","c","e") );
  88. var expectedDic = DS.Dictionary ( new { b=2, d=4, } );
  89. DS.DictionaryHelper.AssertDictionaryEqual(expectedDic, dic3);
  90. }
  91. [TestMethod]
  92. public void Add_DifferentTypeOfValues() {
  93. var dic1 = DS.Dictionary( new { a=1, b="B", c=33 } ); // c=33 will be ovweritten
  94. var dic2 = DS.Dictionary( new { c=3, d=2.2 } );
  95. var dic3 = DS.Dictionary( new { a=1, b="B", c=3, d=2.2 } );
  96. var r = dic1.Add(dic2);
  97. DS.DictionaryHelper.AssertDictionaryEqual(dic3, r);
  98. }
  99. [TestMethod]
  100. public void Add_DifferentTypeOfValues_DoNotOverWrite() {
  101. var dic1 = DS.Dictionary( new { a=1, b="B" } );
  102. var dic2 = DS.Dictionary( new { b="BB", c=3, d=2.2 } ); //b="BB" will be ignore
  103. var dic3 = DS.Dictionary( new { a=1, b="B", c=3, d=2.2 } );
  104. DS.DictionaryHelper.AssertDictionaryEqual(dic3, dic1.Add(dic2, false));
  105. }
  106. [TestMethod]
  107. public void Add_Int() {
  108. var dic1 = DS.Dictionary<int>( new { a=1, b=2 } );
  109. var dic2 = DS.Dictionary<int>( new { c=3, d=4 } );
  110. var dic3 = DS.Dictionary<int>( new { a=1, b=2, c=3, d=4 } );
  111. DS.DictionaryHelper.AssertDictionaryEqual(dic3, dic1.Add(dic2));
  112. }
  113. [TestMethod]
  114. public void Clone() {
  115. Dictionary<string, int> dic = DS.Dictionary<int>( new { a=1, b=2, c=3 } );
  116. DS.DictionaryHelper.AssertDictionaryEqual(dic, dic.Clone());
  117. }
  118. [TestMethod]
  119. public void Max_Int() {
  120. Dictionary<string, int> dic = DS.Dictionary<int>( new { a=1, b=2, c=3, d=4 } );
  121. Assert.AreEqual("d", dic.Max());
  122. Assert.AreEqual("d", dic.Max( DS.List("a","b","c","d")));
  123. Assert.AreEqual("b", dic.Max( DS.List("a","b")));
  124. }
  125. [TestMethod]
  126. public void Min_Int()
  127. {
  128. Dictionary<string, int> dic = DS.Dictionary<int>(new { a = 1, b = 2, c = 3, d = 4 });
  129. Assert.AreEqual("a", dic.Min());
  130. Assert.AreEqual("b", dic.Min(DS.List("b", "c", "d")));
  131. }
  132. [TestMethod]
  133. public void Max_String() {
  134. Dictionary<string, int> dic = DS.Dictionary<int>( new { a="1", b="2", c="3", d="4" } );
  135. Assert.AreEqual("d", dic.Max( DS.List("a","b","c","d")));
  136. Assert.AreEqual("b", dic.Max( DS.List("a","b")));
  137. }
  138. }
  139. }