Skip to content

Commit

Permalink
Manages module references
Browse files Browse the repository at this point in the history
  • Loading branch information
jecisc committed Dec 20, 2024
1 parent 82e3278 commit f1eae7a
Show file tree
Hide file tree
Showing 3 changed files with 158 additions and 11 deletions.
5 changes: 3 additions & 2 deletions src/Famix-Python-Entities/FamixPythonModule.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,16 @@
| Relation | Origin | Opposite | Type | Comment |
|---|
| `incomingImports` | `FamixTImportable` | `importedEntity` | `FamixTImport` | List of imports of this entity|
| `incomingReferences` | `FamixTReferenceable` | `referredEntity` | `FamixTReference` | References to this entity by other entities.|
"
Class {
#name : 'FamixPythonModule',
#superclass : 'FamixPythonContainerEntity',
#traits : 'FamixTImportable + FamixTModule + FamixTPackageable + FamixTWithAccesses + FamixTWithComments + FamixTWithFunctions + FamixTWithGlobalVariables + FamixTWithInvocations + FamixTWithLambdas + FamixTWithReferences',
#classTraits : 'FamixTImportable classTrait + FamixTModule classTrait + FamixTPackageable classTrait + FamixTWithAccesses classTrait + FamixTWithComments classTrait + FamixTWithFunctions classTrait + FamixTWithGlobalVariables classTrait + FamixTWithInvocations classTrait + FamixTWithLambdas classTrait + FamixTWithReferences classTrait',
#traits : 'FamixTImportable + FamixTModule + FamixTPackageable + FamixTReferenceable + FamixTWithAccesses + FamixTWithComments + FamixTWithFunctions + FamixTWithGlobalVariables + FamixTWithInvocations + FamixTWithLambdas + FamixTWithReferences',
#classTraits : 'FamixTImportable classTrait + FamixTModule classTrait + FamixTPackageable classTrait + FamixTReferenceable classTrait + FamixTWithAccesses classTrait + FamixTWithComments classTrait + FamixTWithFunctions classTrait + FamixTWithGlobalVariables classTrait + FamixTWithInvocations classTrait + FamixTWithLambdas classTrait + FamixTWithReferences classTrait',
#category : 'Famix-Python-Entities-Entities',
#package : 'Famix-Python-Entities',
#tag : 'Entities'
Expand Down
1 change: 1 addition & 0 deletions src/Famix-Python-Generator/FamixPythonGenerator.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ FamixPythonGenerator >> defineHierarchy [
module --|> #TWithLambdas.
module --|> #TWithAccesses.
module --|> #TWithReferences.
module --|> #TReferenceable.

namedEntity --|> sourcedEntity.
namedEntity --|> #TInvocationsReceiver.
Expand Down
163 changes: 154 additions & 9 deletions src/Famix-Python-Importer-Tests/FamixPythonProject1Test.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -2114,6 +2114,151 @@ FamixPythonProject1Test >> testModuleInSubpackageImportsGlobalVariableFromAModul
self assert: import sourceText equals: 'from .moduleInSubPackage3 import moduleInSubpackage31Variable'
]

{ #category : 'tests - references' }
FamixPythonProject1Test >> testModuleReferenceFromClass [

| module class reference |
module := self moduleNamed: 'moduleToReference3'.
class := self classNamed: 'ClassReferencingAModule'.

reference := module incomingReferences detect: [ :aReference | aReference referencer = class ].

self assert: reference class equals: FamixPythonReference.
self assert: reference source equals: class.
self assert: reference referencer equals: class.
self assert: reference target equals: module.
self assert: reference referredEntity equals: module.
self assert: (class outgoingReferences anySatisfy: [ :aReference | aReference referredEntity = module ])
]

{ #category : 'tests - references' }
FamixPythonProject1Test >> testModuleReferenceFromFunction [

| module function reference |
module := self moduleNamed: 'moduleToReference5'.
function := self functionNamed: 'function_referencing_module'.

reference := module incomingReferences detect: [ :aReference | aReference referencer = function ].

self assert: reference class equals: FamixPythonReference.
self assert: reference source equals: function.
self assert: reference referencer equals: function.
self assert: reference target equals: module.
self assert: reference referredEntity equals: module.
self assert: (function outgoingReferences anySatisfy: [ :aReference | aReference referredEntity = module ])
]

{ #category : 'tests - references' }
FamixPythonProject1Test >> testModuleReferenceFromImportedEntityWithAlias [

| module module2 reference |
module := self moduleNamed: 'moduleToReference7'.
module2 := self moduleNamed: 'modulesReferencingThings'.

reference := module incomingReferences detect: [ :aReference | aReference referencer = module2 ].

self assert: reference class equals: FamixPythonReference.
self assert: reference source equals: module2.
self assert: reference referencer equals: module2.
self assert: reference target equals: module.
self assert: reference referredEntity equals: module.
self assert: (module2 outgoingReferences anySatisfy: [ :aReference | aReference referredEntity = module ])
]

{ #category : 'tests - references' }
FamixPythonProject1Test >> testModuleReferenceFromImportedEntityWithAliasSourceAnchor [

| module module2 reference |
module := self moduleNamed: 'moduleToReference7'.
module2 := self moduleNamed: 'modulesReferencingThings'.

reference := module incomingReferences detect: [ :aReference | aReference referencer = module2 ].

self assert: reference sourceAnchor isNotNil.
self assert: reference sourceText equals: 'module'
]

{ #category : 'tests - references' }
FamixPythonProject1Test >> testModuleReferenceFromLambda [

| module lambda reference |
module := self moduleNamed: 'moduleToReference6'.
lambda := (self functionNamed: 'function_with_lambda_with_module_ref') lambdas anyOne.

reference := module incomingReferences detect: [ :aReference | aReference referencer = lambda ].

self assert: reference class equals: FamixPythonReference.
self assert: reference source equals: lambda.
self assert: reference referencer equals: lambda.
self assert: reference target equals: module.
self assert: reference referredEntity equals: module.
self assert: (lambda outgoingReferences anySatisfy: [ :aReference | aReference referredEntity = module ])
]

{ #category : 'tests - references' }
FamixPythonProject1Test >> testModuleReferenceFromMethod [

| module method reference |
module := self moduleNamed: 'moduleToReference4'.
method := self methodNamed: 'method_referencing_module'.

reference := module incomingReferences detect: [ :aReference | aReference referencer = method ].

self assert: reference class equals: FamixPythonReference.
self assert: reference source equals: method.
self assert: reference referencer equals: method.
self assert: reference target equals: module.
self assert: reference referredEntity equals: module.
self assert: (method outgoingReferences anySatisfy: [ :aReference | aReference referredEntity = module ])
]

{ #category : 'tests - references' }
FamixPythonProject1Test >> testModuleReferenceFromModule [

| module module2 reference |
module := self moduleNamed: 'moduleToReference2'.
module2 := self moduleNamed: 'modulesReferencingThings'.

reference := module incomingReferences detect: [ :aReference | aReference referencer = module2 ].

self assert: reference class equals: FamixPythonReference.
self assert: reference source equals: module2.
self assert: reference referencer equals: module2.
self assert: reference target equals: module.
self assert: reference referredEntity equals: module.
self assert: (module2 outgoingReferences anySatisfy: [ :aReference | aReference referredEntity = module ])
]

{ #category : 'tests - references' }
FamixPythonProject1Test >> testModuleReferenceFromPackage [

| module package reference |
module := self moduleNamed: 'moduleToReference1'.
package := self packageNamed: 'subsubpackage1'.

reference := module incomingReferences detect: [ :aReference | aReference referencer = package ].

self assert: reference class equals: FamixPythonReference.
self assert: reference source equals: package.
self assert: reference referencer equals: package.
self assert: reference target equals: module.
self assert: reference referredEntity equals: module.
self assert: (package outgoingReferences anySatisfy: [ :aReference | aReference referredEntity = module ])
]

{ #category : 'tests - references' }
FamixPythonProject1Test >> testModuleReferenceSourceAnchor [

| module class reference |
module := self moduleNamed: 'moduleToReference3'.
class := self classNamed: 'ClassReferencingAModule'.

reference := module incomingReferences detect: [ :aReference | aReference referencer = class ].

self assert: reference sourceAnchor isNotNil.
self assert: reference sourceText equals: 'packagesToReference.moduleToReference3'
]

{ #category : 'tests - modules' }
FamixPythonProject1Test >> testModulesWithTheSameNameAreDifferent [

Expand Down Expand Up @@ -2350,28 +2495,28 @@ FamixPythonProject1Test >> testPackageReferenceFromFunction [
{ #category : 'tests - references' }
FamixPythonProject1Test >> testPackageReferenceFromImportedEntityWithAlias [

| package class reference |
| package module reference |
package := self packageNamed: 'packagesToReference7'.
class := self moduleNamed: 'modulesReferencingThings'.
module := self moduleNamed: 'modulesReferencingThings'.

reference := package incomingReferences detect: [ :aReference | aReference referencer = class ].
reference := package incomingReferences detect: [ :aReference | aReference referencer = module ].

self assert: reference class equals: FamixPythonReference.
self assert: reference source equals: class.
self assert: reference referencer equals: class.
self assert: reference source equals: module.
self assert: reference referencer equals: module.
self assert: reference target equals: package.
self assert: reference referredEntity equals: package.
self assert: (class outgoingReferences anySatisfy: [ :aReference | aReference referredEntity = package ])
self assert: (module outgoingReferences anySatisfy: [ :aReference | aReference referredEntity = package ])
]

{ #category : 'tests - references' }
FamixPythonProject1Test >> testPackageReferenceFromImportedEntityWithAliasSourceAnchor [

| package class reference |
| package module reference |
package := self packageNamed: 'packagesToReference7'.
class := self moduleNamed: 'modulesReferencingThings'.
module := self moduleNamed: 'modulesReferencingThings'.

reference := package incomingReferences detect: [ :aReference | aReference referencer = class ].
reference := package incomingReferences detect: [ :aReference | aReference referencer = module ].

self assert: reference sourceAnchor isNotNil.
self assert: reference sourceText equals: 'package'
Expand Down

0 comments on commit f1eae7a

Please sign in to comment.