CS 535 Object-Oriented Programming & Design Fall Semester, 2001 Object, Debugging, Testing |
||
---|---|---|
© 2001, All Rights Reserved, SDSU & Roger Whitney San Diego State University -- This page last updated 27-Sep-01 |
For more Information on:
Numbers
Object
All 'things' in Smalltalk are objects
Objects are created from classes
The class Object is the parent class of all classes
Object class contains common methods for all objects
Determines behavior for all objects
Some Important Behavior Defined in Object
printString
|
a |
|
Result
printed
|
Transcript
|
|
clear; |
|
print:
a isNil;
|
true |
cr; |
|
show:
a printString;
|
'nil'
|
cr; |
|
print:
a class;
|
UndefinedObject
|
cr. |
|
a
:= 5.
|
|
Transcript
|
|
print:
a isNil;
|
false
|
cr; |
|
show:
a printString;
|
5 |
cr; |
|
print:
a
|
5 |
cr; |
|
printString
Since implemented in Object all objects inherit the method
printString is sent to an object when
Object>>printString "Answer a String whose characters are a description of the receiver." | aStream | aStream := WriteStream on: (String new: 16). self printOn: aStream. ^aStream contents
Overriding Default printString Behavior
Implement printOn: not printString
printOn:'s argument is a stream
Stream Writing methods:
Example
Smalltalk.CS535 defineClass: #Counter superclass: #{Core.Object} indexedType: #none private: false instanceVariableNames: 'count ' classInstanceVariableNames: '' imports: '' category: 'Course-Examples'
CS535.Counter class methodsFor: 'instance creation'
new ^super new initialize
CS535.Counter class methodsFor: 'examples'
example "Counter example" | a | a := Counter new. a increase; increase. ^a count
CS535.Counter methodsFor: 'accessing'
count ^count
decrease count := count - 1
increase count := count + 1
CS535.Counter methodsFor: 'initialize'
initialize count := 0
CS535.Counter methodsFor: 'printing'
printOn: aStream aStream nextPutAll: 'Counter('; print: count; nextPutAll: ')'
Equality
All objects are allocated on the heap
Variables are references (like a pointer) to objects
A == B
| a b | a := SimpleCircle origin: (0 @ 0) radius: 1. b := a.
a := SimpleCircle origin: (0 @ 0) radius: 1. b := SimpleCircle origin: (2 @ 5) radius: 3.
a := SimpleCircle origin: (0 @ 0) radius: 1. b := SimpleCircle origin: (0 @ 0) radius: 1..
Defining the Meaning of =
Object
SimpleCircle Example
CS535.SimpleCircle methodsFor: 'comparing'
= aCircle ^(origin = aCircle origin) & (radius = aCircle radius)
hash ^origin hash + radius hash
Comparing floats for equality is dangerous
This is not a great hash function, but works as an example
Debugging
Ways debugger is called by
Code for Debugging Example
Smalltalk.CS535 defineClass: #Counter superclass: #{Core.Object} indexedType: #none private: false instanceVariableNames: 'count ' classInstanceVariableNames: '' imports: '' category: 'Course-Examples'
Class method new ^super new initialize
Instance Methods count ^count
decrease self increment: 1 "Note the error"
increase self increment: 1
increment: anInteger self halt. count := count + anInteger
initialize count := 0
Sample Test code in Workspace
Execute the following code with "Do it"
| count | count := Counter new. count decrease; decrease; increase.
When the statement "self halt" is executed you will see a window like:
When you click on the debug button, you get the debugger. The debugger looks like:
Debugger Components Execution Stack Pane
Instance Variable List Pane
Testing
Johnson's Law
If it is not tested it does not work
Types of tests
Why Unit Testing
If it is not tested it does not work
The more time between coding and testing
Unit Tests Must be Easy To Run
Must be able to
Testing First
First write the tests
Then write the code to be tested
Writing tests first:
SUnit
Testing framework for automating running of unit tests in Smalltalk
In SUnit
How to Use SUnit
1. Make test class a subclass of TestCase
Smalltalk.CS535 defineClass: #TestCounter superclass: #{XProgramming.SUnit.TestCase} indexedType: #none private: false instanceVariableNames: '' classInstanceVariableNames: '' imports: '' category: 'Course-Examples'
You do not have to remember the full name of TestCase. Just use the name TestCase and it will be expand the full name for you.
2. Make test methods
How to Use SUnit 3. Start TestRunner
TestRunner open
4. Select test class and click on "Run"
TestCase methods of interest
Methods to assert conditions:
assert: aBooleanExpression deny: aBooleanExpression should: [aBooleanExpression] should: [aBooleanExpression] raise: AnExceptionClass shouldnt: [aBooleanExpression] shouldnt: [aBooleanExpression] raise: AnExceptionClass signalFailure: aString setUp
tearDown
More Test Examples to Show Syntax
Smalltalk.CS535 defineClass: #TestCounter superclass: #{XProgramming.SUnit.TestCase} indexedType: #none private: false instanceVariableNames: 'counter ' classInstanceVariableNames: '' imports: '' category: 'Course-Examples'
CS535.TestCounter methodsFor: 'testing'
setUp counter := Counter new.
tearDown counter := nil.
testDecrease counter decrease. self assert: counter count = -1.
testDecreaseWithShould "Just an example to show should: syntax" counter decrease. self should: [counter count = -1].
testIncrease self deny: counter isNil. counter increase. self assert: counter count = 1.
testZeroDivide "Just an example to show should:raise: syntax" self should: [1/0] raise: ZeroDivide. self shouldnt: [1/2] raise: ZeroDivide
Copyright ©, All rights reserved.
2000 SDSU & Roger Whitney, 5500 Campanile Drive, San Diego, CA 92182-7700 USA.
OpenContent license defines the copyright on this document.
Previous    visitors since 27-Sep-01    Next