Skip to content

Commit

Permalink
adding a method as an alternative to bestNodeFor and tests for it
Browse files Browse the repository at this point in the history
  • Loading branch information
myroslavarm committed Jul 23, 2019
1 parent a7dda8e commit a0bf935
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 0 deletions.
48 changes: 48 additions & 0 deletions src/AST-Core-Tests/RBMethodNodeTest.class.st
Expand Up @@ -57,6 +57,54 @@ RBMethodNodeTest >> testCommentedMethodHasComments [

]

{ #category : #tests }
RBMethodNodeTest >> testNodeForOffsetAssignment [
"test the case of a Assignment"
| source ast foundNode |
source := 'method Object := Class'.
ast := RBParser parseMethod: source.
foundNode := ast nodeForOffset: 9.
self assert: (foundNode class == RBVariableNode).
foundNode := ast nodeForOffset: 14.
self assert: (foundNode class == RBAssignmentNode).
foundNode := ast nodeForOffset: 19.
self assert: (foundNode class == RBVariableNode)
]

{ #category : #tests }
RBMethodNodeTest >> testNodeForOffsetMessage [
"test the case of Messages"
| source ast foundNode |
source := 'method Object doit: Class'.
ast := RBParser parseMethod: source.
foundNode := ast nodeForOffset: 9.
self assert: (foundNode class == RBVariableNode).
foundNode := ast nodeForOffset: 14.
self assert: (foundNode class == RBMessageNode).
foundNode := ast nodeForOffset: 22.
self assert: (foundNode class == RBVariableNode)
]

{ #category : #tests }
RBMethodNodeTest >> testNodeForOffsetTempDefinition [
"test the case of Messages"
| source ast foundNode |
source := 'method | temp |'.
ast := RBParser parseFaultyMethod: source.
foundNode := ast nodeForOffset: 12.
self assert: (foundNode class == RBVariableNode).
]

{ #category : #tests }
RBMethodNodeTest >> testNodeForOffsetVar [
"test the case of a variable"
| source ast foundNode |
source := 'method Object'.
ast := RBParser parseMethod: source.
foundNode := ast nodeForOffset: 9.
self assert: (foundNode class == RBVariableNode)
]

{ #category : #tests }
RBMethodNodeTest >> testSelectorAndArgumentNames [

Expand Down
11 changes: 11 additions & 0 deletions src/AST-Core/RBProgramNode.class.st
Expand Up @@ -633,6 +633,17 @@ RBProgramNode >> newSource [
^self formattedCode
]

{ #category : #'node access' }
RBProgramNode >> nodeForOffset: anInteger [
| children |
"choosing the best node on the specific offset"
children := self children.
"when we are on a leaf, we take the leaf node"
(children isEmpty) ifTrue: [ (self sourceInterval includes: anInteger) ifTrue: [^self]].
"if the node has children then we check the children"
children do: [:each | (each sourceInterval includes: anInteger) ifTrue: [^each nodeForOffset: anInteger] ].
]

{ #category : #iterating }
RBProgramNode >> nodesDo: aBlock [
aBlock value: self.
Expand Down

0 comments on commit a0bf935

Please sign in to comment.