#!/usr/local/bin/ruby
file = ARGV.shift
fname = File.basename(file)
tdir = "tests/"
tscript = tdir + "test-" + fname

if File.exist?(tscript)
  puts "#{tscript} is exist. (push RETURN to go or ^C for abort)"
  gets
end

methods = []
open(file) do |s|
  s.each do |line|
    line.chop!
    if /^\s*(?:def|alias)\s+([\w_?!\[\]+\-*<=>\/%@~^&|.`~]+)/ =~ line #/
      method_name = $1
      new_name = $1.dup
      {/==/ => "_equal",
	/\+/ => "_plus",
	/-/ => "_minus",
	/\*/ => "_star",
	/\*\*/ => "_star2",
	/\// => "_slash", #/
	/&/ => "_amp",
	/\|/ => "_stroke",
	/~/ => "_tilde",
	/\^/ => "_caret",
	/%/ => "_parcent",
	/\./ => "_dot_",
	/`/ => "_baq",
	/>>/ => "_ggt",
	/>/ => "_gt",
	/<</ => "_llt",
	/</ => "_lt",
	/@/ => "_at",
	/\[\]/ => "_bra",
      }.each do |k, v|
	new_name.sub!(k, v)
      end
      methods.push [method_name, new_name]
    end
  end
end

class_name = File.basename(file).sub(/\.rb/, '').split(/-/).collect{|r| r.capitalize}.join("")

n0 = 70
s = "#" + "#" * n0 + "#\n"
s << "#" + " " * n0 + "#\n"
s << "#" + (k = "   Test Script for #{file}") + " "*(n0-k.size) + "#\n"
s << "#" + " " * n0 + "#\n"
s << "#" + "#" * n0 + "#\n"
s << <<__EOS
require "rubyunit"
require "#{fname}"

class Rational;def inspect; to_s; end;end

class Test#{class_name} < Runit
__EOS

methods.each do |me, name|
  u =<<__EOS
  def test_#{name} #test for #{me}
    assert_equal(0, 0)
    assert(true)
  end
__EOS
  s << u << "\n"
end

s << <<__EOS
end
Tests(Test#{class_name})
__EOS

open(tscript, "w") do |t|
  t.puts s
end
