From f1eae7a2ca254df9d42e732676aedcf44a9e9c31 Mon Sep 17 00:00:00 2001 From: CyrilFerlicot Date: Fri, 20 Dec 2024 17:24:44 +0100 Subject: [PATCH] Manages module references --- .../FamixPythonModule.class.st | 5 +- .../FamixPythonGenerator.class.st | 1 + .../FamixPythonProject1Test.class.st | 163 +++++++++++++++++- 3 files changed, 158 insertions(+), 11 deletions(-) diff --git a/src/Famix-Python-Entities/FamixPythonModule.class.st b/src/Famix-Python-Entities/FamixPythonModule.class.st index fbc255f..ea37634 100644 --- a/src/Famix-Python-Entities/FamixPythonModule.class.st +++ b/src/Famix-Python-Entities/FamixPythonModule.class.st @@ -27,6 +27,7 @@ | 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.| @@ -34,8 +35,8 @@ 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' diff --git a/src/Famix-Python-Generator/FamixPythonGenerator.class.st b/src/Famix-Python-Generator/FamixPythonGenerator.class.st index e1df12b..4d7a41e 100644 --- a/src/Famix-Python-Generator/FamixPythonGenerator.class.st +++ b/src/Famix-Python-Generator/FamixPythonGenerator.class.st @@ -168,6 +168,7 @@ FamixPythonGenerator >> defineHierarchy [ module --|> #TWithLambdas. module --|> #TWithAccesses. module --|> #TWithReferences. + module --|> #TReferenceable. namedEntity --|> sourcedEntity. namedEntity --|> #TInvocationsReceiver. diff --git a/src/Famix-Python-Importer-Tests/FamixPythonProject1Test.class.st b/src/Famix-Python-Importer-Tests/FamixPythonProject1Test.class.st index 81d8619..9c753ee 100644 --- a/src/Famix-Python-Importer-Tests/FamixPythonProject1Test.class.st +++ b/src/Famix-Python-Importer-Tests/FamixPythonProject1Test.class.st @@ -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 [ @@ -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'