~ubuntu-branches/ubuntu/utopic/monodevelop/utopic

« back to all changes in this revision

Viewing changes to external/guiunit/src/framework/Constraints/AttributeConstraint.cs

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2013-10-10 14:50:04 UTC
  • mfrom: (10.3.4)
  • Revision ID: package-import@ubuntu.com-20131010145004-80l130sny21b17sb
Tags: 4.0.12+dfsg-1
* [5dcb6e1] Fix debian/watch for new source tarball name format
* [5c68cb5] Refresh list of files removed by get-orig-source to 
  reflect 4.0.12
* [96d60a0] Imported Upstream version 4.0.12+dfsg
* [b989752] Refresh debian/patches/no_appmenu to ensure it applies
* [2a4c351] Ensure every assembly in external/ is cleaned properly
* [92762f7] Add more excluded Mac-specific modulerefs
* [bc698ba] Add symlinks to NUnit assemblies (Closes: #714246)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// ***********************************************************************
 
2
// Copyright (c) 2008 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
 
 
24
using System;
 
25
 
 
26
namespace NUnit.Framework.Constraints
 
27
{
 
28
    /// <summary>
 
29
    /// AttributeConstraint tests that a specified attribute is present
 
30
    /// on a Type or other provider and that the value of the attribute
 
31
    /// satisfies some other constraint.
 
32
    /// </summary>
 
33
    public class AttributeConstraint : PrefixConstraint
 
34
    {
 
35
        private readonly Type expectedType;
 
36
        private Attribute attrFound;
 
37
 
 
38
        /// <summary>
 
39
        /// Constructs an AttributeConstraint for a specified attriute
 
40
        /// Type and base constraint.
 
41
        /// </summary>
 
42
        /// <param name="type"></param>
 
43
        /// <param name="baseConstraint"></param>
 
44
        public AttributeConstraint(Type type, Constraint baseConstraint)
 
45
            : base(baseConstraint)
 
46
        {
 
47
            this.expectedType = type;
 
48
 
 
49
            if (!typeof(Attribute).IsAssignableFrom(expectedType))
 
50
                throw new ArgumentException(string.Format(
 
51
                    "Type {0} is not an attribute", expectedType), "type");
 
52
        }
 
53
 
 
54
        /// <summary>
 
55
        /// Determines whether the Type or other provider has the 
 
56
        /// expected attribute and if its value matches the
 
57
        /// additional constraint specified.
 
58
        /// </summary>
 
59
        public override bool Matches(object actual)
 
60
        {
 
61
            this.actual = actual;
 
62
            System.Reflection.ICustomAttributeProvider attrProvider =
 
63
                actual as System.Reflection.ICustomAttributeProvider;
 
64
 
 
65
            if (attrProvider == null)
 
66
                throw new ArgumentException(string.Format("Actual value {0} does not implement ICustomAttributeProvider", actual), "actual");
 
67
 
 
68
            Attribute[] attrs = (Attribute[])attrProvider.GetCustomAttributes(expectedType, true);
 
69
            if (attrs.Length == 0)
 
70
                throw new ArgumentException(string.Format("Attribute {0} was not found", expectedType), "actual");
 
71
 
 
72
            attrFound = attrs[0];
 
73
            return baseConstraint.Matches(attrFound);
 
74
        }
 
75
 
 
76
        /// <summary>
 
77
        /// Writes a description of the attribute to the specified writer.
 
78
        /// </summary>
 
79
        public override void WriteDescriptionTo(MessageWriter writer)
 
80
        {
 
81
            writer.WritePredicate("attribute " + expectedType.FullName);
 
82
            if (baseConstraint != null)
 
83
            {
 
84
                if (baseConstraint is EqualConstraint)
 
85
                    writer.WritePredicate("equal to");
 
86
                baseConstraint.WriteDescriptionTo(writer);
 
87
            }
 
88
        }
 
89
 
 
90
        /// <summary>
 
91
        /// Writes the actual value supplied to the specified writer.
 
92
        /// </summary>
 
93
        public override void WriteActualValueTo(MessageWriter writer)
 
94
        {
 
95
            writer.WriteActualValue(attrFound);
 
96
        }
 
97
 
 
98
        /// <summary>
 
99
        /// Returns a string representation of the constraint.
 
100
        /// </summary>
 
101
        protected override string GetStringRepresentation()
 
102
        {
 
103
            return string.Format("<attribute {0} {1}>", expectedType, baseConstraint);
 
104
        }
 
105
    }
 
106
}
 
 
b'\\ No newline at end of file'