[NUI] Rebase develnui (DevelNUI only patches --> master) (#3910)
[platform/core/csapi/tizenfx.git] / test / Tizen.NUI.Devel.Tests.Ubuntu / nunit.framework / Constraints / ComparisonAdapter.cs
1 // ***********************************************************************
2 // Copyright (c) 2009 Charlie Poole
3 //
4 // Permission is hereby granted, free of charge, to any person obtaining
5 // a copy of this software and associated documentation files (the
6 // "Software"), to deal in the Software without restriction, including
7 // without limitation the rights to use, copy, modify, merge, publish,
8 // distribute, sublicense, and/or sell copies of the Software, and to
9 // permit persons to whom the Software is furnished to do so, subject to
10 // the following conditions:
11 // 
12 // The above copyright notice and this permission notice shall be
13 // included in all copies or substantial portions of the Software.
14 // 
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 // ***********************************************************************
23 #define PORTABLE
24 #define TIZEN
25 #define NUNIT_FRAMEWORK
26 #define NUNITLITE
27 #define NET_4_5
28 #define PARALLEL
29 using System;
30 using System.Collections;
31 using System.Collections.Generic;
32 using System.Reflection;
33 using NUnit.Compatibility;
34
35 namespace NUnit.Framework.Constraints
36 {
37     /// <summary>
38     /// ComparisonAdapter class centralizes all comparisons of
39     /// _values in NUnit, adapting to the use of any provided
40     /// <see cref="IComparer"/>, <see cref="IComparer{T}"/>
41     /// or <see cref="Comparison{T}"/>.
42     /// </summary>
43     public abstract class ComparisonAdapter
44     {
45         /// <summary>
46         /// Gets the default ComparisonAdapter, which wraps an
47         /// NUnitComparer object.
48         /// </summary>
49         public static ComparisonAdapter Default
50         {
51             get { return new DefaultComparisonAdapter(); }
52         }
53
54         /// <summary>
55         /// Returns a ComparisonAdapter that wraps an <see cref="IComparer"/>
56         /// </summary>
57         public static ComparisonAdapter For(IComparer comparer)
58         {
59             return new ComparerAdapter(comparer);
60         }
61
62         /// <summary>
63         /// Returns a ComparisonAdapter that wraps an <see cref="IComparer{T}"/>
64         /// </summary>
65         public static ComparisonAdapter For<T>(IComparer<T> comparer)
66         {
67             return new ComparerAdapter<T>(comparer);
68         }
69
70         /// <summary>
71         /// Returns a ComparisonAdapter that wraps a <see cref="Comparison{T}"/>
72         /// </summary>
73         public static ComparisonAdapter For<T>(Comparison<T> comparer)
74         {
75             return new ComparisonAdapterForComparison<T>(comparer);
76         }
77
78         /// <summary>
79         /// Compares two objects
80         /// </summary>
81         public abstract int Compare(object expected, object actual);
82
83         class DefaultComparisonAdapter : ComparerAdapter
84         {
85             /// <summary>
86             /// Construct a default ComparisonAdapter
87             /// </summary>
88             public DefaultComparisonAdapter() : base( NUnitComparer.Default ) { }
89         }
90
91         class ComparerAdapter : ComparisonAdapter
92         {
93             private readonly IComparer comparer;
94
95             /// <summary>
96             /// Construct a ComparisonAdapter for an <see cref="IComparer"/>
97             /// </summary>
98             public ComparerAdapter(IComparer comparer)
99             {
100                 this.comparer = comparer;
101             }
102
103             /// <summary>
104             /// Compares two objects
105             /// </summary>
106             /// <param name="expected"></param>
107             /// <param name="actual"></param>
108             /// <returns></returns>
109             public override int Compare(object expected, object actual)
110             {
111                 return comparer.Compare(expected, actual);
112             }
113         }
114
115         /// <summary>
116         /// ComparerAdapter extends <see cref="ComparisonAdapter"/> and
117         /// allows use of an <see cref="IComparer{T}"/> or <see cref="Comparison{T}"/>
118         /// to actually perform the comparison.
119         /// </summary>
120         class ComparerAdapter<T> : ComparisonAdapter
121         {
122             private readonly IComparer<T> comparer;
123
124             /// <summary>
125             /// Construct a ComparisonAdapter for an <see cref="IComparer{T}"/>
126             /// </summary>
127             public ComparerAdapter(IComparer<T> comparer)
128             {
129                 this.comparer = comparer;
130             }
131
132             /// <summary>
133             /// Compare a Type T to an object
134             /// </summary>
135             public override int Compare(object expected, object actual)
136             {
137                 if (!typeof(T).GetTypeInfo().IsAssignableFrom(expected.GetType().GetTypeInfo()))
138                     throw new ArgumentException("Cannot compare " + expected.ToString());
139
140                 if (!typeof(T).GetTypeInfo().IsAssignableFrom(actual.GetType().GetTypeInfo()))
141                     throw new ArgumentException("Cannot compare to " + actual.ToString());
142
143                 return comparer.Compare((T)expected, (T)actual);
144             }
145         }
146
147         class ComparisonAdapterForComparison<T> : ComparisonAdapter
148         {
149             private readonly Comparison<T> comparison;
150
151             /// <summary>
152             /// Construct a ComparisonAdapter for a <see cref="Comparison{T}"/>
153             /// </summary>
154             public ComparisonAdapterForComparison(Comparison<T> comparer)
155             {
156                 this.comparison = comparer;
157             }
158
159             /// <summary>
160             /// Compare a Type T to an object
161             /// </summary>
162             public override int Compare(object expected, object actual)
163             {
164                 if (!typeof(T).GetTypeInfo().IsAssignableFrom(expected.GetType().GetTypeInfo()))
165                     throw new ArgumentException("Cannot compare " + expected.ToString());
166
167                 if (!typeof(T).GetTypeInfo().IsAssignableFrom(actual.GetType().GetTypeInfo()))
168                     throw new ArgumentException("Cannot compare to " + actual.ToString());
169
170                 return comparison.Invoke((T)expected, (T)actual);
171             }
172         }
173     }
174 }