In the need of Pharo magic

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

In the need of Pharo magic

Stephane Ducasse-3

I would love to get a functionality that dynamically removes from my code all the self halt that are encountered during test execution. 
This way I will not need to run the test, walk the stack, edit the code and retart the process. 

It would be really cool
Reply | Threaded
Open this post in threaded view
|

Re: In the need of Pharo magic

funone
Hi Stef,
see an idea below..

Stephane Ducasse wrote:

I would love to get a functionality that dynamically removes from my code all the self halt that are encountered during test execution. 
This way I will not need to run the test, walk the stack, edit the code and retart the process. 

It would be really cool

One approach I have used is to have a global called "DebugMode" which is s boolean.
The, usie two types of debugging statements, like:
DebugMode ifTrue: [self halt].

and

DebugMode: ifTrue: [Transcrip show: somethingUseful ]

Then you can just turn DebugMode on or off.

This could be made simpler if the self halt of the system at object level checked such a flag
and if the Transcript had a subclass which wouldn't log if DebugMode was false.

KR
Graham


graham.vcf (484 bytes) Download Attachment
Graham McLeod
graham@inspired.org
www.inspired.org
Reply | Threaded
Open this post in threaded view
|

Re: In the need of Pharo magic

Henrik-Nergaard
In reply to this post by Stephane Ducasse-3

Here is an example of using the ast to remove -all- halts from the code if one or more is encountered during execution.

------------------------------------------------------------

| haltTypes haltingMethods testCode |

 

haltTypes := #( halt halt: haltIf: haltIfNil haltOnCount: haltOnce).

haltingMethods := IdentitySet new.

 

"example code"

Object compile: 'aMethod | a | self halt. a := 42. self haltIf: a = 42; haltOnCount: 2; yourself. self haltOnce; halt: ''Halting!''. self yourself; haltIfNil; yourself. [ 1 halt ] value.  ^ a'.

testCode := (Object >> #aMethod) ast formattedCode.

 

[ "do stuff here"

             

              Object new perform: #aMethod

             

] on: Halt do: [ :ex |

              haltingMethods add: ex signalerContext method.

              "ex debug" "<- show debugger on a halt"

              ex resume.

].

 

haltingMethods do: [ :method | | ast cascades |

              ast := method ast copy.

              cascades := IdentitySet new.

             

              ast allChildren

                             select: [ :child | child isMessage and: [ haltTypes includes: child selector ] ]

                             thenDo: [ :child | | parent |

                                           parent := child parent.

                                           parent isCascade

                                                          ifTrue: [

                                                                        parent messages remove: child.

                                                                        cascades add: parent

                                                          ]

                                                          ifFalse: [ parent removeNode: child ].

                             ].

                            

              cascades

                             select: [ :cascadeNode | cascadeNode messages isEmpty]

                             thenDo: [ :cascadeNode | cascadeNode parent removeNode: cascadeNode ].

             

              method origin

                             compile: ast formattedCode

                             classified: method protocol.

 

].

 

(

DiffMorph

              from: testCode

              to: (Object >>#aMethod) sourceCode

              contextClass: Object

) openInWindow            

------------------------------------------------------------

 

Best regards,

Henrik

 

Fra: Pharo-dev [mailto:[hidden email]] På vegne av Stephane Ducasse
Sendt: 19 January 2017 18:46
Til: Pharo Development List <[hidden email]>
Emne: [Pharo-dev] In the need of Pharo magic

 

 

I would love to get a functionality that dynamically removes from my code all the self halt that are encountered during test execution. 

This way I will not need to run the test, walk the stack, edit the code and retart the process. 

 

It would be really cool

Reply | Threaded
Open this post in threaded view
|

Re: In the need of Pharo magic

Peter Uhnak
In reply to this post by Stephane Ducasse-3
On Thu, Jan 19, 2017 at 06:46:22PM +0100, Stephane Ducasse wrote:
> I would love to get a functionality that dynamically removes from my code
> all the self halt that are encountered during test execution.
> This way I will not need to run the test, walk the stack, edit the code and
> retart the process.
>
> It would be really cool

You mean to _permanently_ remove them after the test passed, or just temporarily disable them during the execution?

The latter can be done with MetaLinks, e.g.:

Something>>add: aNumber to: anotherNumber
        self halt.
        ^ anotherNumber + aNumber

SomethingTest>>testAddTo
        self assert: (Something new add: 4 to: 10) equals: 14

SomethingTest>>setUp
        | halts |
        super setUp.
        halts := (Something >> #add:to:) ast sendNodes select: [ :each | each selector = #halt ].
        link := MetaLink new control: #instead.
        halts do: [ :each | each link: link ].

SomethingTest>>tearDown
        super tearDown.
        link uninstall

Now if I call the method by hand it will halt, but the test will skip the halts.

Peter