~ubuntu-branches/debian/sid/nunit/sid

« back to all changes in this revision

Viewing changes to src/NUnitFramework/framework/Constraints/ExactCountConstraint.cs

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2014-09-16 13:43:36 UTC
  • mfrom: (1.1.5)
  • Revision ID: package-import@ubuntu.com-20140916134336-kjxz48tty6lx2ja5
Tags: 2.6.3+dfsg-1
* [c7bd1b5] Imported Upstream version 2.6.3+dfsg
* [bcb4bf8] Move nunit-console-runner to GAC-installed libnunit2.6, 
  don't treat it as a private lib. This lib is signed, and treated 
  as a GAC lib by consumers such as MonoDevelop.
* [7f08e99] Bump version to 2.6.3 as required
* [84535eb] Refreshed patches
* [8479f61] Split package up into per-assembly packages. This makes 
  ABI tracking easier in the future, as we can meaningfully have GAC 
  policy for cases where ABI isn't truly bumped, and no policy for 
  cases where it is. For example, if nunit.framework bumps ABI but 
  nunit.core does not, previously we would need to rebuild everything 
  using NUnit, but under the new split packaging, that rebuild would 
  not be needed for apps only using nunit.core.
* [17a7dc7] Add missing nunit.mocks.dll to nunit.pc

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// ****************************************************************
 
2
// Copyright 2011, 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
 
 
7
using System;
 
8
using System.Collections;
 
9
 
 
10
namespace NUnit.Framework.Constraints
 
11
{
 
12
    /// <summary>
 
13
    /// ExactCountConstraint applies another constraint to each
 
14
    /// item in a collection, succeeding only if a specified
 
15
    /// number of items succeed.
 
16
    /// </summary>
 
17
    public class ExactCountConstraint : PrefixConstraint
 
18
    {
 
19
        private int expectedCount;
 
20
 
 
21
        /// <summary>
 
22
        /// Construct an ExactCountConstraint on top of an existing constraint
 
23
        /// </summary>
 
24
        /// <param name="expectedCount"></param>
 
25
        /// <param name="itemConstraint"></param>
 
26
        public ExactCountConstraint(int expectedCount, Constraint itemConstraint)
 
27
            : base(itemConstraint)
 
28
        {
 
29
            this.DisplayName = "one";
 
30
            this.expectedCount = expectedCount;
 
31
        }
 
32
 
 
33
        /// <summary>
 
34
        /// Apply the item constraint to each item in the collection,
 
35
        /// succeeding only if the expected number of items pass.
 
36
        /// </summary>
 
37
        /// <param name="actual"></param>
 
38
        /// <returns></returns>
 
39
        public override bool Matches(object actual)
 
40
        {
 
41
            this.actual = actual;
 
42
 
 
43
            if (!(actual is IEnumerable))
 
44
                throw new ArgumentException("The actual value must be an IEnumerable", "actual");
 
45
 
 
46
            int count = 0;
 
47
            foreach (object item in (IEnumerable)actual)
 
48
                if (baseConstraint.Matches(item))
 
49
                    count++;
 
50
 
 
51
            return count == expectedCount;
 
52
        }
 
53
 
 
54
        /// <summary>
 
55
        /// Write a description of this constraint to a MessageWriter
 
56
        /// </summary>
 
57
        /// <param name="writer"></param>
 
58
        public override void WriteDescriptionTo(MessageWriter writer)
 
59
        {
 
60
            switch (expectedCount)
 
61
            {
 
62
                case 0:
 
63
                    writer.WritePredicate("no item");
 
64
                    break;
 
65
                case 1:
 
66
                    writer.WritePredicate("exactly one item");
 
67
                    break;
 
68
                default:
 
69
                    writer.WritePredicate("exactly " + expectedCount.ToString() + " items");
 
70
                    break;
 
71
            }
 
72
 
 
73
            baseConstraint.WriteDescriptionTo(writer);
 
74
        }
 
75
    }
 
76
}