[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Dynamically add a method to a TestCase

Achim Domma

1/2/2008 6:53:00 PM

Hi,

I'm experimenting with the dynamic features of ruby to add methods to
a TestCase. Here is my test script:

require 'watir'
require 'test/unit'

class MyTest < Test::Unit::TestCase
end

for num in ["a","b","c"]
MyTest.send(:define_method,"test_" + num) {
assert(false,"->"+num)
}
end

It works fine so far, but all asserts display "->c" as error message.
I don't understand why! Any hint? How to fix it?

regards,
Achim
4 Answers

Jason Roelofs

1/2/2008 7:07:00 PM

0

[Note: parts of this message were removed to make it a legal post.]

On Jan 2, 2008 1:55 PM, Achim Domma <domma@procoders.net> wrote:

> Hi,
>
> I'm experimenting with the dynamic features of ruby to add methods to
> a TestCase. Here is my test script:
>
> require 'watir'
> require 'test/unit'
>
> class MyTest < Test::Unit::TestCase
> end
>
> for num in ["a","b","c"]
> MyTest.send(:define_method,"test_" + num) {
> assert(false,"->"+num)
> }
> end
>
> It works fine so far, but all asserts display "->c" as error message.
> I don't understand why! Any hint? How to fix it?
>
> regards,
> Achim
>

That's exactly what you're telling assert to do. Method definition is
something like:

assert(expression, error_message = nil)

What exactly are you wanting to do?

Jason

Jason Roelofs

1/2/2008 7:09:00 PM

0

[Note: parts of this message were removed to make it a legal post.]

On Jan 2, 2008 2:06 PM, Jason Roelofs <jameskilton@gmail.com> wrote:

> On Jan 2, 2008 1:55 PM, Achim Domma <domma@procoders.net> wrote:
>
> > Hi,
> >
> > I'm experimenting with the dynamic features of ruby to add methods to
> > a TestCase. Here is my test script:
> >
> > require 'watir'
> > require 'test/unit'
> >
> > class MyTest < Test::Unit::TestCase
> > end
> >
> > for num in ["a","b","c"]
> > MyTest.send(:define_method,"test_" + num) {
> > assert(false,"->"+num)
> > }
> > end
> >
> > It works fine so far, but all asserts display "->c" as error message.
> > I don't understand why! Any hint? How to fix it?
> >
> > regards,
> > Achim
> >
>
> That's exactly what you're telling assert to do. Method definition is
> something like:
>
> assert(expression, error_message = nil)
>
> What exactly are you wanting to do?
>
> Jason
>
>
Dah crap, excuse my previous email. I need to work on my reading
comprehension.

That is odd. I would assume that a puts there would always say "c", though
it can't hurt to try.

Jason

Joel VanderWerf

1/2/2008 7:21:00 PM

0

Achim Domma wrote:
> Hi,
>
> I'm experimenting with the dynamic features of ruby to add methods to
> a TestCase. Here is my test script:
>
> require 'watir'
> require 'test/unit'
>
> class MyTest < Test::Unit::TestCase
> end
>
> for num in ["a","b","c"]
> MyTest.send(:define_method,"test_" + num) {
> assert(false,"->"+num)
> }
> end
>
> It works fine so far, but all asserts display "->c" as error message.
> I don't understand why! Any hint? How to fix it?

It's the way the 'for' construct scopes the 'num' variable. All three
method definition blocks are seeing 'num' in the same scope, so they all
get the last assigned value, "c".

Try this:

procs = []
for num in ["a","b","c"]
#["a","b","c"].each do |num|
procs << proc {
num
}
end

procs.each do |pr|
puts pr.call
end

That prints three 'c's.

Now uncomment the 'each' line and comment out the 'for' line, and you'll
see the scope difference!

--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

Morton Goldberg

1/3/2008 2:53:00 AM

0


On Jan 2, 2008, at 1:55 PM, Achim Domma wrote:

> Hi,
>
> I'm experimenting with the dynamic features of ruby to add methods to
> a TestCase. Here is my test script:
>
> require 'watir'
> require 'test/unit'
>
> class MyTest < Test::Unit::TestCase
> end
>
> for num in ["a","b","c"]
> MyTest.send(:define_method,"test_" + num) {
> assert(false,"->"+num)
> }
> end
>
> It works fine so far, but all asserts display "->c" as error message.
> I don't understand why! Any hint? How to fix it?


One way to fix it is to use eval rather then send:

<code>
require 'test/unit'

class MyTest < Test::Unit::TestCase
for ch in %w[a b c]
eval %[define_method("test_#{ch}") { assert(false, "->#{ch}") }]
end
end
</code>

Regards, Morton