1 // ***********************************************************************
2 // Copyright (c) 2007-2015 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;
32 namespace NUnit.Framework.Constraints
35 /// CollectionContainsConstraint is used to test whether a collection
36 /// contains an expected object as a member.
38 public class CollectionContainsConstraint : CollectionItemsEqualConstraint
41 /// Construct a CollectionContainsConstraint
43 /// <param name="expected"></param>
44 public CollectionContainsConstraint(object expected)
51 /// The display name of this Constraint for use by ToString().
52 /// The default value is the name of the constraint with
53 /// trailing "Constraint" removed. Derived classes may set
54 /// this to another name in their constructors.
56 public override string DisplayName { get { return "Contains"; } }
59 /// The Description of what this constraint tests, for
60 /// use in messages and in the ConstraintResult.
62 public override string Description
64 get { return "collection containing " + MsgUtils.FormatValue(Expected); }
68 /// Gets the expected object
70 protected object Expected { get; private set; }
73 /// Test whether the expected item is contained in the collection
75 /// <param name="actual"></param>
76 /// <returns></returns>
77 protected override bool Matches(IEnumerable actual)
79 foreach (object obj in actual)
80 if (ItemsEqual(obj, Expected))
88 /// Flag the constraint to use the supplied predicate function
90 /// <param name="comparison">The comparison function to use.</param>
91 /// <returns>Self.</returns>
92 public CollectionContainsConstraint Using<TCollectionType, TMemberType>(Func<TCollectionType, TMemberType, bool> comparison)
94 // reverse the order of the arguments to match expectations of PredicateEqualityComparer
95 Func<TMemberType, TCollectionType, bool> invertedComparison = (actual, expected) => comparison.Invoke(expected, actual);
97 base.Using(EqualityAdapter.For(invertedComparison));