1
// ***********************************************************************
2
// Copyright (c) 2008 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
// ***********************************************************************
26
namespace NUnit.Framework.Constraints
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.
33
public class AttributeConstraint : PrefixConstraint
35
private readonly Type expectedType;
36
private Attribute attrFound;
39
/// Constructs an AttributeConstraint for a specified attriute
40
/// Type and base constraint.
42
/// <param name="type"></param>
43
/// <param name="baseConstraint"></param>
44
public AttributeConstraint(Type type, Constraint baseConstraint)
45
: base(baseConstraint)
47
this.expectedType = type;
49
if (!typeof(Attribute).IsAssignableFrom(expectedType))
50
throw new ArgumentException(string.Format(
51
"Type {0} is not an attribute", expectedType), "type");
55
/// Determines whether the Type or other provider has the
56
/// expected attribute and if its value matches the
57
/// additional constraint specified.
59
public override bool Matches(object actual)
62
System.Reflection.ICustomAttributeProvider attrProvider =
63
actual as System.Reflection.ICustomAttributeProvider;
65
if (attrProvider == null)
66
throw new ArgumentException(string.Format("Actual value {0} does not implement ICustomAttributeProvider", actual), "actual");
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");
73
return baseConstraint.Matches(attrFound);
77
/// Writes a description of the attribute to the specified writer.
79
public override void WriteDescriptionTo(MessageWriter writer)
81
writer.WritePredicate("attribute " + expectedType.FullName);
82
if (baseConstraint != null)
84
if (baseConstraint is EqualConstraint)
85
writer.WritePredicate("equal to");
86
baseConstraint.WriteDescriptionTo(writer);
91
/// Writes the actual value supplied to the specified writer.
93
public override void WriteActualValueTo(MessageWriter writer)
95
writer.WriteActualValue(attrFound);
99
/// Returns a string representation of the constraint.
101
protected override string GetStringRepresentation()
103
return string.Format("<attribute {0} {1}>", expectedType, baseConstraint);
b'\\ No newline at end of file'