Skip to content

Commit

Permalink
Make tests more robust by ensuring we select the right entities all t…
Browse files Browse the repository at this point in the history
…he time and not a random one if two have the same name + add test on shadowing a global with a class
  • Loading branch information
jecisc committed Nov 14, 2024
1 parent d807eb7 commit b5324bd
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,34 @@ Class {
{ #category : 'accessing' }
FamixPythonAbstractImporterTest >> attributeNamed: aName [

^ self model allAttributes detect: [ :attribute | attribute name = aName ]
^ self entityNamed: aName in: self model allAttributes
]

{ #category : 'accessing' }
FamixPythonAbstractImporterTest >> classNamed: aName [

^ self model allClasses detect: [ :class | class name = aName ]
^ self entityNamed: aName in: self model allClasses
]

{ #category : 'accessing' }
FamixPythonAbstractImporterTest >> entityNamed: aName in: aCollection [

| entities |
entities := aCollection select: [ :entity | entity name = aName ].
entities size ~= 1 ifTrue: [ self error: 'More than 1 entity named ' , aName ].
^ entities anyOne
]

{ #category : 'accessing' }
FamixPythonAbstractImporterTest >> functionNamed: aName [

^ self model allFunctions detect: [ :function | function name = aName ]
^ self entityNamed: aName in: self model allFunctions
]

{ #category : 'accessing' }
FamixPythonAbstractImporterTest >> globalVariableNamed: aName [

^ self model allGlobalVariables detect: [ :variable | variable name = aName ]
^ self entityNamed: aName in: self model allGlobalVariables
]

{ #category : 'accessing' }
Expand All @@ -38,13 +47,13 @@ FamixPythonAbstractImporterTest >> importNamed: aName [
{ #category : 'accessing' }
FamixPythonAbstractImporterTest >> localVariableNamed: aName [

^ self model allLocalVariables detect: [ :variable | variable name = aName ]
^ self entityNamed: aName in: self model allLocalVariables
]

{ #category : 'accessing' }
FamixPythonAbstractImporterTest >> methodNamed: aName [

^ self model allMethods detect: [ :method | method name = aName ]
^ self entityNamed: aName in: self model allMethods
]

{ #category : 'accessing' }
Expand All @@ -62,19 +71,19 @@ FamixPythonAbstractImporterTest >> moduleName [
{ #category : 'accessing' }
FamixPythonAbstractImporterTest >> moduleNamed: aName [

^ self model allModules detect: [ :module | module name = aName ]
^ self entityNamed: aName in: self model allModules
]

{ #category : 'accessing' }
FamixPythonAbstractImporterTest >> packageNamed: aName [

^ self model allPackages detect: [ :package | package name = aName ]
^ self entityNamed: aName in: self model allPackages
]

{ #category : 'accessing' }
FamixPythonAbstractImporterTest >> parameterNamed: aName [

^ self model allParameters detect: [ :parameter | parameter name = aName ]
^ self entityNamed: aName in: self model allParameters
]

{ #category : 'accessing' }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ FamixPythonImporterWithClassesTest >> testKnowsItsModule [
FamixPythonImporterWithClassesTest >> testMethod [

| mth |
mth := self methodNamed: '__init__'.
mth := (self classNamed: 'Player') methods detect: [ :m | m name = '__init__' ].
self assert: mth class equals: FamixPythonMethod.
self assert: mth parents anyOne name equals: 'Player'.
self assert: mth name equals: '__init__'.
Expand All @@ -102,7 +102,7 @@ FamixPythonImporterWithClassesTest >> testMethodIsInModel [
FamixPythonImporterWithClassesTest >> testMethodKnowsItsClass [

| mth cl |
mth := self methodNamed: '__init__'.
mth := (self classNamed: 'Player') methods detect: [ :m | m name = '__init__' ].
cl := self classNamed: 'Player'.
self assert: mth parentType equals: cl.
self assert: mth parentType class equals: FamixPythonClass
Expand All @@ -112,8 +112,8 @@ FamixPythonImporterWithClassesTest >> testMethodKnowsItsClass [
FamixPythonImporterWithClassesTest >> testMethodWithParameter [

| param mth |
mth := self methodNamed: '__init__'.
param := self parameterNamed: 'y'.
mth := (self classNamed: 'Player') methods detect: [ :m | m name = '__init__' ].
param := mth parameters detect: [ :p | p name = 'y' ].

mth := param parentBehaviouralEntity.

Expand Down Expand Up @@ -162,7 +162,7 @@ FamixPythonImporterWithClassesTest >> testParameter [

self assert: param class equals: FamixPythonParameter.
self assert: param name equals: 'joystick_no'.
self assert: param parentBehaviouralEntity equals: (self methodNamed: '__init__')
self assert: param parentBehaviouralEntity equals: ((self classNamed: 'Player') methods detect: [ :m | m name = '__init__' ]).
]

{ #category : 'tests - classes' }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ FamixPythonImporterWithFunctionsTest >> testFunctions [
FamixPythonImporterWithFunctionsTest >> testParameter [

| param |
param := self parameterNamed: 'word'.
param := (self functionNamed: 'tryToPlaceWord') parameters detect: [ :p | p name = 'word' ].
self assert: param class equals: FamixPythonParameter.
self assert: param name equals: 'word'.
self assert: param parentBehaviouralEntity equals: (self functionNamed: 'tryToPlaceWord')
Expand Down
24 changes: 24 additions & 0 deletions src/Famix-Python-Importer-Tests/FamixPythonProject1Test.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -654,6 +654,30 @@ FamixPythonProject1Test >> testGetterAndSetter [
self deny: setter isConstructor
]

{ #category : 'tests - shadowing' }
FamixPythonProject1Test >> testGlobalShadowedByClass [

| shadower shadowed |
shadower := (self model allClasses select: [ :entity | entity name = 'global_then_function_then_global_then_class' ]) asOrderedCollection detectMax: [
:entity | entity sourceAnchor startPos ].
shadowed := (self model allGlobalVariables select: [ :entity | entity name = 'global_then_function_then_global_then_class' ]) asOrderedCollection detectMax: [
:entity | entity sourceAnchor startPos ].

self assert: shadower name equals: 'global_then_function_then_global_then_class'.
self deny: shadower isShadowed.
self assert: shadower typeContainer equals: (self moduleNamed: 'moduleWithShadowing').
self assert: shadower shadowedEntity equals: shadowed.
self assert: shadower sourceText equals: ('class global_then_function_then_global_then_class:
def __init__(self):
print(16)' copyReplaceAll: String cr with: String lf).

self assert: shadowed name equals: 'global_then_function_then_global_then_class'.
self assert: shadowed isShadowed.
self assert: shadowed parentScope equals: (self moduleNamed: 'moduleWithShadowing').
self assert: shadowed shadowingEntity equals: shadower.
self assert: shadowed sourceText equals: 'global_then_function_then_global_then_class'
]

{ #category : 'tests - shadowing' }
FamixPythonProject1Test >> testGlobalShadowedByFunction [

Expand Down

0 comments on commit b5324bd

Please sign in to comment.