author | Claus Gittinger <cg@exept.de> |
Sun, 08 Sep 2019 01:38:42 +0200 | |
changeset 4338 | b91474d7fd90 |
parent 4241 | 8c18cc3470d8 |
permissions | -rw-r--r-- |
4241 | 1 |
"{ Encoding: utf8 }" |
2 |
||
88 | 3 |
" |
4 |
COPYRIGHT (c) 1995 by Claus Gittinger |
|
114
e577a2f332d0
uff - version methods changed to return stings
Claus Gittinger <cg@exept.de>
parents:
96
diff
changeset
|
5 |
All Rights Reserved |
88 | 6 |
|
7 |
This software is furnished under a license and may be used |
|
8 |
only in accordance with the terms of that license and with the |
|
9 |
inclusion of the above copyright notice. This software may not |
|
10 |
be provided or otherwise made available to, or used by, any |
|
11 |
other person. No title to or ownership of the software is |
|
12 |
hereby transferred. |
|
13 |
" |
|
3940 | 14 |
"{ Package: 'stx:libview2' }" |
88 | 15 |
|
3940 | 16 |
"{ NameSpace: Smalltalk }" |
1554
d01ffa42ca7a
implements -> includesSelector
Claus Gittinger <cg@exept.de>
parents:
1317
diff
changeset
|
17 |
|
55 | 18 |
Model subclass:#Plug |
2100 | 19 |
instanceVariableNames:'simulatedProtocol inheritedClasses' |
162 | 20 |
classVariableNames:'' |
21 |
poolDictionaries:'' |
|
22 |
category:'Kernel-Objects' |
|
54 | 23 |
! |
24 |
||
25 |
!Plug class methodsFor:'documentation'! |
|
26 |
||
88 | 27 |
copyright |
28 |
" |
|
29 |
COPYRIGHT (c) 1995 by Claus Gittinger |
|
114
e577a2f332d0
uff - version methods changed to return stings
Claus Gittinger <cg@exept.de>
parents:
96
diff
changeset
|
30 |
All Rights Reserved |
88 | 31 |
|
32 |
This software is furnished under a license and may be used |
|
33 |
only in accordance with the terms of that license and with the |
|
34 |
inclusion of the above copyright notice. This software may not |
|
35 |
be provided or otherwise made available to, or used by, any |
|
36 |
other person. No title to or ownership of the software is |
|
37 |
hereby transferred. |
|
38 |
" |
|
39 |
! |
|
40 |
||
54 | 41 |
documentation |
42 |
" |
|
228 | 43 |
A Plug is an object which simulates a protocol and evaluates |
54 | 44 |
a corresponding block when receiving messages. |
2100 | 45 |
A plug's interface can be changed dynamically. |
46 |
A plug can also be told to simulate inheriting messages from other classes, |
|
47 |
even multiple inheritance is possible. |
|
228 | 48 |
|
226 | 49 |
Its main use is for the demo doIts, to play the role of a model, |
228 | 50 |
when no actual modelClass is available for the demonstration. |
51 |
However, it can be used wherever some object is needed which responds to |
|
52 |
some protocol AND you do not want to add a class for it |
|
53 |
(lightWeight objects). |
|
54 |
||
55 |
There is a slight performance penalty - compared to `normal' objects, |
|
56 |
getting `normal' messages, though. |
|
224 | 57 |
|
3940 | 58 |
[caveat:] |
59 |
The name 'plug' was chosen because instances can be 'plugged' in anywhere. |
|
60 |
That name was invented way before the name 'mock' became popular; |
|
61 |
nowadays, it would probably be called 'PluggableMock'... |
|
62 |
||
224 | 63 |
[author:] |
64 |
Claus Gittinger |
|
228 | 65 |
|
66 |
[see also:] |
|
67 |
Model |
|
54 | 68 |
" |
69 |
! |
|
70 |
||
71 |
examples |
|
72 |
" |
|
2100 | 73 |
a simple plug: |
228 | 74 |
[exBegin] |
2100 | 75 |
|plug| |
54 | 76 |
|
2100 | 77 |
plug := Plug new. |
78 |
plug respondTo:#foo with:[Transcript showCR:'Plug received foo']. |
|
79 |
plug respondTo:#foo: with:[:arg | Transcript showCR:'Plug received foo: ', arg printString]. |
|
54 | 80 |
|
2100 | 81 |
plug foo. |
82 |
plug foo:'some argument' |
|
228 | 83 |
[exEnd] |
162 | 84 |
|
2101 | 85 |
|
360 | 86 |
using a plug as a generator (simulates a readStream): |
87 |
[exBegin] |
|
2100 | 88 |
|generator num| |
360 | 89 |
|
2100 | 90 |
num := 0. |
91 |
generator := Plug new. |
|
92 |
generator respondTo:#next with:[num := num + 1. num]. |
|
93 |
generator respondTo:#atEnd with:[false]. |
|
360 | 94 |
|
2100 | 95 |
10 timesRepeat:[ |
96 |
Transcript showCR:(generator next) |
|
97 |
] |
|
360 | 98 |
[exEnd] |
162 | 99 |
|
2101 | 100 |
|
101 |
simulating a big collection: |
|
102 |
[exBegin] |
|
103 |
|virtualList| |
|
104 |
||
105 |
virtualList := Plug new. |
|
106 |
virtualList inheritFrom:SequenceableCollection. |
|
107 |
virtualList respondTo:#size with:[ 1000000 ]. |
|
108 |
virtualList respondTo:#at: with:[:lineNr | 'List line Nr. ' , lineNr printString ]. |
|
109 |
virtualList inspect. |
|
110 |
[exEnd] |
|
111 |
||
112 |
||
162 | 113 |
simulating ``instance variables'': |
114 |
(actually, this is somewhat expensive - the contexts locals are used for them ...) |
|
115 |
be careful with unintended variable sharing (if plugs are created in a loop ..) |
|
228 | 116 |
[exBegin] |
2100 | 117 |
|plug1 plug2 local1 local2| |
118 |
||
119 |
plug1 := Plug new. |
|
120 |
plug1 respondTo:#get with:[local1]. |
|
121 |
plug1 respondTo:#set: with:[:arg | local1 := arg]. |
|
162 | 122 |
|
2100 | 123 |
plug2 := Plug new. |
124 |
plug2 respondTo:#get with:[local2]. |
|
125 |
plug2 respondTo:#set: with:[:arg | local2 := arg]. |
|
126 |
||
127 |
Transcript show:'plug1''s value: '; showCR:plug1 get. |
|
128 |
Transcript show:'plug2''s value: '; showCR:plug2 get. |
|
129 |
||
130 |
plug1 set:5. |
|
131 |
plug2 set:17. |
|
162 | 132 |
|
2100 | 133 |
Transcript show:'plug1''s value: '; showCR:plug1 get. |
134 |
Transcript show:'plug2''s value: '; showCR:plug2 get. |
|
135 |
[exEnd] |
|
136 |
||
2101 | 137 |
|
2100 | 138 |
simulating a big list in a ListView |
139 |
(real applications would read the lines from a database or file): |
|
140 |
[exBegin] |
|
141 |
|virtualList top lv| |
|
162 | 142 |
|
2100 | 143 |
virtualList := Plug new. |
144 |
virtualList inheritFrom:SequenceableCollection. |
|
145 |
virtualList respondTo:#size with:[ 1000000 ]. |
|
146 |
virtualList respondTo:#at: with:[:lineNr | 'List line Nr. ' , lineNr printString ]. |
|
162 | 147 |
|
2100 | 148 |
top := StandardSystemView extent:200@200. |
162 | 149 |
|
2100 | 150 |
lv := ScrollableView for:ListView in:top. |
151 |
lv origin:0.0 @ 0.0 corner:1.0 @ 1.0. |
|
152 |
lv list:virtualList expandTabs:false scanForNonStrings:false includesNonStrings:false. |
|
153 |
||
154 |
top open. |
|
228 | 155 |
[exEnd] |
54 | 156 |
" |
157 |
! ! |
|
158 |
||
159 |
!Plug class methodsFor:'instance creation'! |
|
160 |
||
161 |
new |
|
232 | 162 |
^ super basicNew privateInitialize |
228 | 163 |
|
164 |
"Modified: 27.4.1996 / 16:16:59 / cg" |
|
54 | 165 |
! ! |
166 |
||
137 | 167 |
!Plug methodsFor:'initialization'! |
168 |
||
232 | 169 |
privateInitialize |
228 | 170 |
"this method is NOT called `#initialize' to allow plugging that |
137 | 171 |
selector ..." |
172 |
||
173 |
simulatedProtocol := IdentityDictionary new. |
|
228 | 174 |
|
175 |
"Modified: 27.4.1996 / 16:15:45 / cg" |
|
176 |
"Created: 27.4.1996 / 16:17:07 / cg" |
|
137 | 177 |
! ! |
178 |
||
54 | 179 |
!Plug methodsFor:'message sending'! |
180 |
||
1760 | 181 |
at:key |
182 |
"catch this one - its so common" |
|
183 |
||
4338 | 184 |
^ self doesNotUnderstand:(Message selector:#at: arguments:{ key }) |
185 |
! |
|
186 |
||
187 |
at:key ifAbsent:exceptionValue |
|
188 |
"catch this one - its so common" |
|
189 |
||
190 |
|block| |
|
191 |
||
192 |
block := simulatedProtocol at:#at:ifAbsent: ifAbsent:[]. |
|
193 |
block notNil ifTrue:[ |
|
194 |
^ block valueWithArguments:{ key . exceptionValue} |
|
195 |
]. |
|
196 |
^ super at:key ifAbsent:exceptionValue. |
|
1760 | 197 |
! |
198 |
||
199 |
at:key put:value |
|
4338 | 200 |
^ self doesNotUnderstand:(Message selector:#at:put: arguments:{ key . value }) |
1760 | 201 |
! |
202 |
||
54 | 203 |
doesNotUnderstand:aMessage |
228 | 204 |
"catch unhandled messages by looking in my simulated protocol |
205 |
definition; if there is some block for it, return its value. |
|
206 |
Otherwise, fall into the real doesNotUnderstand error." |
|
207 |
||
54 | 208 |
|block| |
209 |
||
2041 | 210 |
block := simulatedProtocol at:(aMessage selector) ifAbsent:[]. |
2100 | 211 |
block notNil ifTrue:[ |
212 |
^ block valueWithArguments:(aMessage arguments) |
|
54 | 213 |
]. |
2100 | 214 |
inheritedClasses notNil ifTrue:[ |
215 |
inheritedClasses do:[:eachClass | |
|
216 |
|method| |
|
217 |
||
218 |
method := eachClass lookupMethodFor:(aMessage selector). |
|
219 |
method notNil ifTrue:[ |
|
220 |
^ method valueWithReceiver:self arguments:(aMessage arguments). |
|
221 |
]. |
|
222 |
]. |
|
223 |
]. |
|
224 |
^ super doesNotUnderstand:aMessage |
|
228 | 225 |
|
226 |
"Modified: 27.4.1996 / 16:15:34 / cg" |
|
377 | 227 |
! |
228 |
||
2101 | 229 |
isSequenceable |
4241 | 230 |
"return true, if the receiver is some kind of sequenceableCollection, |
231 |
i.e. if its elements are accessable by an integer index, |
|
232 |
and support the do:-protocol." |
|
233 |
||
2101 | 234 |
"catch this one - its so common" |
235 |
||
236 |
^ self doesNotUnderstand:(Message selector:#isSequenceable arguments:#()) |
|
4241 | 237 |
|
238 |
"Modified (comment): / 03-03-2019 / 00:09:24 / Claus Gittinger" |
|
2101 | 239 |
! |
240 |
||
2100 | 241 |
size |
242 |
"catch this one - its so common" |
|
243 |
||
244 |
^ self doesNotUnderstand:(Message selector:#size arguments:#()) |
|
245 |
! |
|
246 |
||
1317 | 247 |
update:something with:aParameter from:changedObject |
248 |
"catch unhandled messages by looking in my simulated protocol |
|
249 |
definition; if there is some block for it, return its value. |
|
250 |
Otherwise, fall into the real update." |
|
251 |
||
252 |
|block| |
|
253 |
||
254 |
block := simulatedProtocol at:#'update:with:from:' ifAbsent:[]. |
|
255 |
block isNil ifTrue:[ |
|
256 |
^ super update:something with:aParameter from:changedObject |
|
257 |
]. |
|
258 |
^ block valueWithArguments:(Array with:something with:aParameter with:changedObject) |
|
259 |
||
260 |
! |
|
261 |
||
377 | 262 |
value |
263 |
"catch this one - its so common" |
|
264 |
||
1982
61c4a1986224
special case for #value message
Claus Gittinger <cg@exept.de>
parents:
1760
diff
changeset
|
265 |
|block| |
377 | 266 |
|
1982
61c4a1986224
special case for #value message
Claus Gittinger <cg@exept.de>
parents:
1760
diff
changeset
|
267 |
block := simulatedProtocol at:#value ifAbsent:[]. |
61c4a1986224
special case for #value message
Claus Gittinger <cg@exept.de>
parents:
1760
diff
changeset
|
268 |
block isNil ifTrue:[ |
61c4a1986224
special case for #value message
Claus Gittinger <cg@exept.de>
parents:
1760
diff
changeset
|
269 |
^ self |
61c4a1986224
special case for #value message
Claus Gittinger <cg@exept.de>
parents:
1760
diff
changeset
|
270 |
]. |
61c4a1986224
special case for #value message
Claus Gittinger <cg@exept.de>
parents:
1760
diff
changeset
|
271 |
^ block valueWithArguments:#() |
377 | 272 |
! |
273 |
||
274 |
value:arg |
|
275 |
"catch this one - its so common" |
|
276 |
||
277 |
^ self doesNotUnderstand:(Message selector:#value: argument:arg) |
|
278 |
||
279 |
"Created: 21.1.1997 / 15:21:36 / cg" |
|
54 | 280 |
! ! |
281 |
||
137 | 282 |
!Plug methodsFor:'protocol definition'! |
283 |
||
228 | 284 |
forgetAbout:aSelector |
285 |
"tell the receiver to forget about how to respond to the given by selector" |
|
286 |
||
287 |
simulatedProtocol removeKey:aSelector ifAbsent:nil |
|
288 |
||
289 |
" |
|
290 |
|p| |
|
291 |
||
292 |
p := Plug new. |
|
268 | 293 |
p respondTo:#foo with:[Transcript showCR:'foo']. |
294 |
p respondTo:#foo: with:[:arg | Transcript show:'foo:'; showCR:arg]. |
|
228 | 295 |
|
296 |
p foo. |
|
297 |
p foo:'hello'. |
|
298 |
||
299 |
p forgetAbout:#foo. |
|
300 |
||
301 |
p foo. |
|
302 |
" |
|
303 |
||
304 |
"Modified: 27.4.1996 / 16:14:19 / cg" |
|
305 |
"Created: 27.4.1996 / 16:19:08 / cg" |
|
306 |
! |
|
307 |
||
2100 | 308 |
inheritFrom:aClass |
309 |
"very tricky - change the inheritance. |
|
310 |
This cannot be done by changing my class directly, because the instance layout |
|
311 |
must still be correct for Plugs instance variables. |
|
312 |
Therefore, the inheritance is remembered, and done dynamically in the doesNotUnderstand |
|
313 |
implementation." |
|
314 |
||
315 |
self assert:(aClass instSize == 0). |
|
316 |
inheritedClasses isNil ifTrue:[ |
|
317 |
inheritedClasses := Array with:aClass |
|
318 |
] ifFalse:[ |
|
319 |
inheritedClasses := inheritedClasses copyWith:aClass |
|
320 |
]. |
|
321 |
! |
|
322 |
||
137 | 323 |
respondTo:aSelector with:aBlock |
228 | 324 |
"tell the receiver to respond to a message given by selector, |
325 |
with evaluating aBlock. The number of arguments as defined by the |
|
326 |
selector must match the number of blockArsg expected by the block. |
|
327 |
The value returned from aBlock will be the value returned from the |
|
328 |
message." |
|
329 |
||
1554
d01ffa42ca7a
implements -> includesSelector
Claus Gittinger <cg@exept.de>
parents:
1317
diff
changeset
|
330 |
(self class includesSelector:aSelector) ifFalse:[ |
444 | 331 |
(self class superclass canUnderstand:aSelector) ifTrue:[ |
377 | 332 |
|
333 |
"/ sorry - this implementation is too quick of a hack. |
|
334 |
"/ (must be rewritten to inherit from nil, in order |
|
335 |
"/ to be able to catch more ...) |
|
336 |
||
337 |
self error:'inherited message cannot be redefined: ' , aSelector. |
|
338 |
] |
|
339 |
]. |
|
137 | 340 |
simulatedProtocol at:aSelector put:aBlock |
228 | 341 |
|
342 |
" |
|
343 |
|p| |
|
344 |
||
345 |
p := Plug new. |
|
268 | 346 |
p respondTo:#foo with:[Transcript showCR:'foo']. |
347 |
p respondTo:#foo: with:[:arg | Transcript show:'foo:'; showCR:arg]. |
|
228 | 348 |
|
349 |
p foo. |
|
350 |
p foo:'hello' |
|
351 |
" |
|
352 |
||
444 | 353 |
"Modified: 21.2.1997 / 18:35:07 / cg" |
137 | 354 |
! ! |
355 |
||
82 | 356 |
!Plug methodsFor:'queries'! |
357 |
||
358 |
respondsTo:aSelector |
|
228 | 359 |
"return true, if the receiver responds to a message" |
360 |
||
82 | 361 |
(simulatedProtocol includesKey:aSelector) ifTrue:[^ true]. |
362 |
^ super respondsTo:aSelector |
|
228 | 363 |
|
364 |
"Modified: 27.4.1996 / 16:14:41 / cg" |
|
82 | 365 |
! ! |
366 |
||
137 | 367 |
!Plug class methodsFor:'documentation'! |
54 | 368 |
|
137 | 369 |
version |
3940 | 370 |
^ '$Header$' |
54 | 371 |
! ! |
3940 | 372 |