View
215
Download
0
Embed Size (px)
Citation preview
© Oscar Nierstrasz
ST — Smalltalk Basics
2.1
Change sets
> Make sure your changes are logged to a new change set
© Oscar Nierstrasz
ST — Smalltalk Basics
2.2
SUnit
© Oscar Nierstrasz
ST — Smalltalk Basics
2.3
Money
TestCase subclass: #MoneyTestinstanceVariableNames: 'chf2 chf8 chf10'classVariableNames: ''poolDictionaries: ''category: 'Money'
TestCase subclass: #MoneyTestinstanceVariableNames: 'chf2 chf8 chf10'classVariableNames: ''poolDictionaries: ''category: 'Money'
> We will implement the Money example in Smalltalk— First, we develop a test case for a single currency
NB: This is just a message sent to the TestCase class object (!)
© Oscar Nierstrasz
ST — Smalltalk Basics
2.4
SetUp
> We will need setters for the private Money state
MoneyTest>>setUpchf2 := Money new currency: 'CHF'; amount: 2.chf8 := Money new currency: 'CHF'; amount: 8.chf10 := Money new currency: 'CHF'; amount: 10.
MoneyTest>>setUpchf2 := Money new currency: 'CHF'; amount: 2.chf8 := Money new currency: 'CHF'; amount: 8.chf10 := Money new currency: 'CHF'; amount: 10.
© Oscar Nierstrasz
ST — Smalltalk Basics
2.5
MoneyTest>>testEquals
> Some obvious tests
MoneyTest>>testEqualsself assert: chf2 = chf2.self assert: chf2 = (Money new currency: 'CHF'; amount: 2).self assert: chf2 != chf8.
MoneyTest>>testEqualsself assert: chf2 = chf2.self assert: chf2 = (Money new currency: 'CHF'; amount: 2).self assert: chf2 != chf8.
© Oscar Nierstrasz
ST — Smalltalk Basics
2.6
Money
> We define Money as a subclass of Object, with getters and setters
Object subclass: #MoneyinstanceVariableNames: 'currency amount'classVariableNames: ''poolDictionaries: ''category: 'Money'
Object subclass: #MoneyinstanceVariableNames: 'currency amount'classVariableNames: ''poolDictionaries: ''category: 'Money'
Money>>currency: aStringcurrency := aString.
Money>>currency: aStringcurrency := aString.
Money>>currency^ currency
Money>>currency^ currency
© Oscar Nierstrasz
ST — Smalltalk Basics
2.7
Failing tests
© Oscar Nierstrasz
ST — Smalltalk Basics
2.8
Comparisons
Money>>= aMoney^ self currency = aMoney currencyand: [ self amount = aMoney amount ]
Money>>= aMoney^ self currency = aMoney currencyand: [ self amount = aMoney amount ]
Money>>!= aMoney^ (self = aMoney) not
Money>>!= aMoney^ (self = aMoney) not
© Oscar Nierstrasz
ST — Smalltalk Basics
2.9
Constructors
MoneyTest>>testEqualsself assert: chf2 = chf2.self assert: chf2 =
(Money currency: 'CHF' amount: 2).self assert: chf2 != chf8.
MoneyTest>>testEqualsself assert: chf2 = chf2.self assert: chf2 =
(Money currency: 'CHF' amount: 2).self assert: chf2 != chf8.
We need a constructor on the class side of Money
QuickTime™ and aTIFF (Uncompressed) decompressor
are needed to see this picture.
© Oscar Nierstrasz
ST — Smalltalk Basics
2.10
Class methods
NB: What “self” is referred to in the method body?
© Oscar Nierstrasz
ST — Smalltalk Basics
2.11
Addition
Money>>+ aMoney^ Money currency: self currency
amount: self amount + aMoney amount
Money>>+ aMoney^ Money currency: self currency
amount: self amount + aMoney amount
MoneyTest>>testAddself assert: chf2 + chf8 = chf10
MoneyTest>>testAddself assert: chf2 + chf8 = chf10
And so on …
© Oscar Nierstrasz
ST — Smalltalk Basics
2.12
Filing out your changes
> You can “file out” all your changes so they can be loaded into another image
© Oscar Nierstrasz
ST — Smalltalk Basics
2.13
License
> http://creativecommons.org/licenses/by-sa/2.5/
Attribution-ShareAlike 2.5You are free:• to copy, distribute, display, and perform the work• to make derivative works• to make commercial use of the work
Under the following conditions:
Attribution. You must attribute the work in the manner specified by the author or licensor.
Share Alike. If you alter, transform, or build upon this work, you may distribute the resulting work only under a license identical to this one.
• For any reuse or distribution, you must make clear to others the license terms of this work.• Any of these conditions can be waived if you get permission from the copyright holder.
Your fair use and other rights are in no way affected by the above.
Attribution-ShareAlike 2.5You are free:• to copy, distribute, display, and perform the work• to make derivative works• to make commercial use of the work
Under the following conditions:
Attribution. You must attribute the work in the manner specified by the author or licensor.
Share Alike. If you alter, transform, or build upon this work, you may distribute the resulting work only under a license identical to this one.
• For any reuse or distribution, you must make clear to others the license terms of this work.• Any of these conditions can be waived if you get permission from the copyright holder.
Your fair use and other rights are in no way affected by the above.