#!/usr/bin/ruby -I. -w

Dir.chdir ".." if Dir.pwd =~ /bin.?$/

require "runit/testcase"
require "runit/cui/testrunner"

Dir.entries('test').each do |file|
	require "test/#{file}" if file =~ /\.rb$/
end
#require "test/core_test"
#require "test/xpath_test"
#require "test/sub_test"
#require "test/contrib_test"
#require "test/elements"
#require "test/attributes"
#require "test/listenertest"
#require 'test/entity'

# Possible usage:
# 	suite.rb xpath				runs the XPath suite
# 	suite.rb xpath <test>	runs <test> from the XPath suite
# 	suite.rb <test>			runs <test> from the core suite
# 	suite.rb						runs all tests
if ARGV.size > 0
	if ARGV[0] == '--help'
		puts "USAGE: #$0 [--help|--list [<suite>]|<suite> [<method>]]\n"+
		"  --list\tList the available test suites\n"+
		"    <suite>\tList the methods of a suite\n"+
		"  <suite>\tRun all of the tests in a suite\n"+
		"  <method>\tRun the given method"
		exit
	elsif ARGV[0] == '--list'
		if ARGV[1]
			suite = ARGV[1]
			ObjectSpace.each_object(Class){|ob|suite = ob if ob.to_s == suite}
			puts((suite.instance_methods - Object.methods).collect{|mn| "  "+mn})
		else
			ObjectSpace.each_object(Class){|ob| 
				puts ob.to_s if ob.ancestors.include? RUNIT::TestCase and ob.to_s != 'RUNIT::TestCase'
			}
		end
		exit
	end
	test = ARGV[0]
	# This is really inefficient, but how else?
	ObjectSpace.each_object(Class) {|ob| test = ob if ob.to_s == test }
	if ARGV.size == 2
		test = test.new(ARGV[1])
	else
		test = test.suite
	end
	RUNIT::CUI::TestRunner.run( test )
else
	suite = RUNIT::TestSuite.new
	ObjectSpace.each_object(Class){|ob| 
		if ob.ancestors.include? RUNIT::TestCase and ob.to_s != 'RUNIT::TestCase'
			suite.add_test ob.suite
		end
	}
	RUNIT::CUI::TestRunner.run( suite )
end
