Object subclass: #LSStepCounter
	instanceVariableNames: 'count'
	classVariableNames: ''
	poolDictionaries: ''
	category: 'LearnSqueak-Refactoring'!

!LSStepCounter methodsFor: 'initialization'!
initialize
	super initialize.
	count := 0
! !

!LSStepCounter methodsFor: 'accessing'!
count
	^ count
! !

!LSStepCounter methodsFor: 'changing'!
add: amount
	count := count + amount
! !

!LSStepCounter methodsFor: 'changing'!
reset
	count := 0
! !

TestCase subclass: #LSStepCounterTest
	instanceVariableNames: 'counter'
	classVariableNames: ''
	poolDictionaries: ''
	category: 'LearnSqueak-Refactoring'!

!LSStepCounterTest methodsFor: 'running'!
setUp
	super setUp.
	counter := LSStepCounter new
! !

!LSStepCounterTest methodsFor: 'tests'!
testStartsAtZero
	self assert: counter count equals: 0
! !

!LSStepCounterTest methodsFor: 'tests'!
testAddsOneThenFive
	counter add: 1.
	counter add: 5.
	self assert: counter count equals: 6
! !

!LSStepCounterTest methodsFor: 'tests'!
testResetPreservesOriginalBehavior
	counter add: 1; add: 5; reset.
	self assert: counter count equals: 0
! !

!LSStepCounterTest methodsFor: 'tests'!
testTwoCountersAreIndependent
	| other |
	other := LSStepCounter new.
	counter add: 5.
	self assert: other count equals: 0
! !

Morph subclass: #LSStepCounterView
	instanceVariableNames: 'counter countLabel oneButton fiveButton resetButton'
	classVariableNames: ''
	poolDictionaries: ''
	category: 'LearnSqueak-Refactoring'!

!LSStepCounterView methodsFor: 'initialization'!
initialize
	super initialize.
	self color: (Color r: 0.96 g: 0.93 b: 1).
	self extent: 450@170.
	counter := LSStepCounter new.
	countLabel := StringMorph contents: ''.
	countLabel position: self position + (20@25).
	self addMorph: countLabel.
	oneButton := self button: '+1' action: #addOne at: 20@85.
	fiveButton := self button: '+5' action: #addFive at: 125@85.
	resetButton := self button: 'Reset' action: #resetCount at: 230@85.
	self refresh
! !

!LSStepCounterView methodsFor: 'building'!
button: aString action: aSelector at: aPoint
	| button |
	button := SimpleButtonMorph new
		label: aString;
		target: self;
		actionSelector: aSelector;
		position: self position + aPoint;
		yourself.
	self addMorph: button.
	^ button
! !

!LSStepCounterView methodsFor: 'accessing'!
model
	^ counter
! !

!LSStepCounterView methodsFor: 'accessing'!
model: aCounter
	counter := aCounter.
	self refresh
! !

!LSStepCounterView methodsFor: 'accessing'!
countText
	^ countLabel contents
! !

!LSStepCounterView methodsFor: 'accessing'!
oneButton
	^ oneButton
! !

!LSStepCounterView methodsFor: 'accessing'!
fiveButton
	^ fiveButton
! !

!LSStepCounterView methodsFor: 'accessing'!
resetButton
	^ resetButton
! !

!LSStepCounterView methodsFor: 'updating'!
refresh
	countLabel contents: 'Count: ', counter count printString
! !

!LSStepCounterView methodsFor: 'actions'!
add: amount
	counter add: amount.
	self refresh
! !

!LSStepCounterView methodsFor: 'actions'!
addOne
	self add: 1
! !

!LSStepCounterView methodsFor: 'actions'!
addFive
	self add: 5
! !

!LSStepCounterView methodsFor: 'actions'!
resetCount
	counter reset.
	self refresh
! !

TestCase subclass: #LSStepCounterViewTest
	instanceVariableNames: 'counter view'
	classVariableNames: ''
	poolDictionaries: ''
	category: 'LearnSqueak-Refactoring'!

!LSStepCounterViewTest methodsFor: 'running'!
setUp
	super setUp.
	counter := LSStepCounter new.
	view := LSStepCounterView new model: counter
! !

!LSStepCounterViewTest methodsFor: 'running'!
tearDown
	[view ifNotNil: [view delete]]
		ensure: [super tearDown]
! !

!LSStepCounterViewTest methodsFor: 'tests'!
testInitialDisplay
	self assert: view countText equals: 'Count: 0'
! !

!LSStepCounterViewTest methodsFor: 'tests'!
testOneThenFiveStillDisplaysSix
	view oneButton doButtonAction.
	view fiveButton doButtonAction.
	self assert: counter count equals: 6.
	self assert: view countText equals: 'Count: 6'
! !

!LSStepCounterViewTest methodsFor: 'tests'!
testResetStillDisplaysZero
	view fiveButton doButtonAction.
	view resetButton doButtonAction.
	self assert: view countText equals: 'Count: 0'.
	self assert: counter count equals: 0
! !

!LSStepCounterViewTest methodsFor: 'tests'!
testTwoViewsHaveIndependentModels
	| other |
	other := LSStepCounterView new.
	[view oneButton doButtonAction.
	 self assert: other countText equals: 'Count: 0']
		ensure: [other delete]
! !

!LSStepCounterViewTest methodsFor: 'tests'!
testViewCanDisplayExistingModel
	counter add: 12.
	view refresh.
	self assert: view countText equals: 'Count: 12'
! !

