/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
- // ****************************************************************
- // Copyright 2009, Charlie Poole
- // This is free software licensed under the NUnit license. You may
- // obtain a copy of the license at http://nunit.org
- // ****************************************************************
-
- using System;
- using System.Collections;
- using System.Reflection;
- #if NET_2_0
- using System.Collections.Generic;
- #endif
-
- namespace NUnit.Framework.Constraints
- {
- /// <summary>
- /// ComparisonAdapter class centralizes all comparisons of
- /// values in NUnit, adapting to the use of any provided
- /// IComparer, IComparer<T> or Comparison<T>
- /// </summary>
- public abstract class ComparisonAdapter
- {
- /// <summary>
- /// Gets the default ComparisonAdapter, which wraps an
- /// NUnitComparer object.
- /// </summary>
- public static ComparisonAdapter Default
- {
- get { return new DefaultComparisonAdapter(); }
- }
-
- /// <summary>
- /// Returns a ComparisonAdapter that wraps an IComparer
- /// </summary>
- public static ComparisonAdapter For(IComparer comparer)
- {
- return new ComparerAdapter(comparer);
- }
-
- #if NET_2_0
- /// <summary>
- /// Returns a ComparisonAdapter that wraps an IComparer<T>
- /// </summary>
- public static ComparisonAdapter For<T>(IComparer<T> comparer)
- {
- return new ComparerAdapter<T>(comparer);
- }
-
- /// <summary>
- /// Returns a ComparisonAdapter that wraps a Comparison<T>
- /// </summary>
- public static ComparisonAdapter For<T>(Comparison<T> comparer)
- {
- return new ComparisonAdapterForComparison<T>(comparer);
- }
- #endif
-
- /// <summary>
- /// Compares two objects
- /// </summary>
- public abstract int Compare(object expected, object actual);
-
- class DefaultComparisonAdapter : ComparerAdapter
- {
- /// <summary>
- /// Construct a default ComparisonAdapter
- /// </summary>
- public DefaultComparisonAdapter() : base( NUnitComparer.Default ) { }
- }
-
- class ComparerAdapter : ComparisonAdapter
- {
- private IComparer comparer;
-
- /// <summary>
- /// Construct a ComparisonAdapter for an IComparer
- /// </summary>
- public ComparerAdapter(IComparer comparer)
- {
- this.comparer = comparer;
- }
-
- /// <summary>
- /// Compares two objects
- /// </summary>
- /// <param name="expected"></param>
- /// <param name="actual"></param>
- /// <returns></returns>
- public override int Compare(object expected, object actual)
- {
- return comparer.Compare(expected, actual);
- }
- }
-
- #if NET_2_0
- /// <summary>
- /// ComparisonAdapter<T> extends ComparisonAdapter and
- /// allows use of an IComparer<T> or Comparison<T>
- /// to actually perform the comparison.
- /// </summary>
- class ComparerAdapter<T> : ComparisonAdapter
- {
- private IComparer<T> comparer;
-
- /// <summary>
- /// Construct a ComparisonAdapter for an IComparer<T>
- /// </summary>
- public ComparerAdapter(IComparer<T> comparer)
- {
- this.comparer = comparer;
- }
-
- /// <summary>
- /// Compare a Type T to an object
- /// </summary>
- public override int Compare(object expected, object actual)
- {
- if (!typeof(T).IsAssignableFrom(expected.GetType()))
- throw new ArgumentException("Cannot compare " + expected.ToString());
-
- if (!typeof(T).IsAssignableFrom(actual.GetType()))
- throw new ArgumentException("Cannot compare to " + actual.ToString());
-
- return comparer.Compare((T)expected, (T)actual);
- }
- }
-
- class ComparisonAdapterForComparison<T> : ComparisonAdapter
- {
- private Comparison<T> comparison;
-
- /// <summary>
- /// Construct a ComparisonAdapter for a Comparison<T>
- /// </summary>
- public ComparisonAdapterForComparison(Comparison<T> comparer)
- {
- this.comparison = comparer;
- }
-
- /// <summary>
- /// Compare a Type T to an object
- /// </summary>
- public override int Compare(object expected, object actual)
- {
- if (!typeof(T).IsAssignableFrom(expected.GetType()))
- throw new ArgumentException("Cannot compare " + expected.ToString());
-
- if (!typeof(T).IsAssignableFrom(actual.GetType()))
- throw new ArgumentException("Cannot compare to " + actual.ToString());
-
- return comparison.Invoke((T)expected, (T)actual);
- }
- }
- #endif
- }
- }