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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

From the scenario that a person might have a birthday <strong><strong>an</strong>d</strong> therefore receive the message birthday,<br />

<strong><strong>an</strong>d</strong> from examining the source code of birthday (<strong><strong>an</strong>d</strong> incrementAge) we c<strong>an</strong> obtain the thread in<br />

Figure 23.2.<br />

birthday<br />

incrementAge<br />

age<br />

age:<br />

age inst<strong>an</strong>ce<br />

variable<br />

Figure 23.2: A thread of execution through <strong>an</strong> object<br />

23.1.3 <strong>Object</strong> state<br />

Another point to bear in mind when testing a class, is that both inst<strong>an</strong>ces <strong><strong>an</strong>d</strong> classes have state. For<br />

example, a class may have a class in st<strong>an</strong>ce variable which is used to keep a record of the number of<br />

inst<strong>an</strong>ces which have been created of that class. This information may be used purely for book keeping<br />

or may be used to limit the number of inst<strong>an</strong>ces created. In the latter case, a test needs to be performed<br />

which sets the counter to its limit <strong><strong>an</strong>d</strong> then a new inst<strong>an</strong>ce should be requested. This could be done,<br />

either by creating the required number of inst<strong>an</strong>ces, or by m<strong>an</strong>ually setting the counter to the<br />

appropriate value (e.g. by using the debugger or inspector).<br />

It is also import<strong>an</strong>t to remember that although a method has been tested in the context of a state<br />

inst<strong>an</strong>ce, it is possible that some unintended sequence of message sends could result in the method being<br />

executed with the object in <strong>an</strong> unin tended state. It is therefore not possible to test individual methods in<br />

isolation thoroughly, nor is it sensible to ignore the state of the object when performing these tests. It is<br />

of course quite possible to ignore the state, however the tester does so at their peril.<br />

23.1.4 Private methods<br />

Some methods are defined such that the user of the class is never expected to see them. This may be<br />

because they do some housekeeping which is local to the object (for example the initialization method)<br />

or because they provide facilities which are relev<strong>an</strong>t to the internals of the object <strong><strong>an</strong>d</strong> not to the external<br />

interface to the object In <strong>Smalltalk</strong>, this method hiding is not enforced, it is usually left implicit (unlike<br />

C++ or Java in which the equivalent of privat e methods really are private <strong><strong>an</strong>d</strong> are not available as<br />

external messages). For example, in VisualWorks <strong>Smalltalk</strong> this c<strong>an</strong> be done by placing the appropriate<br />

set of methods in a protocol entitled private or private-*. The convention is that a user of this class is not<br />

expected to use or to need to know about these methods. This is useful from a testing point of view as<br />

the tester c<strong>an</strong> test the individual execution of these methods in a black box fashion. They c<strong>an</strong> also use<br />

the approaches described above to perf orm white box testing. However, it also me<strong>an</strong>s that a user of the<br />

class, if they so wish, c<strong>an</strong> call these methods. As this is not what is intended it is arguably reasonable to<br />

ignore this scenario. However, it may be wise in some situations at least to know what the effects will<br />

be <strong><strong>an</strong>d</strong> whether the object’s methods will respond in a me<strong>an</strong>ingful or at least well defined m<strong>an</strong>ner. For<br />

example, it may be useful to know what will happen if <strong>an</strong>other object sends the message initialize to the<br />

current object at some point after the object has started to be used.<br />

23.1.5 Pool variables<br />

An additional problem with testing classes (as opposed to unit testing in other l<strong>an</strong>guages) is the presence<br />

of pool variables. These are rather like limited scope global variables. They are common to a set of<br />

classes <strong><strong>an</strong>d</strong> may affect the operation of these classes. As <strong>an</strong>y one of the classes associated with the pool<br />

c<strong>an</strong> ch<strong>an</strong>ge their value it is possible for one class (or its inst<strong>an</strong>ces) to have a profound effect on the<br />

operation of a completely different class or its inst<strong>an</strong>ces elsewhere in the system. This is something<br />

which requires careful thought.<br />

191

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

Saved successfully!

Ooh no, something went wrong!