Utils.cs 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. //
  2. // Utils.cs
  3. //
  4. // Author:
  5. // Marek Habersack <[email protected]>
  6. //
  7. // Copyright (c) 2010, Marek Habersack
  8. //
  9. // All rights reserved.
  10. //
  11. // Redistribution and use in source and binary forms, with or without modification, are permitted
  12. // provided that the following conditions are met:
  13. //
  14. // * Redistributions of source code must retain the above copyright notice, this list of
  15. // conditions and the following disclaimer.
  16. // * Redistributions in binary form must reproduce the above copyright notice, this list of
  17. // conditions and the following disclaimer in the documentation and/or other materials
  18. // provided with the distribution.
  19. // * Neither the name of Marek Habersack nor the names of its contributors may be used to
  20. // endorse or promote products derived from this software without specific prior written
  21. // permission.
  22. //
  23. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  24. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  25. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  26. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  27. // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  28. // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  29. // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  30. // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  31. // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  32. // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  33. // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  34. //
  35. using System;
  36. using System.Collections.Generic;
  37. using System.ComponentModel;
  38. using System.IO;
  39. using System.Text;
  40. using System.Web.Caching;
  41. using System.Xml;
  42. using System.Xml.XPath;
  43. namespace Tester
  44. {
  45. static class Utils
  46. {
  47. public static T GetRequiredAttribute <T> (this XPathNavigator nav, string name)
  48. {
  49. string value = nav.GetAttribute (name, String.Empty);
  50. if (String.IsNullOrEmpty (value))
  51. throw new InvalidOperationException (String.Format ("Required attribute '{0}' missing.", name));
  52. if (typeof (T) == typeof (string))
  53. return (T)((object)value);
  54. // Special cases because we use ticks
  55. if (typeof (T) == typeof (DateTime))
  56. return (T)((object) new DateTime (Int64.Parse (value)));
  57. else if (typeof (T) == typeof (TimeSpan))
  58. return (T)((object) new TimeSpan (Int64.Parse (value)));
  59. TypeConverter cvt = TypeDescriptor.GetConverter (typeof (T));
  60. if (cvt == null)
  61. throw new InvalidOperationException (String.Format ("Type converter for type '{0}' cannot be found.", typeof (T)));
  62. if (!cvt.CanConvertFrom (typeof (string)))
  63. throw new InvalidOperationException (String.Format ("Conversion from string to type '{0}' is not supported.", typeof (T)));
  64. return (T) cvt.ConvertFrom (value);
  65. }
  66. public static void SequenceMethodStart (this StringBuilder sb, string indent, string fileName, int seqNum)
  67. {
  68. sb.Append ("\n" + indent);
  69. sb.AppendFormat ("[Test (Description=\"Generated from sequence file {0}\")]\n", Path.GetFileName (fileName));
  70. sb.Append (indent);
  71. sb.AppendFormat ("public void Sequence_{0:0000} ()\n", seqNum);
  72. sb.Append (indent);
  73. sb.Append ("{\n");
  74. }
  75. public static void SequenceMethodEnd (this StringBuilder sb, string indent)
  76. {
  77. sb.Append (indent);
  78. sb.Append ("}\n");
  79. }
  80. public static void FormatQueueSize (this StringBuilder sb, string indent, PriorityQueueState qs)
  81. {
  82. sb.Append (indent);
  83. sb.AppendFormat ("Assert.AreEqual ({0}, {1}.Count, \"Queue size after sequence\");\n\n",
  84. qs.Queue.Count, qs.QueueName);
  85. }
  86. public static void FormatDisableItem (this StringBuilder sb, string indent, PriorityQueueState qs, List <CacheItem> list, int index)
  87. {
  88. CacheItem item = list [index];
  89. sb.Append (indent);
  90. sb.AppendFormat ("{0} = {1} [{2}];\n", qs.ItemName, qs.ListName, index);
  91. sb.Append (indent);
  92. if (item == null) {
  93. sb.AppendFormat ("Assert.IsNull ({0}, \"Disable-{1:0000}-1\");\n",
  94. qs.ItemName, qs.DisableCount);
  95. return;
  96. }
  97. sb.AppendFormat ("Assert.IsNotNull ({0}, \"Disable-{1:0000}-1\");\n",
  98. qs.ItemName, qs.DisableCount);
  99. sb.Append (indent);
  100. sb.AppendFormat ("Assert.AreEqual (\"{0}\", {1}.Guid.ToString(), \"Disable-{2:0000}-3\");\n",
  101. item.Guid.ToString (), qs.ItemName, qs.DisableCount);
  102. sb.Append (indent);
  103. sb.AppendFormat ("Assert.AreEqual ({0}, {1}.Disabled, \"Disable-{2:0000}-3\");\n",
  104. item.Disabled.ToString ().ToLowerInvariant (),
  105. qs.ItemName, qs.DisableCount);
  106. sb.Append (indent);
  107. sb.AppendFormat ("{0}.Disabled = true;\n\n", qs.ItemName);
  108. item.Disabled = true;
  109. qs.DisableCount++;
  110. }
  111. public static void FormatDequeue (this StringBuilder sb, string indent, PriorityQueueState qs)
  112. {
  113. CacheItem item = qs.Dequeue ();
  114. sb.Append (indent);
  115. sb.AppendFormat ("{0} = {1}.Dequeue ();\n", qs.ItemName, qs.QueueName);
  116. sb.Append (indent);
  117. if (item != null)
  118. sb.AppendFormat ("Assert.IsNotNull ({0}, \"Dequeue-{1:0000}-1\");\n", qs.ItemName, qs.DequeueCount);
  119. else
  120. sb.AppendFormat ("Assert.IsNull ({0}, \"Dequeue-{1:0000}-1\");\n", qs.ItemName, qs.DequeueCount);
  121. sb.Append (indent);
  122. sb.AppendFormat ("Assert.AreEqual ({0}, {1}.Count, \"Dequeue-{2:0000}-2\");\n",
  123. qs.Queue.Count, qs.QueueName, qs.DequeueCount);
  124. if (item != null) {
  125. sb.Append (indent);
  126. sb.AppendFormat ("Assert.AreEqual (\"{0}\", {1}.Guid.ToString (), \"Dequeue-{2:0000}-3\");\n",
  127. item.Guid.ToString (), qs.ItemName, qs.DequeueCount);
  128. sb.Append (indent);
  129. sb.AppendFormat ("Assert.AreEqual ({0}, {1}.Disabled, \"Dequeue-{2:0000}-4\");\n\n",
  130. item.Disabled.ToString ().ToLowerInvariant (), qs.ItemName, qs.DequeueCount);
  131. }
  132. qs.DequeueCount++;
  133. }
  134. public static void FormatPeek (this StringBuilder sb, string indent, PriorityQueueState qs)
  135. {
  136. CacheItem item = qs.Peek ();
  137. sb.Append (indent);
  138. sb.AppendFormat ("{0} = {1}.Peek ();\n", qs.ItemName, qs.QueueName);
  139. sb.Append (indent);
  140. if (item != null)
  141. sb.AppendFormat ("Assert.IsNotNull ({0}, \"Peek-{1:0000}-1\");\n", qs.ItemName, qs.PeekCount);
  142. else
  143. sb.AppendFormat ("Assert.IsNull ({0}, \"Peek-{1:0000}-1\");\n", qs.ItemName, qs.PeekCount);
  144. sb.Append (indent);
  145. sb.AppendFormat ("Assert.AreEqual ({0}, {1}.Count, \"Peek-{2:0000}-2\");\n", qs.Queue.Count, qs.QueueName, qs.PeekCount);
  146. if (item != null) {
  147. sb.Append (indent);
  148. sb.AppendFormat ("Assert.AreEqual (\"{0}\", {1}.Guid.ToString (), \"Peek-{2:0000}-3\");\n",
  149. item.Guid.ToString (), qs.ItemName, qs.PeekCount);
  150. sb.Append (indent);
  151. sb.AppendFormat ("Assert.AreEqual ({0}, {1}.Disabled, \"Peek-{2:0000}-4\");\n\n",
  152. item.Disabled.ToString ().ToLowerInvariant (), qs.ItemName, qs.PeekCount);
  153. }
  154. qs.PeekCount++;
  155. }
  156. public static void FormatEnqueue (this StringBuilder sb, string indent, PriorityQueueState qs, List <CacheItem> list, int index)
  157. {
  158. qs.Enqueue (list [index]);
  159. sb.Append (indent);
  160. sb.AppendFormat ("{0}.Enqueue ({1} [{2}]);\n", qs.QueueName, qs.ListName, index);
  161. sb.Append (indent);
  162. sb.AppendFormat ("Assert.AreEqual ({0}, {1}.Count, \"Enqueue-{2:0000}-1\");\n",
  163. qs.Queue.Count, qs.QueueName, qs.EnqueueCount);
  164. sb.Append (indent);
  165. sb.AppendFormat ("Assert.AreEqual (\"{0}\", {1}.Peek ().Guid.ToString(), \"Enqueue-{2:0000}-2\");\n\n",
  166. qs.Peek ().Guid.ToString (), qs.QueueName, qs.EnqueueCount);
  167. qs.EnqueueCount++;
  168. }
  169. public static void FormatList (this StringBuilder sb, string indent, string listName, List <CacheItem> list)
  170. {
  171. if (list == null || list.Count == 0) {
  172. sb.AppendFormat (indent + "var {0} = new List <CacheItem> ();\n", listName);
  173. return;
  174. }
  175. sb.AppendFormat (indent + "var {0} = new List <CacheItem> {{\n", listName);
  176. foreach (CacheItem ci in list)
  177. CreateNewCacheItemInstanceCode (indent + "\t", sb, ci);
  178. sb.Append (indent + "};\n");
  179. }
  180. static void CreateNewCacheItemInstanceCode (string indent, StringBuilder sb, CacheItem item)
  181. {
  182. sb.Append (indent + "new CacheItem {");
  183. sb.AppendFormat ("Key = \"{0}\", ", item.Key.Replace ("\n", "\\n").Replace ("\r", "\\r"));
  184. sb.AppendFormat ("AbsoluteExpiration = DateTime.Parse (\"{0}\"), ", item.AbsoluteExpiration.ToString ());
  185. sb.AppendFormat ("SlidingExpiration = TimeSpan.Parse (\"{0}\"), ", item.SlidingExpiration.ToString ());
  186. sb.AppendFormat ("Priority = CacheItemPriority.{0}, ", item.Priority);
  187. sb.AppendFormat ("LastChange = DateTime.Parse (\"{0}\"), ", item.LastChange.ToString ());
  188. sb.AppendFormat ("ExpiresAt = {0}, ", item.ExpiresAt);
  189. sb.AppendFormat ("Disabled = {0}, ", item.Disabled.ToString ().ToLowerInvariant ());
  190. sb.AppendFormat ("Guid = new Guid (\"{0}\")}}, \n", item.Guid.ToString ());
  191. }
  192. }
  193. }