Presentation is loading. Please wait.

Presentation is loading. Please wait.

© Oscar Nierstrasz ST — Smalltalk Basics 2.1 Change sets  Make sure your changes are logged to a new change set.

Similar presentations


Presentation on theme: "© Oscar Nierstrasz ST — Smalltalk Basics 2.1 Change sets  Make sure your changes are logged to a new change set."— Presentation transcript:

1 © Oscar Nierstrasz ST — Smalltalk Basics 2.1 Change sets  Make sure your changes are logged to a new change set

2 © Oscar Nierstrasz ST — Smalltalk Basics 2.2 SUnit

3 © Oscar Nierstrasz ST — Smalltalk Basics 2.3 Money TestCase subclass: #MoneyTest instanceVariableNames: 'chf2 chf8 chf10' classVariableNames: '' poolDictionaries: '' category: 'Money' TestCase subclass: #MoneyTest instanceVariableNames: '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 (!)

4 © Oscar Nierstrasz ST — Smalltalk Basics 2.4 SetUp  We will need setters for the private Money state MoneyTest>>setUp chf2 := Money new currency: 'CHF'; amount: 2. chf8 := Money new currency: 'CHF'; amount: 8. chf10 := Money new currency: 'CHF'; amount: 10. MoneyTest>>setUp chf2 := Money new currency: 'CHF'; amount: 2. chf8 := Money new currency: 'CHF'; amount: 8. chf10 := Money new currency: 'CHF'; amount: 10.

5 © Oscar Nierstrasz ST — Smalltalk Basics 2.5 MoneyTest>>testEquals  Some obvious tests MoneyTest>>testEquals self assert: chf2 = chf2. self assert: chf2 = (Money new currency: 'CHF'; amount: 2). self assert: chf2 != chf8. MoneyTest>>testEquals self assert: chf2 = chf2. self assert: chf2 = (Money new currency: 'CHF'; amount: 2). self assert: chf2 != chf8.

6 © Oscar Nierstrasz ST — Smalltalk Basics 2.6 Money  We define Money as a subclass of Object, with getters and setters Object subclass: #Money instanceVariableNames: 'currency amount' classVariableNames: '' poolDictionaries: '' category: 'Money' Object subclass: #Money instanceVariableNames: 'currency amount' classVariableNames: '' poolDictionaries: '' category: 'Money' Money>>currency: aString currency := aString. Money>>currency: aString currency := aString. Money>>currency ^ currency Money>>currency ^ currency

7 © Oscar Nierstrasz ST — Smalltalk Basics 2.7 Failing tests

8 © Oscar Nierstrasz ST — Smalltalk Basics 2.8 Comparisons Money>>= aMoney ^ self currency = aMoney currency and: [ self amount = aMoney amount ] Money>>= aMoney ^ self currency = aMoney currency and: [ self amount = aMoney amount ] Money>>!= aMoney ^ (self = aMoney) not Money>>!= aMoney ^ (self = aMoney) not

9 © Oscar Nierstrasz ST — Smalltalk Basics 2.9 Constructors MoneyTest>>testEquals self assert: chf2 = chf2. self assert: chf2 = (Money currency: 'CHF' amount: 2). self assert: chf2 != chf8. MoneyTest>>testEquals self 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

10 © Oscar Nierstrasz ST — Smalltalk Basics 2.10 Class methods NB: What “self” is referred to in the method body?

11 © 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>>testAdd self assert: chf2 + chf8 = chf10 MoneyTest>>testAdd self assert: chf2 + chf8 = chf10 And so on …

12 © 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

13 © Oscar Nierstrasz ST — Smalltalk Basics 2.13 License > http://creativecommons.org/licenses/by-sa/2.5/ Attribution-ShareAlike 2.5 You 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.5 You 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.


Download ppt "© Oscar Nierstrasz ST — Smalltalk Basics 2.1 Change sets  Make sure your changes are logged to a new change set."

Similar presentations


Ads by Google