/
RGClassDefinitionTest.class.st
417 lines (336 loc) · 12.6 KB
/
RGClassDefinitionTest.class.st
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
"
SUnit tests for class definitions
"
Class {
#name : #RGClassDefinitionTest,
#superclass : #TestCase,
#category : #'Ring-Definitions-Tests-Core-Base'
}
{ #category : #deprecation }
RGClassDefinitionTest class >> isDeprecated [
^true
]
{ #category : #testing }
RGClassDefinitionTest >> testAddingMethods [
| newMethod newClass |
newClass:= RGClassDefinition named: #OrderedCollection.
newMethod:= (RGMethodDefinition named: #add:)
parent: newClass;
protocol: 'adding';
sourceCode: 'add: newObject
^self addLast: newObject'.
self assert: (newMethod isMetaSide not).
self assert: (newClass hasMethods not).
newClass addMethod: newMethod.
newClass addSelector: #size
classified: 'accessing'
sourced: 'fakeMethod
^lastIndex - firstIndex + 1'.
self assert: (newClass hasMethods).
self assert: (newClass selectors asSet = #(add: size) asSet).
self assert: (newClass includesSelector: #add:).
self assert: ((newClass methodNamed: #add:) = newMethod).
self assert: (newClass methods size = 2).
self assert: (newClass selectors size = 2).
self assert: (newClass allSelectors size = 2). "no hierarchy"
newMethod:= newClass methodNamed: #size.
self assert: (newMethod parent = newClass).
self assert: ((newClass compiledMethodNamed: #size) notNil).
self assert: ((newClass compiledMethodNamed: #fakeMethod) isNil)
]
{ #category : #testing }
RGClassDefinitionTest >> testAsClassDefinition [
| newClass |
newClass:= OrderedCollection asRingDefinition.
self assert: (newClass isRingObject).
self assert: (newClass isClass).
self assert: (newClass name == #OrderedCollection).
self assert: (newClass category notNil).
self assert: (newClass superclassName notNil).
self assert: (newClass classSide isRingObject).
self assert: (newClass classSide isClass).
]
{ #category : #testing }
RGClassDefinitionTest >> testAsClassDefinition2 [
| newClass |
newClass:= MOPTestClassC asRingDefinition.
self assert: (newClass isRingObject).
self assert: (newClass isClass).
self assert: (newClass name == #MOPTestClassC).
self assert: (newClass category notNil).
self assert: (newClass superclassName notNil).
self assert: (newClass traitCompositionSource = 'Trait2').
self assert: (newClass classSide isRingObject).
self assert: (newClass classSide isClass).
self assert: (newClass classSide traitCompositionSource = 'Trait2 classTrait').
]
{ #category : #testing }
RGClassDefinitionTest >> testAsClassDefinitionSourceDefinition [
| newClass |
newClass:= MOPTestClassC asRingDefinition.
self assert: (newClass definitionSource = 'Object subclass: #MOPTestClassC
uses: Trait2
instanceVariableNames: ''''
classVariableNames: ''''
poolDictionaries: ''''
category: ''Tests-Traits-MOP''').
self assert: (newClass classSide definitionSource = 'MOPTestClassC class
uses: Trait2 classTrait
instanceVariableNames: ''''').
]
{ #category : #testing }
RGClassDefinitionTest >> testAsRingDefinition [
self assert: OrderedCollection asRingDefinition asRingDefinition isRingObject
]
{ #category : #testing }
RGClassDefinitionTest >> testClassEquality [
| newClass |
self assert: OrderedCollection asRingDefinition = OrderedCollection asRingDefinition.
newClass := (OrderedCollection asRingDefinition)
category: #Kernel.
self assert: (OrderedCollection asRingDefinition = newClass)
]
{ #category : #testing }
RGClassDefinitionTest >> testExistingClass [
| newClass metaClass |
newClass:= RGClassDefinition named: #OrderedCollection.
self assert: (newClass isClass).
self assert: (newClass isDefined).
self assert: (newClass realClass = OrderedCollection).
self assert: (newClass isMeta not).
newClass withMetaclass.
self assert: (newClass hasMetaclass).
metaClass:= newClass classSide.
self assert: (metaClass isMeta).
self assert: (metaClass name = 'OrderedCollection class').
self assert: (metaClass instanceSide = newClass).
self assert: (metaClass realClass = OrderedCollection class).
]
{ #category : #testing }
RGClassDefinitionTest >> testNonExistingClass [
| newClass |
newClass := RGClassDefinition named: #Connection.
self assert: newClass isClass.
self assertEmpty: newClass instanceVariables.
self assertEmpty: newClass classVariables.
self assertEmpty: newClass sharedPools.
self assert: newClass hasMetaclass not.
self assert: newClass hasComment not.
self assert: newClass hasStamp not.
self assert: newClass parent = Smalltalk globals.
self assert: newClass package isNil.
self assert: newClass category isNil.
self assert: newClass hasMethods not.
self assert: newClass hasSuperclass not.
self assert: newClass hasTraitComposition not.
self assert: newClass isDefined not.
self assert: newClass hasProtocols not
]
{ #category : #testing }
RGClassDefinitionTest >> testReadFrom [
| st rg |
rg := Point asRingDefinition.
st := String streamContents: [:s |
rg storeOn: s.
s contents].
self assert: (Object readFrom: st) = rg.
rg := Point class asRingDefinition.
st := String streamContents: [:s |
rg storeOn: s.
s contents].
self assert: (Object readFrom: st) = rg
]
{ #category : #testing }
RGClassDefinitionTest >> testRemovingMethods [
| newMethod newClass |
newClass:= RGClassDefinition named: #OrderedCollection.
newMethod:= (RGMethodDefinition named: #add:)
parent: newClass;
protocol: 'adding';
sourceCode: 'add: newObject
^self addLast: newObject'.
self assert: (newClass hasMethods not).
newClass addMethod: newMethod.
newClass addSelector: #size
classified: 'accessing'
sourced: 'size
^ lastIndex - firstIndex + 1'.
self assert: (newClass selectors asSet = #(add: size) asSet).
newClass removeSelector: #join:.
self assert: (newClass selectors asSet = #(add: size) asSet).
newClass removeMethod: newMethod.
self assert: ((newClass includesSelector: #add:) not).
newClass removeSelector: #size.
self assert: (newClass hasMethods not).
]
{ #category : #testing }
RGClassDefinitionTest >> testStoreOn [
| st |
st := String streamContents: [:s |
(Point) asRingDefinition storeOn: s.
s contents].
self assert: st = '(RGClassDefinition named: #Point)'.
st := String streamContents: [:s |
(Point class) asRingDefinition storeOn: s.
s contents].
self assert: st = '((RGMetaclassDefinition named: #''Point class'') baseClass:(RGClassDefinition named: #Point))'
]
{ #category : #testing }
RGClassDefinitionTest >> testWithCategory [
| newClass |
newClass:= (RGClassDefinition named: #Object)
category: 'Kernel-Objects';
yourself.
self assert: (newClass package isNil).
self assert: (newClass category = 'Kernel-Objects').
]
{ #category : #testing }
RGClassDefinitionTest >> testWithClassInstanceVariables [
| newClass metaClass classInstVar |
newClass := RGClassDefinition named: #HashTableSizes.
newClass withMetaclass.
metaClass := newClass classSide.
metaClass addInstanceVariables: #(sizes).
self assert: metaClass instanceVariables size equals: 1.
self assert: metaClass instVarNames size equals: 1.
self assert: metaClass allInstVarNames size equals: 1.
classInstVar := metaClass instanceVariableNamed: #sizes.
self assert: classInstVar notNil.
self assert: classInstVar parent = metaClass.
self assert: classInstVar isClassInstanceVariable.
self assert: classInstVar isVariable.
self assert: classInstVar parentName = metaClass name.
self assert: classInstVar realClass = HashTableSizes class.
metaClass removeInstVarNamed: #sizes.
self assertEmpty: metaClass instanceVariables.
self assert: (metaClass instanceVariableNamed: #sizes) isNil
]
{ #category : #testing }
RGClassDefinitionTest >> testWithClassVariables [
| newClass classVar |
newClass:= (RGClassDefinition named: #Object)
addClassVariables: #(DependentsFields);
addClassVarNamed: #FakeVariable;
yourself.
self assert: (newClass classVariables size = 2).
self assert: (newClass classVarNames size = 2).
self assert: (newClass allClassVarNames size = 2). "no hierarchy"
classVar:= newClass classVarNamed: #DependentsFields.
self assert: (classVar notNil).
self assert: (classVar isClassVariable).
self assert: (classVar isVariable).
self assert: (classVar parent = newClass).
self assert: (classVar parentName == newClass name).
self assert: (classVar realClass = Object).
newClass withMetaclass.
self assert: (newClass classSide allClassVarNames size = 2).
newClass removeClassVarNamed: #DependentsFields.
self assert: (newClass classVariables size = 1).
]
{ #category : #testing }
RGClassDefinitionTest >> testWithComment [
| newClass newComment |
newClass:= RGClassDefinition named: #Object.
newComment:= RGCommentDefinition new
parent: newClass;
content: 'This is a comment for test';
stamp: 'VeronicaUquillas 3/22/2011 14:51';
yourself.
newClass comment: newComment.
self assert: (newClass hasComment).
self assert: (newClass hasStamp).
self assert: (newClass = newComment parent).
self assert: (newComment content = 'This is a comment for test').
self assert: (newComment author = 'VeronicaUquillas').
self assert: (newComment timeStamp = '3/22/2011 14:51' asDateAndTime).
newClass comment: nil.
self assert: (newClass hasComment not).
newClass comment: 'This is a comment for test';
stamp: 'VeronicaUquillas 3/22/2011 14:51'.
self assert: (newClass comment isRingObject).
]
{ #category : #testing }
RGClassDefinitionTest >> testWithDefaultNamespace [
| newClass |
newClass:= RGClassDefinition named: #Object.
self assert: (newClass parent = Smalltalk globals).
self assert: (newClass realClass = Object).
]
{ #category : #testing }
RGClassDefinitionTest >> testWithInstanceVariables [
| newClass instVar |
newClass:= RGClassDefinition named: #OrderedCollection.
newClass addInstanceVariables: #(array firstIndex).
newClass addInstVarNamed: #lastIndex.
self assert: (newClass instanceVariables size = 3).
self assert: (newClass instVarNames size = 3).
self assert: (newClass allInstVarNames size = 3).
instVar:= newClass instanceVariableNamed: #firstIndex.
self assert: (instVar notNil).
self assert: (instVar parent = newClass).
self assert: (instVar isInstanceVariable).
self assert: (instVar isVariable).
self assert: (instVar parentName == newClass name).
self assert: (instVar realClass = OrderedCollection).
newClass removeInstVarNamed: #array.
self assert: (newClass instanceVariables size = 2).
self assert: ((newClass instanceVariableNamed: #array) isNil).
]
{ #category : #testing }
RGClassDefinitionTest >> testWithPoolDictionaries [
| newClass poolVar |
newClass := (RGClassDefinition named: #Text)
addSharedPoolNamed: #TextConstants;
yourself.
self assert: newClass sharedPools size = 1.
self assert: newClass sharedPoolNames size = 1.
self assert: newClass allSharedPools size = 1. "no hierarchy"
self assert: newClass allSharedPoolNames size = 1.
poolVar := newClass sharedPoolNamed: #TextConstants.
self assert: poolVar notNil.
self assert: poolVar isPoolVariable.
self assert: poolVar isVariable.
self assert: poolVar parent = newClass.
self assert: poolVar parentName == newClass name.
self assert: poolVar realClass = Text.
newClass withMetaclass.
self assert: newClass classSide allSharedPoolNames size = 1.
newClass removeSharedPoolNamed: #TextConstants.
self assertEmpty: newClass sharedPools
]
{ #category : #testing }
RGClassDefinitionTest >> testWithProtocols [
| newMethod newClass |
newClass := RGClassDefinition named: #OrderedCollection.
newMethod := (RGMethodDefinition named: #add:)
parent: newClass;
protocol: 'adding';
sourceCode:
'add: newObject
^self addLast: newObject'.
newClass addMethod: newMethod.
newClass addProtocol: 'accessing'.
self assert: newClass hasProtocols.
self assert: newClass protocols size = 2.
self assert: (newClass includesProtocol: 'accessing').
self assert: (newClass methodsInProtocol: 'adding') size = 1.
self assertEmpty: (newClass methodsInProtocol: 'accessing')
]
{ #category : #testing }
RGClassDefinitionTest >> testWithSuperclass [
| newClass supClass |
supClass:= (RGClassDefinition named: #Object)
superclassName: #ProtoObject;
yourself.
self assert: (supClass hasSuperclass not).
self assert: (supClass superclassName == #ProtoObject). "kept as annotation"
self assert: (supClass annotations size = 1).
newClass := (RGClassDefinition named: #OrderedCollection) superclass: supClass.
self assert: (newClass superclass = supClass).
self assert: (newClass superclassName == #Object).
self assert: (newClass withAllSuperclasses size = 2).
self assert: (newClass allSuperclasses size = 1).
self assert: (supClass subclasses size = 1).
self assert: (supClass withAllSubclasses size = 2).
self assert: (supClass allSubclasses size = 1).
]