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
// ****************************************************************
9
namespace NUnit.Framework.Constraints
12
/// AttributeExistsConstraint tests for the presence of a
13
/// specified attribute on a Type.
15
public class AttributeExistsConstraint : Constraint
17
private Type expectedType;
20
/// Constructs an AttributeExistsConstraint for a specific attribute Type
22
/// <param name="type"></param>
23
public AttributeExistsConstraint(Type type)
26
this.expectedType = type;
28
if (!typeof(Attribute).IsAssignableFrom(expectedType))
29
throw new ArgumentException(string.Format(
30
"Type {0} is not an attribute", expectedType), "type");
34
/// Tests whether the object provides the expected attribute.
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)
41
System.Reflection.ICustomAttributeProvider attrProvider =
42
actual as System.Reflection.ICustomAttributeProvider;
44
if (attrProvider == null)
45
throw new ArgumentException(string.Format("Actual value {0} does not implement ICustomAttributeProvider", actual), "actual");
47
return attrProvider.GetCustomAttributes(expectedType, true).Length > 0;
51
/// Writes the description of the constraint to the specified writer
53
public override void WriteDescriptionTo(MessageWriter writer)
55
writer.WritePredicate("type with attribute");
56
writer.WriteExpectedValue(expectedType);
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.
65
public class AttributeConstraint : PrefixConstraint
67
private Type expectedType;
68
private Attribute attrFound;
71
/// Constructs an AttributeConstraint for a specified attriute
72
/// Type and base constraint.
74
/// <param name="type"></param>
75
/// <param name="baseConstraint"></param>
76
public AttributeConstraint(Type type, Constraint baseConstraint)
77
: base( baseConstraint )
79
this.expectedType = type;
81
if (!typeof(Attribute).IsAssignableFrom(expectedType))
82
throw new ArgumentException(string.Format(
83
"Type {0} is not an attribute", expectedType), "type");
87
/// Determines whether the Type or other provider has the
88
/// expected attribute and if its value matches the
89
/// additional constraint specified.
91
public override bool Matches(object actual)
94
System.Reflection.ICustomAttributeProvider attrProvider =
95
actual as System.Reflection.ICustomAttributeProvider;
97
if (attrProvider == null)
98
throw new ArgumentException(string.Format("Actual value {0} does not implement ICustomAttributeProvider", actual), "actual");
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");
104
this.attrFound = attrs[0];
105
return baseConstraint.Matches(attrFound);
109
/// Writes a description of the attribute to the specified writer.
111
public override void WriteDescriptionTo(MessageWriter writer)
113
writer.WritePredicate("attribute " + expectedType.FullName);
114
if (baseConstraint != null)
116
if (baseConstraint is EqualConstraint)
117
writer.WritePredicate("equal to");
118
baseConstraint.WriteDescriptionTo(writer);
123
/// Writes the actual value supplied to the specified writer.
125
public override void WriteActualValueTo(MessageWriter writer)
127
writer.WriteActualValue(attrFound);
131
/// Returns a string representation of the constraint.
133
protected override string GetStringRepresentation()
135
return string.Format("<attribute {0} {1}>", expectedType, baseConstraint);