[Pharo-project] About testing that ensure works

Igor Stasenko siguctua at gmail.com
Sun Apr 24 11:49:42 CEST 2011


On 24 April 2011 11:59, Stéphane Ducasse <stephane.ducasse at inria.fr> wrote:
>> First question what i wanna ask is:
>> - why do you need to play with two different instances of organizer,
>> and especially in such manner (temporary swapping forth and back)?
>>
>> This code doesn't looks to be good.
>
> Igor can you reply to my question instead of questioning because it just slows me down.
>
> Now the answer:
>        when we run  the tests we do not run them in the default organizer because if something wrong happens
>        then this is simply harakiri.
>        So all the tests are performed in a separated created on the fly organizer.
>        And I think that this is good to do it like that else I have to have a global variable and the rest.
>        This design is the result of changes doru pushed me to do and they are good.
>        Now this good is bad and not written my me and I'm trying to clean it and this is why I need to
>        write tests because I want the Package code to be bullet proof.
>

i am puzzled. You wanna to be sure that ensure block are executed?
It is.
However if error was triggered during ensure block evaluation, it may
not complete everything you put there.

And for test you can use non-local return to trigger ensure block:


 testWithDoIsCorrectlyReinstallingDefault
        "self debug: #testWithDoIsCorrectlyReinstallingDefault"

        | current empty |
        current := RPackageOrganizer default.
        empty := RPackageOrganizer basicNew initialize.

        self doWith:current and:empty.

        self assert: (SystemAnnouncer announcer hasSubscriber: current).
        self deny: (SystemAnnouncer announcer hasSubscriber: empty)


doWith:current and:empty

        RPackage withOrganizer: empty
                 do: [  ^ self  ].


Or with error:

testWithDoIsCorrectlyReinstallingDefaultEvenIfHalt
        "self debug: #testWithDoIsCorrectlyReinstallingDefaultEvenIfHalt"

        | current empty |
        current := RPackageOrganizer default.
        empty := RPackageOrganizer basicNew initialize.

[
        RPackage withOrganizer: empty
                 do: [ self error ].
] on: Error do: [:ex |  ].

        self assert: (SystemAnnouncer announcer hasSubscriber: current).
        self deny: (SystemAnnouncer announcer hasSubscriber: empty)


> Stef
>>
>>
>> On 24 April 2011 00:44, Stéphane Ducasse <stephane.ducasse at inria.fr> wrote:
>>> Hi guys
>>>
>>> in RPackage there is a method named withOrganizer:do:  defined as follows and I would like to test it to understand really if it does
>>> what it says.
>>>
>>> withOrganizer: aNewOrganizer do: aBlock
>>>        "Perform an action locally to aNewOrganizer. Does not impact any other organizers."
>>>
>>>        | old shouldRegisterAtTheEnd|
>>>        [
>>>        old := self organizer.
>>>        shouldRegisterAtTheEnd := false.
>>>        old hasRegistered ifTrue: [
>>>                old unregister.
>>>                shouldRegisterAtTheEnd := true.
>>>                ].
>>>        self organizer: aNewOrganizer.
>>>        aBlock cull: aNewOrganizer.] ensure: [
>>>                self organizer: old.
>>>                shouldRegisterAtTheEnd ifTrue: [
>>>                        self organizer register.
>>>                        ].
>>>                aNewOrganizer unregister.
>>>                ]
>>>
>>>
>>> So I wrote two tests
>>>
>>> testWithDoIsCorrectlyReinstallingDefault
>>>        "self debug: #testWithDoIsCorrectlyReinstallingDefault"
>>>
>>>        | current empty |
>>>        current := RPackageOrganizer default.
>>>        empty := RPackageOrganizer basicNew initialize.
>>>        RPackage withOrganizer: empty
>>>                 do: [ self assert: (SystemAnnouncer announcer hasSubscriber: empty).
>>>                        self deny: (SystemAnnouncer announcer hasSubscriber: current)].
>>>        self assert: (SystemAnnouncer announcer hasSubscriber: current).
>>>        self deny: (SystemAnnouncer announcer hasSubscriber: empty)
>>>
>>>
>>> and
>>>
>>>
>>> testWithDoIsCorrectlyReinstallingDefaultEvenIfHalt
>>>        "self debug: #testWithDoIsCorrectlyReinstallingDefaultEvenIfHalt"
>>>
>>>        | current empty |
>>>        current := RPackageOrganizer default.
>>>        empty := RPackageOrganizer basicNew initialize.
>>>        RPackage withOrganizer: empty
>>>                 do: [ self error.
>>>                        self assert: (SystemAnnouncer announcer hasSubscriber: empty).
>>>                        self deny: (SystemAnnouncer announcer hasSubscriber: current)].
>>>        self assert: (SystemAnnouncer announcer hasSubscriber: current).
>>>        self deny: (SystemAnnouncer announcer hasSubscriber: empty)
>>>
>>> Now I would like to make sure that I raise an error or whaever to make sure that the ensure: block argument
>>> is executed. Does anybody have an idea how I can do that. because self error does not work.
>>>
>>> Stef
>>>
>>>
>>>
>>>
>>>
>>
>>
>>
>> --
>> Best regards,
>> Igor Stasenko AKA sig.
>>
>
>
>



-- 
Best regards,
Igor Stasenko AKA sig.



More information about the Pharo-project mailing list