From b5324bde6dcf0f9420407a1f74f98c4a1e8c1fd3 Mon Sep 17 00:00:00 2001 From: CyrilFerlicot Date: Thu, 14 Nov 2024 16:38:39 +0100 Subject: [PATCH] Make tests more robust by ensuring we select the right entities all the time and not a random one if two have the same name + add test on shadowing a global with a class --- .../FamixPythonAbstractImporterTest.class.st | 27 ++++++++++++------- ...amixPythonImporterWithClassesTest.class.st | 10 +++---- ...ixPythonImporterWithFunctionsTest.class.st | 2 +- .../FamixPythonProject1Test.class.st | 24 +++++++++++++++++ 4 files changed, 48 insertions(+), 15 deletions(-) diff --git a/src/Famix-Python-Importer-Tests/FamixPythonAbstractImporterTest.class.st b/src/Famix-Python-Importer-Tests/FamixPythonAbstractImporterTest.class.st index 584009a..57f5931 100644 --- a/src/Famix-Python-Importer-Tests/FamixPythonAbstractImporterTest.class.st +++ b/src/Famix-Python-Importer-Tests/FamixPythonAbstractImporterTest.class.st @@ -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' } @@ -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' } @@ -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' } diff --git a/src/Famix-Python-Importer-Tests/FamixPythonImporterWithClassesTest.class.st b/src/Famix-Python-Importer-Tests/FamixPythonImporterWithClassesTest.class.st index 0bd459e..5a4f4f0 100644 --- a/src/Famix-Python-Importer-Tests/FamixPythonImporterWithClassesTest.class.st +++ b/src/Famix-Python-Importer-Tests/FamixPythonImporterWithClassesTest.class.st @@ -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__'. @@ -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 @@ -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. @@ -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' } diff --git a/src/Famix-Python-Importer-Tests/FamixPythonImporterWithFunctionsTest.class.st b/src/Famix-Python-Importer-Tests/FamixPythonImporterWithFunctionsTest.class.st index d2010fc..e291a04 100644 --- a/src/Famix-Python-Importer-Tests/FamixPythonImporterWithFunctionsTest.class.st +++ b/src/Famix-Python-Importer-Tests/FamixPythonImporterWithFunctionsTest.class.st @@ -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') diff --git a/src/Famix-Python-Importer-Tests/FamixPythonProject1Test.class.st b/src/Famix-Python-Importer-Tests/FamixPythonProject1Test.class.st index ee43f7b..7df3fad 100644 --- a/src/Famix-Python-Importer-Tests/FamixPythonProject1Test.class.st +++ b/src/Famix-Python-Importer-Tests/FamixPythonProject1Test.class.st @@ -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 [