On 19 July 2011 01:02, Sean P. DeNigris <sean@clipperadams.com> wrote:
Ah, so the lookups are done once when the method is compiled and then stored in the CompiledMethod...
My vision is to write something like: Â Â Â Â cannedNow := DateAndTime stub: #now; andReturn: [ DateAndTime year: 2011 month: 12 day: 24 hour: 12 minute: 34 second: 55 ]. Â Â Â Â testableClass := MyClass usingDouble: cannedNow.
The first line seems easy, and for the second line, I have so far (very rough, but working):
    | class copy |     class := MyClass.     copy := MyClass copy.
    copy methods do: [ :m | | classRef |         (m hasLiteralSuchThat: [ :l | ((l isKindOf: Association) and: [ l key = #DateAndTime])])             ifTrue: [ | newMeth refIndex |                 newMeth := copy methodDictionary at: m selector put: m copy.                 refIndex := newMeth indexOfLiteralSuchThat: [ :l | ((l isKindOf: Association) and: [ l key = #DateAndTime])].                 newMeth literalAt: refIndex put: #DateAndTime->MockedClass ] ].     ^ copy.
Where CompiledMethod>>indexOfLiteralSuchThat: aBlock     "Answer the literal index of the argument, literal, or zero if none."     2 to: self numLiterals - 1 "exclude superclass + selector/properties"      do:         [:index |         (aBlock value: (self objectAt: index)) ifTrue: [^index - 1]].     ^0
you doing something really complex. on your place i would just implement #dateAndTimeClass so, you can just write self dateAndTimeClass new and depending on implementor it would answer one or another class.
Thanks again. Sean
-- View this message in context: http://forum.world.st/Stubbing-class-side-methods-tp3674065p3676637.html Sent from the Pharo Smalltalk mailing list archive at Nabble.com.
-- Best regards, Igor Stasenko AKA sig.