PageRenderTime 35ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/src/NUnit/framework/Constraints/ComparisonAdapter.cs

#
C# | 156 lines | 81 code | 20 blank | 55 comment | 4 complexity | cd54b5091bbabe86c795cb23fe8bc4bb MD5 | raw file
Possible License(s): GPL-2.0
  1. // ****************************************************************
  2. // Copyright 2009, Charlie Poole
  3. // This is free software licensed under the NUnit license. You may
  4. // obtain a copy of the license at http://nunit.org
  5. // ****************************************************************
  6. using System;
  7. using System.Collections;
  8. using System.Reflection;
  9. #if NET_2_0
  10. using System.Collections.Generic;
  11. #endif
  12. namespace NUnit.Framework.Constraints
  13. {
  14. /// <summary>
  15. /// ComparisonAdapter class centralizes all comparisons of
  16. /// values in NUnit, adapting to the use of any provided
  17. /// IComparer, IComparer&lt;T&gt; or Comparison&lt;T&gt;
  18. /// </summary>
  19. public abstract class ComparisonAdapter
  20. {
  21. /// <summary>
  22. /// Gets the default ComparisonAdapter, which wraps an
  23. /// NUnitComparer object.
  24. /// </summary>
  25. public static ComparisonAdapter Default
  26. {
  27. get { return new DefaultComparisonAdapter(); }
  28. }
  29. /// <summary>
  30. /// Returns a ComparisonAdapter that wraps an IComparer
  31. /// </summary>
  32. public static ComparisonAdapter For(IComparer comparer)
  33. {
  34. return new ComparerAdapter(comparer);
  35. }
  36. #if NET_2_0
  37. /// <summary>
  38. /// Returns a ComparisonAdapter that wraps an IComparer&lt;T&gt;
  39. /// </summary>
  40. public static ComparisonAdapter For<T>(IComparer<T> comparer)
  41. {
  42. return new ComparerAdapter<T>(comparer);
  43. }
  44. /// <summary>
  45. /// Returns a ComparisonAdapter that wraps a Comparison&lt;T&gt;
  46. /// </summary>
  47. public static ComparisonAdapter For<T>(Comparison<T> comparer)
  48. {
  49. return new ComparisonAdapterForComparison<T>(comparer);
  50. }
  51. #endif
  52. /// <summary>
  53. /// Compares two objects
  54. /// </summary>
  55. public abstract int Compare(object expected, object actual);
  56. class DefaultComparisonAdapter : ComparerAdapter
  57. {
  58. /// <summary>
  59. /// Construct a default ComparisonAdapter
  60. /// </summary>
  61. public DefaultComparisonAdapter() : base( NUnitComparer.Default ) { }
  62. }
  63. class ComparerAdapter : ComparisonAdapter
  64. {
  65. private IComparer comparer;
  66. /// <summary>
  67. /// Construct a ComparisonAdapter for an IComparer
  68. /// </summary>
  69. public ComparerAdapter(IComparer comparer)
  70. {
  71. this.comparer = comparer;
  72. }
  73. /// <summary>
  74. /// Compares two objects
  75. /// </summary>
  76. /// <param name="expected"></param>
  77. /// <param name="actual"></param>
  78. /// <returns></returns>
  79. public override int Compare(object expected, object actual)
  80. {
  81. return comparer.Compare(expected, actual);
  82. }
  83. }
  84. #if NET_2_0
  85. /// <summary>
  86. /// ComparisonAdapter&lt;T&gt; extends ComparisonAdapter and
  87. /// allows use of an IComparer&lt;T&gt; or Comparison&lt;T&gt;
  88. /// to actually perform the comparison.
  89. /// </summary>
  90. class ComparerAdapter<T> : ComparisonAdapter
  91. {
  92. private IComparer<T> comparer;
  93. /// <summary>
  94. /// Construct a ComparisonAdapter for an IComparer&lt;T&gt;
  95. /// </summary>
  96. public ComparerAdapter(IComparer<T> comparer)
  97. {
  98. this.comparer = comparer;
  99. }
  100. /// <summary>
  101. /// Compare a Type T to an object
  102. /// </summary>
  103. public override int Compare(object expected, object actual)
  104. {
  105. if (!typeof(T).IsAssignableFrom(expected.GetType()))
  106. throw new ArgumentException("Cannot compare " + expected.ToString());
  107. if (!typeof(T).IsAssignableFrom(actual.GetType()))
  108. throw new ArgumentException("Cannot compare to " + actual.ToString());
  109. return comparer.Compare((T)expected, (T)actual);
  110. }
  111. }
  112. class ComparisonAdapterForComparison<T> : ComparisonAdapter
  113. {
  114. private Comparison<T> comparison;
  115. /// <summary>
  116. /// Construct a ComparisonAdapter for a Comparison&lt;T&gt;
  117. /// </summary>
  118. public ComparisonAdapterForComparison(Comparison<T> comparer)
  119. {
  120. this.comparison = comparer;
  121. }
  122. /// <summary>
  123. /// Compare a Type T to an object
  124. /// </summary>
  125. public override int Compare(object expected, object actual)
  126. {
  127. if (!typeof(T).IsAssignableFrom(expected.GetType()))
  128. throw new ArgumentException("Cannot compare " + expected.ToString());
  129. if (!typeof(T).IsAssignableFrom(actual.GetType()))
  130. throw new ArgumentException("Cannot compare to " + actual.ToString());
  131. return comparison.Invoke((T)expected, (T)actual);
  132. }
  133. }
  134. #endif
  135. }
  136. }