squeak_screencasts: HowTo-SUnit.st

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