Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Copy generator #51

Merged
merged 5 commits into from
Feb 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
"
A FASTCopyVisitorCodeGeneratorTest is a test class for testing the behavior of FASTCopyVisitorCodeGenerator
"
Class {
#name : #FASTCopyVisitorCodeGeneratorTest,
#superclass : #TestCase,
#instVars : [
'visitorClass',
'visitorMethods',
'generator'
],
#category : #'FAST-Core-Tools-Tests-VisitorGenerator'
}

{ #category : #running }
FASTCopyVisitorCodeGeneratorTest >> assertGeneratedMethod: aBlock [

visitorMethods
detect: [ :asso | aBlock value: asso key ]
ifNone: [ self fail ]
]

{ #category : #running }
FASTCopyVisitorCodeGeneratorTest >> setUp [
super setUp.

generator := FASTCopyVisitorCodeGenerator new.

visitorClass := Mock named: 'visitor'.
visitorMethods := OrderedCollection new.

(visitorClass stub compile: Any classified: Any) will: [ :method :category |
visitorMethods add: method -> category ]
]

{ #category : #tests }
FASTCopyVisitorCodeGeneratorTest >> testGenerateCopyMethod [

generator rootClass: FmxTraitsTestGenerateAccessorBEntity visitorClass: visitorClass.

self assertGeneratedMethod: [ :methodCode |
methodCode beginsWith: 'copy: ' ]
]

{ #category : #tests }
FASTCopyVisitorCodeGeneratorTest >> testShouldCopyPropertyFor [

| property |

property := FmxTraitsTestGenerateAccessorBClassA asMooseDescription
allProperties detect: [ :prop | prop name = 'relationToB' ].

generator package: (FmxTraitsTestGenerateAccessorBClassA metamodel packages detect: [ :p | p name = 'Famix-MetamodelBuilder-TestsTraitsResources-A' ]).
self assert: (generator shouldCopyProperty: property for: FmxTraitsTestGenerateAccessorBClassA).


]

{ #category : #tests }
FASTCopyVisitorCodeGeneratorTest >> testShouldNotCopyDerivedProperty [

| property |

property := FmxTraitsTestGenerateAccessorBClassB asMooseDescription
allProperties detect: [ :prop | prop name = 'relationToA' ].

self assert: property isDerived.
self deny: (generator shouldCopyProperty: property for: FmxTraitsTestGenerateAccessorBClassB)
]

{ #category : #tests }
FASTCopyVisitorCodeGeneratorTest >> testShouldNotCopyPropertyFromOtherPackage [

| property |

property := FmxTraitsTestGenerateAccessorBClassB asMooseDescription
allProperties detect: [ :prop | prop name = 'relationToA' ].

generator package: (FmxTraitsTestGenerateAccessorBClassA metamodel packages detect: [ :p | p name = 'Famix-MetamodelBuilder-TestsTraitsResources-B' ]).
self assert: (generator shouldCopyProperty: property for: FmxTraitsTestGenerateAccessorBClassA)
]
14 changes: 0 additions & 14 deletions src/FAST-Core-Tools-Tests/FASTLocalResolverVisitorTest.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -93,20 +93,6 @@ FASTLocalResolverVisitorTest >> testLocalDeclarationFor [
self assert: declNode localUses first equals: refNode
]

{ #category : #tests }
FASTLocalResolverVisitorTest >> testNonLocalDeclarationWithName [
"testing helper method #nonLocalDeclaration:withName:"
| node |
node := FASTEntity new.
localResolver resetScopes.

localResolver nonLocalDeclaration: node withName: 'blah'.

self assert: node localDeclaration notNil.
self assert: node localDeclaration class equals: FASTNonLocalDeclaration.
self assert: node localDeclaration name equals: 'blah'
]

{ #category : #tests }
FASTLocalResolverVisitorTest >> testResetScopes [
localResolver resetScopes.
Expand Down
32 changes: 25 additions & 7 deletions src/FAST-Core-Tools/FASTCopyVisitorCodeGenerator.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ FASTCopyVisitorCodeGenerator >> generateVisit: aModelClass in: aVisitorClass [
self generateVisitBody: aModelClass in: outputStream.
].

aVisitorClass compile: code classified: AbstractProtocol unclassified
aVisitorClass compile: code classified: Protocol unclassified
]

{ #category : #'code generation' }
Expand All @@ -136,6 +136,30 @@ FASTCopyVisitorCodeGenerator >> isSamePackage: aFMProperty [
^package = aFMProperty mmClass package
]

{ #category : #'accessing - private tests' }
FASTCopyVisitorCodeGenerator >> metamodel [

^ metamodel
]

{ #category : #'accessing - private tests' }
FASTCopyVisitorCodeGenerator >> metamodel: anObject [

metamodel := anObject
]

{ #category : #'accessing - private tests' }
FASTCopyVisitorCodeGenerator >> package [

^ package
]

{ #category : #'accessing - private tests' }
FASTCopyVisitorCodeGenerator >> package: anObject [

package := anObject
]

{ #category : #run }
FASTCopyVisitorCodeGenerator >> rootClass: aFASTEntityClass visitorClass: aFASTVisitorClass [

Expand All @@ -152,12 +176,6 @@ FASTCopyVisitorCodeGenerator >> rootClass: aFASTEntityClass visitorClass: aFASTV
{ #category : #'code generation' }
FASTCopyVisitorCodeGenerator >> shouldCopyProperty: property for: aModelClass [

"Dealing with some special cases (errors in the meta-model, they should be marked #isContainer)"
(property name beginsWith: 'parent') ifTrue: [ ^false ].

(#(assignedIn invokedIn container) anySatisfy: [ :specialCase | property name = specialCase ])
ifTrue: [ ^false ].

(#(startPos endPos) anySatisfy: [ :specialCase | property name = specialCase ])
ifTrue: [ ^true ].

Expand Down
31 changes: 13 additions & 18 deletions src/FAST-Core-Tools/FASTLocalResolverVisitor.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,13 @@ FASTLocalResolverVisitor >> currentScope [

{ #category : #private }
FASTLocalResolverVisitor >> findDeclaration: aName [
"search for 'aName' in the current scope and its parent.
Returns nil if not found"

^self currentScope
at: aName
ifAbsent: [ |scope decl|
decl := nil.
scope := self popScope.
self hasScopes
ifTrue: [ decl := self findDeclaration: aName ].
Expand All @@ -57,12 +61,6 @@ FASTLocalResolverVisitor >> localDeclaration: declarationNode for: referingNode
declarationNode addLocalUse: referingNode
]

{ #category : #private }
FASTLocalResolverVisitor >> nonLocalDeclaration: referingNode withName: name [
self localDeclaration: (self scopeAddNonLocalDeclaration: name) for: referingNode

]

{ #category : #api }
FASTLocalResolverVisitor >> on: aFASTBehaviouralEntity [

Expand Down Expand Up @@ -141,17 +139,11 @@ FASTLocalResolverVisitor >> visitFASTIdentifierExpression: aFASTIdentifier [
- if not found, creates a NonLocalDeclaration for it
- binds this reference to the declaration"

(self findDeclaration: aFASTIdentifier name)
ifNil: [
self
nonLocalDeclaration: aFASTIdentifier
withName: aFASTIdentifier name
]
ifNotNil: [ :decl |
self
localDeclaration: decl
for: aFASTIdentifier
]
self
localDeclaration: (
(self findDeclaration: aFASTIdentifier name)
ifNil: [ self scopeAddNonLocalDeclaration: aFASTIdentifier name ])
for: aFASTIdentifier
]

{ #category : #visiting }
Expand All @@ -171,6 +163,9 @@ FASTLocalResolverVisitor >> visitFASTScopeChildren: childrenNodes [
"creates a new scope and visit its children nodes
Order of nodes is important to ensure declarations are visited before statements"

self shouldNotImplement.
"Actually does not seem to be used anywhere ..."

self pushScope.

(childrenNodes sorted: [:a :b | a startPos < b startPos])
Expand All @@ -179,7 +174,7 @@ FASTLocalResolverVisitor >> visitFASTScopeChildren: childrenNodes [
^self popScope
]

{ #category : #generated }
{ #category : #visiting }
FASTLocalResolverVisitor >> visitFASTTStatementBlock: aFASTJavaStatementBlock [
self pushScope.

Expand Down
Loading