summaryrefslogtreecommitdiff
path: root/pharo-mooc/redo/src/MyCounter
diff options
context:
space:
mode:
authorEugen Wissner <belka@caraus.de>2025-11-23 17:01:59 +0100
committerEugen Wissner <belka@caraus.de>2025-11-23 17:01:59 +0100
commitbf11813e4fa859a4833cab226c4ea560765d6d77 (patch)
tree480e175d8dbb27376841c27dd4250f89f7bfb9a9 /pharo-mooc/redo/src/MyCounter
parent8568518b57dba05500b63a4e471f8fa65883d10a (diff)
downloadbook-exercises-bf11813e4fa859a4833cab226c4ea560765d6d77.tar.gz
Add ReDo Pharo excercises
Diffstat (limited to 'pharo-mooc/redo/src/MyCounter')
-rw-r--r--pharo-mooc/redo/src/MyCounter/Counter.class.st60
-rw-r--r--pharo-mooc/redo/src/MyCounter/CounterTest.class.st55
-rw-r--r--pharo-mooc/redo/src/MyCounter/package.st1
3 files changed, 116 insertions, 0 deletions
diff --git a/pharo-mooc/redo/src/MyCounter/Counter.class.st b/pharo-mooc/redo/src/MyCounter/Counter.class.st
new file mode 100644
index 0000000..2d65fa1
--- /dev/null
+++ b/pharo-mooc/redo/src/MyCounter/Counter.class.st
@@ -0,0 +1,60 @@
+"
+Counter is a simple concrete class which supports incrementing and
+decrementing a counter.
+
+Its API is
+- decrement, increment
+- count
+Its creation API is message startingAt:
+"
+Class {
+ #name : #Counter,
+ #superclass : #Object,
+ #instVars : [
+ 'count'
+ ],
+ #category : #MyCounter
+}
+
+{ #category : #accessing }
+Counter class >> startingAt: anInteger [
+ ^ self new count: anInteger
+]
+
+{ #category : #accessing }
+Counter >> count [
+
+ ^ count
+]
+
+{ #category : #accessing }
+Counter >> count: anInteger [
+
+ count := anInteger
+]
+
+{ #category : #operation }
+Counter >> decrement [
+
+ count := count - 1
+]
+
+{ #category : #operation }
+Counter >> increment [
+
+ count := count + 1
+]
+
+{ #category : #initialization }
+Counter >> initialize [
+ super initialize.
+ count := 0
+]
+
+{ #category : #accessing }
+Counter >> printOn: aStream [
+
+ super printOn: aStream. "a Counter"
+ aStream nextPutAll: ' withValue: ', count printString.
+ aStream cr
+]
diff --git a/pharo-mooc/redo/src/MyCounter/CounterTest.class.st b/pharo-mooc/redo/src/MyCounter/CounterTest.class.st
new file mode 100644
index 0000000..ba2afe5
--- /dev/null
+++ b/pharo-mooc/redo/src/MyCounter/CounterTest.class.st
@@ -0,0 +1,55 @@
+Class {
+ #name : #CounterTest,
+ #superclass : #TestCase,
+ #category : #MyCounter
+}
+
+{ #category : #tests }
+CounterTest >> testCountIsSetAndRead [
+ | c |
+ c := Counter new.
+ c count: 7.
+ self assert: c count equals: 7
+]
+
+{ #category : #tests }
+CounterTest >> testCounterWellInitialized [
+ self assert: (Counter new increment; increment; count) equals: 2
+]
+
+{ #category : #tests }
+CounterTest >> testDecrement [
+ | c |
+ c := Counter new.
+ c count: 2.
+ c decrement; decrement.
+ self assert: c count equals: 0
+]
+
+{ #category : #tests }
+CounterTest >> testIncrement [
+ | c |
+ c := Counter new.
+ c count: 2.
+ c increment; increment.
+ self assert: c count equals: 4
+]
+
+{ #category : #tests }
+CounterTest >> testInizialize [
+ self assert: Counter new count equals: 0
+]
+
+{ #category : #tests }
+CounterTest >> testStartingAt5 [
+ | c |
+ c := Counter startingAt: 5.
+ self assert: c count equals: 5
+]
+
+{ #category : #tests }
+CounterTest >> testStartingAt5Increment [
+ | c |
+ c := Counter startingAt: 5.
+ self assert: (c increment; count) equals: 6
+]
diff --git a/pharo-mooc/redo/src/MyCounter/package.st b/pharo-mooc/redo/src/MyCounter/package.st
new file mode 100644
index 0000000..242e6c9
--- /dev/null
+++ b/pharo-mooc/redo/src/MyCounter/package.st
@@ -0,0 +1 @@
+Package { #name : #MyCounter }