29.11.2014 Views

Smalltalk and Object Orientation: an Introduction - Free

Smalltalk and Object Orientation: an Introduction - Free

Smalltalk and Object Orientation: an Introduction - Free

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

control the execution of the method in a number of ways. For example, a tester c<strong>an</strong> ch<strong>an</strong>ge the state of<br />

<strong>an</strong> object or the input parameter values using the inspectors at the bottom of the debugger. They c<strong>an</strong><br />

control the execution of the method either by stepping through the method a line at a time, descending<br />

into methods called by this method or proceeding to run the whole method as though the debugger had<br />

never been entered. By default the tester controls the execution of the method from the point<br />

immediately after the halt message. However, it is also possible to re-run the whole method.<br />

22.5.3.2 Subclassing the test class<br />

A more object oriented approach is to define a test subclass for the class being tested (this approach has<br />

already been mentioned above). The tester c<strong>an</strong> then define <strong>an</strong> appropriate set of test harness methods<br />

which exercise the class in the desired m<strong>an</strong>ner. As the test harness methods have the same ability to<br />

access the state of the object as the methods under test, it is possible to determin e the object’s state<br />

before <strong><strong>an</strong>d</strong> after a method is executed. The results of these tests c<strong>an</strong> then be written to the Tr<strong>an</strong>script <strong><strong>an</strong>d</strong><br />

/ or to a file. If the results are written to a file, the tests c<strong>an</strong> be re -run at a later date <strong><strong>an</strong>d</strong> the new results<br />

compared with the previous results. This is cle<strong>an</strong>er th<strong>an</strong> writing print statements within the source code<br />

of the methods to be tested, however it does not allow the internal operation of the methods to be<br />

monitored as closely.<br />

To monitor the actual execution of the desi red methods, it is possible to place a self halt.<br />

message just prior to <strong>an</strong>y super message. This invokes the debugger <strong><strong>an</strong>d</strong> allows the developer to step<br />

down into the super method’s code. For example:<br />

age<br />

self halt.<br />

super age.<br />

Of course, in practice a com bination of all of the above may be necessary to accomplish both black<br />

box <strong><strong>an</strong>d</strong> white box testing effectively.<br />

22.6 Polymorphism<br />

Polymorphism is the ability of objects to send the same message to inst<strong>an</strong>ces of different classes which<br />

do not have a common superclass. It is made possible by the dynamic binding of messages to objects.<br />

That is, it is only at run time that it is possible to determine to which object the messages will be sent.<br />

From the point of view of testing, there are a n umber of problems associated with polymorphism. Each<br />

of these is discussed separately below.<br />

22.6.1 Undecidability<br />

Polymorphism allows the programmer to specify which message will be sent to <strong>an</strong> object, but not which<br />

object will receive that message. This will onl y be determined at run time. It is therefore not possible to<br />

provide <strong>an</strong>y form of static check to see that the message will be “understood” by the object (other th<strong>an</strong><br />

checking that at least one class somewhere in the system will respond to that message). In addition,<br />

even when it is known that the object receiving the message will underst<strong><strong>an</strong>d</strong> the message (e.g. all<br />

objects in <strong>Smalltalk</strong> underst<strong><strong>an</strong>d</strong> printString), it is not possible to identify which version or<br />

implementation of the method will be executed. That is , the tester will know that the object will<br />

underst<strong><strong>an</strong>d</strong> the message printString, however, which version of printString will be used will depend on<br />

the class of the receiver. It might be that defined in class <strong>Object</strong>, that defined in class Collection<br />

etc.<br />

It is therefore impossible for the tester to know what would be <strong>an</strong> appropriate r<strong>an</strong>ge of tests for a<br />

polymorhpic message expression. They therefore have to make assumptions about the r<strong>an</strong>ge of objects<br />

which are likely to be sent the message. In some cases this r<strong>an</strong>ge may be very large indeed. In such<br />

situations it is necessary to produce one or more class hierarchies for different implementations of the<br />

method. These trees c<strong>an</strong> then be used to identify those classes in which new definitions of the method<br />

are provi ded. The assumptions being used in the tests should, of course, be made explicit in the test<br />

report, so that they c<strong>an</strong> be challenged if it is felt that they were inappropriate.<br />

187

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!