Theres got to be a better way to test this

ok, so i’m writing some tests to check if requiring some files will work
(right now it’s all sorts of broken, but thats besides the point…)
anyway, my (rather simple code) is this (so far):

some tests to see if the shogun libraries are loading properly

require ‘test/unit’

class TestLibs < Test::Unit::TestCase

def setup
@libs = %w{ narray Classifier Distance Evaluation Kernel
Preprocessor
Structure Clustering Distribution Library Regression }
end

def test_load
@libs.each do |lib|
assert load_lib( lib ), “ERROR!!! #{lib} didn’t load!!”
end
end

def load_lib lib
begin
return require lib
rescue LoadError
return false
end
end

end

end testing good-ness!!

so, this works-ish. NArray loads, and throws no error, but when
Classifier
tries to load, the assert fires & the whole method stops, so my output
is:

moar kode stufz

$ ./test_libs.rb

Loaded suite ./test_libs
Started
F
Finished in 0.465465 seconds.

  1. Failure:
    test_load(TestLibs) [./test_libs.rb:15]:
    ERROR!!! Classifier didn’t load!!

1 tests, 2 assertions, 1 failures, 0 errors, 0 skips

no moar kode stufz

i was wondering if there was a more elegant way to do this other than
writing a bunch of … IDEA!!! ffs!! idea is epic fail! ['twas to us
metaprogramming to make the methods, but apparently it wont
automagically
run anything but test_* stuff ;( ] ok, so yeah, anyone have any ideas??
thanks in advance!
hex

Other than the fact Linux has a cool name, could someone explain why I
should use Linux over BSD?

No. That’s it. The cool name, that is. We worked very hard on
creating a name that would appeal to the majority of people, and it
certainly paid off: thousands of people are using linux just to be able
to say “OS/2? Hah. I’ve got Linux. What a cool name”. 386BSD made the
mistake of putting a lot of numbers and weird abbreviations into the
name, and is scaring away a lot of people just because it sounds too
technical.
– Linus Torvalds’ follow-up to a question about Linux

How about this:

require ‘test/unit’

class TestLibs < Test::Unit::TestCase

libs = %w{set rexml googoodolls}

libs.each do |lib|
TestLibs.class_eval do
define_method(“test_load_#{lib}”) do
assert_nothing_thrown(“ERROR!!! #{lib} didn’t load!!”) do
require(lib)
end
end
end
end

end

7stud – wrote in post #1003153:

How about this:

require ‘test/unit’

class TestLibs < Test::Unit::TestCase

libs = %w{set rexml googoodolls}

libs.each do |lib|
TestLibs.class_eval do
define_method(“test_load_#{lib}”) do
assert_nothing_thrown(“ERROR!!! #{lib} didn’t load!!”) do

Make that:

    assert_nothing_raised("ERROR!!!!.....)

7stud – wrote in post #1003161:

You could also do something simpler like this:

require ‘test/unit’

class TestLibs < Test::Unit::TestCase

def test_load
libs = %w{set rexml googoodolls}

results = {}

libs.each do |lib|
  results[lib] = load_lib(lib)
end

assert(
  results.values.any? {|key, val| val == false},

…and make that:

    results.any? {|key, val| .....

7stud – wrote in post #1003158:

7stud – wrote in post #1003153:

How about this:

require ‘test/unit’

class TestLibs < Test::Unit::TestCase

libs = %w{set rexml googoodolls}

libs.each do |lib|
TestLibs.class_eval do
define_method(“test_load_#{lib}”) do
assert_nothing_thrown(“ERROR!!! #{lib} didn’t load!!”) do

Make that:

    assert_nothing_raised("ERROR!!!!.....)

And this is probably better yet:

require ‘test/unit’

class TestLibs < Test::Unit::TestCase

libs = %w{set rexml googoodolls}

TestLibs.class_eval do
libs.each do |lib|
define_method(“test_load_#{lib}”) do
assert_nothing_raised do
require lib
end
end
end
end

end

–output:–
Loaded suite prog
Started
FF.
Finished in 0.050709 seconds.

  1. Failure:
    test_load_googoodolls(TestLibs) [prog.rb:10]:
    Exception raised:
    <#<LoadError: no such file to load – googoodolls>>.

  2. Failure:
    test_load_rexml(TestLibs) [prog.rb:10]:
    Exception raised:
    <#<LoadError: no such file to load – rexml>>.

3 tests, 3 assertions, 2 failures, 0 errors, 0 skips

You could also do something simpler like this:

require ‘test/unit’

class TestLibs < Test::Unit::TestCase

def test_load
libs = %w{set rexml googoodolls}

results = {}

libs.each do |lib|
  results[lib] = load_lib(lib)
end

assert(
  results.values.any? {|key, val| val == false},
  results.to_s
)

end

def load_lib lib
begin
return require lib
rescue LoadError
return false
end
end

end

–output:–
Loaded suite prog
Started
F
Finished in 0.052878 seconds.

  1. Failure:
    test_load(TestLibs) [prog.rb:15]:
    {“set”=>true, “rexml”=>false, “googoodolls”=>false}

1 tests, 1 assertions, 1 failures, 0 errors, 0 skips

Test run options: --seed 19079

Then you can examine the output hash to see which libraries failed to
load.