1 // ***********************************************************************
2 // Copyright (c) 2007 Charlie Poole
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:
12 // The above copyright notice and this permission notice shall be
13 // included in all copies or substantial portions of the Software.
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 // ***********************************************************************
25 #define NUNIT_FRAMEWORK
30 using System.Collections;
31 using System.Collections.Generic;
33 namespace NUnit.Framework.Constraints
36 /// CollectionItemsEqualConstraint is the abstract base class for all
37 /// collection constraints that apply some notion of item equality
38 /// as a part of their operation.
40 public abstract class CollectionItemsEqualConstraint : CollectionConstraint
42 private readonly NUnitEqualityComparer comparer = NUnitEqualityComparer.Default;
45 /// Construct an empty CollectionConstraint
47 protected CollectionItemsEqualConstraint() { }
50 /// Construct a CollectionConstraint
52 /// <param name="arg"></param>
53 protected CollectionItemsEqualConstraint(object arg) : base(arg) { }
58 /// Flag the constraint to ignore case and return self.
60 public CollectionItemsEqualConstraint IgnoreCase
64 comparer.IgnoreCase = true;
70 /// Flag the constraint to use the supplied IComparer object.
72 /// <param name="comparer">The IComparer object to use.</param>
73 /// <returns>Self.</returns>
74 public CollectionItemsEqualConstraint Using(IComparer comparer)
76 this.comparer.ExternalComparers.Add(EqualityAdapter.For(comparer));
81 /// Flag the constraint to use the supplied IComparer object.
83 /// <param name="comparer">The IComparer object to use.</param>
84 /// <returns>Self.</returns>
85 public CollectionItemsEqualConstraint Using<T>(IComparer<T> comparer)
87 this.comparer.ExternalComparers.Add(EqualityAdapter.For(comparer));
92 /// Flag the constraint to use the supplied Comparison object.
94 /// <param name="comparer">The IComparer object to use.</param>
95 /// <returns>Self.</returns>
96 public CollectionItemsEqualConstraint Using<T>(Comparison<T> comparer)
98 this.comparer.ExternalComparers.Add(EqualityAdapter.For(comparer));
103 /// Flag the constraint to use the supplied IEqualityComparer object.
105 /// <param name="comparer">The IComparer object to use.</param>
106 /// <returns>Self.</returns>
107 public CollectionItemsEqualConstraint Using(IEqualityComparer comparer)
109 this.comparer.ExternalComparers.Add(EqualityAdapter.For(comparer));
114 /// Flag the constraint to use the supplied IEqualityComparer object.
116 /// <param name="comparer">The IComparer object to use.</param>
117 /// <returns>Self.</returns>
118 public CollectionItemsEqualConstraint Using<T>(IEqualityComparer<T> comparer)
120 this.comparer.ExternalComparers.Add(EqualityAdapter.For(comparer));
124 internal CollectionItemsEqualConstraint Using(EqualityAdapter adapter)
126 comparer.ExternalComparers.Add(adapter);
133 /// Compares two collection members for equality
135 protected bool ItemsEqual(object x, object y)
137 Tolerance tolerance = Tolerance.Default;
138 return comparer.AreEqual(x, y, ref tolerance);
142 /// Return a new CollectionTally for use in making tests
144 /// <param name="c">The collection to be included in the tally</param>
145 protected CollectionTally Tally(IEnumerable c)
147 return new CollectionTally(comparer, c);