2009/12/29 Intransition <transfire / gmail.com>:
>
>
> On Dec 29, 9:37=A0am, Robert Klemme <shortcut... / googlemail.com> wrote:
>> 2009/12/29 Intransition <transf... / gmail.com>:
>
>> I see to be a bit slow today: how do you want to use modules there?
>> Do you want to include them in the test code? =A0What about:
>>
>> class TestCase
>> =A0 def new(cl, &bl)

Btw, that method's name should have read "initialize".

>> =A0 =A0 =A0@cl =3D cl
>> =A0 =A0 =A0instance_eval(&bl)
>> =A0 end
>>
>> =A0 alias include extend
>> end
>>
>> TestCase.new String do
>> =A0 extend Foo
>> =A0 include Bar
>> end
>
> No no. You got it exactly. Problem is the module's constants are not
> coming through the #extend (which is effectively the same as the
> include in the singleton class). Let say I have a library:
>
> =A0module MyApp
> =A0 =A0module SomeSpace
> =A0 =A0 =A0class FooClass
> =A0 =A0 =A0 =A0...
> =A0 =A0 =A0 end
> =A0 =A0end
> =A0end
>
> In the test cases, instead of having to spell out
> MyApp::SomeSpace::FooClass everywhere it is needed, it would be nice
> to include MyApp::SomeSpace, and then just reference FooClass in the
> tests.
>
>
>> > Instead of making an instance of TestCase for each case, at this point
>> > it looks like I'll have to create a new subclass of it.
>
>> Sorry, you lost me here.
>
> Instead of the code you presented I'd have to do something like:
>
> =A0class TestCase
> =A0 =A0alias :_new, :new
> =A0 =A0def self.new(&block)
> =A0 =A0 =A0Class.new(self, &block)._new
> =A0 =A0end
> =A0end

Then your block needs to contain a class definition.  You can use the
block only for _one_ thing - either class / module def or code you
want to execute.  Granted, you can have code executed in a class
definition as well but the definition becomes only usable *after* the
definition has been executed in its entirety.  Maybe you haven't
decided yet what you want the block for and that is causing your
headaches.

> Haven't tested it yet, but that should allow #include to work no
> problem. Unfortunately it means defining all my dsl methods at the
> class level --not even sure the instance level would be of any use in
> this case either, in which case the ._new can be dropped -- kind of
> stupid, just to get #include to work. But what else can I do?
>
> I almost feel like I'm having a mental block and there is actually an
> easy way to do this.

What stops you from doing the include outside?

include Your::Module::Of::Choice

TestCase.new Foo do
  x =3D ChoiceClass.new
  x.method_invocation(123)
end

Kind regards

robert


--=20
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/