Skip to content

Commit

Permalink
Refactor tests of imports and start to clean properties of imports
Browse files Browse the repository at this point in the history
  • Loading branch information
jecisc committed Sep 26, 2024
1 parent 20fd303 commit b86866a
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ FamixPythonAbstractImporterTest >> globalVariableNamed: aName [
{ #category : 'accessing' }
FamixPythonAbstractImporterTest >> importNamed: aName [

^ self model allImports detect: [ :import | import entityName = aName ]
^ self model allImports detect: [ :import | import importedEntity name = aName ]
]

{ #category : 'accessing' }
Expand Down
64 changes: 11 additions & 53 deletions src/Famix-Python-Importer-Tests/FamixPythonImporterTest.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Class {
{ #category : 'accessing' }
FamixPythonImporterTest >> model [

^ importer model
^ importer visitor model
]

{ #category : 'running' }
Expand All @@ -23,21 +23,19 @@ FamixPythonImporterTest >> moduleName2 [

{ #category : 'running' }
FamixPythonImporterTest >> parseCode: aString [

^ importer accept: ((PythonParser parseWithErrors: aString) ifNotNil: [ :each |
(each isKindOf: SmaCCParseNode) ifTrue: [
each completeSource: aString.
each filename: 'fileForTest.py' asFileReference ].
each ])

| file |
file := FileSystem memory / 'testPythonModelName.py'.
file writeStreamDo: [ :s | s nextPutAll: aString ].
^ importer import: file
]

{ #category : 'running' }
FamixPythonImporterTest >> setUp [

super setUp.

importer := FamixPythonImporterVisitor new.
importer model name: 'testPythonModelName'
importer := FamixPythonImporter new
]

{ #category : 'tests' }
Expand Down Expand Up @@ -202,53 +200,10 @@ def identity():
self assert: famix functionOwner name equals: 'identity'
]

{ #category : 'tests - import' }
FamixPythonImporterTest >> testImport [

self parseCode: '
import pygame
import random
'.

self assert: (self importNamed: 'pygame') isNotNil.
self assert: (self importNamed: 'random') isNotNil
]

{ #category : 'tests - import' }
FamixPythonImporterTest >> testImportClassFromModule [

self parseCode: '
from keras.layers import Input
'.

self assert: (self importNamed: 'Input') isNotNil.
self assert: (self importNamed: 'Input') fromName equals: 'keras.layers'
]

{ #category : 'tests - import' }
FamixPythonImporterTest >> testImportFunction [

self parseCode: '
import matplotlib.pyplot
'.

self assert: (self importNamed: 'matplotlib.pyplot') isNotNil
]

{ #category : 'tests - import' }
FamixPythonImporterTest >> testImportFunctionFromModule [

self parseCode: '
from random import randint
'.

self assert: (self importNamed: 'randint') isNotNil.
self assert: (self importNamed: 'randint') fromName equals: 'random'
]

{ #category : 'tests - import' }
FamixPythonImporterTest >> testImportModuleWithAlias [

self skip. "Should be managed in FamixPythonProject1Test"
self parseCode: '
import math as m
import matplotlib.pyplot as plt
Expand All @@ -264,6 +219,7 @@ import matplotlib.pyplot as plt
{ #category : 'tests - import' }
FamixPythonImporterTest >> testImportModulesWithAliases [

self skip. "Should be managed in FamixPythonProject1Test"
self parseCode: '
import math as m, matplotlib.pyplot as plt
'.
Expand All @@ -278,6 +234,7 @@ import math as m, matplotlib.pyplot as plt
{ #category : 'tests - import' }
FamixPythonImporterTest >> testImportMultiClassesFromModule [

self skip. "Should be managed in FamixPythonProject1Test"
self parseCode: '
from keras.layers import Flatten, Activation, AveragePooling2D, BatchNormalization
'.
Expand All @@ -290,6 +247,7 @@ from keras.layers import Flatten, Activation, AveragePooling2D, BatchNormalizati
{ #category : 'tests - import' }
FamixPythonImporterTest >> testImportMultiFunctionsFromModule [

self skip. "Should be managed in FamixPythonProject1Test"
self parseCode: '
from maths import add, subtract
'.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,9 @@ FamixPythonFromImportResolvable >> resolveAbsolutePathFor: currentEntity [
possibleEntities := possibleEntities flatCollect: [ :possibleEntity |
possibleEntity children select: [ :child | (child isPackage or: [ child isModule ]) and: [ child name = childName ] ] ] ].
possibleEntities ifEmpty: [ NotFound signal ].

possibleEntities size = 1 ifFalse: [ self error: 'There should be only one possible entity to import.' ].

possibleEntities := possibleEntities anyOne allChildren select: [ :child | child isNamedEntity and: [ child name = entityName ] ].
"It is possible that we have multiple entities with the same name and in that case, python select the last one declared. So we sort by source anchor position to find this one."
^ possibleEntities detectMax: [ :possibleEntity | possibleEntity sourceAnchor startPos ]
Expand Down
6 changes: 6 additions & 0 deletions src/Famix-Python-Importer/FamixPythonImporter.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,9 @@ FamixPythonImporter >> isPythonExtension: aString [

^aString = 'py'
]

{ #category : 'accessing' }
FamixPythonImporter >> visitor [

^ visitor
]
2 changes: 0 additions & 2 deletions src/Famix-Python-Importer/FamixPythonImporterVisitor.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,7 @@ FamixPythonImporterVisitor >> createImport: anImport ofName: aName from: fromNam

| import |
import := model newImport
entityName: aName;
asName: alias;
fromName: fromName;
yourself.

self setSourceAnchor: import from: anImport.
Expand Down

0 comments on commit b86866a

Please sign in to comment.