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

« back to all changes in this revision

Viewing changes to src/NUnitFramework/framework/Constraints/AttributeConstraints.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 2008, 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
 
 
9
 
namespace NUnit.Framework.Constraints
10
 
{
11
 
    /// <summary>
12
 
    /// AttributeExistsConstraint tests for the presence of a
13
 
    /// specified attribute on  a Type.
14
 
    /// </summary>
15
 
    public class AttributeExistsConstraint : Constraint
16
 
    {
17
 
        private Type expectedType;
18
 
 
19
 
        /// <summary>
20
 
        /// Constructs an AttributeExistsConstraint for a specific attribute Type
21
 
        /// </summary>
22
 
        /// <param name="type"></param>
23
 
        public AttributeExistsConstraint(Type type)
24
 
            : base(type)
25
 
        {
26
 
            this.expectedType = type;
27
 
 
28
 
            if (!typeof(Attribute).IsAssignableFrom(expectedType))
29
 
                throw new ArgumentException(string.Format(
30
 
                    "Type {0} is not an attribute", expectedType), "type");
31
 
        }
32
 
 
33
 
        /// <summary>
34
 
        /// Tests whether the object provides the expected attribute.
35
 
        /// </summary>
36
 
        /// <param name="actual">A Type, MethodInfo, or other ICustomAttributeProvider</param>
37
 
        /// <returns>True if the expected attribute is present, otherwise false</returns>
38
 
        public override bool Matches(object actual)
39
 
        {
40
 
            this.actual = actual;
41
 
            System.Reflection.ICustomAttributeProvider attrProvider =
42
 
                actual as System.Reflection.ICustomAttributeProvider;
43
 
 
44
 
            if (attrProvider == null)
45
 
                throw new ArgumentException(string.Format("Actual value {0} does not implement ICustomAttributeProvider", actual), "actual");
46
 
 
47
 
            return attrProvider.GetCustomAttributes(expectedType, true).Length > 0;
48
 
        }
49
 
 
50
 
        /// <summary>
51
 
        /// Writes the description of the constraint to the specified writer
52
 
        /// </summary>
53
 
        public override void WriteDescriptionTo(MessageWriter writer)
54
 
        {
55
 
            writer.WritePredicate("type with attribute");
56
 
            writer.WriteExpectedValue(expectedType);
57
 
        }
58
 
    }
59
 
 
60
 
    /// <summary>
61
 
    /// AttributeConstraint tests that a specified attribute is present
62
 
    /// on a Type or other provider and that the value of the attribute
63
 
    /// satisfies some other constraint.
64
 
    /// </summary>
65
 
    public class AttributeConstraint : PrefixConstraint
66
 
    {
67
 
        private Type expectedType;
68
 
        private Attribute attrFound;
69
 
 
70
 
        /// <summary>
71
 
        /// Constructs an AttributeConstraint for a specified attriute
72
 
        /// Type and base constraint.
73
 
        /// </summary>
74
 
        /// <param name="type"></param>
75
 
        /// <param name="baseConstraint"></param>
76
 
        public AttributeConstraint(Type type, Constraint baseConstraint)
77
 
            : base( baseConstraint )
78
 
        {
79
 
            this.expectedType = type;
80
 
 
81
 
            if (!typeof(Attribute).IsAssignableFrom(expectedType))
82
 
                throw new ArgumentException(string.Format(
83
 
                    "Type {0} is not an attribute", expectedType), "type");
84
 
        }
85
 
 
86
 
        /// <summary>
87
 
        /// Determines whether the Type or other provider has the 
88
 
        /// expected attribute and if its value matches the
89
 
        /// additional constraint specified.
90
 
        /// </summary>
91
 
        public override bool Matches(object actual)
92
 
        {
93
 
            this.actual = actual;
94
 
            System.Reflection.ICustomAttributeProvider attrProvider =
95
 
                actual as System.Reflection.ICustomAttributeProvider;
96
 
 
97
 
            if (attrProvider == null)
98
 
                throw new ArgumentException(string.Format("Actual value {0} does not implement ICustomAttributeProvider", actual), "actual");
99
 
 
100
 
            Attribute[] attrs = (Attribute[])attrProvider.GetCustomAttributes(expectedType, true);
101
 
            if (attrs.Length == 0)
102
 
                throw new ArgumentException(string.Format("Attribute {0} was not found", expectedType), "actual");
103
 
 
104
 
            this.attrFound = attrs[0];
105
 
            return baseConstraint.Matches(attrFound);
106
 
        }
107
 
 
108
 
        /// <summary>
109
 
        /// Writes a description of the attribute to the specified writer.
110
 
        /// </summary>
111
 
        public override void WriteDescriptionTo(MessageWriter writer)
112
 
        {
113
 
            writer.WritePredicate("attribute " + expectedType.FullName);
114
 
            if (baseConstraint != null)
115
 
            {
116
 
                if (baseConstraint is EqualConstraint)
117
 
                    writer.WritePredicate("equal to");
118
 
                baseConstraint.WriteDescriptionTo(writer);
119
 
            }
120
 
        }
121
 
 
122
 
        /// <summary>
123
 
        /// Writes the actual value supplied to the specified writer.
124
 
        /// </summary>
125
 
        public override void WriteActualValueTo(MessageWriter writer)
126
 
        {
127
 
            writer.WriteActualValue(attrFound);
128
 
        }
129
 
 
130
 
        /// <summary>
131
 
        /// Returns a string representation of the constraint.
132
 
        /// </summary>
133
 
        protected override string GetStringRepresentation()
134
 
        {
135
 
            return string.Format("<attribute {0} {1}>", expectedType, baseConstraint);
136
 
        }
137
 
    }
138
 
}