PageRenderTime 38ms CodeModel.GetById 18ms app.highlight 13ms RepoModel.GetById 1ms app.codeStats 1ms

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

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