Ryan Leavengood said:
> Another possible issue is the fact that there is no equivalent to Java
> annotations, though I've done some prototyping and figured out a way to
> fairly easily have the equivalent in Ruby, at least for methods.
I see there has been quite a few additional discussions of this. I decided
to code up a more thorough prototype of how Java method annotations could
be simulated in Ruby:
class Class
attr_reader :configs, :groups, :test_methods
def config(hash)
@config_method = true
@temp_configs = []
hash.each do |key, value|
if value
@temp_configs << key
end
end
end
def test(hash)
@test_method = true
@current_groups = hash[:groups]
end
def method_added(method)
if @config_method or @test_method
if @current_groups
@groups||={}
@current_groups.each do |group|
(@groups[group]||=[]) << method
end
end
if @config_method
@configs||={}
@temp_configs.each do |config|
(@configs[config]||=[]) << method
end
end
if @test_method
(@test_methods||=[]) << method
end
end
@config_method = false
@current_groups = nil
@temp_configs=nil
@test_method = false
end
end
class Test
config :before_test => true
def setup
puts "Test#setup called"
end
config :after_test => true
def teardown
puts "Test#teardown called"
end
test :groups => ['one']
def test_method1
puts "Test#test_method1 called"
end
test :groups => ['two']
def test_method2
puts "Test#test_method2 called"
end
end
class Test2
config :before_test => true
def setup
puts "Test2#setup called"
end
config :after_test => true
def teardown
puts "Test2#teardown called"
end
test :groups => ['one']
def test_method1
puts "Test2#test_method1 called"
end
test :groups => ['two']
def test_method2
puts "Test2#test_method2 called"
end
end
def run_tests(tests, test_instance, test_class)
tests.each do |method|
if test_class.configs[:before_test]
test_class.configs[:before_test].each do |config_method|
test_instance.__send__(config_method)
end
end
test_instance.__send__(method)
if test_class.configs[:after_test]
test_class.configs[:after_test].each do |config_method|
test_instance.__send__(config_method)
end
end
end
end
if $0 == __FILE__
%w{Test Test2}.each do |klass|
klass = Module.const_get(klass)
test = klass.new
run_tests(klass.test_methods, test, klass)
klass.groups.each do |group, list|
puts "\nTesting group #{group}:"
run_tests(list, test, klass)
end
puts
end
end
__END__
Ryan