From de302af2557a8c853b76d2f1e213a4c24fa6642c Mon Sep 17 00:00:00 2001 From: Gabriel Darbord Date: Wed, 20 Nov 2024 17:03:35 +0100 Subject: [PATCH] Refactor helper creation --- .../FamixUTAbstractExporter.class.st | 13 +++++++- .../FamixUTHelperSetUpStrategy.class.st | 30 ++++++++++++++----- 2 files changed, 35 insertions(+), 8 deletions(-) diff --git a/src/Famix-UnitTest-Exporter/FamixUTAbstractExporter.class.st b/src/Famix-UnitTest-Exporter/FamixUTAbstractExporter.class.st index 0001dba..f7f51ab 100644 --- a/src/Famix-UnitTest-Exporter/FamixUTAbstractExporter.class.st +++ b/src/Famix-UnitTest-Exporter/FamixUTAbstractExporter.class.st @@ -53,7 +53,7 @@ FamixUTAbstractExporter >> export: aFamixUTModel [ self exportCaseCompilationUnit: case ]. (aFamixUTModel allWithType: FamixUTSuite) do: [ :suite | self exportSuite: suite ]. - self valueExporter makeHelper. + self finalizeHelpers. ^ self model ] @@ -123,6 +123,17 @@ FamixUTAbstractExporter >> exportSuite: aFamixUTSuite [ self subclassResponsibility ] +{ #category : 'exporting' } +FamixUTAbstractExporter >> finalizeHelpers [ + + self valueExporter finalizeHelpers. + self methodSetUpStrategy helpers do: [ :helper | + helper compilationUnit importDeclarations: + self valueExporter builder makeImportDeclarations + , (self valueExporter objectExportStrategy dependencyImportsOn: + self model) ] +] + { #category : 'initialization' } FamixUTAbstractExporter >> initializeWith: aFamixEntityFinder [ "Use the finder to resolve the case superclass name to the corresponding Famix class." diff --git a/src/Famix-UnitTest-Exporter/FamixUTHelperSetUpStrategy.class.st b/src/Famix-UnitTest-Exporter/FamixUTHelperSetUpStrategy.class.st index edb71bf..7c705e9 100644 --- a/src/Famix-UnitTest-Exporter/FamixUTHelperSetUpStrategy.class.st +++ b/src/Famix-UnitTest-Exporter/FamixUTHelperSetUpStrategy.class.st @@ -4,6 +4,9 @@ I export Arrange elements as methods in an external helper class. Class { #name : 'FamixUTHelperSetUpStrategy', #superclass : 'FamixUTAbstractSetUpStrategy', + #instVars : [ + 'helpers' + ], #category : 'Famix-UnitTest-Exporter-Strategies', #package : 'Famix-UnitTest-Exporter', #tag : 'Strategies' @@ -16,9 +19,9 @@ FamixUTHelperSetUpStrategy >> ensureHelperClassOn: aFamixUTExporter [ | model className | model := aFamixUTExporter model. className := aFamixUTExporter currentClass name , 'Helper'. - ^ model allFASTJavaClassDeclaration - detect: [ :class | class name = className ] - ifNone: [ "add import for the helper class and create it" + ^ helpers + at: className + ifAbsentPut: [ "add import for the helper class and create it" aFamixUTExporter currentCompilationUnit addImportDeclaration: (model newImportDeclaration qualifiedName: @@ -40,16 +43,18 @@ FamixUTHelperSetUpStrategy >> export: aFamixUTSetUp on: aFamixUTExporter [ valueExporter := aFamixUTExporter valueExporter. comment := '/**{@link ' , aFamixUTSetUp method case name , '#' , aFamixUTSetUp method name , '()}*/'. + "Only make helper if necessary => at least one non-empty object, collection or dictionary." aFamixUTSetUp values do: [ :value | ((value isOfObject or: [ value isOfCollection or: [ value isOfDictionary ] ]) and: [ value value isNotEmpty ]) ifFalse: [ value accept: valueExporter ] - ifTrue: [ + ifTrue: [ "make the helper method to reconstruct the value" | methodName | methodName := ((WriteStream with: 'given_') << value roleName << '_for_' << aFamixUTSetUp method name) contents. + helperClass ifNil: [ helperClass := self ensureHelperClassOn: aFamixUTExporter ]. helperClass addDeclaration: @@ -57,9 +62,11 @@ FamixUTHelperSetUpStrategy >> export: aFamixUTSetUp on: aFamixUTExporter [ name: methodName; addComment: (model newComment content: comment); yourself). + "add necessary imports to the helper class" helperClass compilationUnit importDeclarations addAll: (valueExporter builder makeImportDeclarations: value). + "IMPORTANT: build the method before calling it and assigning it to the variable, because a value is considered already exported if it has been named!" valueExporter statementBlock addStatement: @@ -71,6 +78,18 @@ FamixUTHelperSetUpStrategy >> export: aFamixUTSetUp on: aFamixUTExporter [ yourself) ] ] ] +{ #category : 'accessing' } +FamixUTHelperSetUpStrategy >> helpers [ + + ^ helpers +] + +{ #category : 'initialization' } +FamixUTHelperSetUpStrategy >> initialize [ + + helpers := Dictionary new +] + { #category : 'ast' } FamixUTHelperSetUpStrategy >> makeHelperClassNamed: className on: aFamixUTExporter [ "Make a compilation unit containing the helper class and return the latter." @@ -82,9 +101,6 @@ FamixUTHelperSetUpStrategy >> makeHelperClassNamed: className on: aFamixUTExport (model newQualifiedName name: aFamixUTExporter currentCompilationUnit packageDeclaration qualifiedName name)); - importDeclarations: - (aFamixUTExporter valueExporter objectExportStrategy - dependencyImportsOn: model); addClassDeclaration: (model newClassDeclaration name: className) ]