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

« back to all changes in this revision

Viewing changes to doc/customConstraints.html

  • 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
 
<!-- saved from url=(0014)about:internet --><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN">
2
 
<html>
3
 
<!-- Standard Head Part -->
4
 
<head>
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">
11
 
</head>
12
 
<!-- End Standard Head Part -->
13
 
 
14
 
<body>
15
 
 
16
 
<!-- Standard Header for NUnit.org -->
17
 
<div id="header">
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">
 
2
<html>
 
3
<!-- Standard Head Part -->
 
4
<head>
 
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">
 
11
</head>
 
12
<!-- End Standard Head Part -->
 
13
 
 
14
<body>
 
15
 
 
16
<!-- Standard Header for NUnit.org -->
 
17
<div id="header">
 
18
  <a id="logo" href="http://www.nunit.org"><img src="img/logo.gif" alt="NUnit.org" title="NUnit.org"></a>
19
19
  <div id="nav">
20
20
    <a href="http://www.nunit.org">NUnit</a>
21
21
    <a class="active" href="index.html">Documentation</a>
22
 
  </div>
23
 
</div>
24
 
<!-- End of Header -->
25
 
 
26
 
<div id="content">
27
 
 
28
 
<h2>Custom Constraints (NUnit 2.4 / 2.5)</h2>
29
 
 
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:
35
 
   
36
 
<div class="code" style="width: 36em">
37
 
<pre>public abstract class Constraint
38
 
{
39
 
        ...
40
 
    public abstract bool Matches( object actual );
41
 
    public virtual bool Matches( ActualValueDelegate del );
42
 
    public virtual bool Matches&lt;T&gt;( ref T actual );
43
 
    public abstract void WriteDescriptionTo( MessageWriter writer );
44
 
    public virtual void WriteMessageTo( MessageWriter writer );
45
 
    public virtual void WriteActualValueTo( MessageWriter writer );
46
 
        ...
47
 
}</pre>
48
 
</div>   
49
 
 
50
 
<p>Your derived class should save the actual argument to Matches in the protected
51
 
field <b>actual</b> for later use.
52
 
 
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.
57
 
 
58
 
<h3>Custom Constraint Syntax</h3>
59
 
 
60
 
<p>Having written a custom constraint class, you can use it directly through its constructor:
61
 
 
62
 
<div class="code">
63
 
<pre>Assert.That(myObject, new CustomConstraint());</pre>
64
 
</div>
65
 
 
66
 
<p>You may also use it in expressions through NUnit's <b>Matches</b> syntax element:
67
 
<div class="code">
68
 
<pre>Assert.That(myObject, Is.Not.Null.And.Matches(new CustomConstraint());</pre>
69
 
</div>
70
 
 
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...
74
 
 
75
 
<div class="code">
76
 
<pre>Assert.That( myArray, Is.All.InRange(1,100) );</pre>
77
 
</div>
78
 
 
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:
81
 
 
82
 
<ol>
83
 
 
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:
88
 
 
89
 
<div class="code">
90
 
<pre>Assert.That( myObject, Is.Custom(x,y) );</pre>
91
 
</div>
92
 
 
93
 
<li>Provide an extension method for NUnit's <b>ConstraintExpression</b>, allowing
94
 
you to write things like:
95
 
 
96
 
<div class="code">
97
 
<pre>Assert.That( myList, Is.Not.All.Custom(x,y) );</pre>
98
 
</div>
99
 
 
100
 
</ol>
101
 
 
102
 
 
103
 
</div>
104
 
 
105
 
<!-- Submenu -->
106
 
<div id="subnav">
107
 
<ul>
108
 
<li><a href="index.html">NUnit 2.6</a></li>
109
 
<ul>
110
 
<li><a href="getStarted.html">Getting&nbsp;Started</a></li>
111
 
<li><a href="writingTests.html">Writing&nbsp;Tests</a></li>
112
 
<li><a href="runningTests.html">Running&nbsp;Tests</a></li>
113
 
<li><a href="extensibility.html">Extensibility</a></li>
114
 
<ul>
115
 
<li id="current"><a href="customConstraints.html">Custom&nbsp;Constraints</a></li>
116
 
<li><a href="nunitAddins.html">NUnit&nbsp;Addins</a></li>
117
 
<li><a href="extensionTips.html">Tips&nbsp;for&nbsp;Extenders</a></li>
118
 
</ul>
119
 
<li><a href="releaseNotes.html">Release&nbsp;Notes</a></li>
120
 
<li><a href="samples.html">Samples</a></li>
121
 
<li><a href="license.html">License</a></li>
122
 
</ul>
123
 
<li><a href="vsTestAdapter.html">NUnit&nbsp;Test&nbsp;Adapter&nbsp;0.90</a></li>
124
 
<ul>
125
 
<li><a href="vsTestAdapterLicense.html">License</a></li>
126
 
</ul>
127
 
<li><a href="&r=2.6.html"></a></li>
128
 
<li><a href="&r=2.6.html"></a></li>
129
 
</ul>
130
 
</div>
131
 
<!-- End of Submenu -->
132
 
 
133
 
 
134
 
<!-- Standard Footer for NUnit.org -->
135
 
<div id="footer">
136
 
  Copyright &copy; 2012 Charlie Poole. All Rights Reserved.
137
 
</div>
138
 
<!-- End of Footer -->
139
 
 
140
 
</body>
141
 
</html>
 
22
  </div>
 
23
</div>
 
24
<!-- End of Header -->
 
25
 
 
26
<div id="content">
 
27
 
 
28
<h2>Custom Constraints (NUnit 2.4 / 2.5)</h2>
 
29
 
 
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:
 
35
   
 
36
<div class="code" style="width: 36em">
 
37
<pre>public abstract class Constraint
 
38
{
 
39
        ...
 
40
    public abstract bool Matches( object actual );
 
41
    public virtual bool Matches( ActualValueDelegate del );
 
42
    public virtual bool Matches&lt;T&gt;( ref T actual );
 
43
    public abstract void WriteDescriptionTo( MessageWriter writer );
 
44
    public virtual void WriteMessageTo( MessageWriter writer );
 
45
    public virtual void WriteActualValueTo( MessageWriter writer );
 
46
        ...
 
47
}</pre>
 
48
</div>   
 
49
 
 
50
<p>Your derived class should save the actual argument to Matches in the protected
 
51
field <b>actual</b> for later use.
 
52
 
 
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.
 
57
 
 
58
<h3>Custom Constraint Syntax</h3>
 
59
 
 
60
<p>Having written a custom constraint class, you can use it directly through its constructor:
 
61
 
 
62
<div class="code">
 
63
<pre>Assert.That(myObject, new CustomConstraint());</pre>
 
64
</div>
 
65
 
 
66
<p>You may also use it in expressions through NUnit's <b>Matches</b> syntax element:
 
67
<div class="code">
 
68
<pre>Assert.That(myObject, Is.Not.Null.And.Matches(new CustomConstraint());</pre>
 
69
</div>
 
70
 
 
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...
 
74
 
 
75
<div class="code">
 
76
<pre>Assert.That( myArray, Is.All.InRange(1,100) );</pre>
 
77
</div>
 
78
 
 
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:
 
81
 
 
82
<ol>
 
83
 
 
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:
 
88
 
 
89
<div class="code">
 
90
<pre>Assert.That( myObject, Is.Custom(x,y) );</pre>
 
91
</div>
 
92
 
 
93
<li>Provide an extension method for NUnit's <b>ConstraintExpression</b>, allowing
 
94
you to write things like:
 
95
 
 
96
<div class="code">
 
97
<pre>Assert.That( myList, Is.Not.All.Custom(x,y) );</pre>
 
98
</div>
 
99
 
 
100
</ol>
 
101
 
 
102
 
 
103
</div>
 
104
 
 
105
<!-- Submenu -->
 
106
<div id="subnav">
 
107
<ul>
 
108
<li><a href="index.html">NUnit 2.6.3</a></li>
 
109
<ul>
 
110
<li><a href="getStarted.html">Getting&nbsp;Started</a></li>
 
111
<li><a href="writingTests.html">Writing&nbsp;Tests</a></li>
 
112
<li><a href="runningTests.html">Running&nbsp;Tests</a></li>
 
113
<li><a href="extensibility.html">Extensibility</a></li>
 
114
<ul>
 
115
<li id="current"><a href="customConstraints.html">Custom&nbsp;Constraints</a></li>
 
116
<li><a href="nunitAddins.html">NUnit&nbsp;Addins</a></li>
 
117
<li><a href="extensionTips.html">Tips&nbsp;for&nbsp;Extenders</a></li>
 
118
</ul>
 
119
<li><a href="releaseNotes.html">Release&nbsp;Notes</a></li>
 
120
<li><a href="samples.html">Samples</a></li>
 
121
<li><a href="license.html">License</a></li>
 
122
</ul>
 
123
<li><a href="vsTestAdapter.html">NUnit&nbsp;Test&nbsp;Adapter</a></li>
 
124
<ul>
 
125
<li><a href="vsTestAdapterLicense.html">License</a></li>
 
126
<li><a href="vsTestAdapterReleaseNotes.html">Release&nbsp;Notes</a></li>
 
127
</ul>
 
128
<li><a href="&r=2.6.3.html"></a></li>
 
129
<li><a href="&r=2.6.3.html"></a></li>
 
130
</ul>
 
131
</div>
 
132
<!-- End of Submenu -->
 
133
 
 
134
 
 
135
<!-- Standard Footer for NUnit.org -->
 
136
<div id="footer">
 
137
  Copyright &copy; 2012 Charlie Poole. All Rights Reserved.
 
138
</div>
 
139
<!-- End of Footer -->
 
140
 
 
141
</body>
 
142
</html>