PageRenderTime 39ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/VersionOne.ServiceHost.Tests/Utility/ListAssert.cs

https://bitbucket.org/szczepiq/cqi
C# | 308 lines | 12 code | 17 blank | 279 comment | 0 complexity | 1d069f12ef3ce9545e2570d6f124e66c MD5 | raw file
Possible License(s): GPL-2.0, MIT
  1. /*(c) Copyright 2009, VersionOne, Inc. All rights reserved. (c)*/
  2. using System;
  3. using System.Collections;
  4. using System.Collections.ObjectModel;
  5. using VersionOne.ServiceHost.Tests.Utility;
  6. namespace NUnit.Framework
  7. {
  8. public class ListAssert
  9. {
  10. private static readonly ITransformer _defaultconverter = new NoopTransformer();
  11. private ListAssert() { }
  12. // public static void Contains(object expectedItem, IEnumerable actualList, ITransformer transformer, string message, params object[] args)
  13. // {
  14. // ArrayList all = new ArrayList();
  15. //
  16. // Assert.Contains(expectedItem, new List() actualList, message); //DoAssert(new ContainsAsserter(true, expectedItem, null, actualList, transformer, message, args));
  17. // }
  18. // public static void Contains(object expectedItem, IEnumerable actualList, string message, params object[] args)
  19. // {
  20. // Contains(expectedItem, actualList, _defaultconverter, message, args);
  21. // }
  22. // public static void Contains(object expectedItem, IEnumerable actualList, ITransformer transformer)
  23. // {
  24. // Contains(expectedItem, actualList, transformer, null);
  25. // }
  26. // public static void Contains(object expectedItem, IEnumerable actualList)
  27. // {
  28. // Contains(expectedItem, actualList, _defaultconverter, null);
  29. // }
  30. // public static void NotContains(object expectedItem, IEnumerable actualList, ITransformer transformer, string message, params object[] args)
  31. // {
  32. // Assert.DoAssert(new ContainsAsserter(false, expectedItem, null, actualList, transformer, message, args));
  33. // }
  34. // public static void NotContains(object expectedItem, IEnumerable actualList, string message, params object[] args)
  35. // {
  36. // NotContains(expectedItem, actualList, _defaultconverter, message, args);
  37. // }
  38. // public static void NotContains(object expectedItem, IEnumerable actualList, ITransformer transformer)
  39. // {
  40. // NotContains(expectedItem, actualList, transformer, null);
  41. // }
  42. // public static void NotContains(object expectedItem, IEnumerable actualList)
  43. // {
  44. // NotContains(expectedItem, actualList, _defaultconverter, null);
  45. // }
  46. // public static void AreEqualIgnoringOrder(IList expectedItems, IEnumerable actualList, ITransformer transformer, string message, params object[] args)
  47. // {
  48. // Assert.DoAssert(new AreEqualIgnoringOrderAsserter(expectedItems, null, actualList, transformer, message, args));
  49. // }
  50. // public static void AreEqualIgnoringOrder(IList expectedItems, IEnumerable actualList, string message, params object[] args)
  51. // {
  52. // AreEqualIgnoringOrder(expectedItems, actualList, _defaultconverter, message, args);
  53. // }
  54. // public static void AreEqualIgnoringOrder(IList expectedItems, IEnumerable actualList, ITransformer transformer)
  55. // {
  56. // AreEqualIgnoringOrder(expectedItems, actualList, transformer, null);
  57. // }
  58. // public static void AreEqualIgnoringOrder(IList expectedItems, IEnumerable actualList)
  59. // {
  60. // AreEqualIgnoringOrder(expectedItems, actualList, _defaultconverter, null);
  61. // }
  62. // public static void AreWithinIgnoringOrder(object epsilon, IList expectedItems, IEnumerable actualList, ITransformer transformer, string message, params object[] args)
  63. // {
  64. // Assert.DoAssert(new AreEqualIgnoringOrderAsserter(expectedItems, epsilon, actualList, transformer, message, args));
  65. // }
  66. // public static void AreWithinIgnoringOrder(object epsilon, IList expectedItems, IEnumerable actualList, string message, params object[] args)
  67. // {
  68. // AreWithinIgnoringOrder(epsilon, expectedItems, actualList, _defaultconverter, message, args);
  69. // }
  70. // public static void AreWithinIgnoringOrder(object epsilon, IList expectedItems, IEnumerable actualList, ITransformer transformer)
  71. // {
  72. // AreWithinIgnoringOrder(epsilon, expectedItems, actualList, transformer, null);
  73. // }
  74. // public static void AreWithinIgnoringOrder(object epsilon, IList expectedItems, IEnumerable actualList)
  75. // {
  76. // AreWithinIgnoringOrder(epsilon, expectedItems, actualList, _defaultconverter, null);
  77. // }
  78. //
  79. // public static void AreEqual(IList expectedItems, IEnumerable actualList, ITransformer transformer, string message, params object[] args)
  80. // {
  81. // Assert.DoAssert(new AreEqualAsserter(expectedItems, null, actualList, transformer, message, args));
  82. // }
  83. // public static void AreEqual(IList expectedItems, IEnumerable actualList, string message, params object[] args)
  84. // {
  85. // AreEqual(expectedItems, actualList, _defaultconverter, message, args);
  86. // }
  87. // public static void AreEqual(IList expectedItems, IEnumerable actualList, ITransformer transformer)
  88. // {
  89. // AreEqual(expectedItems, actualList, transformer, null);
  90. // }
  91. // public static void AreEqual(IList expectedItems, IEnumerable actualList)
  92. // {
  93. // AreEqual(expectedItems, actualList, _defaultconverter, null);
  94. // }
  95. //
  96. // public static void AreWithin(object epsilon, IList expectedItems, IEnumerable actualList, ITransformer transformer, string message, params object[] args)
  97. // {
  98. // Assert.DoAssert(new AreEqualAsserter(expectedItems, epsilon, actualList, transformer, message, args));
  99. // }
  100. // public static void AreWithin(object epsilon, IList expectedItems, IEnumerable actualList, string message, params object[] args)
  101. // {
  102. // AreWithin(epsilon, expectedItems, actualList, _defaultconverter, message, args);
  103. // }
  104. // public static void AreWithin(object epsilon, IList expectedItems, IEnumerable actualList, ITransformer transformer)
  105. // {
  106. // AreWithin(epsilon, expectedItems, actualList, transformer, null);
  107. // }
  108. // public static void AreWithin(object epsilon, IList expectedItems, IEnumerable actualList)
  109. // {
  110. // AreWithin(epsilon, expectedItems, actualList, _defaultconverter, null);
  111. // }
  112. //
  113. // private abstract class ListAsserter : AbstractAsserter
  114. // {
  115. // protected IEnumerable _list;
  116. // protected ITransformer _converter;
  117. // protected object _epsilon;
  118. //
  119. // public ListAsserter(object epsilon, IEnumerable list, ITransformer transformer, string message, params object[] args)
  120. // : base(message, args)
  121. // {
  122. // _list = list;
  123. // _converter = transformer;
  124. // _epsilon = epsilon;
  125. // }
  126. //
  127. // protected bool CompareEqual(object expected, object actual)
  128. // {
  129. // if (_epsilon != null)
  130. // {
  131. // if (_epsilon is TimeSpan && expected is DateTime && actual is DateTime)
  132. // return Math.Abs(((DateTime)expected).Subtract((DateTime)actual).Ticks) <= ((TimeSpan)_epsilon).Ticks;
  133. // else if (_epsilon is double && expected is double && actual is double)
  134. // return Math.Abs((double)expected - (double)actual) <= (double)_epsilon;
  135. // else if (_epsilon is float && expected is float && actual is float)
  136. // return Math.Abs((float)expected - (float)actual) <= (float)_epsilon;
  137. // }
  138. // return object.Equals(expected, actual);
  139. // }
  140. // }
  141. // private class ContainsAsserter : ListAsserter
  142. // {
  143. // private bool _polarity;
  144. // private object _item;
  145. //
  146. // public ContainsAsserter(bool polarity, object item, object epsilon, IEnumerable list, ITransformer transformer, string message, params object[] args)
  147. // : base(epsilon, list, transformer, message, args)
  148. // {
  149. // _polarity = polarity;
  150. // _item = item;
  151. // }
  152. //
  153. // public override bool Test()
  154. // {
  155. // FailureMessage msg = new FailureMessage(Message, args);
  156. // foreach (object value in _list)
  157. // if (CompareEqual(_item, _converter.Transform(value)))
  158. // {
  159. // if (_polarity) return true;
  160. // msg.DisplayExtra(_item);
  161. // throw new AssertionException(msg.ToString());
  162. // }
  163. // if (!_polarity) return true;
  164. // msg.DisplayMissing(_item);
  165. // throw new AssertionException(msg.ToString());
  166. // }
  167. // }
  168. // private class AreEqualIgnoringOrderAsserter : ListAsserter
  169. // {
  170. // private IList _items;
  171. //
  172. // public AreEqualIgnoringOrderAsserter(IList items, object epsilon, IEnumerable list, ITransformer transformer, string message, params object[] args)
  173. // : base(epsilon, list, transformer, message, args)
  174. // {
  175. // _items = items;
  176. // }
  177. //
  178. // public override bool Test()
  179. // {
  180. // FailureMessage msg = new FailureMessage(Message, args);
  181. // bool isfail = false;
  182. // bool[] matched = new bool[_items.Count];
  183. // int count = 0;
  184. // foreach (object value in _list)
  185. // {
  186. // ++count;
  187. // object nvalue = _converter.Transform(value);
  188. // int foundat = Find(_items, nvalue, 0);
  189. // while (foundat < _items.Count && matched[foundat])
  190. // foundat = Find(_items, nvalue, foundat + 1);
  191. // if (foundat >= _items.Count)
  192. // {
  193. // msg.DisplayExtra(nvalue);
  194. // isfail = true;
  195. // }
  196. // else
  197. // matched[foundat] = true;
  198. // }
  199. // for (int i = 0; i < matched.Length; ++i)
  200. // {
  201. // if (!matched[i])
  202. // msg.DisplayMissing(_items[i]);
  203. // }
  204. // if (count != _items.Count)
  205. // {
  206. // isfail = true;
  207. // msg.DisplayLengthsDiff(_items.Count, count);
  208. // }
  209. // if (isfail)
  210. // throw new AssertionException(msg.ToString());
  211. // return true;
  212. // }
  213. //
  214. // private int Find(IList list, object value, int index)
  215. // {
  216. // while (index < list.Count)
  217. // {
  218. // if (CompareEqual(value, list[index]))
  219. // break;
  220. // ++index;
  221. // }
  222. // return index;
  223. // }
  224. // }
  225. // private class AreEqualAsserter : ListAsserter
  226. // {
  227. // private IList _items;
  228. //
  229. // public AreEqualAsserter(IList items, object epsilon, IEnumerable list, ITransformer transformer, string message, params object[] args)
  230. // : base(epsilon, list, transformer, message, args)
  231. // {
  232. // _items = items;
  233. // }
  234. //
  235. // public override bool Test()
  236. // {
  237. // FailureMessage msg = new FailureMessage(Message, args);
  238. // bool isfail = false;
  239. // int count = 0;
  240. // foreach (object value in _list)
  241. // {
  242. // object nvalue = _converter.Transform(value);
  243. // if (!isfail && count < _items.Count && !CompareEqual(_items[count], nvalue))
  244. // {
  245. // msg.DisplayMismatch(_items[count], nvalue, count);
  246. // isfail = true;
  247. // }
  248. // ++count;
  249. // }
  250. // if (count != _items.Count)
  251. // {
  252. // isfail = true;
  253. // msg.DisplayLengthsDiff(_items.Count, count);
  254. // }
  255. // if (isfail)
  256. // throw new AssertionException(msg.ToString());
  257. // return true;
  258. // }
  259. // }
  260. // private class FailureMessage : AssertionFailureMessage
  261. // {
  262. // public FailureMessage(string message, object[] args) : base(message, args) { }
  263. //
  264. // public void DisplayMismatch(object expected, object actual, int index)
  265. // {
  266. // WriteLine();
  267. // Write(" Mismatch at index {0}", index);
  268. // DisplayExpectedAndActual(expected, actual);
  269. // }
  270. //
  271. // public void DisplayMissing(object value)
  272. // {
  273. // WriteLine();
  274. // Write(" Expected: {0}", FormatObjectForDisplay(value));
  275. // }
  276. //
  277. // public void DisplayExtra(object value)
  278. // {
  279. // WriteLine();
  280. // Write(" Unexpected: {0}", FormatObjectForDisplay(value));
  281. // }
  282. //
  283. // public void DisplayLengthsDiff(int expected, int actual)
  284. // {
  285. // WriteLine();
  286. // Write(" Lengths are different:");
  287. // DisplayExpectedAndActual(expected, actual);
  288. // }
  289. // }
  290. }
  291. }