-
-
Notifications
You must be signed in to change notification settings - Fork 346
/
SystemNavigationTest.class.st
213 lines (167 loc) · 7.28 KB
/
SystemNavigationTest.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
"
I am an sunit test for SystemNavigation with the default value for its environment (Smalltalk globals).
"
Class {
#name : 'SystemNavigationTest',
#superclass : 'TestCase',
#instVars : [
'classFactory'
],
#category : 'System-Support-Tests-Image',
#package : 'System-Support-Tests',
#tag : 'Image'
}
{ #category : 'accessing' }
SystemNavigationTest >> classFactory [
^classFactory
]
{ #category : 'utilities' }
SystemNavigationTest >> createClassFactory [
^ ClassFactoryForTestCase environment: self systemNavigationToTest environment
]
{ #category : 'private' }
SystemNavigationTest >> performTest [
self class codeChangeAnnouncer suspendAllWhile: [ ^ super performTest ]
]
{ #category : 'running' }
SystemNavigationTest >> setUp [
super setUp.
classFactory := self createClassFactory
]
{ #category : 'accessing' }
SystemNavigationTest >> systemNavigationToTest [
^SystemNavigation new
]
{ #category : 'running' }
SystemNavigationTest >> tearDown [
self classFactory cleanUp.
super tearDown
]
{ #category : 'tests' }
SystemNavigationTest >> testAllClassesImplementing [
| implementingClasses |
implementingClasses := self systemNavigationToTest
allClassesImplementing: #isSymbol.
self
assertCollection: implementingClasses
hasSameElements: {Object. Symbol}
]
{ #category : 'tests' }
SystemNavigationTest >> testAllExistingProtocolsFor [
| instSideProtocols classSideProtocols |
instSideProtocols := self systemNavigationToTest allExistingProtocolsFor: true.
classSideProtocols := self systemNavigationToTest allExistingProtocolsFor: false.
self assert: (instSideProtocols allSatisfy: #isSymbol).
self assert: (classSideProtocols allSatisfy: #isSymbol).
self assert: (instSideProtocols includesAll: Object protocolNames).
self assert: (classSideProtocols includesAll: Object class protocolNames)
]
{ #category : 'tests' }
SystemNavigationTest >> testAllGlobalNamesStartingWithDoCaseSensitive [
| result |
result := OrderedCollection new.
self systemNavigationToTest allGlobalNamesStartingWith: 'Sma' do: [ :name | result add: name ] caseSensitive: true.
self assert: (result includes: 'Smalltalk').
result := OrderedCollection new.
self systemNavigationToTest allGlobalNamesStartingWith: 'sma' do: [ :name | result add: name ] caseSensitive: true.
self deny: (result includes: 'Smalltalk').
result := OrderedCollection new.
self systemNavigationToTest allGlobalNamesStartingWith: 'sma' do: [ :name | result add: name ] caseSensitive: false.
self assert: (result includes: 'Smalltalk')
]
{ #category : 'tests' }
SystemNavigationTest >> testAllReferencesToDo [
"test trying to not hard code numbers as they will be different"
| counterSymbol counterClass |
counterSymbol := 0.
SystemNavigation new allReferencesTo: #Object do: [:ref | counterSymbol := counterSymbol +1 ].
self assert: counterSymbol > 10.
counterClass := 0.
SystemNavigation new allReferencesTo: Object binding do: [:ref | counterClass := counterClass +1 ].
self assert: counterClass > 10.
"and there are more refs to the class then to the symbol"
self assert: counterClass > counterSymbol.
]
{ #category : 'tests' }
SystemNavigationTest >> testAllSendersOfASelector [
| senders selector class otherClass callers |
selector := ('aMethod' , 'WithSenders') asSymbol.
class := self classFactory newClass.
class compileSilently: selector asString , ' ^ self'.
class compileSilently: 'anotherMethod ^self ' , selector asString.
otherClass := self classFactory newClass.
otherClass
compileSilently: 'yetAnotherMethod ^self ' , selector asString.
senders := self systemNavigationToTest allSendersOf: selector.
self assert: senders size equals: 2.
callers := senders collect: [ :methodRef | methodRef selector ].
self
assert: (callers includesAll: #(#anotherMethod #yetAnotherMethod))
]
{ #category : 'tests' }
SystemNavigationTest >> testIsMessageSentInSystemWithClassesActuallySendngTheMessage [
| classesSendingMessage sentMessageSelector |
sentMessageSelector := 'MessageSentOnlyByTestClassesXXXShouldNotBeRealyDefined' asSymbol.
5 timesRepeat: [ self classFactory make: [ :aBuilder | aBuilder tag: #One ] ].
5 timesRepeat: [ self classFactory make: [ :aBuilder | aBuilder tag: #Two ] ].
classesSendingMessage := (self classFactory createdClasses asArray first: 2) , (self classFactory createdClasses asArray last: 3).
classesSendingMessage do: [ :class | class compileSilently: 'meth self ' , sentMessageSelector ].
self
assertCollection: (self systemNavigationToTest allSendersOf: sentMessageSelector)
hasSameElements: (classesSendingMessage collect: [ :class | class >> #meth ])
]
{ #category : 'tests' }
SystemNavigationTest >> testIsMessageSentInSystemWithTheSelectorInsideAnArray [
| classesSendingMessage sentMessageSelector |
sentMessageSelector := 'MessageSentOnlyByTestClassesXXXShouldNotBeRealyDefined' asSymbol.
5 timesRepeat: [ self classFactory make: [ :aBuilder | aBuilder tag: #One ] ].
5 timesRepeat: [ self classFactory make: [ :aBuilder | aBuilder tag: #Two ] ].
classesSendingMessage := (self classFactory createdClasses asArray first: 2) , (self classFactory createdClasses asArray last: 3).
classesSendingMessage do: [ :class | class compileSilently: 'meth ^#(a b ' , sentMessageSelector , ' c)' ].
self
assertCollection: (self systemNavigationToTest allSendersOf: sentMessageSelector)
hasSameElements: (classesSendingMessage collect: [ :class | class >> #meth ])
]
{ #category : 'tests' }
SystemNavigationTest >> testIsUnsentMessage [
| class |
class := self classFactory newClass.
class compileSilently: 'messageNeverSentInTheSystemXXXXThisIsForTest ^self'.
self assert: (self systemNavigationToTest isUnsentMessage: class selectors anyOne)
]
{ #category : 'tests' }
SystemNavigationTest >> testReferencesToAClassInBlock [
| referencingMethods class otherClass callers |
class := self classFactory newClass.
class compileSilently: 'aMethod ^ 42'.
otherClass := self classFactory newClass.
otherClass
compileSilently: 'anotherMethod ^[[' , class name asString , ']]'.
referencingMethods := self systemNavigationToTest allReferencesTo: class binding.
self assert: referencingMethods size equals: 1.
callers := referencingMethods collect: [ :methodRef | methodRef selector ].
self
assert: (callers includesAll: #(#anotherMethod))
]
{ #category : 'tests' }
SystemNavigationTest >> testRemoveMethodInClass [
| methodToRemove selector class |
selector := #aMethod.
class := self classFactory silentlyNewClass.
class compileSilently: selector asString , ' ^ self'.
methodToRemove := class >> selector.
self assert: (self systemNavigationToTest removeMethod: methodToRemove inClass: class).
self should: [ class >> selector ] raise: NotFound
]
{ #category : 'tests' }
SystemNavigationTest >> testSenderOfASelectorInBlock [
| senders selector class otherClass |
selector := ('aMethod' , 'WithSenders') asSymbol.
class := self classFactory silentlyNewClass.
class compileSilently: selector asString , ' ^ self'.
class compileSilently: 'anotherMethod ^ [self ' , selector asString , ']'.
otherClass := self classFactory silentlyNewClass.
otherClass compileSilently: 'yetAnotherMethod ^[[self ' , selector asString , ']]'.
senders := self systemNavigationToTest allSendersOf: selector.
self assert: (senders collect: [ :method | method selector ]) asSet equals: { #anotherMethod. #yetAnotherMethod } asSet
]