Generate test method with their body in pharo

85 Views Asked by At

I advanced a little in my code but I find myself facing another problem for two days. I would like to generate a test method using only the source code. But I have no idea how to do it.

I have a method that allows me to build the name of a test method but I can't write in it.

buildSelectorFor: aMethod
    ^ String streamContents: [:i || capitalize |
        capitalize := true.
        i << 'test'.
        aMethod selector do: [:charactar |
            charactar= $:
                ifTrue: [ capitalize := true ]
                ifFalse: [ capitalize
                            ifTrue: [ 
                                capitalize := false.
                                i << charactar asUppercase. ]
                            ifFalse:[ i << charactar ]]]]

so if I execute this method with this for example:

buildSelectorFor:Car>>#speed:mark:

I get this:

testSpeedMark

my goal is to get something like

testSpeedMark
    self assert:....equals:...

I added a method writeTestMethod.

writeTestMethod: aMethod with: anObject 
    ^(self buildTestSelectorFor: aMethod),'
      |classMethod setter instObject method|
      classMethod := aMethod methodClass.
    setter := (classMethod  allSelectorsInProtocol: #setter) asArray.
    instObject := classMethod new.
    (setter with: anObject do: [:set :ivar | instObject  perform: set with: ivar]).
     self assert: instObject class equals: (classMethod new) class.'

So here is what I get: enter image description here

I don't know how to integrate the parameters of writetestMethod in the code I want to generate

0

There are 0 best solutions below