1
<!-- saved from url=(0014)about:internet --><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN">
3
<!-- Standard Head Part -->
5
<title>NUnit - CustomConstraints</title>
6
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
7
<meta http-equiv="Content-Language" content="en-US">
8
<meta name="norton-safeweb-site-verification" content="tb6xj01p4hgo5x-8wscsmq633y11-e6nhk-bnb5d987bseanyp6p0uew-pec8j963qlzj32k5x9h3r2q7wh-vmy8bbhek5lnpp5w4p8hocouuq39e09jrkihdtaeknua" />
9
<link rel="stylesheet" type="text/css" href="nunit.css">
10
<link rel="shortcut icon" href="favicon.ico">
12
<!-- End Standard Head Part -->
16
<!-- Standard Header for NUnit.org -->
18
<a id="logo" href="http://www.nunit.org"><img src="img/logo.gif" alt="NUnit.org" title="NUnit.org"></a>
1
<!-- saved from url=(0014)about:internet --><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN">
3
<!-- Standard Head Part -->
5
<title>NUnit - CustomConstraints</title>
6
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
7
<meta http-equiv="Content-Language" content="en-US">
8
<meta name="norton-safeweb-site-verification" content="tb6xj01p4hgo5x-8wscsmq633y11-e6nhk-bnb5d987bseanyp6p0uew-pec8j963qlzj32k5x9h3r2q7wh-vmy8bbhek5lnpp5w4p8hocouuq39e09jrkihdtaeknua" />
9
<link rel="stylesheet" type="text/css" href="nunit.css">
10
<link rel="shortcut icon" href="favicon.ico">
12
<!-- End Standard Head Part -->
16
<!-- Standard Header for NUnit.org -->
18
<a id="logo" href="http://www.nunit.org"><img src="img/logo.gif" alt="NUnit.org" title="NUnit.org"></a>
20
20
<a href="http://www.nunit.org">NUnit</a>
21
21
<a class="active" href="index.html">Documentation</a>
24
<!-- End of Header -->
28
<h2>Custom Constraints (NUnit 2.4 / 2.5)</h2>
30
<p>You can implement your own custom constraints by creating a class that
31
inherits from the <b>Constraint</b> abstract class, which supports performing a
32
test on an actual value and generating appropriate messages. The class includes
33
two abstract methods, which you must override and four virtual methods with
34
default implementation that may be overridden as needed:
36
<div class="code" style="width: 36em">
37
<pre>public abstract class Constraint
40
public abstract bool Matches( object actual );
41
public virtual bool Matches( ActualValueDelegate del );
42
public virtual bool Matches<T>( ref T actual );
43
public abstract void WriteDescriptionTo( MessageWriter writer );
44
public virtual void WriteMessageTo( MessageWriter writer );
45
public virtual void WriteActualValueTo( MessageWriter writer );
50
<p>Your derived class should save the actual argument to Matches in the protected
51
field <b>actual</b> for later use.
53
<p>The MessageWriter abstract class is implemented in the framework by
54
TextMessageWriter. Examining the source for some of the builtin constraints
55
should give you a good idea of how to use it if you have special formatting
56
requirements for error messages.
58
<h3>Custom Constraint Syntax</h3>
60
<p>Having written a custom constraint class, you can use it directly through its constructor:
63
<pre>Assert.That(myObject, new CustomConstraint());</pre>
66
<p>You may also use it in expressions through NUnit's <b>Matches</b> syntax element:
68
<pre>Assert.That(myObject, Is.Not.Null.And.Matches(new CustomConstraint());</pre>
71
<p>The direct construction approach is not very convenient or easy to read.
72
For its built-in constraints, NUnit includes classes that implement a special
73
constraint syntax, allowing you to write things like...
76
<pre>Assert.That( myArray, Is.All.InRange(1,100) );</pre>
79
<p>Ideally, that's what you would like to do with the custom constraint as well.
80
To accomplish this, two separate steps are required:
84
<li>Provide a static class patterned after NUnit's <b>Is</b> class, with properties
85
or methods that constuct your custom constructor. If you like, you can even call it
86
<b>Is</b>, provided you place it in your own namespace and avoid any conflicts. This
87
allows you to write things like:
90
<pre>Assert.That( myObject, Is.Custom(x,y) );</pre>
93
<li>Provide an extension method for NUnit's <b>ConstraintExpression</b>, allowing
94
you to write things like:
97
<pre>Assert.That( myList, Is.Not.All.Custom(x,y) );</pre>
108
<li><a href="index.html">NUnit 2.6</a></li>
110
<li><a href="getStarted.html">Getting Started</a></li>
111
<li><a href="writingTests.html">Writing Tests</a></li>
112
<li><a href="runningTests.html">Running Tests</a></li>
113
<li><a href="extensibility.html">Extensibility</a></li>
115
<li id="current"><a href="customConstraints.html">Custom Constraints</a></li>
116
<li><a href="nunitAddins.html">NUnit Addins</a></li>
117
<li><a href="extensionTips.html">Tips for Extenders</a></li>
119
<li><a href="releaseNotes.html">Release Notes</a></li>
120
<li><a href="samples.html">Samples</a></li>
121
<li><a href="license.html">License</a></li>
123
<li><a href="vsTestAdapter.html">NUnit Test Adapter 0.90</a></li>
125
<li><a href="vsTestAdapterLicense.html">License</a></li>
127
<li><a href="&r=2.6.html"></a></li>
128
<li><a href="&r=2.6.html"></a></li>
131
<!-- End of Submenu -->
134
<!-- Standard Footer for NUnit.org -->
136
Copyright © 2012 Charlie Poole. All Rights Reserved.
138
<!-- End of Footer -->
24
<!-- End of Header -->
28
<h2>Custom Constraints (NUnit 2.4 / 2.5)</h2>
30
<p>You can implement your own custom constraints by creating a class that
31
inherits from the <b>Constraint</b> abstract class, which supports performing a
32
test on an actual value and generating appropriate messages. The class includes
33
two abstract methods, which you must override and four virtual methods with
34
default implementation that may be overridden as needed:
36
<div class="code" style="width: 36em">
37
<pre>public abstract class Constraint
40
public abstract bool Matches( object actual );
41
public virtual bool Matches( ActualValueDelegate del );
42
public virtual bool Matches<T>( ref T actual );
43
public abstract void WriteDescriptionTo( MessageWriter writer );
44
public virtual void WriteMessageTo( MessageWriter writer );
45
public virtual void WriteActualValueTo( MessageWriter writer );
50
<p>Your derived class should save the actual argument to Matches in the protected
51
field <b>actual</b> for later use.
53
<p>The MessageWriter abstract class is implemented in the framework by
54
TextMessageWriter. Examining the source for some of the builtin constraints
55
should give you a good idea of how to use it if you have special formatting
56
requirements for error messages.
58
<h3>Custom Constraint Syntax</h3>
60
<p>Having written a custom constraint class, you can use it directly through its constructor:
63
<pre>Assert.That(myObject, new CustomConstraint());</pre>
66
<p>You may also use it in expressions through NUnit's <b>Matches</b> syntax element:
68
<pre>Assert.That(myObject, Is.Not.Null.And.Matches(new CustomConstraint());</pre>
71
<p>The direct construction approach is not very convenient or easy to read.
72
For its built-in constraints, NUnit includes classes that implement a special
73
constraint syntax, allowing you to write things like...
76
<pre>Assert.That( myArray, Is.All.InRange(1,100) );</pre>
79
<p>Ideally, that's what you would like to do with the custom constraint as well.
80
To accomplish this, two separate steps are required:
84
<li>Provide a static class patterned after NUnit's <b>Is</b> class, with properties
85
or methods that constuct your custom constructor. If you like, you can even call it
86
<b>Is</b>, provided you place it in your own namespace and avoid any conflicts. This
87
allows you to write things like:
90
<pre>Assert.That( myObject, Is.Custom(x,y) );</pre>
93
<li>Provide an extension method for NUnit's <b>ConstraintExpression</b>, allowing
94
you to write things like:
97
<pre>Assert.That( myList, Is.Not.All.Custom(x,y) );</pre>
108
<li><a href="index.html">NUnit 2.6.3</a></li>
110
<li><a href="getStarted.html">Getting Started</a></li>
111
<li><a href="writingTests.html">Writing Tests</a></li>
112
<li><a href="runningTests.html">Running Tests</a></li>
113
<li><a href="extensibility.html">Extensibility</a></li>
115
<li id="current"><a href="customConstraints.html">Custom Constraints</a></li>
116
<li><a href="nunitAddins.html">NUnit Addins</a></li>
117
<li><a href="extensionTips.html">Tips for Extenders</a></li>
119
<li><a href="releaseNotes.html">Release Notes</a></li>
120
<li><a href="samples.html">Samples</a></li>
121
<li><a href="license.html">License</a></li>
123
<li><a href="vsTestAdapter.html">NUnit Test Adapter</a></li>
125
<li><a href="vsTestAdapterLicense.html">License</a></li>
126
<li><a href="vsTestAdapterReleaseNotes.html">Release Notes</a></li>
128
<li><a href="&r=2.6.3.html"></a></li>
129
<li><a href="&r=2.6.3.html"></a></li>
132
<!-- End of Submenu -->
135
<!-- Standard Footer for NUnit.org -->
137
Copyright © 2012 Charlie Poole. All Rights Reserved.
139
<!-- End of Footer -->