-
-
Notifications
You must be signed in to change notification settings - Fork 353
/
MethodPragmaTest.class.st
356 lines (292 loc) · 11.9 KB
/
MethodPragmaTest.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
"
SUnit tests for method pragmas
"
Class {
#name : #MethodPragmaTest,
#superclass : #TestCase,
#category : #'OpalCompiler-Tests-Misc'
}
{ #category : #utilities }
MethodPragmaTest >> assertPragma: aString givesKeyword: aSymbol arguments: anArray [
| pragma |
pragma := self pragma: aString selector: #zork.
self assert: pragma keyword = aSymbol.
self assert: pragma arguments = anArray.
]
{ #category : #utilities }
MethodPragmaTest >> compile: aString selector: aSelector [
self class
compileSilently: aSelector , String lf , aString
classified: self methodCategory.
^ self class >> aSelector.
]
{ #category : #utilities }
MethodPragmaTest >> methodCategory [
^ #generated
]
{ #category : #utilities }
MethodPragmaTest >> pragma: aString selector: aSelector [
^ (self compile: '<' , aString , '>' selector: aSelector)
pragmas first.
]
{ #category : #utilities }
MethodPragmaTest >> pragma: aSymbol selector: aSelector times: anInteger [
^ (self
compile: (String streamContents: [ :stream |
(1 to: anInteger) asArray shuffled do: [ :each |
stream
nextPut: $<; nextPutAll: aSymbol; space;
print: each; nextPut: $>; cr ] ])
selector: aSelector)
pragmas.
]
{ #category : #running }
MethodPragmaTest >> tearDown [
(self class organization listAtCategoryNamed: self methodCategory)
do: [ :each | self class removeSelectorSilently: each ].
self class organization removeCategory: self methodCategory.
super tearDown
]
{ #category : #'testing-finding' }
MethodPragmaTest >> testAllNamedFromTo [
| pragmasCompiled pragmasDetected |
pragmasCompiled := self pragma: #foo: selector: #bar times: 5.
pragmasDetected := Pragma allNamed: #foo: from: self class to: Object.
self assert: pragmasDetected = pragmasCompiled.
pragmasDetected := Pragma allNamed: #foo: from: Object to: Object.
self assertEmpty: pragmasDetected
]
{ #category : #'testing-finding' }
MethodPragmaTest >> testAllNamedFromToSortedByArgument [
| pragmasCompiled pragmasDetected |
pragmasCompiled := self pragma: #foo: selector: #bar times: 5.
pragmasDetected := Pragma allNamed: #foo: from: self class to: Object sortedByArgument: 1.
self assert: pragmasDetected = (pragmasCompiled
sort: [ :a :b | (a argumentAt: 1) < (b argumentAt: 1) ])
]
{ #category : #'testing-finding' }
MethodPragmaTest >> testAllNamedFromToSortedUsing [
| pragmasCompiled pragmasDetected |
pragmasCompiled := self pragma: #foo: selector: #bar times: 5.
pragmasDetected := Pragma
allNamed: #foo: from: self class to: Object
sortedUsing: [ :a :b | (a argumentAt: 1) > (b argumentAt: 1) ].
self assert: pragmasDetected = (pragmasCompiled
sort: [ :a :b | (a argumentAt: 1) > (b argumentAt: 1) ]).
]
{ #category : #'testing-finding' }
MethodPragmaTest >> testAllNamedIn [
| pragmasCompiled pragmasDetected |
pragmasCompiled := self pragma: #foo: selector: #bar times: 5.
pragmasDetected := Pragma allNamed: #foo: in: self class.
self assert: pragmasDetected = pragmasCompiled.
pragmasDetected := Pragma allNamed: #foo: in: Object.
self assertEmpty: pragmasDetected
]
{ #category : #'testing-finding' }
MethodPragmaTest >> testAllNamedInSortedByArgument [
| pragmasCompiled pragmasDetected |
pragmasCompiled := self pragma: #foo: selector: #bar times: 5.
pragmasDetected := Pragma allNamed: #foo: in: self class sortedByArgument: 1.
self assert: pragmasDetected = (pragmasCompiled
sort: [ :a :b | (a argumentAt: 1) < (b argumentAt: 1) ])
]
{ #category : #'testing-finding' }
MethodPragmaTest >> testAllNamedInSortedUsing [
| pragmasCompiled pragmasDetected |
pragmasCompiled := self pragma: #foo: selector: #bar times: 5.
pragmasDetected := Pragma
allNamed: #foo: in: self class
sortedUsing: [ :a :b | (a argumentAt: 1) > (b argumentAt: 1) ].
self assert: pragmasDetected = (pragmasCompiled
sort: [ :a :b | (a argumentAt: 1) > (b argumentAt: 1) ]).
]
{ #category : #testing }
MethodPragmaTest >> testArgumentAt [
| pragma |
pragma := Pragma keyword: #value:value:value: arguments: #( 3 2 1 ).
self assert: (pragma argumentAt: 1) = 3.
self assert: (pragma argumentAt: 2) = 2.
self assert: (pragma argumentAt: 3) = 1
]
{ #category : #'testing-pragma' }
MethodPragmaTest >> testArguments [
| pragma |
pragma := Pragma keyword: #foo: arguments: #( 123 ).
self assert: pragma arguments = #( 123 ).
]
{ #category : #'testing-compiler' }
MethodPragmaTest >> testCompileArray [
self assertPragma: 'foo: #()' givesKeyword: #foo: arguments: #( () ).
self assertPragma: 'foo: #( foo )' givesKeyword: #foo: arguments: #( ( foo ) ).
self assertPragma: 'foo: #( foo: )' givesKeyword: #foo: arguments: #( ( foo: ) ).
self assertPragma: 'foo: #( 12 )' givesKeyword: #foo: arguments: #( ( 12 ) ).
self assertPragma: 'foo: #( true )' givesKeyword: #foo: arguments: #( ( true ) ).
]
{ #category : #'testing-compiler' }
MethodPragmaTest >> testCompileBinary [
self assertPragma: ' = 1' givesKeyword: #= arguments: #( 1 ).
self assertPragma: ' , 3' givesKeyword: #, arguments: #( 3 ).
self assertPragma: ' > 4' givesKeyword: #> arguments: #( 4 ).
self assertPragma: ' < 5' givesKeyword: #< arguments: #( 5 ).
self assertPragma: ' == 1' givesKeyword: #== arguments: #( 1 ).
self assertPragma: ' <> 3' givesKeyword: #<> arguments: #( 3 ).
self assertPragma: ' >< 4' givesKeyword: #>< arguments: #( 4 ).
self assertPragma: ' ** 5' givesKeyword: #** arguments: #( 5 )
]
{ #category : #'testing-compiler' }
MethodPragmaTest >> testCompileCharacter [
self assertPragma: 'foo: $a' givesKeyword: #foo: arguments: #( $a ).
self assertPragma: 'foo: $ ' givesKeyword: #foo: arguments: #( $ ).
]
{ #category : #'testing-compiler' }
MethodPragmaTest >> testCompileEmpty [
self assertPragma: 'foo' givesKeyword: #foo arguments: #().
]
{ #category : #'testing-compiler' }
MethodPragmaTest >> testCompileFull [
self assertPragma: 'foo: 1' givesKeyword: #foo: arguments: #( 1 ).
self assertPragma: 'foo: 1 bar: 2' givesKeyword: #foo:bar: arguments: #( 1 2 ).
]
{ #category : #'testing-compiler' }
MethodPragmaTest >> testCompileInvalid [
"Invalid pragmas should properly raise an error."
self should: [ self compile: '<>' selector: #zork ] raise: SyntaxErrorNotification.
self should: [ self compile: '<1>' selector: #zork ] raise: SyntaxErrorNotification.
self should: [ self compile: '<#123>' selector: #zork ] raise: SyntaxErrorNotification.
self should: [ self compile: '<foo bar>' selector: #zork ] raise: SyntaxErrorNotification.
self should: [ self compile: '<foo 1>' selector: #zork ] raise: SyntaxErrorNotification.
self should: [ self compile: '<foo bar zork>' selector: #zork ] raise: SyntaxErrorNotification.
self should: [ self compile: '<foo bar 1>' selector: #zork ] raise: SyntaxErrorNotification.
self should: [ self compile: '<foo: #bar: zork:>' selector: #zork ] raise: SyntaxErrorNotification.
self should: [ self compile: '<<1>' selector: #zork ] raise: SyntaxErrorNotification.
self should: [ self compile: '<=2>' selector: #zork ] raise: SyntaxErrorNotification.
self should: [ self compile: '< =1 = >' selector: #zork ] raise: SyntaxErrorNotification.
self should: [ self compile: '< =1 =2 >' selector: #zork ] raise: SyntaxErrorNotification.
]
{ #category : #'testing-compiler' }
MethodPragmaTest >> testCompileNumber [
self assertPragma: 'foo: 123' givesKeyword: #foo: arguments: #( 123 ).
self assertPragma: 'foo: -123' givesKeyword: #foo: arguments: #( -123 ).
self assertPragma: 'foo: 12.3' givesKeyword: #foo: arguments: #( 12.3 ).
self assertPragma: 'foo: -12.3' givesKeyword: #foo: arguments: #( -12.3 ).
]
{ #category : #'testing-compiler' }
MethodPragmaTest >> testCompileString [
self assertPragma: 'foo: ''''' givesKeyword: #foo: arguments: #( '' ).
self assertPragma: 'foo: ''bar''' givesKeyword: #foo: arguments: #( 'bar' ).
]
{ #category : #'testing-compiler' }
MethodPragmaTest >> testCompileSymbol [
self assertPragma: 'foo: #bar' givesKeyword: #foo: arguments: #( bar ).
self assertPragma: 'foo: #bar:' givesKeyword: #foo: arguments: #( bar: ).
self assertPragma: 'foo: #bar:zork:' givesKeyword: #foo: arguments: #( bar:zork: ).
]
{ #category : #'testing-compiler' }
MethodPragmaTest >> testCompileTemps [
"Pragmas should be placeable before and after temps."
self assert: (self compile: '| temps | <foo>' selector: #zork) pragmas notEmpty.
self assert: (self compile: '<foo> | temps |' selector: #zork) pragmas notEmpty
]
{ #category : #'testing-compiler' }
MethodPragmaTest >> testCompileValue [
self assertPragma: 'foo: true' givesKeyword: #foo: arguments: #( true ).
self assertPragma: 'foo: false' givesKeyword: #foo: arguments: #( false ).
self assertPragma: 'foo: nil' givesKeyword: #foo: arguments: #( nil )
]
{ #category : #'testing-pragma' }
MethodPragmaTest >> testKeyword [
| pragma |
pragma := Pragma keyword: #foo: arguments: #( 123 ).
self assert: pragma keyword = #foo:.
]
{ #category : #'testing-pragma' }
MethodPragmaTest >> testMessage [
| pragma message |
pragma := Pragma keyword: #foo: arguments: #( 123 ).
message := pragma message.
self assert: message selector = #foo:.
self assert: message arguments = #( 123 ).
]
{ #category : #'testing-method' }
MethodPragmaTest >> testMethod [
| pragma |
pragma := self pragma: 'foo' selector: #bar.
self assert: pragma method == (self class >> #bar).
]
{ #category : #'testing-method' }
MethodPragmaTest >> testMethodClass [
| pragma |
pragma := self pragma: 'foo' selector: #bar.
self assert: pragma methodClass == self class.
]
{ #category : #'testing-method' }
MethodPragmaTest >> testMethodSelector [
| pragma |
pragma := self pragma: 'foo' selector: #bar.
self assert: pragma methodSelector == #bar.
]
{ #category : #'testing-compiled' }
MethodPragmaTest >> testNoPragma [
| method |
method := self compile: '' selector: #foo.
self assert: method pragmas = #().
]
{ #category : #testing }
MethodPragmaTest >> testNumArgs [
| pragma |
pragma := Pragma keyword: #value arguments: #().
self assert: pragma numArgs = 0.
pragma := Pragma keyword: #+ arguments: #( 1 ).
self assert: pragma numArgs = 1.
pragma := Pragma keyword: #value:value: arguments: #( 1 2 ).
self assert: pragma numArgs = 2
]
{ #category : #'testing-primitives' }
MethodPragmaTest >> testPrimitiveIndexed1 [
"This test useses the #instVarAt: primitive."
self compile: '<primitive: 74> ^ #inst' selector: #inst.
self assert: self inst = #inst.
]
{ #category : #'testing-primitives' }
MethodPragmaTest >> testPrimitiveIndexed2 [
"This test useses the #identityHash primitive."
self compile: '<primitive: 75> ^ #idHash' selector: #idHash.
self assert: self idHash = self basicIdentityHash.
]
{ #category : #'testing-primitives' }
MethodPragmaTest >> testPrimitiveNamed1 [
"This test useses the #primitiveDirectoryLookup primitive."
self compile: '<primitive: ''primitiveDirectoryLookup'' module: ''FilePlugin''> ^ #lookup' selector: #lookup.
self assert: self lookup equals: #lookup.
]
{ #category : #'testing-primitives' }
MethodPragmaTest >> testPrimitiveNamed2 [
"This test useses the #primPathNameDelimiter primitive."
self compile: '<primitive: ''primitiveDirectoryDelimitor'' module: ''FilePlugin''> ^ #delim' selector: #delim.
self assert: self delim equals: File delimiter.
]
{ #category : #'testing-primitives' }
MethodPragmaTest >> testPrimitiveNamedErrorCode2 [
"This test useses the #primitiveDirectoryLookup primitive."
self compile: '<primitive: ''primitiveDirectoryLookup'' module: ''FilePlugin'' error: errorCode> ^ errorCode' selector: #lookup.
self assert: self lookup equals: nil.
]
{ #category : #testing }
MethodPragmaTest >> testSendTo [
| pragma wasHere |
pragma := Pragma keyword: #value:value: arguments: #( 1 2 ).
self assert: (pragma sendTo: [ :a :b |
self assert: a = 1; assert: b = 2.
wasHere := true ]).
self assert: wasHere
]
{ #category : #testing }
MethodPragmaTest >> testWithArgumentsDo [
| pragma wasHere |
pragma := Pragma keyword: #add:after: arguments: #( 1 2 ).
self assert: (pragma withArgumentsDo: [ :a :b |
self assert: a = 1; assert: b = 2.
wasHere := true ]).
self assert: wasHere
]