#!/usr/local/bin/ruby
ENV["RUBYLIB"] += ":tests"
$SUDDEN_DETH = true

def mktitle(head, ch = "#", ch0 = ch)
  title = ch0 + "  #{head}  " + ch0
  f = ""
  f << ch * (title.size/ch.size) << "\n"
  f << ch0 <<  " " * (title.size-ch0.size*2) << ch0 << "\n"
  f << title << "\n"
  f << ch0 <<  " " * (title.size-ch0.size*2) << ch0 << "\n"
  f << ch * (title.size/ch.size) << "\n"
  f
end

def testscript(fname)
  script = File.basename(fname)
  command = "ruby" + " " + fname
  print mktitle("TEST of '#{script}'")
  r = system(command)
  if r
    print mktitle("TEST SUCCEEDED. '#{script}'", "*", "|")
    puts
  else
    print mktitle("TEST FAILED. '#{script}'", "X")
    puts
    exit if $SUDDEN_DETH
  end
end

starttime = Time.new
ARGV.each do |fname|
  script = File.basename(fname)
  dir = File.dirname(fname)
  if script =~ /^test-.*.list$/
    open(fname) do |f|
      f.each do |line|
	next if line =~ /^#/ || line =~ /^\s*$/
	x = dir + "/" + line.chomp
	testscript(x)
      end
    end
  elsif script =~ /^test-00-.*\.rb$/
    #testscript(fname)
  elsif script =~ /^test-.*\.rb$/
    testscript(fname)
  elsif script =~ /^sample-.*\.rb$/ &&  script !~ /^sample-factorize04\.rb$/
    testscript(fname)
  else
    puts "SKIP: #{fname}"
    next
  end
end
spendtime = Time.now - starttime
puts
print mktitle("ALL TEST SUCCEEDED. (#{spendtime} sec.)", "-", "|")
