CS 535 Object-Oriented Programming Spring Semester, 2003 Object, Debugging, Testing |
||
---|---|---|
© 2003, All Rights Reserved, SDSU & Roger Whitney San Diego State University -- This page last updated 05-Feb-03 |
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
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
Two GUI Interfaces for viewing Test Results
Loading Browser SUnit Extensions
Open the Parcel Manager by selecting the “Parcel Manager” item in the “System” menu of the launcher.
In the parcel manager:
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. The browser will expand it to the full name for you.
2. Make test methods
Running the Test using Browser SUnit Extensions
If you have loaded RBSUintExtensions you will see a Run button below the code pane in any method whose selector starts with “test” in a subclass of TestCase. Just click on the run button to run the test. It the test passes you will get a green bar below the code pane, if it fails you will get a red bar. If you get a red bar you can click on the “Debug” button to run the test in the debugger. When you have more tests, you can select just the class and the run button will run all the tests in the class.
How to Use TestRunner
TestRunner open
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.
2003 SDSU & Roger Whitney, 5500 Campanile Drive, San Diego, CA 92182-7700 USA.
OpenContent license defines the copyright on this document.
Previous    visitors since 05-Feb-03    Next