Smalltalk and Object Orientation: an Introduction - Free
Smalltalk and Object Orientation: an Introduction - Free
Smalltalk and Object Orientation: an Introduction - Free
You also want an ePaper? Increase the reach of your titles
YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.
23.4 Self testing<br />
An approach, suggested by Charles Weir of <strong>Object</strong> Designers Ltd., would be to provide facilities which<br />
enable code self-testing within <strong>Smalltalk</strong>. A simple technique is to make the code do its own self-testing<br />
using assertions. This is a code statement that is always true; it tests that the code is behaving as<br />
expected. If the statement is false the assertion stops processing to allow debugging.<br />
An implementation of assertions in <strong>Smalltalk</strong> is<br />
assert<br />
self value ifFalse: [self halt].<br />
This method should be added to the BlockContext class <strong><strong>an</strong>d</strong> allows the developer to verify<br />
assertions wherever required, for example:<br />
[r<strong>an</strong>ge first == 1] assert.<br />
Of course in the released version of the system, these tests ar e redund<strong>an</strong>t <strong><strong>an</strong>d</strong> waste processing time. So<br />
they could be replaced by a null version of the method:<br />
assert<br />
self.<br />
The overhead of the extra method dispatch is relatively small; of course if it is still signific<strong>an</strong>t, we<br />
could write a utility to find the senders of the method <strong><strong>an</strong>d</strong> to comment out the assertion statements.<br />
Assertions are particularly useful for testing preconditions, postconditions <strong><strong>an</strong>d</strong> class invari<strong>an</strong>ts. The<br />
Syntropy design method makes extensive use of such assertions [Cook <strong><strong>an</strong>d</strong> D<strong>an</strong>iels 1994 ]. It would<br />
therefore be possible to use the assertions identified during the design as the basis of the assertions to<br />
place in the <strong>Smalltalk</strong> code.<br />
This approach could be taken further with the introduction of universal qu<strong>an</strong>tifiers (there exists <strong><strong>an</strong>d</strong><br />
for al l). This would allow expression such as if there exists a value x. The introduction of such<br />
qu<strong>an</strong>tifiers is not without its problems but should be possible. In addition assertions could also be used<br />
to ensure that the relationships between pre <strong><strong>an</strong>d</strong> post values during some operation is appropriate. Again<br />
this would not necessarily be straight forward, but it should be possible.<br />
23.5 Summary<br />
Throughout the chapter, guid<strong>an</strong>ce has been given to ways to overcome the problems inherent in testing<br />
object oriented systems. In the next chapter we shall consider a simple set of extensions to the basic<br />
VisualWorks environment which greatly simplify the whole testing process.<br />
23.6 Further reading<br />
As there is so little written about testing object oriented systems, this section provides <strong>an</strong> extended set of<br />
references on the subject.<br />
In 1994 a special issue of the Communications of the ACM was published which focused on testing<br />
object oriented systems [Binder 1994]. In this special issue a number of papers cover topics related to<br />
object oriented testing. For example, [Binder 1994b] discusses design for testability in object oriented<br />
systems which is a particularly import<strong>an</strong>t issue given the effects of inherit<strong>an</strong>ce, polymorphism <strong><strong>an</strong>d</strong><br />
particularly encapsulation on testing.<br />
Other papers relev<strong>an</strong>t to object oriented testing include [Beck 1994], [Hoffm<strong>an</strong> <strong><strong>an</strong>d</strong> Strooper 1995],<br />
[Hunt 1995], [Jorgensen <strong><strong>an</strong>d</strong> Erickson 1994], [Siepm<strong>an</strong>n <strong><strong>an</strong>d</strong> Newton 1994].<br />
The workshops on testing held at the OOPSLA conferences are also a useful source of references.<br />
For example the workshop held as the OOPSLA ‘95 Conference. For more information contact Barbara<br />
Yates, OOPSLA Workshop, 2002 Parkside Court, West Linn, Oregon, 97068 -2767, USA (Email:<br />
barbara.bytesmiths@acm.org).<br />
193