Hi --

On Sat, 18 Nov 2006, Max Russell wrote:

> I'm writing a harness for some tests that use watir. Rather than having
> to boilerplate code in the definition of each test like
> def test_1
>   load test1
> end
>
> I want to read the testnames in from a file which will be easier for the
> user. What I'd like to do is something along this structure (which I
> adapted from a really good blog in metaprogramming
> http://invisibleblocks.wordpress.com/2006/06/14/ruby-meta-programming-and-watir/):
>
> testlist = Array.new
>
> File.open('readme.txt', 'r') do |eachline|
> 	while (line = eachline.gets)
> 	testlist << line
> 	end
> end
> #puts testlist
>
> testlist.each { |entry|
> 	code = <<METHOD_TEMPLATE
> 		def test_#{entry}()
> 			puts "testing #{entry}"
> 		end
> METHOD_TEMPLATE
> }
>
>
> and then call each method I've defined. However, I'm not sure I'm going
> about this the correct way.

We (Austin Codefest of 2002) did something very like this in the tests
for Ruby scanf.  If you look for test_scanf.rb in the Ruby source,
you'll see it, near the bottom of the file.  We used define_method.
(I think that was back in the days when I habitually switched the
order of actual and expected in my assertions, so you have to
compensate for that :-)

It's not exactly the same as yours but I'd say that define_method is
your best bet too.


David

-- 
                   David A. Black | dblack / rubypal.com
Author of "Ruby for Rails"   [1] | Ruby/Rails training & consultancy [3]
DABlog (DAB's Weblog)        [2] | Co-director, Ruby Central, Inc.   [4]
[1] http://www.manning.com/black | [3] http://www.rubypowerandlight.com
[2] http://dablog.rubypal.com    | [4] http://www.rubycentral.org