squeak_screencasts: HowTo-SUnit.st

File HowTo-SUnit.st, 3.0 KB (added by willi.mueller, 5 years ago)
Line 
1Error subclass: #EmptyStackError
2        instanceVariableNames: ''
3        classVariableNames: ''
4        poolDictionaries: ''
5        category: 'HowTo-SUnit'!
6
7
8Object subclass: #MyElement
9        instanceVariableNames: 'item next'
10        classVariableNames: ''
11        poolDictionaries: ''
12        category: 'HowTo-SUnit'!
13
14!MyElement methodsFor: 'accessing' stamp: 'wm 10/26/2012 20:03:47.176'!
15item
16        "Answer the value of item"
17
18        ^ item! !
19
20!MyElement methodsFor: 'accessing' stamp: 'wm 10/26/2012 20:03:47.193'!
21item: anObject
22        "Set the value of item"
23
24        item := anObject! !
25
26!MyElement methodsFor: 'accessing' stamp: 'wm 10/26/2012 20:03:47.211'!
27next
28        "Answer the value of next"
29
30        ^ next! !
31
32!MyElement methodsFor: 'accessing' stamp: 'wm 10/26/2012 20:03:47.224'!
33next: anObject
34        "Set the value of next"
35
36        next := anObject! !
37
38"-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- "!
39
40MyElement class
41        instanceVariableNames: ''!
42
43!MyElement class methodsFor: 'as yet unclassified' stamp: 'wm 10/26/2012 20:04:59.441'!
44newWith: anObject andNext: anotherObject
45        ^ self new
46                item: anObject;
47                next: anotherObject! !
48
49
50Object subclass: #MyStack
51        instanceVariableNames: 'head'
52        classVariableNames: ''
53        poolDictionaries: ''
54        category: 'HowTo-SUnit'!
55
56!MyStack methodsFor: 'as yet unclassified' stamp: 'wm 10/26/2012 19:53:42.303'!
57isEmpty
58        ^ self head isNil! !
59
60!MyStack methodsFor: 'as yet unclassified' stamp: 'wm 10/26/2012 20:08:25.072'!
61pop
62        | result |
63        self isEmpty ifTrue: [EmptyStackError signal: 'Cannot pop from empty stack'].
64        result := self head item.
65        self head: self head next.
66        ^ result   
67        ! !
68
69!MyStack methodsFor: 'as yet unclassified' stamp: 'wm 10/26/2012 20:03:06.602'!
70push: anObject
71        self head: (MyElement newWith: anObject andNext: self head)! !
72
73
74!MyStack methodsFor: 'accessing' stamp: 'wm 10/26/2012 19:54:23.282'!
75head
76        "Answer the value of head"
77
78        ^ head! !
79
80!MyStack methodsFor: 'accessing' stamp: 'wm 10/26/2012 19:54:23.295'!
81head: anObject
82        "Set the value of head"
83
84        head := anObject! !
85
86
87TestCase subclass: #MyStackTest
88        instanceVariableNames: 'stack'
89        classVariableNames: ''
90        poolDictionaries: ''
91        category: 'HowTo-SUnit'!
92
93!MyStackTest methodsFor: 'accessing' stamp: 'wm 10/28/2012 17:36:19.845'!
94stack
95        ^ stack! !
96
97!MyStackTest methodsFor: 'accessing' stamp: 'wm 10/28/2012 17:36:15.529'!
98stack: anObject
99        stack := anObject! !
100
101
102!MyStackTest methodsFor: 'testing' stamp: 'wm 10/28/2012 17:35:24.569'!
103setUp
104        self stack: MyStack new! !
105
106!MyStackTest methodsFor: 'testing' stamp: 'wm 10/26/2012 19:52:56.721'!
107testEmptyStackIsEmpty
108        self assert: MyStack new isEmpty  ! !
109
110!MyStackTest methodsFor: 'testing' stamp: 'wm 10/26/2012 20:11:29.244'!
111testNonEmptyStackIsNotEmpty
112        self stack push: Object new.
113        self deny: self stack isEmpty   ! !
114
115!MyStackTest methodsFor: 'testing' stamp: 'wm 10/26/2012 19:55:08.976'!
116testPopEmptyStackThrowsAnError
117        self should: [MyStack new pop] raise: EmptyStackError! !
118
119!MyStackTest methodsFor: 'testing' stamp: 'wm 10/26/2012 20:15:16.304'!
120testPushPrependsElementPopRemovesIt
121        | o |
122        o := Object new.
123        self assert: (self stack push: o) pop equals: o.
124        self assert: self stack isEmpty ! !